Go源码分析:mapstructure

文章目录

  • 1、工程包与文档
  • 2、配置说明
    • a.DecodeHook
    • b.ErrorUnused
    • c.WeaklyTypedInput
    • d.Metadata
    • e.Result
    • f.TagName
  • 3、Decode:结构体精确转换
    • a.零值处理
    • b.Hook调用
    • c.根据类型的decode处理
      • 1.bool
      • 2.Interface
      • 3.string
      • 4.int
      • 5.uint
      • 6.float32
      • 7.struct
        • 类型判断
        • 初始化两个key的数组
        • 循环结构体获取所有的Field
        • 循环Filed数组进行Decode
        • 未转换的key处理
        • Metadata处理
      • 8.map
        • 创建新map
        • 弱类型处理
        • 遍历key处理Map
      • 9.slice
        • 构建新切片
        • 弱类型处理
        • 循环切片转换
    • d.处理metadata
  • 4、DecodePath:部分转换
    • a.类型判断处理
    • b.Field数组处理
      • 1.获取jpath标签tag
      • 2.tag为空字符串的处理
      • 3.分隔tag标签为keys
      • 4.通过keys到map中查找数据
      • 5.数组类型的处理
      • 6.非数组类型的处理
  • 5、DecodeSlicePath:数组类型的部分转换
    • a.检验是否为切片
    • b.初始化decoder
    • c.初始化新数组
    • d.循环处理转换
    • e.设置新数组
  • 6、测试代码
  • 7、总结

Go源码分析:mapstructure_第1张图片

1、工程包与文档

官方文档:https://godoc.org/github.com/mitchellh/mapstructure#DecodeHookFunc

github地址:https://github.com/goinggo/mapstructure

提供Hook地址:https://github.com/mitchellh/mapstructure/blob/master/decode_hooks.go

官方工程包获取方式: go get github.com/goinggo/mapstructure

获取默认hook工程包方式:go get github.com/mitchellh/mapstructure

2、配置说明

    在decoder进行decode调用之前,需要设置一下decoder的配置信息,这边的配置信息都放在DecoderConfig中,具体来看一下对应的字段

// DecoderConfig is the configuration that is used to create a new decoder
// and allows customization of various aspects of decoding.
type DecoderConfig struct {

	DecodeHook DecodeHookFunc

	ErrorUnused bool

	WeaklyTypedInput bool

	Metadata *Metadata

	Result interface{}

	TagName string
}

a.DecodeHook

    DecodeHook是一个hook方法,在调用decode进行字段的转换之前,会先调用hook方法,先对被转换的数据进行处理,然后再对被处理过的数据进行转换

    前提是hook存在,DecodeHook对应的是 DecodeHookFunc方法:

type DecodeHookFunc func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)

    这边的参数解释可以查看官网的说明,并且官网在 decode_hook 提供了一部分转换的hook方法,可以参考

    hook的具体作用可以举例说明:先将字符串对应的字段转换成时间类型,再调用转换方法.
    

b.ErrorUnused

    ErrorUnused默认为false,在需要的情况下可以将该字段设置为true。当该字段为true,如果map转换过程中,有任意一个key的值没办法转换则会产生error数据返回

    

c.WeaklyTypedInput

    WeaklyTypedInput默认是false,该标识主要是控制弱类型转换处理,具体的转换规则如下,在decode区分不同类型转换的时候会具体处理:

  • bools to string (true = “1”, false = “0”)
  • numbers to string (base10)
  • bools to int/uint (true = 1, false = 0)
  • strings to int/uint(base implied by prefix)
  • int to bool (true if value != 0)
  • string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F, FALSE,false, False. Anything else is an error)
  • empty array = empty map and vice versa

d.Metadata

    MetaData的结构如下:

// Metadata contains information about decoding a structure that
// is tedious or difficult to get otherwise.
type Metadata struct {
	// Keys are the keys of the structure which were successfully decoded
	Keys []string

	// Unused is a slice of keys that were found in the raw value but
	// weren't decoded since there was no matching field in the result interface
	Unused []string
}

    Keys存储转换过程中被转换成功的key数组,Unused存储转换过程中没有被转换的key数组,这边的测试表明如果要让metadata生效,必须设置通过Decode处理,DecodePath似乎没办法处理.
    

e.Result

    Result是Decode转换的结果,需要注意的是,这边传入的必须是ptr类型,比如说 Obj是结构体,这边需要传入的是 &Obj,如果不是 &Obj 则配置decode没办法成功。

    这边在配置的时候没有直接报错,而是通过返回error的方式有点奇怪,使用者没办法一下子看出来问题。
    

f.TagName

    TagName默认会处理成 mapstructure,这个是标注在结构体上的tag标识,通过TagName去获取tag上的字段配置,该字段可设置。
    

3、Decode:结构体精确转换

