dig

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2021 License: Apache-2.0 Imports: 1 Imported by: 7

README

Dig

A go package that provides a Ruby-like Hash.dig mechanism for map[string]interface{}, which in YAML terminology is refered to as "Mapping".

Usage

Dig's Mapping is useful for example to use as the Unmarshal target of arbitrary YAML/JSON documents.

Example
package main

import (
  "fmt"

  "github.com/k0sproject/dig"
  "gopkg.in/yaml.v2"
)

var yamlDoc = []byte(`---
i18n:
  hello:
    se: Hejsan
    fi: Morjens
  world:
    se: Värld
    fi: Maailma
`)

func main() {
  m := dig.Mapping{}
  if err := yaml.Unmarshal(yamlDoc, &m); err != nil {
    panic(err)
  }

  // You can use DigMapping to access a deeply nested map and set values.
  // Any missing Mapping level in between will be created.
  m.DigMapping("i18n", "hello")["es"] = "Hola"
  m.DigMapping("i18n", "world")["es"] = "Mundo"

  langs := []string{"fi", "se", "es"}
  for _, l := range langs {

    // You can use Dig to access a deeply nested value
    greeting := m.Dig("i18n", "hello", l).(string)

    // ..or DigString to avoid having to cast it to string.
    target := m.DigString("i18n", "world", l)

    fmt.Printf("%s, %s!\n", greeting, target)
  }
}

Output:

Morjens, Maailma!
Hejsan, Värld!
Hola, Mundo!

Documentation

Overview

Package dig provides a map[string]interface{} Mapping type that has ruby-like "dig" functionality.

It can be used for example to access and manipulate arbitary nested YAML/JSON structures.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mapping

type Mapping map[string]interface{}

Mapping is a nested key-value map where the keys are strings and values are interface{}. In Ruby it is called a Hash (with string keys), in YAML it's called a "mapping".

func (*Mapping) Dig

func (m *Mapping) Dig(keys ...string) interface{}

Dig is a simplistic implementation of a Ruby-like Hash.dig functionality.

It returns a value from a (deeply) nested tree structure.

Example
h := Mapping{
	"greeting": Mapping{
		"target": "world",
	},
}
fmt.Println("Hello,", h.Dig("greeting", "target"))
Output:

Hello, world

func (*Mapping) DigMapping

func (m *Mapping) DigMapping(keys ...string) Mapping

DigMapping always returns a mapping, creating missing or overwriting non-mapping branches in between

Example
h := Mapping{}
h.DigMapping("greeting")["target"] = "world"
fmt.Println("Hello,", h.Dig("greeting", "target"))
Output:

Hello, world

func (*Mapping) DigString

func (m *Mapping) DigString(keys ...string) string

DigString is like Dig but returns the value as string

Example
h := 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) Dup added in v0.2.0

func (m *Mapping) Dup() Mapping

Dup creates a dereferenced copy of the Mapping

func (*Mapping) UnmarshalYAML

func (m *Mapping) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML for supporting yaml.Unmarshal

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL