![Build Status](https://github.com/myENA/consul-decoder/actions/workflows/build.yml/badge.svg)
Note - The latest version supports only go 1.11 or later since swithcing to go modules. For older versions of go, pin to the v0.2.3 tag.
Package decoder - this unmarshals or decodes values from a consul KV store into a struct. The following types are supported:
-
integer (int/int8/int16/int32/int64)
-
unsigned (uint/uint8/uint16/uint32/uint64)
-
float (float64/float32)
-
bool
-
time.Duration
-
net.IP
-
net.IPMask
-
struct - nested struct by default implies a consul folder with the same name.
if the tag modifier "json" is encountered, then the value of in the KV
is unmarshaled as json using json.Unmarshal
-
slice - the type can be most of the supported types, except another slice.
-
map - the key must be a string, the value can be anything but another map.
-
encoding.TextUnmarshaler - any type that implements this will have its UnmarshalText() method called.
Struct tags
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. By default,
in the absence of a decoder tag, it will look for a consul key name with the
same name as the struct field. Only exported struct fields are considered.
The name comparison is case-insensitive by default, but this is configurable
in the Decoder struct. the tag "-" indicates to skip the field. The modifier
",json" appended to the end signals that the value is to be interpreted as
json and unmarshaled rather than interpreted. Similarly, the modififier
",csv" allows comma separated values to be read into a slice, and ",ssv"
allows space separated values to be read intoa slice. For csv and ssv, slices
of string, numeric and boolean are supported.
struct Foo {
FooField1 string `decoder:"whatever"`
FooField2 string `decoder:"-"`
FooField3 map[string]string
FooField4 []string
FooField5 *SomeStruct `decoder:"foofield5,json"`
FooField6 *SomeStruct `decoder:"foofield6"`
FooField7 string `decoder:"arbitrarily/nested/key"`
FooField8 []string `decoder:",csv"`
FooField9 []string `decoder:",ssv"`
}