reflectx

package module
v0.0.0-...-6a804b8 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 8 Imported by: 0

README

ReflectX

The ReflectX package aims to make developers' life easier, inspired by reflectutils, reflections and gpath.

Install

go get github.com/black-06/reflectx

Usage

GetValue

It returns the content of given value by a path, the path is represented by Go's expression which can be parsed by go/parser.ParseExpr.

It can access deeper levels of values than reflections.GetField, more types of values than gpath.At

type (
	Foo struct {
		Names []string
	}
	Bar struct {
		Foo
	}
)

value := Bar{Foo{Names: []string{"a", "b", "c"}}}
// For the value, you can also choose &value1.
// For the path, you can also choose 
// - "Names[1]", because Foo is embed struct
// - "$.Foo.Names[1]", '$' means the root path of the value
// - "$.Names[1]"
v, err := GetValue(value, "Foo.Names[1]") // result v is "b"
v, err = GetValue("bar", "$")             // result v is "bar"
SetValue

It sets the content of given value by a path. Note: the value must be addressable, in general, it is a pointer.

value := Bar{Foo{Names: []string{"some", "string"}}}

err := SetValue(&value, "Foo.Names[1]", "value")     // value is Bar{Foo{Names: []string{"some", "value"}}}
err = SetValue(&value, "Foo.Names[1][0]", byte('m')) // value is Bar{Foo{Names: []string{"some", "malue"}}}
ValueEntryByPath

In fact, both GetValue and SetValue calls ValueEntryByPath. It returns a ValueEntry of given value by a path. The ValueEntry contains many useful values such as the actual reflect.Value.

For example, we can get tag in StructField:

type Data struct {
	ID     string `json:"id"`
	Record struct {
		Num int `json:"num"`
	}
}

entry, err := ValueEntryByPath(Data{}, "Record.Num")
assert.Empty(t, err)

field, ok := entry.StructField()
assert.True(t, ok)

field.Tag.Get("json") // "num"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetValue

func GetValue(value any, path string) (any, error)

func SetValue

func SetValue(value any, path string, v any) error

Types

type ValueEntry

type ValueEntry struct {
	reflect.Value
	Parent reflect.Value

	// Key is current Value's key in Parent map.
	Key reflect.Value
	// Idx is current Value's idx in Parent struct fields or Parent array/slice
	Idx int
}

func ValueEntryByPath

func ValueEntryByPath(value any, path string) (*ValueEntry, error)

func (ValueEntry) StructField

func (e ValueEntry) StructField() (reflect.StructField, bool)

StructField returns current reflect.StructField in Parent struct.

type ValueOptions

type ValueOptions struct {
	IgnorePtr bool
}

func (ValueOptions) DeRef

func (o ValueOptions) DeRef(rv reflect.Value) reflect.Value

func (ValueOptions) GetValue

func (o ValueOptions) GetValue(value any, path string) (any, error)

GetValue get value by ValueEntryByPath. Note: you can't get un-exported field.

func (ValueOptions) SetValue

func (o ValueOptions) SetValue(value any, path string, v any) error

SetValue set value by ValueEntryByPath. Note: you can't set un-exported field and that the field and value types must match.

func (ValueOptions) ValueEntryByPath

func (o ValueOptions) ValueEntryByPath(value any, path string) (*ValueEntry, error)

ValueEntryByPath access a field of value by a path. The path is represented by Go's expression which can be parsed by go/parser.ParseExpr. But the extra feature is, you can use "$" to represent the value root path. You can use selectors and indexes in a path. Slice and arrays index allow only expressions of int. Maps key allow expressions of int, float, complex, char and string.

Jump to

Keyboard shortcuts

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