structquery

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 7 Imported by: 1

README

structquery

Query data structures using string path in Go

Note: This project is incomplete, not stable, and most features don't work. I am not working on it at the moment. I am shelving it for work later.

Using an Xpath-like query language to get, add, remove, or replace values in a Go data structure. Great for dynamic data structures.

Example

package main

import (
  "fmt"
  "github.com/bryanaustin/structquery"
)

func main() {
  x := map[string]int{"one":1, "two":2, "ten":10}
  y, _ := structquery.Get(x, "two")
  fmt.Println(y[0])
  // Output: 2
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedType  = errors.New("unsupported type")
	ErrInvalidIndex     = errors.New("invlaid index")
	ErrInvalidFieldName = errors.New("invlaid field name")
	ErrInvalidKey       = errors.New("invlaid key")
	ErrNilPointer       = errors.New("nil pointer")
)
View Source
var (
	ErrCantSet = errors.New("cannot set")
)

Functions

func Add

func Add(obj any, path string, value any) error

Add the provided value at the specified path. Overrides the value if target is a field. If target is a slice, append to slice. If target is a map and the key doesn't exist, create the key with value. If target is a map and the key does exist, override the value.

func Get

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

Get retrieves the value from the object at the specified path. If a wildcard is used, a slice of matching values is returned.

func Remove

func Remove(obj interface{}, path string) error

Remove the provided value at the specified path. If the value of the target is the field of a struct, it will be zerored. If it is a pointer, it will not be nulled, but indirect will be zeroed. If value of the target is in a map, it will be deleted from the map. If the target is an index of a slice it will be deleted from the slice while presering slice order. If the target is an index to an array, that value will be zeroed.

func Replace

func Replace(obj interface{}, path string, value interface{}) (interface{}, error)

Replace the target with the value provided and return the old value.

Types

type MatchStack

type MatchStack struct {
	Child  *reflect.Value
	Parent *reflect.Value
}

func Match

func Match(obj any, path []string) ([]MatchStack, error)

Match will find elements of the provided obj and return refrences to their values

Jump to

Keyboard shortcuts

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