a.零值处理

    这边主要是处理两种零值的情况,nil和默认值。

    如果被转换的数据是nil,则直接返回nil,如果被转换的数据是该类型的零值,则直接设置Result为零值返回。

    具体看一下代码:

	//read note 结构体为空直接返回
	if data == nil {
		// If the data is nil, then we don't set anything.
		return nil
	}

	dataVal := reflect.ValueOf(data)
	//read note 非IsValid的对象,设置零值
	if !dataVal.IsValid() {
		// If the data value is invalid, then we just set the value
		// to be the zero value.
		val.Set(reflect.Zero(val.Type()))
		return nil
	}

b.Hook调用

    在真正的转换之前,需要判断hookFunc是否为空,不为空的话需要调用hookFunc对被转换的数据进行处理,然后返回被处理后的数据。

    这个被处理后的数据才是下面真正进行转换处理的数据,具体看一下代码:

	//read note hook的调用,调用的结果data会用在下面的判断中
	if d.config.DecodeHook != nil {
		// We have a DecodeHook, so let's pre-process the data.
		var err error
		data, err = d.config.DecodeHook(d.getKind(dataVal), d.getKind(val), data)
		if err != nil {
			return err
		}
	}

c.根据类型的decode处理

    获得转换结果的数据类型,根据数据类型进行不同的处理,这边分成了10个不同的处理,分别看一下:

1.bool

    decodeBool会根据WeakluTypeInput是否开启进行转换或者是构建错误信息,比较简单,具体看代码:

func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
	dataVal := reflect.ValueOf(data)
	dataKind := d.getKind(dataVal)

	//read note 这边的转换规则如下:
	//	1、bool类型直接转换
	//	开启了弱类型转换标识的
	//	2、数值类型判断是否为0,0是false
	//	3、string类型,先进行bool转换,如果不能转换,空字符表示false,否则错误
	//	4、其他类型错误
	switch {
	case dataKind == reflect.Bool:
		val.SetBool(dataVal.Bool())
	case dataKind == reflect.Int && d.config.WeaklyTypedInput:
		val.SetBool(dataVal.Int() != 0)
	case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
		val.SetBool(dataVal.Uint() != 0)
	case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
		val.SetBool(dataVal.Float() != 0)
	case dataKind == reflect.String && d.config.WeaklyTypedInput:
		b, err := strconv.ParseBool(dataVal.String())
		if err == nil {
			val.SetBool(b)
		} else if dataVal.String() == "" {
			val.SetBool(false)
		} else {
			return fmt.Errorf("cannot parse '%s' as bool: %s", name, err)
		}
	default:
		return fmt.Errorf(
			"'%s' expected type '%s', got unconvertible type '%s'",
			name, val.Type(), dataVal.Type())
	}

	return nil
}

2.Interface

    decodeBasic会判断被转换对象是否可以直接转换成interface,如果可以转换,则进行转换,不能则构建错误信息:

// This decodes a basic type (bool, int, string, etc.) and sets the
// value to "data" of that type.
func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error {
	//read note 如果被转换的结果是interface,则只判断是否 AssignableTo
	dataVal := reflect.ValueOf(data)
	dataValType := dataVal.Type()
	if !dataValType.AssignableTo(val.Type()) {
		return fmt.Errorf(
			"'%s' expected type '%s', got '%s'",
			name, val.Type(), dataValType)
	}

	val.Set(dataVal)
	return nil
}

3.string

    decodeString和decodeBool有点类似,具体看代码:

func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
	dataVal := reflect.ValueOf(data)
	dataKind := d.getKind(dataVal)
	
	//read note string这边的转换规则如下:
	//	1、如果被转换的就是string,则直接转换成string
	//	开启了弱类型转换标识的
	//	2、bool转换从1或0
	//	3、数值类型按照十进制转换成字符串
	//	4、float类型,按照64位转换
	switch {
	case dataKind == reflect.String:
		val.SetString(dataVal.String())
	case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
		if dataVal.Bool() {
			val.SetString("1")
		} else {
			val.SetString("0")
		}
	case dataKind == reflect.Int && d.config.WeaklyTypedInput:
		val.SetString(strconv.FormatInt(dataVal.Int(), 10))
	case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
		val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
	case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
		val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
	default:
		return fmt.Errorf(
			"'%s' expected type '%s', got unconvertible type '%s'",
			name, val.Type(), dataVal.Type())
	}

	return nil
}

4.int

    decodeInt和decedeBool有点类似,具体看代码:

func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
	dataVal := reflect.ValueOf(data)
	dataKind := d.getKind(dataVal)

	//read note int这边的转换大致如下
	//	1、数值类型,直接转换成int,不考虑精度
	//	如果开启了弱类型转换标识
	//	2、bool类型按照0,1转换
	//	3、字符串通过 ParseInt转换
	//  4、否则错误
	switch {
	case dataKind == reflect.Int:
		val.SetInt(dataVal.Int())
	case dataKind == reflect.Uint:
		val.SetInt(int64(dataVal.Uint()))
	case dataKind == reflect.Float32:
		val.SetInt(int64(dataVal.Float()))
	case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
		if dataVal.Bool() {
			val.SetInt(1)
		} else {
			val.SetInt(0)
		}
	case dataKind == reflect.String && d.config.WeaklyTypedInput:
		i, err := strconv.ParseInt(dataVal.String(), 0, val.Type().Bits())
		if err == nil {
			val.SetInt(i)
		} else {
			return fmt.Errorf("cannot parse '%s' as int: %s", name, err)
		}
	default:
		return fmt.Errorf(
			"'%s' expected type '%s', got unconvertible type '%s'",
			name, val.Type(), dataVal.Type())
	}

	return nil
}

