vendor/github.com/magiconair/properties/decode.go
changeset 265 05c40b36d3b2
parent 242 2a9ec03fe5a1
equal deleted inserted replaced
264:8f478162d991 265:05c40b36d3b2
     1 // Copyright 2018 Frank Schroeder. All rights reserved.
     1 // Copyright 2013-2022 Frank Schroeder. All rights reserved.
     2 // Use of this source code is governed by a BSD-style
     2 // Use of this source code is governed by a BSD-style
     3 // license that can be found in the LICENSE file.
     3 // license that can be found in the LICENSE file.
     4 
     4 
     5 package properties
     5 package properties
     6 
     6 
    46 // name as map key. The prefix (without dot) can be overridden in the field's
    46 // name as map key. The prefix (without dot) can be overridden in the field's
    47 // tag. Default values are not supported.
    47 // tag. Default values are not supported.
    48 //
    48 //
    49 // Examples:
    49 // Examples:
    50 //
    50 //
    51 //     // Field is ignored.
    51 //	// Field is ignored.
    52 //     Field int `properties:"-"`
    52 //	Field int `properties:"-"`
    53 //
    53 //
    54 //     // Field is assigned value of 'Field'.
    54 //	// Field is assigned value of 'Field'.
    55 //     Field int
    55 //	Field int
    56 //
    56 //
    57 //     // Field is assigned value of 'myName'.
    57 //	// Field is assigned value of 'myName'.
    58 //     Field int `properties:"myName"`
    58 //	Field int `properties:"myName"`
    59 //
    59 //
    60 //     // Field is assigned value of key 'myName' and has a default
    60 //	// Field is assigned value of key 'myName' and has a default
    61 //     // value 15 if the key does not exist.
    61 //	// value 15 if the key does not exist.
    62 //     Field int `properties:"myName,default=15"`
    62 //	Field int `properties:"myName,default=15"`
    63 //
    63 //
    64 //     // Field is assigned value of key 'Field' and has a default
    64 //	// Field is assigned value of key 'Field' and has a default
    65 //     // value 15 if the key does not exist.
    65 //	// value 15 if the key does not exist.
    66 //     Field int `properties:",default=15"`
    66 //	Field int `properties:",default=15"`
    67 //
    67 //
    68 //     // Field is assigned value of key 'date' and the date
    68 //	// Field is assigned value of key 'date' and the date
    69 //     // is in format 2006-01-02
    69 //	// is in format 2006-01-02
    70 //     Field time.Time `properties:"date,layout=2006-01-02"`
    70 //	Field time.Time `properties:"date,layout=2006-01-02"`
    71 //
    71 //
    72 //     // Field is assigned the non-empty and whitespace trimmed
    72 //	// Field is assigned the non-empty and whitespace trimmed
    73 //     // values of key 'Field' split by commas.
    73 //	// values of key 'Field' split by commas.
    74 //     Field []string
    74 //	Field []string
    75 //
    75 //
    76 //     // Field is assigned the non-empty and whitespace trimmed
    76 //	// Field is assigned the non-empty and whitespace trimmed
    77 //     // values of key 'Field' split by commas and has a default
    77 //	// values of key 'Field' split by commas and has a default
    78 //     // value ["a", "b", "c"] if the key does not exist.
    78 //	// value ["a", "b", "c"] if the key does not exist.
    79 //     Field []string `properties:",default=a;b;c"`
    79 //	Field []string `properties:",default=a;b;c"`
    80 //
    80 //
    81 //     // Field is decoded recursively with "Field." as key prefix.
    81 //	// Field is decoded recursively with "Field." as key prefix.
    82 //     Field SomeStruct
    82 //	Field SomeStruct
    83 //
    83 //
    84 //     // Field is decoded recursively with "myName." as key prefix.
    84 //	// Field is decoded recursively with "myName." as key prefix.
    85 //     Field SomeStruct `properties:"myName"`
    85 //	Field SomeStruct `properties:"myName"`
    86 //
    86 //
    87 //     // Field is decoded recursively with "Field." as key prefix
    87 //	// Field is decoded recursively with "Field." as key prefix
    88 //     // and the next dotted element of the key as map key.
    88 //	// and the next dotted element of the key as map key.
    89 //     Field map[string]string
    89 //	Field map[string]string
    90 //
    90 //
    91 //     // Field is decoded recursively with "myName." as key prefix
    91 //	// Field is decoded recursively with "myName." as key prefix
    92 //     // and the next dotted element of the key as map key.
    92 //	// and the next dotted element of the key as map key.
    93 //     Field map[string]string `properties:"myName"`
    93 //	Field map[string]string `properties:"myName"`
    94 func (p *Properties) Decode(x interface{}) error {
    94 func (p *Properties) Decode(x interface{}) error {
    95 	t, v := reflect.TypeOf(x), reflect.ValueOf(x)
    95 	t, v := reflect.TypeOf(x), reflect.ValueOf(x)
    96 	if t.Kind() != reflect.Ptr || v.Elem().Type().Kind() != reflect.Struct {
    96 	if t.Kind() != reflect.Ptr || v.Elem().Type().Kind() != reflect.Struct {
    97 		return fmt.Errorf("not a pointer to struct: %s", t)
    97 		return fmt.Errorf("not a pointer to struct: %s", t)
    98 	}
    98 	}