Socket
Socket
Sign inDemoInstall

go.krak3n.codes/gofig/parsers/yaml

Package Overview
Dependencies
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    go.krak3n.codes/gofig/parsers/yaml


Version published

Readme

Source

YAML Parser

Go Version Example Documentation Workflow Status

This parser loads yaml formatted configuration from an io.ReadCloser.

Example

Click the Playground badge above to see the example running in the Go Playground.

package main

import (
	"fmt"
	"io/ioutil"

	"go.krak3n.codes/gofig"
	"go.krak3n.codes/gofig/parsers/yaml"
)

// Config is a struct to unpack configuration into.
type Config struct {
	Foo struct {
		Bar struct {
			Baz string `gofig:"baz"`
		} `gofig:"bar"`
	} `gofig:"foo"`
	Fizz struct {
		Buzz map[string]string `gofig:"buzz"`
	} `gofig:"fizz"`
	A struct {
		B map[string][]int `gofig:"b"`
	} `gofig:"a"`
	C struct {
		D map[string]map[string][]int `gofig:"d"`
	} `gofig:"c"`
}

const blob string = `
foo:
  bar:
    baz: bar
fizz:
  buzz:
    hello: world
    bill: ben`

func main() {
	var cfg Config

	// Initialise gofig with the struct config values will be placed into
	gfg, err := gofig.New(&cfg)
	gofig.Must(err)

	// Create a parser
	parser := yaml.New()

	// write some data to a config file
	path, err := create()
	gofig.Must(err)

	// Parse in order
	gofig.Must(gfg.Parse(
		gofig.FromFile(parser, path),
		gofig.FromString(parser, blob)))

	fmt.Println(fmt.Sprintf("%+v", cfg))
}

const contents = `
a:
  b:
    c: [1,2,3]
c:
  d:
    e:
      f: [1,2,3]`

func create() (string, error) {
	f, err := ioutil.TempFile("", "yaml")
	if err != nil {
		return "", err
	}

	if _, err := f.Write([]byte(contents)); err != nil {
		return "", err
	}

	return f.Name(), nil
}

FAQs

Last updated on 24 May 2020

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