5.uint

    decodeUint和decodeBool类似,具体看代码:

func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
	dataVal := reflect.ValueOf(data)
	dataKind := d.getKind(dataVal)

	switch {
	case dataKind == reflect.Int:
		val.SetUint(uint64(dataVal.Int()))
	case dataKind == reflect.Uint:
		val.SetUint(dataVal.Uint())
	case dataKind == reflect.Float32:
		val.SetUint(uint64(dataVal.Float()))
	case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
		if dataVal.Bool() {
			val.SetUint(1)
		} else {
			val.SetUint(0)
		}
	case dataKind == reflect.String && d.config.WeaklyTypedInput:
		i, err := strconv.ParseUint(dataVal.String(), 0, val.Type().Bits())
		if err == nil {
			val.SetUint(i)
		} else {
			return fmt.Errorf("cannot parse '%s' as uint: %s", name, err)
		}
	default:
		return fmt.Errorf(
			"'%s' expected type '%s', got unconvertible type '%s'",
			name, val.Type(), dataVal.Type())
	}

	return nil
}

6.float32

    decodeFloat和decodeBool类似,具体看代码:

func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
	dataVal := reflect.ValueOf(data)
	dataKind := d.getKind(dataVal)

	switch {
	case dataKind == reflect.Int:
		val.SetFloat(float64(dataVal.Int()))
	case dataKind == reflect.Uint:
		val.SetFloat(float64(dataVal.Uint()))
	case dataKind == reflect.Float32:
		val.SetFloat(float64(dataVal.Float()))
	case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
		if dataVal.Bool() {
			val.SetFloat(1)
		} else {
			val.SetFloat(0)
		}
	case dataKind == reflect.String && d.config.WeaklyTypedInput:
		f, err := strconv.ParseFloat(dataVal.String(), val.Type().Bits())
		if err == nil {
			val.SetFloat(f)
		} else {
			return fmt.Errorf("cannot parse '%s' as float: %s", name, err)
		}
	default:
		return fmt.Errorf(
			"'%s' expected type '%s', got unconvertible type '%s'",
			name, val.Type(), dataVal.Type())
	}

	return nil
}

    

7.struct

    对结构体的转换分成下面几个步骤
    

类型判断

    这边主要有两个判断,一个是判断被转换的数据结构不是map,需要构建错误信息
    一个是map的key不是string或者interface类型,需要构建错误信息

	dataVal := reflect.Indirect(reflect.ValueOf(data))
	dataValKind := dataVal.Kind()
	//read note 被转换的类型不是map,错误
	if dataValKind != reflect.Map {
		return fmt.Errorf("'%s' expected a map, got '%s'", name, dataValKind)
	}

	dataValType := dataVal.Type()
	//read note map的key不是string或者Interface类型,错误
	if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface {
		return fmt.Errorf("'%s' needs a map with string keys, has '%s' keys", name, dataValType.Key().Kind())
	}

    

初始化两个key的数组

    这边初始化两个跟key相关的数组,分别是dataValKeys 和 dataValKeysUnused。

dataValKeys:帮助查找被转换的map的key.
dataValKeysUnused:存储了没有被转换的key

dataValKeys := make(map[reflect.Value]struct{})
	dataValKeysUnused := make(map[interface{}]struct{})
	//read note 循环map的key,组装key数组和未使用的key数组
	for _, dataValKey := range dataVal.MapKeys() {
		dataValKeys[dataValKey] = struct{}{}
		dataValKeysUnused[dataValKey.Interface()] = struct{}{}
	}

    

循环结构体获取所有的Field

    需要注意的是这边有一个特殊的处理,正常来说,如果是结构体的话,应该只需要遍历这个结构体的Field即可,但是这边提供了特殊的处理,就是对于匿名字段,如果tag上标注了 squash 则会把匿名字段加入到要被循环的结构体队列中,看一下下面定义的变量。

    structs 就是本次需要遍历的结构体数组,一开始是只有传入的这个结构体,然后在循环的过程中,把对应tag标识的匿名字段类型加入到structs中,后面的遍历会处理到,直到所有的structs被遍历则结束。

	structs := make([]reflect.Value, 1, 5)
	structs[0] = val
	errors := make([]string, 0)

	// Compile the list of all the fields that we're going to be decoding from all the structs.
	fields := make(map[*reflect.StructField]reflect.Value) //read note 创建【field->value】的map

    看一下遍历的处理:

    结束条件是structs大小为空,每一次处理第一个的元素,在循环结构体字段的过程中,遇到匿名字段,会获取他的tag标签 ,如果有 squash 标识的需要进入后面的循环,所以append到structs里面。

    并且在遍历的过程中,填充到fields中对应的字段类型和字段值的映射。

