json

假设有下面的一个结构体

func newUser() User {
    name := "user"
    MyGithub := GithubPage{
        URL:  "https://github.com/liangyaopei",
        Star: 1,
    }
    NoDive := StructNoDive{NoDive: 1}
    dateStr := "2020-07-21 12:00:00"
    date, _ := time.Parse(timeLayout, dateStr)
    profile := Profile{
        Experience: "my experience",
        Date:       date,
    }
    return User{
        Name:      name,
        Github:    MyGithub,
        NoDive:    NoDive,
        MyProfile: profile,
    }
}

type User struct {
    Name      string       `map:"name,omitempty"`        // string
    Github    GithubPage   `map:"github,dive,omitempty"` // struct dive
    NoDive    StructNoDive `map:"no_dive,omitempty"`     // no dive struct
    MyProfile Profile      `map:"my_profile,omitempty"`  // struct implements its own method
}

type GithubPage struct {
    URL  string `map:"url"`
    Star int    `map:"star"`
}

type StructNoDive struct {
    NoDive int
}

type Profile struct {
    Experience string    `map:"experience"`
    Date       time.Time `map:"time"`
}

// its own toMap method
func (p Profile) StructToMap() (key string, value interface{}) {
    return "time", p.Date.Format(timeLayout)
}

使用json包

[]byte[]byte
data, _ := json.Marshal(&user)
m := make(map[string]interface{})
json.Unmarshal(data, &m)

优势

  • 使用简单

劣势

  • 效率比较慢
  • 不能支持一些定制的键,也不能支持一些定制的方法,例如将struct的域展开等。

使用反射

mapnewUser()map
map[string]interface{}{
    "name":    "user",
    "no_dive": StructNoDive{NoDive: 1},
    // dive struct field
    "url":     "https://github.com/liangyaopei",
    "star":    1,
    // customized method
    "time":    "2020-07-21 12:00:00",
}

实现思路 & 源码解析

1.标签识别。

readTag

如果选中了一个选项,就讲这个域对应的二进制位置为1.。

const (
    OptIgnore    = "-"
    OptOmitempty = "omitempty"
    OptDive      = "dive"
)

const (
    flagIgnore = 1 << iota
    flagOmiEmpty
    flagDive
)

func readTag(f reflect.StructField, tag string) (string, int) {
    val, ok := f.Tag.Lookup(tag)
    fieldTag := ""
    flag := 0

    // no tag, use field name
    if !ok {
        return f.Name, flag
    }
    opts := strings.Split(val, ",")

    fieldTag = opts[0]
    for i := 1; i < len(opts); i++ {
        switch opts[i] {
        case OptIgnore:
            flag |= flagIgnore
        case OptOmitempty:
            flag |= flagOmiEmpty
        case OptDive:
            flag |= flagDive
        }
    }
    return fieldTag, flag
}

2.结构体的域(field)的遍历。

stringint
for i := 0; i < t.NumField(); i++ {
        ...
        switch fieldValue.Kind() {
        case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
            res[tagVal] = fieldValue.Int()
        case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
            res[tagVal] = fieldValue.Uint()
        case reflect.Float32, reflect.Float64:
            res[tagVal] = fieldValue.Float()
        case reflect.String:
            res[tagVal] = fieldValue.String()
        case reflect.Bool:
            res[tagVal] = fieldValue.Bool()
        default:
        }
    }
}

3.内嵌结构体的转换

StructToMapdive
for i := 0; i < t.NumField(); i++ {
        fieldType := t.Field(i)

        // ignore unexported field
        if fieldType.PkgPath != "" {
            continue
        }
        // read tag
        tagVal, flag := readTag(fieldType, tag)

        if flag&flagIgnore != 0 {
            continue
        }

        fieldValue := v.Field(i)
        if flag&flagOmiEmpty != 0 && fieldValue.IsZero() {
            continue
        }

        // ignore nil pointer in field
        if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
            continue
        }
        if fieldValue.Kind() == reflect.Ptr {
            fieldValue = fieldValue.Elem()
        }

        // get kind
        switch fieldValue.Kind() {
        case reflect.Struct:
            _, ok := fieldValue.Type().MethodByName(methodName)
            if ok {
                key, value, err := callFunc(fieldValue, methodName)
                if err != nil {
                    return nil, err
                }
                res[key] = value
                continue
            }
            // recursive
            deepRes, deepErr := StructToMap(fieldValue.Interface(), tag, methodName)
            if deepErr != nil {
                return nil, deepErr
            }
            if flag&flagDive != 0 {
                for k, v := range deepRes {
                    res[k] = v
                }
            } else {
                res[tagVal] = deepRes
            }
        default:
        }
    }
    ...
}

// call function
func callFunc(fv reflect.Value, methodName string) (string, interface{}, error) {
    methodRes := fv.MethodByName(methodName).Call([]reflect.Value{})
    if len(methodRes) != methodResNum {
        return "", nil, fmt.Errorf("wrong method %s, should have 2 output: (string,interface{})", methodName)
    }
    if methodRes[0].Kind() != reflect.String {
        return "", nil, fmt.Errorf("wrong method %s, first output should be string", methodName)
    }
    key := methodRes[0].String()
    return key, methodRes[1], nil
}

4.array,slice类型的转换

arrayslice
switch fieldValue.Kind() {
        case reflect.Slice, reflect.Array:
            _, ok := fieldValue.Type().MethodByName(methodName)
            if ok {
                key, value, err := callFunc(fieldValue, methodName)
                if err != nil {
                    return nil, err
                }
                res[key] = value
                continue
            }
            res[tagVal] = fieldValue
            ....
}

5.其他类型

map
switch fieldValue.Kind() {
        ...
        case reflect.Map:
            res[tagVal] = fieldValue
        case reflect.Chan:
            res[tagVal] = fieldValue
        case reflect.Interface:
            res[tagVal] = fieldValue.Interface()
        default:
        }


如果觉得这篇文章对你有用,可以点赞。

更多内容,可以访问