vendor/github.com/mitchellh/mapstructure/decode_hooks.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 11 Jul 2021 10:35:56 +0200
changeset 256 6d9efbef00a9
parent 251 1c52a0eeb952
child 260 445e01aede7e
permissions -rw-r--r--
Update dependencies
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package mapstructure
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
     4
	"encoding"
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"errors"
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
     6
	"fmt"
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
     7
	"net"
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
	"reflect"
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
	"strconv"
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"strings"
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"time"
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
)
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
// typedDecodeHook takes a raw DecodeHookFunc (an interface{}) and turns
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
// it into the proper DecodeHookFunc type, such as DecodeHookFuncType.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
func typedDecodeHook(h DecodeHookFunc) DecodeHookFunc {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	// Create variables here so we can reference them with the reflect pkg
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	var f1 DecodeHookFuncType
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
	var f2 DecodeHookFuncKind
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    20
	var f3 DecodeHookFuncValue
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	// Fill in the variables into this interface and the rest is done
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	// automatically using the reflect package.
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    24
	potential := []interface{}{f1, f2, f3}
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	v := reflect.ValueOf(h)
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	vt := v.Type()
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	for _, raw := range potential {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
		pt := reflect.ValueOf(raw).Type()
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
		if vt.ConvertibleTo(pt) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
			return v.Convert(pt).Interface()
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	return nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
// DecodeHookExec executes the given decode hook. This should be used
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
// since it'll naturally degrade to the older backwards compatible DecodeHookFunc
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
// that took reflect.Kind instead of reflect.Type.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
func DecodeHookExec(
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	raw DecodeHookFunc,
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    43
	from reflect.Value, to reflect.Value) (interface{}, error) {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    44
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	switch f := typedDecodeHook(raw).(type) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	case DecodeHookFuncType:
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    47
		return f(from.Type(), to.Type(), from.Interface())
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	case DecodeHookFuncKind:
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    49
		return f(from.Kind(), to.Kind(), from.Interface())
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    50
	case DecodeHookFuncValue:
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    51
		return f(from, to)
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
	default:
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
		return nil, errors.New("invalid decode hook signature")
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
// ComposeDecodeHookFunc creates a single DecodeHookFunc that
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
// automatically composes multiple DecodeHookFuncs.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
//
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
// The composed funcs are called in order, with the result of the
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
// previous transformation.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    63
	return func(f reflect.Value, t reflect.Value) (interface{}, error) {
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
		var err error
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    65
		var data interface{}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    66
		newFrom := f
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
		for _, f1 := range fs {
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    68
			data, err = DecodeHookExec(f1, newFrom, t)
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
			if err != nil {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
				return nil, err
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
			}
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    72
			newFrom = reflect.ValueOf(data)
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
		return data, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
// StringToSliceHookFunc returns a DecodeHookFunc that converts
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
// string to []string by splitting on the given sep.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
func StringToSliceHookFunc(sep string) DecodeHookFunc {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
	return func(
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
		f reflect.Kind,
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
		t reflect.Kind,
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
		data interface{}) (interface{}, error) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
		if f != reflect.String || t != reflect.Slice {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
			return data, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
		raw := data.(string)
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
		if raw == "" {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
			return []string{}, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
		return strings.Split(raw, sep), nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
// StringToTimeDurationHookFunc returns a DecodeHookFunc that converts
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
// strings to time.Duration.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
func StringToTimeDurationHookFunc() DecodeHookFunc {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
	return func(
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
		f reflect.Type,
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
		t reflect.Type,
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
		data interface{}) (interface{}, error) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
		if f.Kind() != reflect.String {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
			return data, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
		if t != reflect.TypeOf(time.Duration(5)) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
			return data, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
		// Convert it by parsing
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
		return time.ParseDuration(data.(string))
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   118
// StringToIPHookFunc returns a DecodeHookFunc that converts
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   119
// strings to net.IP
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   120
func StringToIPHookFunc() DecodeHookFunc {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   121
	return func(
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   122
		f reflect.Type,
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   123
		t reflect.Type,
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   124
		data interface{}) (interface{}, error) {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   125
		if f.Kind() != reflect.String {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   126
			return data, nil
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   127
		}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   128
		if t != reflect.TypeOf(net.IP{}) {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   129
			return data, nil
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   130
		}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   131
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   132
		// Convert it by parsing
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   133
		ip := net.ParseIP(data.(string))
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   134
		if ip == nil {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   135
			return net.IP{}, fmt.Errorf("failed parsing ip %v", data)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   136
		}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   137
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   138
		return ip, nil
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   139
	}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   140
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   141
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   142
// StringToIPNetHookFunc returns a DecodeHookFunc that converts
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   143
// strings to net.IPNet
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   144
func StringToIPNetHookFunc() DecodeHookFunc {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   145
	return func(
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   146
		f reflect.Type,
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   147
		t reflect.Type,
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   148
		data interface{}) (interface{}, error) {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   149
		if f.Kind() != reflect.String {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   150
			return data, nil
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   151
		}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   152
		if t != reflect.TypeOf(net.IPNet{}) {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   153
			return data, nil
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   154
		}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   155
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   156
		// Convert it by parsing
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   157
		_, net, err := net.ParseCIDR(data.(string))
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   158
		return net, err
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   159
	}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   160
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 242
diff changeset
   161
242
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   162
// StringToTimeHookFunc returns a DecodeHookFunc that converts
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   163
// strings to time.Time.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   164
func StringToTimeHookFunc(layout string) DecodeHookFunc {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   165
	return func(
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   166
		f reflect.Type,
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   167
		t reflect.Type,
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   168
		data interface{}) (interface{}, error) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   169
		if f.Kind() != reflect.String {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   170
			return data, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   171
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   172
		if t != reflect.TypeOf(time.Time{}) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   173
			return data, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   174
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   175
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   176
		// Convert it by parsing
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   177
		return time.Parse(layout, data.(string))
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   178
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   179
}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   180
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   181
// WeaklyTypedHook is a DecodeHookFunc which adds support for weak typing to
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   182
// the decoder.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   183
//
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   184
// Note that this is significantly different from the WeaklyTypedInput option
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   185
// of the DecoderConfig.
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   186
func WeaklyTypedHook(
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   187
	f reflect.Kind,
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   188
	t reflect.Kind,
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   189
	data interface{}) (interface{}, error) {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   190
	dataVal := reflect.ValueOf(data)
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   191
	switch t {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   192
	case reflect.String:
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   193
		switch f {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   194
		case reflect.Bool:
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   195
			if dataVal.Bool() {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   196
				return "1", nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   197
			}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   198
			return "0", nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   199
		case reflect.Float32:
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   200
			return strconv.FormatFloat(dataVal.Float(), 'f', -1, 64), nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   201
		case reflect.Int:
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   202
			return strconv.FormatInt(dataVal.Int(), 10), nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   203
		case reflect.Slice:
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   204
			dataType := dataVal.Type()
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   205
			elemKind := dataType.Elem().Kind()
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   206
			if elemKind == reflect.Uint8 {
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   207
				return string(dataVal.Interface().([]uint8)), nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   208
			}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   209
		case reflect.Uint:
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   210
			return strconv.FormatUint(dataVal.Uint(), 10), nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   211
		}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   212
	}
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   213
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   214
	return data, nil
2a9ec03fe5a1 Use vendoring for backward compatibility
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   215
}
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   216
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   217
func RecursiveStructToMapHookFunc() DecodeHookFunc {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   218
	return func(f reflect.Value, t reflect.Value) (interface{}, error) {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   219
		if f.Kind() != reflect.Struct {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   220
			return f.Interface(), nil
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   221
		}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   222
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   223
		var i interface{} = struct{}{}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   224
		if t.Type() != reflect.TypeOf(&i).Elem() {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   225
			return f.Interface(), nil
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   226
		}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   227
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   228
		m := make(map[string]interface{})
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   229
		t.Set(reflect.ValueOf(m))
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   230
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   231
		return f.Interface(), nil
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   232
	}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   233
}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   234
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   235
// TextUnmarshallerHookFunc returns a DecodeHookFunc that applies
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   236
// strings to the UnmarshalText function, when the target type
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   237
// implements the encoding.TextUnmarshaler interface
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   238
func TextUnmarshallerHookFunc() DecodeHookFuncType {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   239
	return func(
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   240
		f reflect.Type,
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   241
		t reflect.Type,
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   242
		data interface{}) (interface{}, error) {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   243
		if f.Kind() != reflect.String {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   244
			return data, nil
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   245
		}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   246
		result := reflect.New(t).Interface()
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   247
		unmarshaller, ok := result.(encoding.TextUnmarshaler)
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   248
		if !ok {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   249
			return data, nil
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   250
		}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   251
		if err := unmarshaller.UnmarshalText([]byte(data.(string))); err != nil {
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   252
			return nil, err
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   253
		}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   254
		return result, nil
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   255
	}
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
   256
}