Documentation ¶
Overview ¶
Access nested properties in Go data structures with Data Queries
Concepts ¶
A query Result is a tuple (result value:any, missed steps:int), if missed steps is 0, the query was successful and the result is OK.
Use “assert functions” or “fallback functions” to easily extract an expected type from a query result. Both are based on “convert functions”.
The convert function for target type T is func(v any) (r T, err error). If conversion of v fails, err != nil and r is T's zero value. Otherwise, r is the conversion result.
Use As to create an assertion function from a convert function.
Use Fallback to create a fallback function from a convert function.
By convention, convert functions for target type <T> are named To<T>(). Predefined assert functions are named As<T>(), predefined fallback functions <T>Or().
Use Get as the general way to perform queries. Read on to find more specific and efficient ways to query:
Index, Field, Key to step with int, string and any. Further SliceAny, DictAny, MapAny to get query results that can be asserted from typical generic Go data.
To extend daq to your own types write convert functions and familiarize with Stepper, Slice, Dict and Map.
Convert Functions for Go's numeric types ¶
Signed and unsigned integer types are promoted to each other as long as the actual runtime value is in target type's range.
Float values are promoted to integer types if the actual float has no fraction, and it is in target type's range.
Complex values with imag()==0 are promoted to integer by promoting the real() part according to the float rules.
Example ¶
var data any json.Unmarshal([]byte(`{ "a": 3.1415, "bs": [1, 2, 3] }`), &data) res, _ := Get(data, "a") // Ignore error in example for brevity fmt.Println(res.Value, res.Missed()) res, err := Get(data, "b") // This will fail, no "b" fmt.Println(res.Value, res.Missed(), err) x, _ := AsFloat64(Get(data, "bs", 1)) // Type assertion (encoding/json uses float64 for numbers) fmt.Println(x) x, _ = AsFloat64(Get(data, "bs", -1)) // Access backwards from end fmt.Println(x) x = Float64Or(-1)(Get(data, "bs", 3)) // Fallback to -1 if Get fails fmt.Println(x)
Output: 3.1415 0 map[a:3.1415 bs:[1 2 3]] 1 <nil> 2 3 -1
Example (Readme_daq) ¶
var data any json.Unmarshal([]byte(`{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "GlossTerm": "Standard Generalized Markup Language" } } } } }`), &data) title := StringOr("-no title-")(Get(data, "glossary", "title")) fmt.Println(title)
Output: example glossary
Example (Readme_plainGo) ¶
var data any json.Unmarshal([]byte(`{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "GlossTerm": "Standard Generalized Markup Language" } } } } }`), &data) title := "-no title-" jObj, ok := data.(map[string]any) if !ok { fmt.Println(title) return } if data = jObj["glossary"]; data == nil { fmt.Println(title) return } if jObj, ok = data.(map[string]any); !ok { fmt.Println(title) return } if data = jObj["title"]; data == nil { fmt.Println(title) return } title, _ = data.(string) fmt.Println(title)
Output: example glossary
Index ¶
- Variables
- func As[T any](conv func(any) (T, error)) func(Result, error) (T, error)
- func AsBool(res Result, err error) (bool, error)
- func AsComplex128(res Result, err error) (complex128, error)
- func AsComplex64(res Result, err error) (complex64, error)
- func AsDuration(res Result, err error) (time.Duration, error)
- func AsFloat32(res Result, err error) (float32, error)
- func AsFloat64(res Result, err error) (float64, error)
- func AsInt(res Result, err error) (int, error)
- func AsInt16(res Result, err error) (int16, error)
- func AsInt32(res Result, err error) (int32, error)
- func AsInt64(res Result, err error) (int64, error)
- func AsInt8(res Result, err error) (int8, error)
- func AsString(res Result, err error) (string, error)
- func AsTime(res Result, err error) (time.Time, error)
- func AsUint(res Result, err error) (uint, error)
- func AsUint16(res Result, err error) (uint16, error)
- func AsUint32(res Result, err error) (uint32, error)
- func AsUint64(res Result, err error) (uint64, error)
- func AsUint8(res Result, err error) (uint8, error)
- func AsUintPtr(res Result, err error) (uintptr, error)
- func BoolOr(fallback bool) func(Result, error) bool
- func Complex128Or(fallback complex128) func(Result, error) complex128
- func Complex64Or(fallback complex64) func(Result, error) complex64
- func DeepEqual(lhs, rhs any) bool
- func DeepEqualFunc(lhs, rhs any, eq func(any, any) bool) bool
- func DictAnyOr(fallback DictAny) func(Result, error) DictAny
- func DictOr(fallback Dict) func(Result, error) Dict
- func DurationOr(fallback time.Duration) func(Result, error) time.Duration
- func Fallback[T any](conv func(any) (T, error), fallback T) func(Result, error) T
- func Float32Or(fallback float32) func(Result, error) float32
- func Float64Or(fallback float64) func(Result, error) float64
- func Int16Or(fallback int16) func(Result, error) int16
- func Int32Or(fallback int32) func(Result, error) int32
- func Int64Or(fallback int64) func(Result, error) int64
- func Int8Or(fallback int8) func(Result, error) int8
- func IntOr(fallback int) func(Result, error) int
- func MapAnyOr(fallback MapAny) func(Result, error) MapAny
- func MapOr(fallback Map) func(Result, error) Map
- func Must[T any](v T, err error) T
- func RecoverDaQ(err *error)
- func SliceAnyOr(fallback SliceAny) func(Result, error) SliceAny
- func SliceOr(fallback Slice) func(Result, error) Slice
- func StepEach(data any, do func(kind reflect.Kind, key, value any) error) error
- func StringOr(fallback string) func(Result, error) string
- func TestingDeepEqual(t testing.TB, lhs, rhs any) (diffCount int)
- func TestingDeepEqualFunc(t testing.TB, lhs, rhs any, eq func(any, any) bool) (diffCount int)
- func TimeOr(fallback time.Time) func(Result, error) time.Time
- func ToBool(v any) (bool, error)
- func ToComplex128(v any) (complex128, error)
- func ToComplex64(v any) (complex64, error)
- func ToDuration(v any) (time.Duration, error)
- func ToFloat32(v any) (float32, error)
- func ToFloat64(v any) (float64, error)
- func ToInt(v any) (int, error)
- func ToInt16(v any) (int16, error)
- func ToInt32(v any) (int32, error)
- func ToInt64(v any) (int64, error)
- func ToInt8(v any) (int8, error)
- func ToString(v any) (string, error)
- func ToTime(v any) (time.Time, error)
- func ToUint(v any) (uint, error)
- func ToUint16(v any) (uint16, error)
- func ToUint32(v any) (uint32, error)
- func ToUint64(v any) (uint64, error)
- func ToUint8(v any) (uint8, error)
- func ToUintPtr(v any) (uintptr, error)
- func Uint16Or(fallback uint16) func(Result, error) uint16
- func Uint32Or(fallback uint32) func(Result, error) uint32
- func Uint64Or(fallback uint64) func(Result, error) uint64
- func Uint8Or(fallback uint8) func(Result, error) uint8
- func UintOr(fallback uint) func(Result, error) uint
- func UintPtrOr(fallback uintptr) func(Result, error) uintptr
- type Alt
- type CollectionTypeError
- type ConvertError
- type Delta
- type Dict
- type DictAny
- func (d DictAny) AsBool(name string) (bool, error)
- func (d DictAny) AsComplex128(name string) (complex128, error)
- func (d DictAny) AsComplex64(name string) (complex64, error)
- func (d DictAny) AsDict(name string) (Dict, error)
- func (d DictAny) AsDictAny(name string) (DictAny, error)
- func (d DictAny) AsDuration(name string) (time.Duration, error)
- func (d DictAny) AsFloat32(name string) (float32, error)
- func (d DictAny) AsFloat64(name string) (float64, error)
- func (d DictAny) AsInt(name string) (int, error)
- func (d DictAny) AsInt16(name string) (int16, error)
- func (d DictAny) AsInt32(name string) (int32, error)
- func (d DictAny) AsInt64(name string) (int64, error)
- func (d DictAny) AsInt8(name string) (int8, error)
- func (d DictAny) AsMap(name string) (Map, error)
- func (d DictAny) AsMapAny(name string) (MapAny, error)
- func (d DictAny) AsSlice(name string) (Slice, error)
- func (d DictAny) AsSliceAny(name string) (SliceAny, error)
- func (d DictAny) AsString(name string) (string, error)
- func (d DictAny) AsTime(name string) (time.Time, error)
- func (d DictAny) AsUint(name string) (uint, error)
- func (d DictAny) AsUint16(name string) (uint16, error)
- func (d DictAny) AsUint32(name string) (uint32, error)
- func (d DictAny) AsUint64(name string) (uint64, error)
- func (d DictAny) AsUint8(name string) (uint8, error)
- func (d DictAny) AsUintPtr(name string) (uintptr, error)
- func (d DictAny) BoolOr(name string, fallback bool) bool
- func (d DictAny) Complex128Or(name string, fallback complex128) complex128
- func (d DictAny) Complex64Or(name string, fallback complex64) complex64
- func (d DictAny) DaQEachKey(do func(key string) error) error
- func (d DictAny) DaQGet(n string) (Result, error)
- func (d DictAny) DurationOr(name string, fallback time.Duration) time.Duration
- func (d DictAny) Float32Or(name string, fallback float32) float32
- func (d DictAny) Float64Or(name string, fallback float64) float64
- func (d DictAny) Get(name string) (Result, error)
- func (d DictAny) Int16Or(name string, fallback int16) int16
- func (d DictAny) Int32Or(name string, fallback int32) int32
- func (d DictAny) Int64Or(name string, fallback int64) int64
- func (d DictAny) Int8Or(name string, fallback int8) int8
- func (d DictAny) IntOr(name string, fallback int) int
- func (d DictAny) StringOr(name string, fallback string) string
- func (d DictAny) TimeOr(name string, fallback time.Time) time.Time
- func (d DictAny) Uint16Or(name string, fallback uint16) uint16
- func (d DictAny) Uint32Or(name string, fallback uint32) uint32
- func (d DictAny) Uint64Or(name string, fallback uint64) uint64
- func (d DictAny) Uint8Or(name string, fallback uint8) uint8
- func (d DictAny) UintOr(name string, fallback uint) uint
- func (d DictAny) UintPtrOr(name string, fallback uintptr) uintptr
- type FirstIdxOf
- type FirstKeyOf
- type FirstNameOf
- type FirstOf
- type Map
- type MapAny
- func (m MapAny) AsBool(key any) (bool, error)
- func (m MapAny) AsComplex128(key any) (complex128, error)
- func (m MapAny) AsComplex64(key any) (complex64, error)
- func (m MapAny) AsDict(key any) (Dict, error)
- func (m MapAny) AsDictAny(key any) (DictAny, error)
- func (m MapAny) AsDuration(key any) (time.Duration, error)
- func (m MapAny) AsFloat32(key any) (float32, error)
- func (m MapAny) AsFloat64(key any) (float64, error)
- func (m MapAny) AsInt(key any) (int, error)
- func (m MapAny) AsInt16(key any) (int16, error)
- func (m MapAny) AsInt32(key any) (int32, error)
- func (m MapAny) AsInt64(key any) (int64, error)
- func (m MapAny) AsInt8(key any) (int8, error)
- func (m MapAny) AsMap(key any) (Map, error)
- func (m MapAny) AsMapAny(key any) (MapAny, error)
- func (m MapAny) AsSlice(key any) (Slice, error)
- func (m MapAny) AsSliceAny(key any) (SliceAny, error)
- func (m MapAny) AsString(key any) (string, error)
- func (m MapAny) AsTime(key any) (time.Time, error)
- func (m MapAny) AsUint(key any) (uint, error)
- func (m MapAny) AsUint16(key any) (uint16, error)
- func (m MapAny) AsUint32(key any) (uint32, error)
- func (m MapAny) AsUint64(key any) (uint64, error)
- func (m MapAny) AsUint8(key any) (uint8, error)
- func (m MapAny) AsUintPtr(key any) (uintptr, error)
- func (m MapAny) BoolOr(key any, fallback bool) bool
- func (m MapAny) Complex128Or(key any, fallback complex128) complex128
- func (m MapAny) Complex64Or(key any, fallback complex64) complex64
- func (m MapAny) DaQEachKey(do func(key any) error) error
- func (m MapAny) DaQGet(key any) (Result, error)
- func (m MapAny) DurationOr(key any, fallback time.Duration) time.Duration
- func (m MapAny) Float32Or(key any, fallback float32) float32
- func (m MapAny) Float64Or(key any, fallback float64) float64
- func (m MapAny) Get(key any) (Result, error)
- func (m MapAny) Int16Or(key any, fallback int16) int16
- func (m MapAny) Int32Or(key any, fallback int32) int32
- func (m MapAny) Int64Or(key any, fallback int64) int64
- func (m MapAny) Int8Or(key any, fallback int8) int8
- func (m MapAny) IntOr(key, fallback int) int
- func (m MapAny) StringOr(key any, fallback string) string
- func (m MapAny) TimeOr(key any, fallback time.Time) time.Time
- func (m MapAny) Uint16Or(key any, fallback uint16) uint16
- func (m MapAny) Uint32Or(key any, fallback uint32) uint32
- func (m MapAny) Uint64Or(key any, fallback uint64) uint64
- func (m MapAny) Uint8Or(key any, fallback uint8) uint8
- func (m MapAny) UintOr(key any, fallback uint) uint
- func (m MapAny) UintPtrOr(key any, fallback uintptr) uintptr
- type Merge
- type Merger
- type NotFound
- type Result
- func Field(data any, name string) (res Result, err error)
- func Get(data any, path ...any) (res Result, err error)
- func Index(data any, index int) (res Result, err error)
- func Key(data, key any) (res Result, err error)
- func NewResult(value any, missedSteps int) Result
- func Step(data, step any) (res Result, err error)
- type Slice
- type SliceAny
- func (s SliceAny) AsBool(i int) (bool, error)
- func (s SliceAny) AsComplex128(i int) (complex128, error)
- func (s SliceAny) AsComplex64(i int) (complex64, error)
- func (s SliceAny) AsDict(i int) (Dict, error)
- func (s SliceAny) AsDictAny(i int) (DictAny, error)
- func (s SliceAny) AsDuration(i int) (time.Duration, error)
- func (s SliceAny) AsFloat32(i int) (float32, error)
- func (s SliceAny) AsFloat64(i int) (float64, error)
- func (s SliceAny) AsInt(i int) (int, error)
- func (s SliceAny) AsInt16(i int) (int16, error)
- func (s SliceAny) AsInt32(i int) (int32, error)
- func (s SliceAny) AsInt64(i int) (int64, error)
- func (s SliceAny) AsInt8(i int) (int8, error)
- func (s SliceAny) AsMap(i int) (Map, error)
- func (s SliceAny) AsMapAny(i int) (MapAny, error)
- func (s SliceAny) AsSlice(i int) (Slice, error)
- func (s SliceAny) AsSliceAny(i int) (SliceAny, error)
- func (s SliceAny) AsString(i int) (string, error)
- func (s SliceAny) AsTime(i int) (time.Time, error)
- func (s SliceAny) AsUint(i int) (uint, error)
- func (s SliceAny) AsUint16(i int) (uint16, error)
- func (s SliceAny) AsUint32(i int) (uint32, error)
- func (s SliceAny) AsUint64(i int) (uint64, error)
- func (s SliceAny) AsUint8(i int) (uint8, error)
- func (s SliceAny) AsUintPtr(i int) (uintptr, error)
- func (s SliceAny) BoolOr(i int, fallback bool) bool
- func (s SliceAny) Complex128Or(i int, fallback complex128) complex128
- func (s SliceAny) Complex64Or(i int, fallback complex64) complex64
- func (s SliceAny) DaQGet(i int) (Result, error)
- func (s SliceAny) DaQLen() int
- func (s SliceAny) DurationOr(i int, fallback time.Duration) time.Duration
- func (s SliceAny) Float32Or(i int, fallback float32) float32
- func (s SliceAny) Float64Or(i int, fallback float64) float64
- func (s SliceAny) Get(index int) (Result, error)
- func (s SliceAny) Int16Or(i int, fallback int16) int16
- func (s SliceAny) Int32Or(i int, fallback int32) int32
- func (s SliceAny) Int64Or(i int, fallback int64) int64
- func (s SliceAny) Int8Or(i int, fallback int8) int8
- func (s SliceAny) IntOr(i, fallback int) int
- func (s SliceAny) StringOr(i int, fallback string) string
- func (s SliceAny) TimeOr(i int, fallback time.Time) time.Time
- func (s SliceAny) Uint16Or(i int, fallback uint16) uint16
- func (s SliceAny) Uint32Or(i int, fallback uint32) uint32
- func (s SliceAny) Uint64Or(i int, fallback uint64) uint64
- func (s SliceAny) Uint8Or(i int, fallback uint8) uint8
- func (s SliceAny) UintOr(i int, fallback uint) uint
- func (s SliceAny) UintPtrOr(i int, fallback uintptr) uintptr
- type StepError
- type Stepper
- type Time
- type Track
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultMerge = Merge{ Time: Time{time.RFC3339, time.RFC3339Nano}, }
Functions ¶
func As ¶ added in v0.4.1
As returns the assert function for the specified type conversion conv to type T. Assert functions take query results as input. In any case, assert functions
return the missed steps of the query result unchanged.
never return error==nil if the query result was not OK. If necessary, a NotFound error is used.
If the query itself returend an error, As returns the null value of T and the error. Otherwise it performs the conversion – even if the result was not OK.
This means that error==nil is only returned if the conversion of a complete query is successfully converted.
Example ¶
data := struct { Foo int Bar string }{ Foo: 4711, Bar: "baz", } fmt.Println(As(ToString)(Get(data, "Foo"))) fmt.Println(As(ToString)(Get(data, "Bar"))) fmt.Println(As(ToString)(Get(data, "void")))
Output: cannot convert int to string baz <nil> cannot convert struct { Foo int; Bar string } to string
func AsBool ¶
Example ¶
fmt.Println(AsBool(Get(true))) fmt.Println(BoolOr(false)(Get(4711)))
Output: true <nil> false
func AsComplex128 ¶ added in v0.6.0
func AsComplex128(res Result, err error) (complex128, error)
func AsString ¶
Example ¶
fmt.Println(StringOr("-")(DictAny(testExample).Get("a"))) fmt.Println(Fallback(ToString, "-")(Get(testExample, "a", 0, "d")))
Output: - foo
func Complex128Or ¶ added in v0.6.0
func Complex128Or(fallback complex128) func(Result, error) complex128
func Complex64Or ¶ added in v0.6.0
func Fallback ¶ added in v0.7.0
Fallback returns a fallback function for the given type conversion conv. A fallback function always returns a value of expected type T. It returns the asserted value of As in case of success, As returns error==nil. Otherwise it returns the fallback.
Example ¶
data := struct { Foo int Bar string }{ Foo: 4711, Bar: "baz", } fmt.Println(Fallback(ToString, "fallback1")(Get(data, "Foo"))) fmt.Println(Fallback(ToString, "fallback2")(Get(data, "Bar"))) fmt.Println(Fallback(ToString, "fallback3")(Get(data, "void")))
Output: fallback1 baz fallback3
func Must ¶ added in v0.6.0
Must panics with err if err is not zero. Otherwise v is returned. – Remember that you must not expect someone else to recover from a panic you have caused. Must is best used in liaison with deferred RecoverDaQ in small scopes you control yourself.
Example ¶
// Use Must / RecoverDaQ in a single function's scope. myScope := func(data any) (err error) { defer RecoverDaQ(&err) val := Must(Get(data, "glossary", "GlossDiv", "GlossList")) fmt.Println("Get 1:", val, err) ls := Must(AsSliceAny(Get(data, "glossary", "GlossDiv", "GlossList"))) fmt.Println("Get 2:", ls, err) return nil } // Parse some JSON data var data any err := json.Unmarshal([]byte(`{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "GlossTerm": "Standard Generalized Markup Language" } } } } }`), &data) if err != nil { fmt.Println(err) return } // Now, run myScope err = myScope(data) if err != nil { fmt.Printf("myScope returned: %s\n", err) }
Output: Get 1: {map[GlossEntry:map[GlossTerm:Standard Generalized Markup Language ID:SGML]] 0} <nil> myScope returned: cannot convert map[string]interface {} to daq.SliceAny
func RecoverDaQ ¶ added in v0.7.0
func RecoverDaQ(err *error)
RecoverDaQ only recovers from panics created with Must and sets err.
func StepEach ¶ added in v0.3.0
Example (Array) ¶
data := [4]any{0, '1', "two", nil} err := StepEach(data, testPrintStep) fmt.Println(err)
Output: kind 'array': key=0 / value=0 kind 'array': key=1 / value=49 kind 'array': key=2 / value="two" kind 'array': key=3 / value=<nil> <nil>
Example (Atom) ¶
err := StepEach(5180, testPrintStep) fmt.Println(err)
Output: kind 'invalid': key=<nil> / value=5180 <nil>
Example (Map) ¶
data := map[any]any{ false: "foo", 7: nil, "bar": 4711, } err := StepEach(data, testPrintStep) fmt.Println(err)
Output: kind 'map': key=false / value="foo" kind 'map': key=7 / value=<nil> kind 'map': key="bar" / value=4711 <nil>
Example (Slice) ¶
data := []any{0, '1', "two", nil} err := StepEach(data, testPrintStep) fmt.Println(err)
Output: kind 'slice': key=0 / value=0 kind 'slice': key=1 / value=49 kind 'slice': key=2 / value="two" kind 'slice': key=3 / value=<nil> <nil>
Example (Struct) ¶
type Tstnest struct { Foo int } data := struct { Tstnest Bar string Nest Tstnest }{ Tstnest: Tstnest{Foo: 4711}, Bar: "baz", Nest: Tstnest{Foo: -1}, } err := StepEach(data, testPrintStep) fmt.Println(err)
Output: kind 'struct': key="Foo" / value=4711 kind 'struct': key="Bar" / value="baz" kind 'struct': key="Nest" / value=daq.Tstnest{Foo:-1} <nil>
func TestingDeepEqual ¶ added in v0.5.0
func TestingDeepEqualFunc ¶ added in v0.5.0
func ToComplex128 ¶ added in v0.6.0
func ToComplex128(v any) (complex128, error)
func ToComplex64 ¶ added in v0.6.0
Types ¶
type Alt ¶
Example ¶
data := []string{"foo", "bar", "baz"} fmt.Println(Step(data, "bar")) fmt.Println(Step(data, Alt{S: "bar", Or: 4711}))
Output: {<nil> 1} key lookup: cannot use []string as daq.Map {4711 0} <nil>
type CollectionTypeError ¶ added in v0.5.0
type CollectionTypeError struct {
// contains filtered or unexported fields
}
func (CollectionTypeError) Error ¶ added in v0.5.0
func (e CollectionTypeError) Error() string
func (CollectionTypeError) Is ¶ added in v0.5.0
func (e CollectionTypeError) Is(err error) bool
type ConvertError ¶ added in v0.7.0
func (ConvertError[T]) Error ¶ added in v0.7.0
func (e ConvertError[T]) Error() string
type Delta ¶ added in v0.5.0
type Delta struct { AtomEqual func(lhs, rhs any) bool OnNotEqual func(path []any, left, right any) error OnLeftOnly func(path []any, val any) error OnRightOnly func(path []any, val any) error }
Example ¶
l := map[string]any{ "foo": true, "bar": 4711, "baz": "John Doe", } r := map[string]any{ "foo": true, "bar": 1174, "quux": "John Doe", } (&Delta{ OnNotEqual: func(p []any, l, r any) error { fmt.Printf("@%v: %v =/= %v\n", p, l, r) return nil }, OnLeftOnly: func(p []any, l any) error { fmt.Printf("@%v left only: %v\n", p, l) return nil }, OnRightOnly: func(p []any, r any) error { fmt.Printf("@%v right only: %v\n", p, r) return nil }, }).Compare(l, r)
Output: @[bar]: 4711 =/= 1174 @[baz] left only: John Doe @[quux] left only: John Doe
type Dict ¶ added in v0.4.1
type Dict interface { DaQGet(name string) (res Result, err error) DaQEachKey(do func(key string) error) error }
Dict is queried with a string as name.
type DictAny ¶ added in v0.4.1
func (DictAny) AsComplex128 ¶ added in v0.7.0
func (d DictAny) AsComplex128(name string) (complex128, error)
func (DictAny) AsComplex64 ¶ added in v0.7.0
func (DictAny) AsDuration ¶ added in v0.7.0
func (DictAny) AsSliceAny ¶ added in v0.7.0
func (DictAny) Complex128Or ¶ added in v0.7.0
func (d DictAny) Complex128Or(name string, fallback complex128) complex128
func (DictAny) Complex64Or ¶ added in v0.7.0
func (DictAny) DaQEachKey ¶ added in v0.4.1
func (DictAny) DurationOr ¶ added in v0.7.0
type FirstIdxOf ¶ added in v0.5.0
type FirstIdxOf []int
Example ¶
data := []string{"foo", "bar", "baz"} fmt.Println(Step(data, FirstIdxOf{99, 1})) // more efficient fmt.Println(Step(data, FirstOf{99, 1})) // more general
Output: {bar 0} <nil> {bar 0} <nil>
type FirstKeyOf ¶ added in v0.5.0
type FirstKeyOf []any
Example ¶
data := map[any]any{ "foo": 1, "bar": 2, "baz": 3, } fmt.Println(Step(data, FirstKeyOf{"quux", "bar"})) // more efficient fmt.Println(Step(data, FirstOf{"quux", "bar"})) // more general
Output: {2 0} <nil> {2 0} <nil>
type FirstNameOf ¶
type FirstNameOf []string
Example ¶
data := map[string]int{ "foo": 1, "bar": 2, "baz": 3, } fmt.Println(Step(data, FirstNameOf{"quux", "bar"})) // more efficient fmt.Println(Step(data, FirstOf{"quux", "bar"})) // more general
Output: {2 0} <nil> {2 0} <nil>
type Map ¶
type Map interface { DaQGet(key any) (res Result, err error) DaQEachKey(do func(key any) error) error }
Map is queried with any comparable value as key.
type MapAny ¶ added in v0.4.1
func (MapAny) AsComplex128 ¶ added in v0.7.0
func (m MapAny) AsComplex128(key any) (complex128, error)
func (MapAny) AsDuration ¶ added in v0.7.0
func (MapAny) Complex128Or ¶ added in v0.7.0
func (m MapAny) Complex128Or(key any, fallback complex128) complex128
func (MapAny) Complex64Or ¶ added in v0.7.0
func (MapAny) DurationOr ¶ added in v0.7.0
type Merge ¶ added in v0.8.0
type Merge struct {
Time Time
}
Example ¶
var config = struct { Logging struct { Enabled bool Level string } SrvAddr string Misc any }{ SrvAddr: "localhost:8080", } // Loading initial data does not need merging json.Unmarshal([]byte(`{ "Logging": "info", "Misc": { "Foo": "bar" } }`), &config) fmt.Printf("%+v\n", config) // Loading more data does not merge Misc json.Unmarshal([]byte(`{ "Misc": { "Baz": "quux" }, "SrvAddr": ":8081" }`), &config) fmt.Printf("%+v\n", config) // Using Merge does not simply overwrite Misc tmp := make(map[string]any) json.Unmarshal([]byte(`{ "Logging": { "Enabled": true, "Level": "debug" }, "Misc": { "Foo": "bar" } }`), &tmp) DefaultMerge.Merge(&config, tmp) fmt.Printf("%+v\n", config)
Output: {Logging:{Enabled:false Level:} SrvAddr:localhost:8080 Misc:map[Foo:bar]} {Logging:{Enabled:false Level:} SrvAddr::8081 Misc:map[Baz:quux]} {Logging:{Enabled:true Level:debug} SrvAddr::8081 Misc:map[Baz:quux Foo:bar]}
type Result ¶ added in v0.7.0
type Result struct { Value any // The found value if Result.OK() // contains filtered or unexported fields }
Result represents the outcome of a single- or multi-step query. It is not considered an error if a query does not find a result. Errors can be caused by invalid queries and are subject to idiomatic Go error handling.
Use Result.OK and Result.Missed to find out if a query did find a result.
func Get ¶
Get queries data for an element by using the path of steps. See Step and Stepper from details on steps. Get terminates the query if a step is not successful. Get returns the last obtained value with the number of missing steps and, if applicable, the error of the last step.
Example ¶
data := map[string]any{ "a": []any{ map[string]any{ "b": map[any]any{"c": 3}, "d": "foo", }, false, }, } var i int = IntOr(-1)(Get(data, "a", 0, "b", "c")) fmt.Println(i) fmt.Println(Get(data, "a", 0, "b", "d")) fmt.Println(Get(nil, "a", 0, "b", "d"))
Output: 3 {map[c:3] 1} <nil> {<nil> 4} <nil>
func Step ¶
Step does a single query step from data. If step implements Stepper, step.Step() is used. Otherwise Go types int and string are acceped as steps in a query.
- With int you can go one step from Slice or Map to an element.
- With string you can go one step from Dict or Map to an element.
func (Result) Missed ¶ added in v0.7.0
Missed returns the number of missed steps of the query that created r. A query is successful if all steps of the query were successful i.e. there are no missed steps.
type Slice ¶
Slice is queried with an int index. Negative indices access from the end with -1 being the last element and -DaQLen() the first.
type SliceAny ¶ added in v0.4.1
type SliceAny []any
func ToSliceAny ¶ added in v0.7.0
func (SliceAny) AsComplex128 ¶ added in v0.7.0
func (s SliceAny) AsComplex128(i int) (complex128, error)
func (SliceAny) AsComplex64 ¶ added in v0.7.0
func (SliceAny) AsDuration ¶ added in v0.7.0
func (SliceAny) Complex128Or ¶ added in v0.7.0
func (s SliceAny) Complex128Or(i int, fallback complex128) complex128
func (SliceAny) Complex64Or ¶ added in v0.7.0
func (SliceAny) DurationOr ¶ added in v0.7.0
type Stepper ¶
Stepper allows to define how Step does the step from a data value to an element value. Implement Stepper for your own strategy to navigate your data.
type Time ¶
type Time []string
Example ¶
data := struct { Time string }{ Time: "2023-12-29T22:04:00Z", } // Asserting time.Time needs parsing, so we use Time() to create a parsing assertion fmt.Println(Fallback(Time{time.RFC3339}.To, time.Time{})(Get(data, "Time"))) fmt.Println(Fallback(Time{time.RFC3339}.To, time.Time{})(Get(data, "void")))
Output: 2023-12-29 22:04:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC