Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1012 Bytes

README.md

File metadata and controls

46 lines (34 loc) · 1012 Bytes

Dot notation map/struct access. Mainly just a wrapper for github.com/oleiade/reflections, so that should get most of the credit.

Setting is in development. Getting works 100%.

Since this is intended to work on public properties, each element in the dot notation string is converted to title case if the object is a struct. They remain as-is if it's a map.

Get

Struct

type ChildStruct struct {
	Prop string
}
type MyStruct struct {
	Nested *ChildStruct
}

myStruct := &MyStruct{
	Nested:&ChildStruct{"foo"},
}

// This will get myStruct.Nested.Prop
val, err := dotaccess.Get(myStruct, "nested.prop")
// returns "foo", nil

val, err = dotaccess.Get(myStruct, "foo.bar")
//returns nil, error

Map

Maps will not return errors if the key does not exist, only nil.

myMap := map[string]interface{}{
	"nested":map[string]string{
		"prop":"foo"
	}
}
val, err := dotaccess.Get(myMap, "nested.prop")
// returns "foo", nil

val, err = dotaccess.Get(myStruct, "foo.bar")
//returns nil, nil