for len(structs) > 0 {
		structVal := structs[0]
		structs = structs[1:]

		structType := structVal.Type()
		//read note 循环结构体的所有Field字段
		for i := 0; i < structType.NumField(); i++ {
			fieldType := structType.Field(i)

			//read note 匿名字段处理
			if fieldType.Anonymous {
				fieldKind := fieldType.Type.Kind()
				//read note 非结构体,错误
				if fieldKind != reflect.Struct {
					errors = appendErrors(errors,
						fmt.Errorf("%s: unsupported type: %s", fieldType.Name, fieldKind))
					continue
				}

				// We have an embedded field. We "squash" the fields down
				// if specified in the tag.
				//read note squash标签只作用在匿名字段上,会对匿名字段结构体的字段进行深入赋值.
				squash := false
				tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",")
				for _, tag := range tagParts[1:] {
					if tag == "squash" {
						squash = true
						break
					}
				}

				//read note 如果是squash标签标识,会加入到structs数组中,在下一次循环开始之前进行循环
				if squash {
					structs = append(structs, val.FieldByName(fieldType.Name))
					continue
				}
			}

			// Normal struct field, store it away
			//read note 组装map指向
			fields[&fieldType] = structVal.Field(i)
		}
	}
循环Filed数组进行Decode

    当所有的Field被组装完成之后,会对Field进行遍历。

    这边的第一步处理是别名的获取,因为我们可以指定map的key名称与结构体字段对应,所以这边会根据TagName设置的标识 结构体的tag中找到对应的 名称

		fieldName := fieldType.Name

		//read note 获取tag标签,进行【,】切割,这边的标识默认是 mapstructure,比如说 mapstructure:name.获取的就是name
		tagValue := fieldType.Tag.Get(d.config.TagName)
		tagValue = strings.SplitN(tagValue, ",", 2)[0]
		if tagValue != "" {
			fieldName = tagValue
		}

    第二步:遍历key数组,获得map中对应的值,这边是不区分大小写的处理,可以查看下面的代码,而且如果key对应的值是默认值也直接跳过,因为默认值不需要处理,初始化结构体就存在了。

if !rawMapVal.IsValid() {
			// Do a slower search by iterating over each key and
			// doing case-insensitive search.
			for dataValKey, _ := range dataValKeys {
				mK, ok := dataValKey.Interface().(string)
				if !ok {
					// Not a string key
					continue
				}

				//read note 不区分大小写
				if strings.EqualFold(mK, fieldName) {
					rawMapKey = dataValKey
					rawMapVal = dataVal.MapIndex(dataValKey)
					break
				}
			}

			if !rawMapVal.IsValid() {
				// There was no matching key in the map for the value in
				// the struct. Just ignore.
				continue
			}
		}

    第三步就是聪Unused的key数组中删除本次进行转换的key:

	// Delete the key we're using from the unused map so we stop tracking
	//read note 未使用的key数组把被转换的key删掉
	delete(dataValKeysUnused, rawMapKey.Interface())

    第四步,零值报错,因为不可能是零值,这个情况是不可能存在的把,并且如果Feild没办法Set,跳过本次处理
结合三四两步,可以看到无论是否处理成功,被识别出处理的key,都会被认为是已经处理过的key

        //read note 非IsValid直接报错
		if !field.IsValid() {
			// This should never happen
			panic("field is not valid")
		}

		// If we can't set the field, then it is unexported or something,
		// and we just continue onwards.
		if !field.CanSet() {
			continue
		}

    第五步:调用decode进行当前Field的处理

	// If the name is empty string, then we're at the root, and we
		// don't dot-join the fields.
		if name != "" {
			fieldName = fmt.Sprintf("%s.%s", name, fieldName)
		}

		//read note 字段再递归进去处理,可能是对应的不同的类型
		if err := d.decode(fieldName, rawMapVal.Interface(), field); err != nil {
			errors = appendErrors(errors, err)
		}
未转换的key处理

    如果ErrorUnused标识是true,则说明如果有未转换的key需要报错,所以这边会配置未转换的key数组进行构造错误的判断处理:

	if d.config.ErrorUnused && len(dataValKeysUnused) > 0 {
		keys := make([]string, 0, len(dataValKeysUnused))
		for rawKey, _ := range dataValKeysUnused {
			keys = append(keys, rawKey.(string))
		}
		sort.Strings(keys)

		err := fmt.Errorf("'%s' has invalid keys: %s", name, strings.Join(keys, ", "))
		errors = appendErrors(errors, err)
	}
