lookup

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 15 Imported by: 2 modules, 3 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

Lookup

lookup is a Go library that provides reflection-based access to nested fields within structs, maps, slices, and arrays using string paths. It allows for dynamic retrieval, modification, and creation of data structures.

Features

  • Get: Retrieve values from deeply nested structures.
  • Set: Update values in nested structures using a path string.
  • Exists: Check if a specific path is populated (not nil).
  • Create: Traverse a path, initializing nil maps, slices, or pointers along the way.
  • Flexible Syntax: Supports dot notation for fields and bracket notation for indexes/keys.
  • Type Conversion: Convert strings to Go types including complex structures.

Installation

go get github.com/your-username/lookup

Note: This package depends on github.com/zauberhaus/reflect_utils.

Usage

Data Structure Example
type Address struct {
	City string
	Zip  int
}

type User struct {
	Name    string
	Address *Address
	Tags    []string
	Meta    map[string]interface{}
}
Get

Retrieve a value from a struct.

user := User{
	Name: "Alice",
	Tags: []string{"admin", "active"},
}

name, err := lookup.Get(user, "Name")
// name: "Alice"

tag, err := lookup.Get(user, "Tags[0]")
// tag: "admin"
Set

Set a value at a specific path. Note that Set requires a pointer to the struct to modify it.

user := &User{}

// Sets the Name field
_, err := lookup.Set(user, "Name", "Bob")

// Automatically expands slices if index is reachable
_, err = lookup.Set(user, "Tags[0]", "guest")
Create

Create ensures that the path exists, initializing nil pointers, maps, or slices with default values if necessary.

user := &User{}

// Initializes the Address pointer if it is nil
_, err := lookup.Create(user, "Address")

// Initializes the Meta map if nil and sets the key
_, err = lookup.Set(user, "Meta[\"login_count\"]", 1)
Path Syntax
  • Struct Fields: Field.SubField (e.g., User.Address.City)
  • Arrays/Slices: List[index] (e.g., Tags[0])
  • Maps: Map["key"] (e.g., Meta["version"]) - supports double quotes, single quotes, or backticks.
Type Conversion

Convert a string to a specific Go type. This supports basic types, pointers, slices, maps, and structs (via JSON).

// Parse an integer
val, err := lookup.Parse("123", reflect.TypeOf(0))
// val: 123

// Parse a slice
val, err = lookup.Parse("1,2,3", reflect.TypeOf([]int{}))
// val: []int{1, 2, 3}

// Parse a map
val, err = lookup.Parse("a=1,b=2", reflect.TypeOf(map[string]int{}))
// val: map[string]int{"a": 1, "b": 2}
Custom Parsing

Register custom parsers for specific types.

type MyType struct {
	Data string
}

hook := lookup.NewParserHook(reflect.TypeOf(MyType{}), func(input string) (any, error) {
	return MyType{Data: input}, nil
})

val, err := lookup.Parse("some data", reflect.TypeOf(MyType{}), hook)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedMap = errors.New("only maps with string keys are supported")
	ErrNotMap         = errors.New("field is not a map")
	ErrNotSlice       = errors.New("field is not an array or slice")
)

Functions

func Create

func Create(obj any, path string) (any, error)

func Exists

func Exists(obj any, path string) (bool, error)

func Get

func Get(obj any, path string) (any, error)

func Parse

func Parse(txt string, t reflect.Type, hooks ...ParserHook) (any, error)

func Set

func Set(obj any, path string, value any, hooks ...ParserHook) (any, error)

Set sets the field at path to value. A string is parsed into the type of the field; hooks parse it into types Parse doesn't know, such as an interface type.

func Split

func Split(txt string, sep rune) []string

Types

type NotFoundError

type NotFoundError struct {
	Name string
}

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type ParserHook

type ParserHook struct {
	To    reflect.Type
	Parse func(txt string) (any, error)
}

func NewParserHook

func NewParserHook(to reflect.Type, f func(txt string) (any, error)) ParserHook

func NewParserHookFor

func NewParserHookFor[T any](f func(txt string) (any, error)) ParserHook

Directories

Path Synopsis
examples
basic command
create command
hooks command
parsing command

Jump to

Keyboard shortcuts

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