dot

package module
v0.8.7 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: MIT Imports: 6 Imported by: 1

README

Dot

Build Status Coverage Status Go Report Card

Simple get/set with dot-notation for Go that can operate on maps, structs and interfaces.

Usage

Get

Get will retrieve the value at the specified dot path. It will return an error if the property is not found.

sample := SampleStruct{
    A: SampleStructA{
        Text: "foo",
    },
    B: 8,
    C: "something",
}

// nested get, make "a" lowercase to demonstrate case-insensitivity
aText, err := Get(sample, "a.text")
if err != nil {
    // handle the error
}

// aText will be equal to "foo"

sampleMap := make(map[string]interface{})
sampleMap["D"] = "onmap"

dText, err := Get(sampleMap, "D")
if err != nil {
    // handle the error
}

// dText will be "onmap"

A fallback mechanism is also available, in which you define a list of properties and the first property with a value will be used for the Get function.

sample := SampleStruct{
    A: 0.65,
    B: 8,
    C: "something",
}

// nested get, make "a" lowercase to demonstrate case-insensitivity
fallbackText, err := dot.Get(sample, "x", "y", "b")
if err != nil {
    // handle the error
}

// fallbackText will be equal to 8
Set

Sets the value at the dot-property provided. Will create map[string]interface{} for any missing levels along the way. If that poses an issue (struct setting, perhaps), it's recommended to allocate structures accordingly.

obj := make(map[string]interface{})
err := dot.Set(obj, "X", "test34")
if err != nil {
	// handle err
}

// obj.X will be "test34"
Keys

Gets the list of keys for an arbitrary structure (non-recursively). In the result below, the result will be ["A", "B"], though it's best to not assume the elements are ordered:

testStruct := TestStruct{
    A: false,
    B: map[string]interface{}{
        "A": 1,
    }
}

keysFromStruct := dot.Keys(testStruct)
Extend

Writes any non-nil, non-default value from the right object to the left object.

TODO: example needed

KeysRecursive

Just like Keys, only recursive

KeysRecursiveLeaves

Just like KeysRecursive, except it returns only items with no "children"

Additional Getters (TODO: Enhance Details)
  • GetString
  • GetInt64
  • GetFloat64

Notable details:

  • Property access is case-insensitive
  • Failure to find a value at the provided property with Get will result in an error (you can still choose to ignore the error and count on a nil value, if you wish)

Common Errors

object must be a pointer to a struct

This error means that you're passing a struct to dot.Set, when you should be passing a pointer to the struct. Add a & before the struct you're passing.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CoerceFloat64

func CoerceFloat64(obj interface{}) (float64, bool)

CoerceFloat64 will make a best-effort to convert the provided argument to an float64. It supports int64, int32, int, float64, float32 as acceptable inputs, but expect this list to expand further with new releases

func CoerceInt64

func CoerceInt64(obj interface{}) (int64, bool)

CoerceInt64 will make a best-effort to convert the provided argument to an int64. It supports int64, int32, int, float64, float32 as acceptable inputs, but expect this list to expand further with new releases

func CoerceString

func CoerceString(objCursor interface{}) (string, bool)

CoerceString will make a best-effort to convert the provided argument to a string. It supports string as well as anything supported by CoerceFloat64 and CoerceInt64. Expect the list of supported argument types to expand.

func Extend

func Extend(to interface{}, from interface{}) error

Extend copies non-nil, non-default values from right to left

func Get

func Get(obj interface{}, props ...string) (interface{}, error)

Get will return the value in obj at the "location" given by dot notation property candidates. The candidates are processed in the order given, and the first non-nil result is returned. If a property

func GetFloat64

func GetFloat64(obj interface{}, props ...string) float64

GetFloat64 does what Get does, except it continues through props until it not only gets a non-nil value, but also gets something that can be cast/coerced to a float64 value. Will return 0 if the property doesn't exist or could not be coerced. It can't be implemented by attempting to coerce Get once complete, because of the fallback logic for when more than one prop is passed.

func GetInt64

func GetInt64(obj interface{}, props ...string) int64

GetInt64 does what Get does, except it continues through props until it not only gets a non-nil value, but also gets something that can be cast/coerced to an int64 value. Will return 0 if the property doesn't exist or could not be coerced. It can't be implemented by attempting to coerce Get once complete, because of the fallback logic for when more than one prop is passed.

func GetString

func GetString(obj interface{}, props ...string) string

GetString does what Get does, except it continues through props until it not only gets a non-nil value, but also gets something that can be cast or coerced to a string that isn't the empty string. Will return "" if the property doesn't exist or could not be coerced. It can't be implemented by attempting to coerce Get once complete, because of the fallback logic for when more than one prop is passed.

func Keys

func Keys(obj interface{}, parentPath ...string) []string

Keys will get the list of keys for an arbitrary structure (non-recursively). In the result below, the result will be ["A", "B"], though it's best to not assume the elements are ordered.

func KeysRecursive

func KeysRecursive(obj interface{}, parentPath ...string) []string

KeysRecursive is just like Keys, only recursive. The ordering of elements in the resulting slice is not to be assumed at any time

func KeysRecursiveLeaves

func KeysRecursiveLeaves(obj interface{}, parentPath ...string) []string

KeysRecursiveLeaves is like KeysRecursive, except it returns only items with no "children"

func KeysWithoutReflection added in v0.8.0

func KeysWithoutReflection(obj interface{}, parentPath ...string) []string

func Set

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

Set will apply the value specified by the value argument at the "position"/attribute in the provided obj argument. It will allocate map[string]interface{} for any missing nodes in the "tree" generated by addressing. Returns an error if it cannot apply the provided value for any reason.

Types

This section is empty.

Jump to

Keyboard shortcuts

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