Metadata处理

    metadata中组转未使用的字段key

	if d.config.Metadata != nil {
		for rawKey, _ := range dataValKeysUnused {
			key := rawKey.(string)
			if name != "" {
				key = fmt.Sprintf("%s.%s", name, key)
			}

			d.config.Metadata.Unused = append(d.config.Metadata.Unused, key)
		}
	}

    

8.map

    decodeMap这边分成几个处理步骤,具体看一下下面的代码

创建新map

    最开始根据对应的key和value的类型就行新map的构建:

	valType := val.Type()
	valKeyType := valType.Key()
	valElemType := valType.Elem()

	// Make a new map to hold our result
	//read note 通过反射创建新的map
	mapType := reflect.MapOf(valKeyType, valElemType)
	valMap := reflect.MakeMap(mapType)
弱类型处理

    如果被转换的数据不是map类型,这边会去判断是否弱类型标识,如果是空数组则转换空map返回,否则需要构建错误信息

	// Check input type
	dataVal := reflect.Indirect(reflect.ValueOf(data))
	//read note 如果被转换的结构体不是map,这边需要满足
	//	1、弱类型标识打开
	//	2、空切片或空数组
	//	才会返回空map,否则组装错误信息
	if dataVal.Kind() != reflect.Map {
		// Accept empty array/slice instead of an empty map in weakly typed mode
		if d.config.WeaklyTypedInput &&
			(dataVal.Kind() == reflect.Slice || dataVal.Kind() == reflect.Array) &&
			dataVal.Len() == 0 {
			val.Set(valMap)
			return nil
		} else {
			return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
		}
	}
遍历key处理Map

    经过上面的判断,可以认定被转换的数据一定是map类型,所以这边直接循环map的key,对map的key和value都进行decode的调用,深入到更底层进行赋值操作。

    如果key和value都成功转换了,就可以进行map key、value的映射。

	//read note 遍历被转换结构体的所有key
	for _, k := range dataVal.MapKeys() {
		fieldName := fmt.Sprintf("%s[%s]", name, k)

		// First decode the key into the proper type
		//read note 对key进行转换 decode
		currentKey := reflect.Indirect(reflect.New(valKeyType))
		if err := d.decode(fieldName, k.Interface(), currentKey); err != nil {
			errors = appendErrors(errors, err)
			continue
		}

		// Next decode the data into the proper type
		//read note 对value进行转换 decode
		v := dataVal.MapIndex(k).Interface()
		currentVal := reflect.Indirect(reflect.New(valElemType))
		if err := d.decode(fieldName, v, currentVal); err != nil {
			errors = appendErrors(errors, err)
			continue
		}

		//read note 对结果map进行组装
		valMap.SetMapIndex(currentKey, currentVal)
	}

    最后就是设置转换结果的值和错误信息处理:

	// Set the built up map to the value
	//read note map设值
	val.Set(valMap)

	// If we had errors, return those
	if len(errors) > 0 {
		return &Error{errors}
	}

    

9.slice

    decodeSlice和decodeMap有点类似,阿是处理起来更加简单

构建新切片

    这边会根据 转换结果的类型,构造出新的切片:

	dataVal := reflect.Indirect(reflect.ValueOf(data))
	dataValKind := dataVal.Kind()
	valType := val.Type()
	valElemType := valType.Elem()

	// Make a new slice to hold our result, same size as the original data.
	//read note 构造新的切片
	sliceType := reflect.SliceOf(valElemType)
	valSlice := reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
弱类型处理

    如果被转换的数据不是Array或者Slice类型,只有当弱类型标识打开的时候,才会对空的map返回空数组为转换结果。否则会构建错误信息。

// Check input type
	//read note 同样的弱类型转换,空的map直接返回空数组(弱类型标识为true的情况下)
	if dataValKind != reflect.Array && dataValKind != reflect.Slice {
		// Accept empty map instead of array/slice in weakly typed mode
		if d.config.WeaklyTypedInput && dataVal.Kind() == reflect.Map && dataVal.Len() == 0 {
			val.Set(valSlice)
			return nil
		} else {
			return fmt.Errorf(
				"'%s': source data must be an array or slice, got %s", name, dataValKind)
		}
	}

循环切片转换

    通过上面的判断可以得到被转换的数据就是切片或者数组类型,这边通过遍历被转换的切片,拿到每一个元素,再通过decode方法深入每个元素进行转换,组装成最终的切片。

	//read note 遍历原来的数组,进行每个元素的转换 decode
	for i := 0; i < dataVal.Len(); i++ {
		currentData := dataVal.Index(i).Interface()
		currentField := valSlice.Index(i)

		fieldName := fmt.Sprintf("%s[%d]", name, i)
		if err := d.decode(fieldName, currentData, currentField); err != nil {
			errors = appendErrors(errors, err)
		}
	}

    最后就是切片的设置以及错误信息的处理:

	// Finally, set the value to the slice we built up
	//read note 设置切片的值
	val.Set(valSlice)

	// If there were errors, we return those
	if len(errors) > 0 {
		return &Error{errors}
	}

    

