Documentation
¶
Overview ¶
Package dig provides a map[string]any Mapping type that has ruby-like "dig" functionality.
It can be used for example to access and manipulate arbitrary nested YAML/JSON structures.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Mapping ¶
Mapping is a nested key-value map where the keys are strings and values are any. In Ruby it is called a Hash (with string keys), in YAML it's called a "mapping".
func (*Mapping) Dig ¶
Dig is a simplistic implementation of a Ruby-like Hash.dig functionality.
It returns a value from a (deeply) nested tree structure.
Example ¶
package main
import (
"fmt"
"github.com/k0sproject/dig"
)
func main() {
h := dig.Mapping{
"greeting": dig.Mapping{
"target": "world",
},
}
fmt.Println("Hello,", h.Dig("greeting", "target"))
}
Output: Hello, world
func (*Mapping) DigMapping ¶
DigMapping always returns a mapping, creating missing or overwriting non-mapping branches in between
Example ¶
package main
import (
"fmt"
"github.com/k0sproject/dig"
)
func main() {
h := dig.Mapping{}
h.DigMapping("greeting")["target"] = "world"
fmt.Println("Hello,", h.Dig("greeting", "target"))
}
Output: Hello, world
func (*Mapping) DigString ¶
DigString is like Dig but returns the value as string
Example ¶
package main
import (
"fmt"
"github.com/k0sproject/dig"
)
func main() {
h := dig.Mapping{}
h.DigMapping("greeting")["target"] = "world"
fmt.Println("Hello,", h.DigString("greeting", "target"), "!")
fmt.Println("Hello,", h.Dig("greeting", "non-existing"), "!")
fmt.Println("Hello,", h.DigString("greeting", "non-existing"), "!")
}
Output: Hello, world ! Hello, <nil> ! Hello, !