wp-go/app/phphelper/unserialize.go

38 lines
810 B
Go
Raw Permalink Normal View History

2023-02-14 11:47:47 +00:00
package phphelper
2023-01-31 16:58:42 +00:00
import (
"github.com/elliotchance/phpserialize"
"github.com/fthvgb1/wp-go/helper/maps"
)
// UnPHPSerializeToStruct 使用 json tag
func UnPHPSerializeToStruct[T any](s string) (r T, err error) {
2023-01-31 16:58:42 +00:00
var rr map[any]any
err = phpserialize.Unmarshal([]byte(s), &rr)
if err == nil {
rx := maps.AnyAnyToStrAny(rr)
r, err = maps.StrAnyMapToStruct[T](rx)
}
return
}
2023-03-06 12:53:51 +00:00
func UnPHPSerializeToStrAnyMap(s string) (map[string]any, error) {
m := map[string]any{}
var r map[any]any
err := phpserialize.Unmarshal([]byte(s), &r)
if err != nil {
return nil, err
}
m = maps.AnyAnyToStrAny(r)
return m, err
}
2023-03-06 12:53:51 +00:00
func UnPHPSerializeToAnyAnyMap(s string) (map[any]any, error) {
var r map[any]any
err := phpserialize.Unmarshal([]byte(s), &r)
if err != nil {
return nil, err
}
return r, err
}