mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
名称转换
This commit is contained in:
@ -1,54 +0,0 @@
|
|||||||
package json
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/charlienet/go-mixed/bytesconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 结构转换为json字符串
|
|
||||||
func StructToJsonIndent(obj any) string {
|
|
||||||
b, _ := MarshalIndent(obj, "", " ")
|
|
||||||
return bytesconv.BytesToString(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 结构转换为json字符串
|
|
||||||
func StructToJson(obj any) string {
|
|
||||||
b, _ := Marshal(obj)
|
|
||||||
return bytesconv.BytesToString(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func StructToMap(obj any) map[string]any {
|
|
||||||
typ := reflect.TypeOf(obj)
|
|
||||||
|
|
||||||
kind := typ.Kind()
|
|
||||||
if kind == reflect.Map {
|
|
||||||
return toMap(obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
val := reflect.ValueOf(obj)
|
|
||||||
|
|
||||||
m := make(map[string]any)
|
|
||||||
for i := 0; i < val.NumField(); i++ {
|
|
||||||
m[typ.Field(i).Name] = val.Field(i).Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func StructToMapViaJson(obj any) map[string]any {
|
|
||||||
m := make(map[string]any)
|
|
||||||
|
|
||||||
j, _ := Marshal(obj)
|
|
||||||
_ = Unmarshal(j, &m)
|
|
||||||
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func toMap(obj any) map[string]any {
|
|
||||||
if h, ok := obj.(map[string]any); ok {
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
|
|
||||||
return StructToMapViaJson(obj)
|
|
||||||
}
|
|
18
json/struct_test.go
Normal file
18
json/struct_test.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package json
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMapModify(t *testing.T) {
|
||||||
|
m := map[string]string{
|
||||||
|
"A": "A",
|
||||||
|
}
|
||||||
|
|
||||||
|
modify(m)
|
||||||
|
t.Log(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func modify(m map[string]string) {
|
||||||
|
m["B"] = "bbb"
|
||||||
|
}
|
@ -28,7 +28,7 @@ func ToMap(o any, opts ...optionFunc) map[string]any {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
m[field.name] = source.Interface()
|
m[opt.NameFunc(field.name)] = source.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
|
@ -8,12 +8,12 @@ import (
|
|||||||
|
|
||||||
func TestStructToMap(t *testing.T) {
|
func TestStructToMap(t *testing.T) {
|
||||||
o := struct {
|
o := struct {
|
||||||
Abc string
|
UserName string
|
||||||
InTagName string `json:"in_tag_name,omitempty"`
|
InTagName string `json:"in_tag_name,omitempty"`
|
||||||
KeepEmpty int
|
KeepEmpty int
|
||||||
OmitEmpty int `json:",omitempty"`
|
OmitEmpty int `json:",omitempty"`
|
||||||
}{
|
}{
|
||||||
Abc: "测试字段",
|
UserName: "测试字段",
|
||||||
InTagName: "具体名称",
|
InTagName: "具体名称",
|
||||||
KeepEmpty: 0,
|
KeepEmpty: 0,
|
||||||
OmitEmpty: 0,
|
OmitEmpty: 0,
|
||||||
@ -22,4 +22,6 @@ func TestStructToMap(t *testing.T) {
|
|||||||
t.Log(structs.ToMap(o))
|
t.Log(structs.ToMap(o))
|
||||||
t.Log(structs.ToMap(o, structs.IgnoreEmpty()))
|
t.Log(structs.ToMap(o, structs.IgnoreEmpty()))
|
||||||
t.Log(structs.ToMap(o, structs.Omitempty()))
|
t.Log(structs.ToMap(o, structs.Omitempty()))
|
||||||
|
t.Log(structs.ToMap(o, structs.Lcfirst()))
|
||||||
|
t.Log(structs.ToMap(o, structs.Camel2Case()))
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,14 @@ package structs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/charlienet/go-mixed/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
type optionFunc func(*option)
|
type optionFunc func(*option)
|
||||||
|
|
||||||
type option struct {
|
type option struct {
|
||||||
|
NameFunc func(string) string
|
||||||
IgnoreEmpty bool
|
IgnoreEmpty bool
|
||||||
DeepCopy bool
|
DeepCopy bool
|
||||||
Omitempty bool
|
Omitempty bool
|
||||||
@ -30,8 +33,23 @@ func Omitempty() optionFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Lcfirst() optionFunc {
|
||||||
|
return func(o *option) {
|
||||||
|
o.NameFunc = json.Lcfirst
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Camel2Case() optionFunc {
|
||||||
|
return func(o *option) {
|
||||||
|
o.NameFunc = json.Camel2Case
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createOptions(opts []optionFunc) *option {
|
func createOptions(opts []optionFunc) *option {
|
||||||
o := &option{}
|
o := &option{
|
||||||
|
NameFunc: func(s string) string { return s },
|
||||||
|
}
|
||||||
|
|
||||||
for _, f := range opts {
|
for _, f := range opts {
|
||||||
f(o)
|
f(o)
|
||||||
}
|
}
|
||||||
@ -42,4 +60,3 @@ func createOptions(opts []optionFunc) *option {
|
|||||||
func shouldIgnore(v reflect.Value, ignoreEmpty bool) bool {
|
func shouldIgnore(v reflect.Value, ignoreEmpty bool) bool {
|
||||||
return ignoreEmpty && v.IsZero()
|
return ignoreEmpty && v.IsZero()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user