d.处理metadata

    被处理的key的名称不为空字符串,则需要添加处理过的key到Metadata中,比较简单,具体看代码:

	// If we reached here, then we successfully decoded SOMETHING, so
	// mark the key as used if we're tracking metadata.
	//read note 对处理玩的Metadata进行组装,但是这边看好像一定是不会处理的,因为传进来的name一直是空字符串
	if d.config.Metadata != nil && name != "" {
		d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
	}

    

4、DecodePath:部分转换

    DecodePath提供一个比较合理的转换方式,就是jpath标签标识字段需要转换的数据层次。比如说我有个字段Age,
需要从map的birth字段转换过来,那么我的map和字段处理如下:

	//map的结构:
	{
		"people":{
			"age":{
				"birth":"10", //这个是需要转换成Age的字段
				"birthDay":"2021-1-27"	
			}
		}
	}

	type example struct{
		Age `jpath:"age.birth"`
	}

    所以DecodePath比较出奇的地方就是通过jpath标签指明字段对应的map中实际的值。

    具体来看一下DecodePath这边的处理:

    

a.类型判断处理

    DecodePath这边会对转换结果的类型进行判断,因为传进来的可能是指针或者是结构体类型,或者是其他的类型,这边会对指定的类型进行处理,指针转换成结构体Value,非指针或者结构体的不进行处理:

	decoded := false

	var val reflect.Value
	reflectRawValue := reflect.ValueOf(rawVal)
	kind := reflectRawValue.Kind()

	//read note 对传入的 转换结果的数据类型进行判断,转换成对应的结构体类型
	switch kind {
	case reflect.Ptr:
		val = reflectRawValue.Elem()
		if val.Kind() != reflect.Struct {
			return decoded, fmt.Errorf("Incompatible Type : %v : Looking For Struct", kind)
		}
	case reflect.Struct:
		var ok bool
		val, ok = rawVal.(reflect.Value)
		if ok == false {
			return decoded, fmt.Errorf("Incompatible Type : %v : Looking For reflect.Value", kind)
		}
	default:
		return decoded, fmt.Errorf("Incompatible Type : %v", kind)
	}

    

b.Field数组处理

    对类型进行判断之后,就是对结构体的所有Field数组进行遍历处理:

	for i := 0; i < val.NumField(); i++ {
		....
	}

    

1.获取jpath标签tag

    获取Field,再查找Field上携带的标签jpath

	valueField := val.Field(i)
	typeField := val.Type().Field(i)
	tag := typeField.Tag
	//read note 这边的tag是通过 jpath来识别的
	tagValue := tag.Get("jpath")

    

2.tag为空字符串的处理

    如果字段上的tag标签为空字符串,这边要进行特殊的处理:

    这个的处理思路是:

  1. 如果是结构体,再次调用DecodePath处理,进入到结构体中进行处理,会遍历该结构体的所有字段,如果是基本类型还是没有jpath标签,则不进行赋值
  2. 如果是指针类型,对Field进行初始化(如果需要的话),再进行第一步的处理
	if tagValue == "" {
		//read note 如果是结构体,调用DecodePath处理
		if valueField.Kind() == reflect.Struct {
			// We have a struct that may have indivdual tags. Process separately
			d.DecodePath(m, valueField)
			continue
		} else if valueField.Kind() == reflect.Ptr && reflect.TypeOf(valueField).Kind() == reflect.Struct {
			//read note 指针的处理,转换成结构体也是类型的DecodePath的处理

			// We have a pointer to a struct
			if valueField.IsNil() {
				// Create the object since it doesn't exist
				valueField.Set(reflect.New(valueField.Type().Elem()))
				decoded, _ = d.DecodePath(m, valueField.Elem())
				if decoded == false {
					// If nothing was decoded for this object return the pointer to nil
					valueField.Set(reflect.NewAt(valueField.Type().Elem(), nil))
				}
				continue
			}
			d.DecodePath(m, valueField.Elem())
			continue
		}
	}

    

3.分隔tag标签为keys

    这边通过 . 进行分割,就是我们上面讲的深入到map中去获取对应key的value

	//read note jpath后面支持的别名可以是递进到更进去的层次,通过.标识,比如说 Age.Birth.  表示 Age:{Birth:100,。。。}
	keys := strings.Split(tagValue, ".")

    

4.通过keys到map中查找数据

    这边通过keys进行数据的查询,本质上就是一个递归,每次通过keys的第一个值进行map的查询,这边支持的map是map[string]interface类型的,这个是需要注意的.

// findData locates the data by walking the keys down the map
func (d *Decoder) findData(m map[string]interface{}, keys []string) interface{} {
	//read note 这是一个递归方法,所以递归的结束就是keys最终变成一个值,查找该值,能找到则返回,否则返回nil
	if len(keys) == 1 {
		if value, ok := m[keys[0]]; ok == true {
			return value
		}
		return nil
	}

	//read note 继续进行递归,下一次递归就是往下一个key开始,可以理解就是map一层一层的下去查找对应的key.
	if value, ok := m[keys[0]]; ok == true {
		if m, ok := value.(map[string]interface{}); ok == true {
			return d.findData(m, keys[1:])
		}
	}

	return nil
}

    

