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: 15 modules, 15 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
⌂ 换网址