Socket
Socket
Sign inDemoInstall

github.com/k0sproject/dig

Package Overview
Dependencies
2
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/k0sproject/dig

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.


Version published

Readme

Source

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!

FAQs

Last updated on 16 Aug 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc