lookup

package module
v0.0.0-...-5415b5b Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 4 Imported by: 25 modules, 27 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

go-lookup Build Status GoDoc

Small library on top of reflect for make lookups to Structs or Maps. Using a very simple DSL you can access to any property, key or value of any value of Go.

Installation

The recommended way to install go-lookup

go get github.com/mcuadros/go-lookup

Example

type Cast struct {
  Actor, Role string
}

type Serie struct {
  Cast []Cast
}

series := map[string]Serie{
  "A-Team": {Cast: []Cast{
    {Actor: "George Peppard", Role: "Hannibal"},
    {Actor: "Dwight Schultz", Role: "Murdock"},
    {Actor: "Mr. T", Role: "Baracus"},
    {Actor: "Dirk Benedict", Role: "Faceman"},
  }},
}

q := "A-Team.Cast.Role"
value, _ := LookupString(series, q)
fmt.Println(q, "->", value.Interface())
// A-Team.Cast.Role -> [Hannibal Murdock Baracus Faceman]

q = "A-Team.Cast[0].Actor"
value, _ = LookupString(series, q)
fmt.Println(q, "->", value.Interface())
// A-Team.Cast[0].Actor -> George Peppard
Case-insensitive matching

Use the LookupI and LookupStringI functions to do a case-insensitive match on struct field names and map keys. It will first look for an exact match; if that fails, it will fall back to a more expensive linear search over fields/keys.

type ExampleStruct struct {
  SoftwareUpdated bool
}

i := ExampleStruct{
  SoftwareUpdated: true,
}

value, _ := LookupStringI(i, "softwareupdated")
fmt.Println(value.Interface())
// Output: true

License

MIT, see LICENSE

Documentation

Overview

Small library on top of reflect for make lookups to Structs or Maps. Using a very simple DSL you can access to any property, key or value of any value of Go.

Index

Examples

Constants

View Source
const (
	SplitToken     = "."
	IndexCloseChar = "]"
	IndexOpenChar  = "["
)

Variables

View Source
var (
	ErrMalformedIndex    = errors.New("malformed index key")
	ErrInvalidIndexUsage = errors.New("invalid index key usage")
	ErrKeyNotFound       = errors.New("unable to find the key")
	ErrIndexOutOfBounds  = errors.New("index out of bounds")
)

Functions

func Lookup

func Lookup(i interface{}, path ...string) (reflect.Value, error)

Lookup performs a lookup into a value, using a path of keys. The key should match with a Field or a MapIndex. For slice you can use the syntax key[index] to access a specific index. If one key owns to a slice and an index is not specificied the rest of the path will be apllied to evaley value of the slice, and the value will be merged into a slice.

Example
type ExampleStruct struct {
	Values struct {
		Foo int
	}
}

i := ExampleStruct{}
i.Values.Foo = 10

value, _ := Lookup(i, "Values", "Foo")
fmt.Println(value.Interface())
Output:
10

func LookupI

func LookupI(i interface{}, path ...string) (reflect.Value, error)

LookupI is the same as Lookup, but the path keys are not case sensitive.

func LookupString

func LookupString(i interface{}, path string) (reflect.Value, error)

LookupString performs a lookup into a value, using a string. Same as `Lookup` but using a string with the keys separated by `.`

Example
type Cast struct {
	Actor, Role string
}

type Serie struct {
	Cast []Cast
}

series := map[string]Serie{
	"A-Team": {Cast: []Cast{
		{Actor: "George Peppard", Role: "Hannibal"},
		{Actor: "Dwight Schultz", Role: "Murdock"},
		{Actor: "Mr. T", Role: "Baracus"},
		{Actor: "Dirk Benedict", Role: "Faceman"},
	}},
}

q := "A-Team.Cast.Role"
value, _ := LookupString(series, q)
fmt.Println(q, "->", value.Interface())

q = "A-Team.Cast[0].Actor"
value, _ = LookupString(series, q)
fmt.Println(q, "->", value.Interface())
Output:
A-Team.Cast.Role -> [Hannibal Murdock Baracus Faceman]
A-Team.Cast[0].Actor -> George Peppard

func LookupStringI

func LookupStringI(i interface{}, path string) (reflect.Value, error)

LookupStringI is the same as LookupString, but the path is not case sensitive.

Example
type ExampleStruct struct {
	SoftwareUpdated bool
}

i := ExampleStruct{
	SoftwareUpdated: true,
}

value, _ := LookupStringI(i, "softwareupdated")
fmt.Println(value.Interface())
Output:
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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