Documentation
¶
Overview ¶
Package object provides utility methods to work with values where the underlying type is 'map[string]any'.
This can be handy when working with unknown or dynamic json/yaml/toml/… data.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get returns the value at the given path, when it exists and is of type T.
Example ¶
package main
import (
"fmt"
"github.com/KrischanCS/go-toolbox/object"
)
func main() {
obj := object.Object{
"int": 42,
"bool": true,
"object": map[string]any{
"nestedSlice": []float64{1.618, 2.718, 9.81},
"nestedObject": map[string]any{
"deepNestedObject": map[string]any{
"deepNestedFloat": 6.28,
},
},
},
"sliceWithObject": []object.Object{
{
"string": "blue gopher",
},
},
}
i, ok := object.Get[int](obj, "int")
fmt.Println("Gets array from property at root:", ok, i)
i, ok = object.Get[int](obj, "anotherInt")
fmt.Println(
"If property doesn't exist, ok will be false and the value is the zero value of the type:",
ok,
i,
)
b, ok := object.Get[bool](obj, "int")
fmt.Println("If type mismatches, ok is also false:", ok, b)
float, ok := object.Get[float64](
obj,
"object",
"nestedObject",
"deepNestedObject",
"deepNestedFloat",
)
fmt.Println("Nested objects can be obtained with complete path:", ok, float)
float, ok = object.Get[float64](obj, "object", "nestedSlice[2]")
fmt.Println(
"Values from arrays can be obtained by adding [{{index}}] to a path element:",
ok,
float,
)
float, ok = object.Get[float64](obj, "object", "nestedSlice[23]")
fmt.Println(
"If index is out of bounds, ok is false and value the zero value of the type:",
ok,
float,
)
s, ok := object.Get[string](obj, "sliceWithObject[0]", "string")
fmt.Println(
"Values from nested objects in arrays can be obtained:",
ok,
s,
)
}
Output: Gets array from property at root: true 42 If property doesn't exist, ok will be false and the value is the zero value of the type: false 0 If type mismatches, ok is also false: false false Nested objects can be obtained with complete path: true 6.28 Values from arrays can be obtained by adding [{{index}}] to a path element: true 9.81 If index is out of bounds, ok is false and value the zero value of the type: false 0 Values from nested objects in arrays can be obtained: true blue gopher
func Set ¶
Set sets the value at the given path. If the path does not exist, it will be created.
Setting a value in an array or nested in an array, requires the type of the array to be []any/[]interface{}.
If an array index is included the index is >= len(array), the value will be appended to the array. Using [] without an index will also append the
If the path is empty, the function does nothing.
Example ¶
package main
import (
"fmt"
"github.com/KrischanCS/go-toolbox/object"
)
func main() {
obj := object.Object{
"int": 23,
"stringToFloat": "text",
"object": object.Object{
"array": []any{1, 2, 3},
},
}
object.Set(obj, 42, "int")
fmt.Println(obj["int"])
object.Set(obj, 6.28, "stringToFloat")
fmt.Println(obj["stringToFloat"])
object.Set(obj, 23, "object", "array[0]")
fmt.Println(obj["object"].(map[string]any)["array"])
object.Set(obj, 2.718, "object", "array[]")
fmt.Println(obj["object"].(map[string]any)["array"])
object.Set(obj, true, "object", "array[47]")
fmt.Println(obj["object"].(map[string]any)["array"])
object.Set(obj, true, "object", "newBool")
fmt.Println(obj["object"])
}
Output: 42 6.28 [23 2 3] [23 2 3 2.718] [23 2 3 2.718 true] map[array:[23 2 3 2.718 true] newBool:true]
Types ¶
Click to show internal directories.
Click to hide internal directories.