Documentation ¶
Overview ¶
Access nested properties in Go data structures with DAta Queries
Example ¶
var data any json.Unmarshal([]byte(`{ "a": 3.1415, "bs": [1, 2, 3] }`), &data) prop, _ := Get(data, "a") // Ignore error in example for brevity fmt.Println(prop) prop, err := Get(data, "b") // This will fail, no "b" fmt.Println(prop, err) x, err := AsFloat64(Get(data, "bs", 1)) // Type assertion (encoding/json uses float64 for numbers) fmt.Println(x, err) x, err = AsFloat64(Get(data, "bs", -1)) // Access backwards from end fmt.Println(x, err) x = Float64Or(-1)(Get(data, "bs", 3)) // Fallback to -1 if Get has error fmt.Println(x)
Output: 3.1415 <nil> step 0 [b]: no field named 'b' 2 <nil> 3 <nil> -1
Index ¶
- func AsBool(v any, err error) (bool, error)
- func AsDuration(v any, err error) (time.Duration, error)
- func AsFloat32(v any, err error) (float32, error)
- func AsFloat64(v any, err error) (float64, error)
- func AsInt(v any, err error) (int, error)
- func AsInt16(v any, err error) (int16, error)
- func AsInt32(v any, err error) (int32, error)
- func AsInt64(v any, err error) (int64, error)
- func AsInt8(v any, err error) (int8, error)
- func AsString(v any, err error) (string, error)
- func AsTime(v any, err error) (time.Time, error)
- func AsUint(v any, err error) (uint, error)
- func AsUint16(v any, err error) (uint16, error)
- func AsUint32(v any, err error) (uint32, error)
- func AsUint64(v any, err error) (uint64, error)
- func AsUint8(v any, err error) (uint8, error)
- func AsUintPtr(v any, err error) (uintptr, error)
- func BoolOr(fallback bool) func(any, error) bool
- func DurationOr(fallback time.Duration) func(any, error) time.Duration
- func Field(data any, name string) (any, error)
- func Float32Or(fallback float32) func(any, error) float32
- func Float64Or(fallback float64) func(any, error) float64
- func Get(data any, path ...any) (res any, err error)
- func Index(data any, i int) (any, error)
- func Int16Or(fallback int16) func(any, error) int16
- func Int32Or(fallback int32) func(any, error) int32
- func Int64Or(fallback int64) func(any, error) int64
- func Int8Or(fallback int8) func(any, error) int8
- func IntOr(fallback int) func(any, error) int
- func MapIndex(data any, key reflect.Value) (any, error)
- func MapOr(fallback Map) func(any, error) Map
- func SliceOr(fallback Slice) func(any, error) Slice
- func Step(data, step any) (any, error)
- func StringOr(fallback string) func(any, error) string
- func TimeOr(fallback time.Time) func(any, error) time.Time
- func Uint16Or(fallback uint16) func(any, error) uint16
- func Uint32Or(fallback uint32) func(any, error) uint32
- func Uint64Or(fallback uint64) func(any, error) uint64
- func Uint8Or(fallback uint8) func(any, error) uint8
- func UintOr(fallback uint) func(any, error) uint
- func UintPtrOr(fallback uintptr) func(any, error) uintptr
- func Val[T any](assert func(any, error) (T, error), fallback T) func(v any, err error) T
- type Alt
- type FirstNameOf
- type FirstOf
- type Map
- type NoValue
- type Slice
- type StepError
- type Stepper
- type Time
- type Track
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsBool ¶
Example ¶
fmt.Println(AsBool(Get(true))) fmt.Println(Val(AsBool, false)(Get(4711)))
Output: true <nil> false
func AsString ¶
Example ¶
fmt.Println(Val(AsString, "-")(Map(testExample).Get("a"))) fmt.Println(Val(AsString, "-")(Get(testExample, "a", 0, "d")))
Output: - foo
Types ¶
type FirstNameOf ¶
type FirstNameOf []string
type Slice ¶
type Slice []any
type Time ¶
type Time string
Example ¶
data := struct { Time string }{ Time: "2023-12-29T22:04:00Z", } fmt.Println(Val(Time(time.RFC3339).As, time.Time{})(Get(data, "Time"))) fmt.Println(Val(Time(time.RFC3339).As, time.Time{})(Get(data, "void")))
Output: 2023-12-29 22:04:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC
Click to show internal directories.
Click to hide internal directories.