dotaccess

package module
v0.0.0-...-74ea4f4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2019 License: MIT Imports: 4 Imported by: 13

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(obj interface{}, prop string) (interface{}, error)

func Set

func Set(obj interface{}, prop string, value interface{}) error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL