Documentation
¶
Overview ¶
Package empaths provides xpath-like path resolution for Go data structures.
Empaths (pronounced "em-paths" - the M stands for Model) allows reflective access to nested values in structs, maps, slices, and arrays using a simple path syntax. It is designed to be fast, memory-efficient, and easy to use.
Path Syntax ¶
Path expressions can contain multiple segments that are evaluated in sequence:
Model References (start with '.'):
.Name - Access a struct field or map key named "Name" .User.Address.City - Access nested fields .Users[0] - Access array/slice element by index (zero-based) .Data["key"] - Access map element by key .GetValue - Call a zero-argument method
String Literals (enclosed in quotes):
'Hello' - Single-quoted string "World" - Double-quoted string 'It\'s' - Escaped quotes within strings
Negation (starts with '!'):
!.IsActive - Negate a boolean value !'true' - Negate a string "true" -> false
Comparisons (start with '?'):
?.Age=='18' - Compare if Age equals 18 ?.Status!='active' - Compare if Status is not "active"
External References (start with ':'):
:config - Resolve using the provided ReferenceResolver
Multiple segments can be combined:
'Hello, ' .User.Name '!' - Concatenates to "Hello, John!"
Array and Slice Access ¶
Arrays and slices are accessed using zero-based integer indices:
.Items[0] - First element .Items[1] - Second element .Matrix[0][1] - Nested array access
Out-of-bounds access returns nil rather than panicking. Negative indices are not supported.
Map Access ¶
Maps can be accessed either with bracket notation or dot notation:
.Data["key"] - Bracket notation (works for any key type) .Data.key - Dot notation (string keys only)
The library supports maps with various key types:
- string, int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- bool, float32, float64
Method Calls ¶
Zero-argument methods can be called as part of a path:
.GetFullName - Calls GetFullName() method .User.String - Calls String() on User
Methods must:
- Take no arguments
- Return at least one value (first value is used)
Error Handling ¶
The library uses graceful failure - invalid paths return nil rather than panicking or returning errors. This design choice simplifies usage in templates and other contexts where nil is an acceptable fallback.
Example Usage ¶
type User struct {
Name string
Age int
Address struct {
City string
}
}
user := User{
Name: "Alice",
Age: 30,
Address: struct{ City string }{City: "NYC"},
}
// Simple field access
name := empaths.Resolve(".Name", user, nil) // "Alice"
// Nested field access
city := empaths.Resolve(".Address.City", user, nil) // "NYC"
// String concatenation
greeting := empaths.Resolve("'Hello, ' .Name '!'", user, nil) // "Hello, Alice!"
// Comparison
isAdult := empaths.Resolve("?.Age=='30'", user, nil) // true
Thread Safety ¶
All functions in this package are safe for concurrent use. The library does not maintain any global state.
Package empaths provides xpath-like path resolution for Go data structures.
Empaths (pronounced "em-paths" - the M stands for Model) allows reflective access to nested values in structs, maps, slices, and arrays using a simple path syntax.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Resolve ¶
func Resolve(path string, data any, refResolver ReferenceResolver) any
Resolve evaluates a path expression against a data model and returns the resolved value.
A path can consist of multiple segments and supports various expression types:
- Model references: Starts with '.' followed by field/property names (e.g., ".User.Name")
- String literals: Enclosed in single or double quotes (e.g., "'Hello'" or "\"World\"")
- Negation: Starts with '!' to negate a boolean value (e.g., "!.IsActive")
- External references: Starts with ':' followed by reference name (e.g., ":config")
- Comparisons: Starts with '?' followed by operands and operator (e.g., "?.Age=='18'")
Character encoding: Path syntax elements (field names, map keys, reference names) should use ASCII characters only. UTF-8 content within string literals is supported, but non-ASCII characters in path syntax have undefined behavior.
Path segments can be combined to form complex expressions, and can include:
- Nested properties: ".User.Address.City"
- Array/slice indexing: ".Users[0].Name"
- Map access: ".Data[\"key\"]" or ".Data.key"
- Method calls: ".User.GetFullName"
Examples:
- ".User.Name" - Accesses the Name property of the User object
- "'Hello ' .User.Name" - Concatenates the string "Hello " with User.Name
- "?.IsAdmin=='true'" - Compares if IsAdmin equals true
- "!.IsBlocked" - Negates the IsBlocked boolean value
- ":config" - References an external value named "config"
Parameters:
- path: The path expression to evaluate
- data: The data model to evaluate the path against
- referenceResolver: Optional function to resolve external references (prefixed with ':')
Returns:
The resolved value from the data model based on the path expression
func ResolveModel ¶
ResolveModel resolves a model reference in a path expression. Model references start with '.' followed by a path to a property or method in the data model. This function can be used directly to resolve a model path against a data object.
A model path can include:
- Simple property access: ".PropertyName"
- Nested property access: ".User.Address.City"
- Array/slice indexing: ".Users[0]"
- Map access: ".Data[\"key\"]" or ".Data.key"
- Method calls: ".GetFullName"
Parameters:
- path: The path expression string
- data: The data model to evaluate against
- index: The current index in the path (should point to the '.' character)
Returns:
- The resolved value from the data model
- The new index after processing
- Error if the path cannot be resolved
Types ¶
type ReferenceResolver ¶
ReferenceResolver is a function type that resolves external references. It takes a reference name and a data context, and returns the resolved value. This can be used to resolve references to templates, configuration values, or any other external data sources.