func setModelIdentifierHook()

in pkg/asyncapi/v2/decode.go [66:83]


func setModelIdentifierHook(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
	if from.Kind() != reflect.Map || to.Kind() != reflect.Map {
		return data, nil
	}

	identifiableInterface := reflect.TypeOf((*asyncapi.Identifiable)(nil)).Elem()
	if to.Key() != reflect.TypeOf("string") || !to.Elem().Implements(identifiableInterface) {
		return data, nil
	}

	fieldName := reflect.New(to.Elem()).Interface().(asyncapi.Identifiable).IDField()
	for k, v := range data.(map[string]interface{}) {
		// setting the value directly in the raw map. The struct needs to keep the mapstructure field tag so it unmarshals the field.
		v.(map[string]interface{})[fieldName] = k
	}

	return data, nil
}