šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

github.com/n0madic/go-simpleyaml

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/n0madic/go-simpleyaml

v0.0.0-20230429220356-758dc15cde1d
Source
Go
Version published
Created
Source

go-simpleyaml

Go Reference

Package simpleyaml provides a simple function to parse YAML files.

Advantages:

  • Simple and fast
  • One file (yaml.go)
  • Easy integration into the project
  • No dependencies
  • No reflection
  • Supported value types: string, int64, float64, bool, list, map
  • Access to the value by path (e.g. yamlMap.Path("root[0].key"))

Disadvantages:

  • No support for YAML versions
  • No support for anchors
  • No support for tags
  • No errors handling

Why?

I needed a simple and fast way to parse YAML files in my project. I did not find a suitable solution, so I wrote my own.

Installation

go get -u github.com/n0madic/go-simpleyaml

or move file yaml.go to your project.

Usage

package main

import (
	"fmt"

	simpleyaml "github.com/n0madic/go-simpleyaml"
)

func main() {
	yamlStr := `---
listen:
    address: 127.0.0.1
    ports:
    - 1555
    - 2222
paths:
- path: /home
  name: Home
- path: /tmp
  name: Temp
`
	yamlMap := simpleyaml.ParseYAML(yamlStr)

	// Get the value by path
	fmt.Printf("%s:%d\n", yamlMap.Path("listen.address"), yamlMap.Path("listen.ports[0]"))

	// Get the value from map by key and type assertion
	paths := yamlMap["paths"].([]interface{})
	for _, path := range paths {
		pathMap := path.(simpleyaml.YAMLNode)
		fmt.Printf("%s: %s\n", pathMap["name"], pathMap["path"])
	}
}

Benchmark

Benchmark versus gopkg.in/yaml.v3

goos: darwin
goarch: arm64
pkg: github.com/n0madic/go-simpleyaml
BenchmarkParseYAML
BenchmarkParseYAML-10          	  184214	      6531 ns/op	    6649 B/op	     156 allocs/op
BenchmarkYAMLV3Unmarshal
BenchmarkYAMLV3Unmarshal-10    	   38187	     31400 ns/op	   21105 B/op	     352 allocs/op

FAQs

Package last updated on 29 Apr 2023

Did you know?

Socket

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