5.数组类型的处理

    如果第4步查找到数据,则这边会分成两部分的处理,一个是查找到的数据是数组,另一个是非数组的处理

    如果查找到的数据的数组,这边的处理步骤如下:

  1. 如果转换结果的字段类型是map数组,没办法在这边进行处理,需要跳转到下一步去处理
  2. 如果被转换的数据是map[string]interface的切片才能继续处理,否则需要跳转到下一步处理
  3. 通过被转换的数据,组装新的map[string]interface的切片数据,调用DecodeSlicePath处理

    DecodeSlicePath请看后面的介绍.

if valueField.Kind() == reflect.Slice {
	// Ignore a slice of maps - This sucks but not sure how to check
	//read note 如果Field是map数组,这边没办法处理,只能通过下面的decode方法进行具体的处理
	if strings.Contains(valueField.Type().String(), "map[") {
		goto normal_decode
	}

	// We have a slice
	mapSlice := data.([]interface{})
	if len(mapSlice) > 0 {
		// Test if this is a slice of more maps
		//read note 转换成切片的数据如果不是 map的数组,则跳转到下面通过 decode处理.因为这边是切片对切片,map对结构体
		_, ok := mapSlice[0].(map[string]interface{})
		if ok == false {
			goto normal_decode
		}

		// Extract the maps out and run it through DecodeSlicePath
		//read note 组转map数组
		ms := make([]map[string]interface{}, len(mapSlice))
		for index, m2 := range mapSlice {
			ms[index] = m2.(map[string]interface{})
		}

		//调用DecodeSlicePath,转换 map数组 -》 结构体数组
		DecodeSlicePath(ms, valueField.Addr().Interface())
		continue
	}
}

    

6.非数组类型的处理

    不是数组的话,就会调用decode进行处理,这一切的前提都是tag需要包含jpath标签,并且再map中能查找到数据。

	//read note 通过decode处理,这边同样应该支持 mapstructure的tag标签
 normal_decode:
	decoded = true
	err := d.decode("", data, valueField)
	if err != nil {
		return false, err
	}

    

5、DecodeSlicePath:数组类型的部分转换

    

a.检验是否为切片

    在进行转换之前,需要判断转换的结果是不是切片,否则就没办法进行转换

	reflectRawSlice := reflect.TypeOf(rawSlice)
	rawKind := reflectRawSlice.Kind()
	rawElement := reflectRawSlice.Elem()

	//read note 校验是否为切片
	if (rawKind == reflect.Ptr && rawElement.Kind() != reflect.Slice) ||
		(rawKind != reflect.Ptr && rawKind != reflect.Slice) {
		return fmt.Errorf("Incompatible Value, Looking For Slice : %v : %v", rawKind, rawElement.Kind())
	}

    

b.初始化decoder

    初始化decoder是为了调用DecodePath方法

	config := &DecoderConfig{
		Metadata: nil,
		Result:   nil,
	}

	decoder, err := NewPathDecoder(config)
	if err != nil {
		return err
	}

    

c.初始化新数组

    初始化一个转换结果的数组,用来承装转换的结果:

	// Create a slice large enough to decode all the values
	//read note 构造一个新的数组
	valSlice := reflect.MakeSlice(rawElement, len(ms), len(ms))

    

d.循环处理转换

    循环处理这边比较简单,需要判断转换结果的数据类型,主要是结构体和指针的不同处理

	// Iterate over the maps and decode each one
	//read note 循环被转换的map数组
	for index, m := range ms {
		sliceElementType := rawElement.Elem()
		if sliceElementType.Kind() != reflect.Ptr {
			// A slice of objects
			//read note 如果转换的结果是结构体类型,处理转换
			obj := reflect.New(rawElement.Elem())
			decoder.DecodePath(m, reflect.Indirect(obj))
			indexVal := valSlice.Index(index)
			indexVal.Set(reflect.Indirect(obj))
		} else {
			// A slice of pointers
			//read note 如果转换的结果是指针类型,处理转换
			obj := reflect.New(rawElement.Elem().Elem())
			decoder.DecodePath(m, reflect.Indirect(obj))
			indexVal := valSlice.Index(index)
			indexVal.Set(obj)
		}
	}

    

e.设置新数组

    设置新数组,转换完成

	// Set the new slice
	//read note 设置转换后的数组数据
	reflect.ValueOf(rawSlice).Elem().Set(valSlice)

    

