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 ¶
- type Mapping
- func (m *Mapping) Dig(keys ...string) any
- func (m *Mapping) DigMapping(keys ...string) Mapping
- func (m *Mapping) DigString(keys ...string) string
- func (m *Mapping) Dup() Mapping
- func (m *Mapping) HasKey(key string) bool
- func (m *Mapping) HasMapping(key string) bool
- func (m Mapping) Merge(source Mapping, opts ...MergeOption)
- func (m *Mapping) UnmarshalJSON(text []byte) error
- func (m *Mapping) UnmarshalYAML(unmarshal func(any) error) error
- type MergeOption
- type MergeOptions
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, !
func (*Mapping) HasMapping ¶ added in v0.4.0
HasMapping checks if the key exists in the Mapping and is a Mapping.
func (Mapping) Merge ¶ added in v0.4.0
func (m Mapping) Merge(source Mapping, opts ...MergeOption)
Merge deep merges the source map into the target map. Regardless of options, Mappings will be merged recursively.
func (*Mapping) UnmarshalJSON ¶ added in v0.3.1
UnmarshalText for supporting json.Unmarshal
type MergeOption ¶ added in v0.4.0
type MergeOption func(*MergeOptions)
func WithAppend ¶ added in v0.4.0
func WithAppend() MergeOption
WithAppend sets the Append option to true.
func WithNillify ¶ added in v0.4.0
func WithNillify() MergeOption
WithNillify sets the Nillify option to true.
func WithOverwrite ¶ added in v0.4.0
func WithOverwrite() MergeOption
WithOverwrite sets the Overwrite option to true.
type MergeOptions ¶ added in v0.4.0
type MergeOptions struct { // Overwrite existing values in the target map Overwrite bool // Nillify removes keys from the target map if the value is nil in the source map Nillify bool // Append slices instead of overwriting them Append bool }
MergeOptions are used to configure the Merge function.