|
| 1 | +[](https://opensource.org/licenses/MIT) |
| 2 | +[](https://goreportcard.com/report/github.com/myENA/consul-decoder) |
| 3 | +[](https://godoc.org/github.com/myENA/consul-decoder) |
| 4 | +[](https://travis-ci.org/myENA/consul-decoder) |
| 5 | + |
| 6 | + |
| 7 | +Package decoder - this unmarshals or decodes values from a consul KV store into a struct. The following types are supported: |
| 8 | + |
| 9 | +* Integer (int/int8/int16/int32/int64) |
| 10 | +* Unsigned (uint/uint8/uint16/uint32/uint64) |
| 11 | +* Float (float64/float32) |
| 12 | +* time.Duration |
| 13 | +* net.IP |
| 14 | +* net.IPMask |
| 15 | +* struct - nested struct by default implies a consul folder with the same name. |
| 16 | + if the tag modifier "json" is encountered, then the value of in the KV |
| 17 | + is unmarshaled as json using json.Unmarshal |
| 18 | + |
| 19 | +* slice - the type can be most of the supported types, except another slice. |
| 20 | +* map - the key must be a string, the value can be anything but another map. |
| 21 | + |
| 22 | +Struct tags |
| 23 | +By default, the decoder packages looks for the struct tag "decoder". However, this can be overridden inside the Decoder struct as shown below. For the purposes of examples, we'll stick with the default "decoder" tag. |
| 24 | + |
| 25 | + |
| 26 | +```go |
| 27 | + |
| 28 | + struct Foo { |
| 29 | + |
| 30 | + // populate the value from key "whatever" into FooField1 |
| 31 | + FooField1 string `decoder:"whatever"` |
| 32 | + |
| 33 | + // skip populating FooField2, "-" signals skip |
| 34 | + FooField2 string `decoder:"-"` |
| 35 | + |
| 36 | + // this looks for a folder named FooField3 |
| 37 | + // and maps keys inside to the keys / values of the map. string |
| 38 | + // is the only valid key type, though the map value can be most any |
| 39 | + // of the other types supported by this package. Notable exception |
| 40 | + // map, as nested maps are not allowed. You can, however, have a |
| 41 | + // map[string]SomeStruct. |
| 42 | + FooField3 map[string]string |
| 43 | + |
| 44 | + // this looks for a folder named FooField4 (case insensitive) |
| 45 | + // this is similar to the map example above, but it ignores the keys |
| 46 | + // inside and maps the values in-order into the slice. nested slices |
| 47 | + // are not allowed, i.e., [][]string. |
| 48 | + FooField4 []string |
| 49 | + |
| 50 | + // this interprets the value of foofield5 as json data and |
| 51 | + // will send it to json.Unmarshal from encoding/json package. |
| 52 | + FooField5 *SomeStruct `decoder:"foofield5,json"` |
| 53 | + |
| 54 | +} |
| 55 | +``` |
0 commit comments