6、测试代码

	import (
		entity2 "Go-StudyExample/entity"
		"encoding/json"
		"fmt"
		"github.com/goinggo/mapstructure"
		"testing"
		"time"
	)
	
	//-----------------------json数据---------------------
	var document = `{"loginName":"sptest1","userType":{"userTypeId":1,"userTypeName":"normal_user","t":"2026-01-02 15:04:05"}}`
	
	var document1 = `{"cobrandId":10010352,"channelId":-1,"locale":"en_US","tncVersion":2,"people":[{"name":"jack","age":{"birth":10,"year":2000,"animals":[{"barks":"yes","tail":"yes"},{"barks":"no","tail":"yes"}]}},{"name":"jill","age":{"birth":11,"year":2001}}]}`
	
	var document2 = `[{"name":"bill"},{"name":"lisa"}]`
	
	var document3 = `{"categories":["12","222","333"],"people":{"name":"jack","age":{"birth":10,"year":2000,"animals":[{"barks":"yes","tail":"yes"},{"barks":"no","tail":"yes"}]}}}`
	
	//-----------------------------结构体-----------------------
	type Animal struct {
		Barks string `jpath:"barks"`
	}
	
	type People struct {
		Age     int      `jpath:"age.birth"` // jpath is relative to the array
		Animals []Animal `jpath:"age.animals"`
	}
	
	type Items struct {
		Categories []string `jpath:"categories"`
		Peoples    []People `jpath:"people"` // Specify the location of the array
	}
	
	type Items1 struct {
		Categories []string `mapstructure:"Categories"`
		Peoples    People1  `mapstructure:"People"` // Specify the location of the array
	}
	
	type People1 struct {
		Age     int      `mapstructure:"age.birth"` // jpath is relative to the array
		Animals []Animal `mapstructure:"age.animals"`
	}
	
	type NameDoc struct {
		Name string `jpath:"name"`
	}
	
	type UserType struct {
		UserTypeId   int
		UserTypeName string
	}
	
	type User struct {
		UserType  UserType  `jpath:"userType"`
		LoginName string    `jpath:"loginName"`
		T         time.Time `jpath:"t"`
	}
	
	
	//---------------------------------测试方法------------------------
	
	//基本测试
	func TestMapStructureTestFunc(t *testing.T) {
		var te entity2.Entity
		m := make(map[string]interface{})
		m["Num"] = 1
		m["S"] = "test"
		m["T"] = map[string]string{"1": "1", "2": "2"}
	
		err := mapstructure.Decode(m, &te)
		if err != nil {
			panic(err.Error())
		}
	
		fmt.Print(te.Num, " ", te.S, " ", te.T)
	}
	
	//基本测试
	func TestMapStructureTestFunc1(t *testing.T) {
		var docMap map[string]interface{}
		_ = json.Unmarshal([]byte(document), &docMap)
	
		var user entity2.User
		err := mapstructure.Decode(docMap, &user)
	
		if err != nil {
			panic(err.Error())
		}
		fmt.Println(user.T.Format("2006-01-02 15:04:05"))
		fmt.Println(user, " ", user.UserType.UserTypeId, " ", user.UserType.UserTypeName)
	}
	
	//测试切片转换
	func TestMapStructureTestFunc2(t *testing.T) {
	
		sliceScript := []byte(document2)
		var sliceMap []map[string]interface{}
		_ = json.Unmarshal(sliceScript, &sliceMap)
	
		var myslice []NameDoc
		err := mapstructure.DecodeSlicePath(sliceMap, &myslice)
	
		if err != nil {
			panic(err.Error())
		}
	
		fmt.Println(myslice[0], " ", myslice[1])
	}
	
	//测试DecodePath
	func TestMapStructureTestFunc3(t *testing.T) {
		docScript := []byte(document1)
		var docMap map[string]interface{}
		_ = json.Unmarshal(docScript, &docMap)
	
		var items Items
		err := mapstructure.DecodePath(docMap, &items)
		if err != nil {
			panic(err.Error())
		}
		fmt.Println(items.Peoples[0], items.Peoples[1])
	}
	
	//MetaData的测试例子
	//decode不支持 mapstructure的标识是  xxxx.xxxx
	//并且这边是对tag别名不区分大小写的
	func TestMetaData(t *testing.T) {
		var items1 Items1
		config := &mapstructure.DecoderConfig{
			Metadata: &mapstructure.Metadata{
				Keys:   nil,
				Unused: nil,
			},
			Result: &items1,
		}
		decoder, _ := mapstructure.NewDecoder(config)
		docScript := []byte(document3)
		var docMap map[string]interface{}
		_ = json.Unmarshal(docScript, &docMap)
	
		_ = decoder.Decode(docMap)
		fmt.Println(config.Metadata)
	}

 

7、总结

  1. mapstructure这个工程主要支持两种tag标签,一个是mapstructure,这个主要是用在Decode中,一个是jpath标签,这个主要是用在DecodePath中。
  2. 在DecodePath中metadata如果是map中包含的是一个结构体(转换结果),才可能会记录这个结构体对应的数据。这个处理有点不行
  3. DecodePath方法对没有标注jpath的字段是不处理的,而Decode如果没有标签,会拿结构体的FieldName进行处理

你可能感兴趣的:(Go语言学习,源码阅读,源码,Go,mapstructure,反射,map转结构体)