Golang library for encoding/decoding data from/to etcd.
It supports primitive data types, structs, slices, map with string keys.
Usage example
import (
"go.etcd.io/etcd/v3/client"
"github.com/netw00rk/encoding/etcd"
)
type StructWithMarshaller struct {
Field string
}
func (c StructWithMarshaller) MarshalJSON() ([]byte, error) {
return []byte(c.Field), nil
}
func (c *StructWithMarshaller) UnmarshalJSON(data []byte) error {
c.Field = string(data)
return nil
}
type NestedComplexStruct struct {
BooleanField bool `etcd:"boolean_field"`
IntMapField map[string]int
IntSliceField []int
}
type ComplexStruct struct {
IntField int
Int64Field int64
Float32Field float32
Float64Field float64
StringField string
StructField NestedComplexStruct
TimeDurationField time.Duration
WithMarshaller StructWithMarshaller
}
func main() {
// ... init etcd keysApi client
var a = ComplexStruct{
IntField: 10,
Int64Field: int64(20),
Float32Field: float32(10.5),
Float64Field: float64(20.5),
StringField: "value",
StructField: NestedComplexStruct{
BooleanField: true,
IntMapField: map[string]int{
"field_1": 30,
"field_2": 40,
},
IntSliceField: []int{50, 60},
},
TimeDurationField: time.Second * 5,
WithMarshaller: StructWithMarshaller{
Field: "foo",
},
}
encoder := NewEncoder(keysApi)
err := encoder.Encode("/path/to/struct", a)
if err != nil {
panic(err)
}
// Result wil be
// /path/to/struct/IntField = "10"
// /path/to/struct/Int64Field = "20"
// /path/to/struct/Float32Field = "10.5"
// /path/to/struct/Float64Field = "20.5"
// /path/to/struct/StringField = "value"
// /path/to/struct/StructField/boolean_field = "true"
// /path/to/struct/StructField/IntMapField/field_1 = "30"
// /path/to/struct/StructField/IntMapField/field_2 = "40"
// /path/to/struct/StructField/IntSliceField/0 = "50"
// /path/to/struct/StructField/IntSliceField/1 = "60"
// /path/to/struct/WithMarshaller = "foo"
var b = new(ComplexStruct)
decoder := etcd.Decoder(keysApi)
err = decoder.Decode("/path/to/struct", b)
if err != nil {
panic(err)
}
}
To skip field during encoding use etcd:"-"
tag.
To skip missing fields during decoding use etcd:",omitempty"
tag;