find

package
v0.0.0-...-d31700d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 2 Imported by: 0

README

value

Get a value indicated by paths, path can be any of "struct field name/map index/slice index/array index/method name".

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find(value reflect.Value, path []string) reflect.Value

Find return a value indicated by path, path can be any of "struct field name/map index/slice index/array index/method name".

func FindStructField

func FindStructField(value reflect.Value, name string) reflect.Value

func Settable

func Settable(value reflect.Value, path []string) reflect.Value

Settable return a settable value indicated by path, path can be any of "struct field name/map index/slice index/array index/method name".

Example
var ts = TestStruct{
	Interface: &TestStruct2{},
}
var v = reflect.ValueOf(&ts)
find.Settable(v, []string{"String"}).Set(reflect.ValueOf("ok"))
fmt.Println(ts.String)

find.Settable(v, []string{"Bool"}).Set(reflect.ValueOf(true))
fmt.Println(ts.Bool)

var time = reflect.ValueOf(time.Date(2022, 1, 12, 19, 30, 30, 0, time.UTC))
find.Settable(v, []string{"Time"}).Set(time)
fmt.Println(ts.Time)

find.Settable(v, []string{"Interface", "Time"}).Set(time)
fmt.Println(ts.Interface.(*TestStruct2).Time)

find.Settable(v, []string{"Interface"}).Set(reflect.ValueOf(9))
fmt.Println(ts.Interface)

var s = "string"
find.Settable(v, []string{"Pointer"}).Set(reflect.ValueOf(&s))
fmt.Println(*ts.Pointer)

fmt.Println(find.Settable(v, []string{"NonExists"}))
Output:

ok
true
2022-01-12 19:30:30 +0000 UTC
2022-01-12 19:30:30 +0000 UTC
9
string
<invalid reflect.Value>
Example (Layer)
var ts = TestStruct{
	Layer: TestStruct2{Slice: []int{0, 1, 2}},
}
var v = reflect.ValueOf(&ts).Elem()

find.Settable(v, []string{"Layer", "Slice", "2"}).Set(reflect.ValueOf(22))
fmt.Println(ts.Layer.Slice[2])
fmt.Println(find.Settable(v, []string{"Layer", "Slice", "3"}))
fmt.Println(find.Settable(v, []string{"Layer", "Slice", "none"}))

find.Settable(v, []string{"Layer", "Map"}).Set(reflect.ValueOf(map[string][]int{"k": {7}}))
fmt.Println(ts.Layer.Map)

find.Settable(v, []string{"PointerLayer", "Map2"}).Set(reflect.ValueOf(map[string]int{"k": 8}))
fmt.Println(ts.PointerLayer.Map2)

fmt.Println(find.Settable(v, []string{"Layer", "Map", "key"}))
fmt.Println(find.Settable(v, []string{"NonExists"}))
Output:

22
<invalid reflect.Value>
<invalid reflect.Value>
map[k:[7]]
map[k:8]
<invalid reflect.Value>
<invalid reflect.Value>
Example (Method)
var ts = TestStruct{}
var v = reflect.ValueOf(&ts)

find.Settable(v, []string{"SettableMethod"}).Elem().Set(reflect.ValueOf("ok"))
fmt.Println(ts.String)

find.Settable(v.Elem(), []string{"SettableMethod"}).Elem().Set(reflect.ValueOf("ok2"))
fmt.Println(ts.String)
Output:

ok
ok2

func SettableStructField

func SettableStructField(value reflect.Value, name string) reflect.Value

Types

This section is empty.

Jump to

Keyboard shortcuts

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