vendor/github.com/mitchellh/mapstructure/decode_hooks.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
    60 // The composed funcs are called in order, with the result of the
    60 // The composed funcs are called in order, with the result of the
    61 // previous transformation.
    61 // previous transformation.
    62 func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
    62 func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
    63 	return func(f reflect.Value, t reflect.Value) (interface{}, error) {
    63 	return func(f reflect.Value, t reflect.Value) (interface{}, error) {
    64 		var err error
    64 		var err error
    65 		var data interface{}
    65 		data := f.Interface()
       
    66 
    66 		newFrom := f
    67 		newFrom := f
    67 		for _, f1 := range fs {
    68 		for _, f1 := range fs {
    68 			data, err = DecodeHookExec(f1, newFrom, t)
    69 			data, err = DecodeHookExec(f1, newFrom, t)
    69 			if err != nil {
    70 			if err != nil {
    70 				return nil, err
    71 				return nil, err
    71 			}
    72 			}
    72 			newFrom = reflect.ValueOf(data)
    73 			newFrom = reflect.ValueOf(data)
    73 		}
    74 		}
    74 
    75 
    75 		return data, nil
    76 		return data, nil
       
    77 	}
       
    78 }
       
    79 
       
    80 // OrComposeDecodeHookFunc executes all input hook functions until one of them returns no error. In that case its value is returned.
       
    81 // If all hooks return an error, OrComposeDecodeHookFunc returns an error concatenating all error messages.
       
    82 func OrComposeDecodeHookFunc(ff ...DecodeHookFunc) DecodeHookFunc {
       
    83 	return func(a, b reflect.Value) (interface{}, error) {
       
    84 		var allErrs string
       
    85 		var out interface{}
       
    86 		var err error
       
    87 
       
    88 		for _, f := range ff {
       
    89 			out, err = DecodeHookExec(f, a, b)
       
    90 			if err != nil {
       
    91 				allErrs += err.Error() + "\n"
       
    92 				continue
       
    93 			}
       
    94 
       
    95 			return out, nil
       
    96 		}
       
    97 
       
    98 		return nil, errors.New(allErrs)
    76 	}
    99 	}
    77 }
   100 }
    78 
   101 
    79 // StringToSliceHookFunc returns a DecodeHookFunc that converts
   102 // StringToSliceHookFunc returns a DecodeHookFunc that converts
    80 // string to []string by splitting on the given sep.
   103 // string to []string by splitting on the given sep.