format.go
func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc {
	return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
		if from.Kind() != reflect.String {
			return data, nil
		}
		for _, v := range f.data {
			tpe, _ := f.GetType(v.Name)
			if to == tpe {
				switch v.Name {
				case "date":
					d, err := time.Parse(RFC3339FullDate, data.(string))
					if err != nil {
						return nil, err
					}
					return Date(d), nil
				case "datetime":
					input := data.(string)
					if len(input) == 0 {
						return nil, fmt.Errorf("empty string is an invalid datetime format")
					}
					return ParseDateTime(input)
				case "duration":
					dur, err := ParseDuration(data.(string))
					if err != nil {
						return nil, err
					}
					return Duration(dur), nil
				case "uri":
					return URI(data.(string)), nil
				case "email":
					return Email(data.(string)), nil
				case "uuid":
					return UUID(data.(string)), nil
				case "uuid3":
					return UUID3(data.(string)), nil
				case "uuid4":
					return UUID4(data.(string)), nil
				case "uuid5":
					return UUID5(data.(string)), nil
				case "hostname":
					return Hostname(data.(string)), nil
				case "ipv4":
					return IPv4(data.(string)), nil
				case "ipv6":
					return IPv6(data.(string)), nil
				case "cidr":
					return CIDR(data.(string)), nil
				case "mac":
					return MAC(data.(string)), nil
				case "isbn":
					return ISBN(data.(string)), nil
				case "isbn10":
					return ISBN10(data.(string)), nil
				case "isbn13":
					return ISBN13(data.(string)), nil
				case "creditcard":
					return CreditCard(data.(string)), nil
				case "ssn":
					return SSN(data.(string)), nil
				case "hexcolor":
					return HexColor(data.(string)), nil
				case "rgbcolor":
					return RGBColor(data.(string)), nil
				case "byte":
					return Base64(data.(string)), nil
				case "password":
					return Password(data.(string)), nil
				default:
					return nil, errors.InvalidTypeName(v.Name)
				}
			}
		}
		return data, nil
	}
}
func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc {
	return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
		if from.Kind() != reflect.String {
			return data, nil
		}
		for _, v := range f.data {
			tpe, _ := f.GetType(v.Name)
			if to == tpe {
				switch v.Name {
				case "date":
					d, err := time.Parse(RFC3339FullDate, data.(string))
					if err != nil {
						return nil, err
					}
					return Date(d), nil
				case "datetime":
					input := data.(string)
					if len(input) == 0 {
						return nil, fmt.Errorf("empty string is an invalid datetime format")
					}
					return ParseDateTime(input)
				case "duration":
					dur, err := ParseDuration(data.(string))
					if err != nil {
						return nil, err
					}
					return Duration(dur), nil
				case "uri":
					return URI(data.(string)), nil
				case "email":
					return Email(data.(string)), nil
				case "uuid":
					return UUID(data.(string)), nil
				case "uuid3":
					return UUID3(data.(string)), nil
				case "uuid4":
					return UUID4(data.(string)), nil
				case "uuid5":
					return UUID5(data.(string)), nil
				case "hostname":
					return Hostname(data.(string)), nil
				case "ipv4":
					return IPv4(data.(string)), nil
				case "ipv6":
					return IPv6(data.(string)), nil
				case "cidr":
					return CIDR(data.(string)), nil
				case "mac":
					return MAC(data.(string)), nil
				case "isbn":
					return ISBN(data.(string)), nil
				case "isbn10":
					return ISBN10(data.(string)), nil
				case "isbn13":
					return ISBN13(data.(string)), nil
				case "creditcard":
					return CreditCard(data.(string)), nil
				case "ssn":
					return SSN(data.(string)), nil
				case "hexcolor":
					return HexColor(data.(string)), nil
				case "rgbcolor":
					return RGBColor(data.(string)), nil
				case "byte":
					return Base64(data.(string)), nil
				case "password":
					return Password(data.(string)), nil
				default:
					return nil, errors.InvalidTypeName(v.Name)
				}
			}
		}
		return data, nil
	}
}
func ( f * defaultFormats ) MapStructureHookFunc ( ) mapstructure . DecodeHookFunc { return func ( from reflect . Type , to reflect . Type , data interface { } ) ( interface { } , error ) { if from . Kind ( ) != reflect . String { return data , nil } for _ , v := range f . data { tpe , _ := f . GetType ( v . Name ) if to == tpe { switch v . Name { case " " : d , err := time . Parse ( RFC3339FullDate , data . ( string ) ) if err != nil { return nil , err } return Date ( d ) , nil case " " : input := data . ( string ) if len ( input ) == 0 { return nil , fmt . Errorf ( " " ) } return ParseDateTime ( input ) case " " : dur , err := ParseDuration ( data . ( string ) ) if err != nil { return nil , err } return Duration ( dur ) , nil case " " : return URI ( data . ( string ) ) , nil case " " : return Email ( data . ( string ) ) , nil case " " : return UUID ( data . ( string ) ) , nil case " " : return UUID3 ( data . ( string ) ) , nil case " " : return UUID4 ( data . ( string ) ) , nil case " " : return UUID5 ( data . ( string ) ) , nil case " " : return Hostname ( data . ( string ) ) , nil case " " : return IPv4 ( data . ( string ) ) , nil case " " : return IPv6 ( data . ( string ) ) , nil case " " : return CIDR ( data . ( string ) ) , nil case " " : return MAC ( data . ( string ) ) , nil case " " : return ISBN ( data . ( string ) ) , nil case " " : return ISBN10 ( data . ( string ) ) , nil case " " : return ISBN13 ( data . ( string ) ) , nil case " " : return CreditCard ( data . ( string ) ) , nil case " " : return SSN ( data . ( string ) ) , nil case " " : return HexColor ( data . ( string ) ) , nil case " " : return RGBColor ( data . ( string ) ) , nil case " " : return Base64 ( data . ( string ) ) , nil case " " : return Password ( data . ( string ) ) , nil default : return nil , errors . InvalidTypeName ( v . Name ) } } } return data , nil } }
// MapStructureHookFunc is a decode hook function for mapstructure
MapStructureHookFunc is a decode hook function for mapstructure