1 // Copyright 2017 Frank Schroeder. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // Decode assigns property values to exported fields of a struct.
17 // Decode traverses v recursively and returns an error if a value cannot be
18 // converted to the field type or a required value is missing for a field.
20 // The following type dependent decodings are used:
22 // String, boolean, numeric fields have the value of the property key assigned.
23 // The property key name is the name of the field. A different key and a default
24 // value can be set in the field's tag. Fields without default value are
25 // required. If the value cannot be converted to the field type an error is
28 // time.Duration fields have the result of time.ParseDuration() assigned.
30 // time.Time fields have the vaule of time.Parse() assigned. The default layout
31 // is time.RFC3339 but can be set in the field's tag.
33 // Arrays and slices of string, boolean, numeric, time.Duration and time.Time
34 // fields have the value interpreted as a comma separated list of values. The
35 // individual values are trimmed of whitespace and empty values are ignored. A
36 // default value can be provided as a semicolon separated list in the field's
39 // Struct fields are decoded recursively using the field name plus "." as
40 // prefix. The prefix (without dot) can be overridden in the field's tag.
41 // Default values are not supported in the field's tag. Specify them on the
42 // fields of the inner struct instead.
44 // Map fields must have a key of type string and are decoded recursively by
45 // using the field's name plus ".' as prefix and the next element of the key
46 // name as map key. The prefix (without dot) can be overridden in the field's
47 // tag. Default values are not supported.
51 // // Field is ignored.
52 // Field int `properties:"-"`
54 // // Field is assigned value of 'Field'.
57 // // Field is assigned value of 'myName'.
58 // Field int `properties:"myName"`
60 // // Field is assigned value of key 'myName' and has a default
61 // // value 15 if the key does not exist.
62 // Field int `properties:"myName,default=15"`
64 // // Field is assigned value of key 'Field' and has a default
65 // // value 15 if the key does not exist.
66 // Field int `properties:",default=15"`
68 // // Field is assigned value of key 'date' and the date
69 // // is in format 2006-01-02
70 // Field time.Time `properties:"date,layout=2006-01-02"`
72 // // Field is assigned the non-empty and whitespace trimmed
73 // // values of key 'Field' split by commas.
76 // // Field is assigned the non-empty and whitespace trimmed
77 // // values of key 'Field' split by commas and has a default
78 // // value ["a", "b", "c"] if the key does not exist.
79 // Field []string `properties:",default=a;b;c"`
81 // // Field is decoded recursively with "Field." as key prefix.
84 // // Field is decoded recursively with "myName." as key prefix.
85 // Field SomeStruct `properties:"myName"`
87 // // Field is decoded recursively with "Field." as key prefix
88 // // and the next dotted element of the key as map key.
89 // Field map[string]string
91 // // Field is decoded recursively with "myName." as key prefix
92 // // and the next dotted element of the key as map key.
93 // Field map[string]string `properties:"myName"`
94 func (p *Properties) Decode(x interface{}) error {
95 t, v := reflect.TypeOf(x), reflect.ValueOf(x)
96 if t.Kind() != reflect.Ptr || v.Elem().Type().Kind() != reflect.Struct {
97 return fmt.Errorf("not a pointer to struct: %s", t)
99 if err := dec(p, "", nil, nil, v); err != nil {
105 func dec(p *Properties, key string, def *string, opts map[string]string, v reflect.Value) error {
108 // value returns the property value for key or the default if provided.
109 value := func() (string, error) {
110 if val, ok := p.Get(key); ok {
116 return "", fmt.Errorf("missing required key %s", key)
119 // conv converts a string to a value of the given type.
120 conv := func(s string, t reflect.Type) (val reflect.Value, err error) {
125 v, err = time.ParseDuration(s)
128 layout := opts["layout"]
130 layout = time.RFC3339
132 v, err = time.Parse(layout, s)
135 v, err = boolVal(s), nil
141 v, err = strconv.ParseFloat(s, 64)
144 v, err = strconv.ParseInt(s, 10, 64)
147 v, err = strconv.ParseUint(s, 10, 64)
150 return reflect.Zero(t), fmt.Errorf("unsupported type %s", t)
153 return reflect.Zero(t), err
155 return reflect.ValueOf(v).Convert(t), nil
158 // keydef returns the property key and the default value based on the
159 // name of the struct field and the options in the tag.
160 keydef := func(f reflect.StructField) (string, *string, map[string]string) {
161 _key, _opts := parseTag(f.Tag.Get("properties"))
164 if d, ok := _opts["default"]; ok {
168 return _key, _def, _opts
170 return f.Name, _def, _opts
174 case isDuration(t) || isTime(t) || isBool(t) || isString(t) || isFloat(t) || isInt(t) || isUint(t):
179 val, err := conv(s, t)
186 return dec(p, key, def, opts, v.Elem())
189 for i := 0; i < v.NumField(); i++ {
191 fk, def, opts := keydef(t.Field(i))
193 return fmt.Errorf("cannot set %s", t.Field(i).Name)
201 if err := dec(p, fk, def, opts, fv); err != nil {
212 vals := split(val, ";")
213 a := reflect.MakeSlice(t, 0, len(vals))
214 for _, s := range vals {
215 val, err := conv(s, t.Elem())
219 a = reflect.Append(a, val)
225 m := reflect.MakeMap(t)
226 for postfix := range p.FilterStripPrefix(key + ".").m {
227 pp := strings.SplitN(postfix, ".", 2)
228 mk, mv := pp[0], reflect.New(valT)
229 if err := dec(p, key+"."+mk, nil, nil, mv); err != nil {
232 m.SetMapIndex(reflect.ValueOf(mk), mv.Elem())
237 return fmt.Errorf("unsupported type %s", t)
242 // split splits a string on sep, trims whitespace of elements
243 // and omits empty elements
244 func split(s string, sep string) []string {
246 for _, v := range strings.Split(s, sep) {
247 if v = strings.TrimSpace(v); v != "" {
254 // parseTag parses a "key,k=v,k=v,..."
255 func parseTag(tag string) (key string, opts map[string]string) {
256 opts = map[string]string{}
257 for i, s := range strings.Split(tag, ",") {
263 pp := strings.SplitN(s, "=", 2)
273 func isArray(t reflect.Type) bool { return t.Kind() == reflect.Array || t.Kind() == reflect.Slice }
274 func isBool(t reflect.Type) bool { return t.Kind() == reflect.Bool }
275 func isDuration(t reflect.Type) bool { return t == reflect.TypeOf(time.Second) }
276 func isMap(t reflect.Type) bool { return t.Kind() == reflect.Map }
277 func isPtr(t reflect.Type) bool { return t.Kind() == reflect.Ptr }
278 func isString(t reflect.Type) bool { return t.Kind() == reflect.String }
279 func isStruct(t reflect.Type) bool { return t.Kind() == reflect.Struct }
280 func isTime(t reflect.Type) bool { return t == reflect.TypeOf(time.Time{}) }
281 func isFloat(t reflect.Type) bool {
282 return t.Kind() == reflect.Float32 || t.Kind() == reflect.Float64
284 func isInt(t reflect.Type) bool {
285 return t.Kind() == reflect.Int || t.Kind() == reflect.Int8 || t.Kind() == reflect.Int16 || t.Kind() == reflect.Int32 || t.Kind() == reflect.Int64
287 func isUint(t reflect.Type) bool {
288 return t.Kind() == reflect.Uint || t.Kind() == reflect.Uint8 || t.Kind() == reflect.Uint16 || t.Kind() == reflect.Uint32 || t.Kind() == reflect.Uint64