structwalk

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2020 License: MIT Imports: 4 Imported by: 1 module, 1 package

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

structwalk Go Report Card GoDoc

Battle-tested Go struct and map traversal utilities. Has been around since 2016.

FieldValue

Returns a value of a field in deeply nested structs, will traverse both maps and structs.

structwalk.FieldValue(path string, in interface{}) (v interface{}, found bool)

Example:

var object = struct {
    Foo struct {
        Bar struct {
            Baz map[string]int
        }
    }
}{}
object.Foo.Bar.Baz = map[string]int{
    "Kek": 5,
}

value, found := structwalk.FieldValue("Foo.Bar.Baz.Kek", object)
// value = 5
// found = true

GetterValue

A special case of FieldValue, that checks if for a field named Foo, there is a method that is named FooBytes(). Useful to avoid allocations when accesing string values that can be returned as a memory pointer instead (e.g. in capnproto).

structwalk.GetterValue(path string, in interface{}) (v interface{}, found bool) 

FieldList

Simply print the list of fields of a struct, recursively.

structwalk.FieldList(in interface{}) []string

Example:

var object = struct {
    Foo struct {
        Bar struct {
            Baz  map[string]int
            Baz2 string
        }
    }
}{}
object.Foo.Bar.Baz = map[string]int{
    "Kek": 5,
    "Lol": 4,
}

list := structwalk.FieldList(object)
// [
//  "Foo.Bar.Baz.Kek",
//  "Foo.Bar.Baz.Lol",
//  "Foo.Bar.Baz2"
// ]

GetterList

Returns list of getter methods that accept no arguments (except the implicit pointer to the struct) and return one value. Can be used in templates to get a value that is not accessible by a method.

structwalk.GetterList(in interface{}) []string

Example:

type SomeDecorated struct{}

func (s SomeDecorated) Foo() string {
    return "foo"
}

func (s SomeDecorated) FooBytes() []byte {
    return []byte("foo")
}

func (s SomeDecorated) Bar() SomeStruct {
    return SomeStruct{}
}

list := structwalk.FieldList(SomeDecorated{})
// [
//   "Bar.Baz",
//   "Foo",
//   "FooBytes"
// ]

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldList

func FieldList(in interface{}) []string

FieldList returns the list of fields of a struct, sorted.

func FieldListNoSort added in v1.1.1

func FieldListNoSort(in interface{}) []string

FieldListNoSort return the list of fields of a struct, recursively without sorting.

func FieldValue

func FieldValue(path string, in interface{}) (v interface{}, found bool)

FieldValue returns a value of a field at path in deeply nested structs, will traverse both maps and structs.

func GetterList

func GetterList(in interface{}) []string

GetterList returns list of getter methods that accept no arguments (except the implicit pointer to the struct) and return one value.

Example: (f *foo) Bar() string

func GetterValue

func GetterValue(path string, in interface{}) (v interface{}, found bool)

GetterValue is a special case of FieldValue, that checks if for a field named Foo, there is a method that is named FooBytes(). Useful to avoid allocations when accesing string values that can be returned as a memory pointer instead (e.g. in capnproto).

func SetFieldValue added in v1.1.0

func SetFieldValue(path string, v interface{}, in interface{})

SetFieldValue sets a value of a field at path in deeply nested structs, will traverse both maps and structs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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