Skip to content

encodeStruct 应该读取json tag #51

@KevinGong2013

Description

@KevinGong2013

func encodeStruct(object interface{}, ignoreZero bool) map[string]interface{} {
v := reflect.Indirect(reflect.ValueOf(object))
t := v.Type()
if v.IsValid() && v.Kind() == reflect.Struct {
encodedMap := make(map[string]interface{})
for i := 0; i < v.NumField(); i++ {
encodedMap[t.Field(i).Name] = encode(v.Field(i).Interface(), ignoreZero)
if encodedMap[t.Field(i).Name] == nil {
delete(encodedMap, t.Field(i).Name)
}
}
return encodedMap
}
return nil
}

例如自定一个class 如下:

type Foo struct {
	leancloud.Object
	Rule               struct {
		Month     int `json:"month"`
		Day       int `json:"day"`
	} `json:"rule"`
}

这个Foo对象save以后 rule列应该是 {"month": 1, "day": 1} 而不是{"Month": 1, "Day": 1}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions