yamlconv

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

yamlconv

Print & Search Yaml struct in go

import in your project

import (
    "github.com/HaesungSeo/yamlconv"
)

example of printing yaml struct

yaml example

---
sriov:
  - network: resource01 # network name
    interface: net1
    ip: 10.10.0.101     # network ip
gpu:
 drivers: video,compute,utility

service:
  type:
    NodePort: 30080

#cloud-config
password: centos
chpasswd: { expire: False }
ssh_pwauth: True

sample code

lets go and play with playground

package main

import (
    "fmt"
    "strings"

    "github.com/HaesungSeo/yamlconv"
    "gopkg.in/yaml.v2"
)

func main() {
    buf := []string{
        "sriov:",
        "  - network: resource01 # network name",
        "    interface: net1",
        "    ip: 10.10.0.101     # network ip",
        "gpu:",
        " drivers: video,compute,utility",
        "service:",
        "  type:",
        "    NodePort: 30080",
        "#cloud-config",
        "password: centos",
        "chpasswd: { expire: False }",
        "ssh_pwauth: True",
    }

    data := yaml.MapSlice{}
    yaml.Unmarshal([]byte(strings.Join(buf, "\n")), &data)
    yamlconv.Print(data, "  ")
    fmt.Printf("\n")
}

run result

$ ./print

M[sriov]
  A[0/1]
    M[network] Str[resource01]
    M[interface] Str[net1]
    M[ip] Str[10.10.0.101]
M[gpu]
  M[drivers] Str[video,compute,utility]
M[service]
  M[type]
    M[NodePort] Int[30080]
M[password] Str[centos]
M[chpasswd]
  M[expire] Bool[false]
M[ssh_pwauth] Bool[true]
$

example of searching keys in yaml struct

yaml example

lets go and play with playground

---
sriov:
  - network: resource01 # network name
    interface: net1
    ip: 10.10.0.101     # network ip
gpu:
 drivers: video,compute,utility

service:
  type:
    NodePort: 30080

#cloud-config
password: centos
chpasswd: { expire: False }
ssh_pwauth: True

sample code

search the keys "sriov[0].ip" in above yaml sample
expected result: "10.10.0.101"

package main

import (
    "fmt"
    "strings"

    "github.com/HaesungSeo/yamlconv"
    "gopkg.in/yaml.v2"
)

func main() {
    buf := []string{
        "sriov:",
        "  - network: resource01 # network name",
        "    interface: net1",
        "    ip: 10.10.0.101     # network ip",
        "gpu:",
        " drivers: video,compute,utility",
        "service:",
        "  type:",
        "    NodePort: 30080",
        "#cloud-config",
        "password: centos",
        "chpasswd: { expire: False }",
        "ssh_pwauth: True",
    }

    data := yaml.MapSlice{}
    yaml.Unmarshal([]byte(strings.Join(buf, "\n")), &data)
    ret, _ := yamlconv.Search(data, []string{"sriov", "[0]", "ip"})
    yamlconv.Print(ret, "  ")
    fmt.Printf("\n")
}

run result

$ ./search
 Str[10.10.0.101]
$

example of subtract some struct in yaml struct

yaml example

lets go and play with playground

---
sriov:
  - network: resource01 # network name
    interface: net1
    ip: 10.10.0.101     # network ip
  - network: resource02 # network name
    interface: net2
    ip: 20.10.0.101     # network ip
gpu:
 drivers: video,compute,utility

service:
  type:
    NodePort: 30080

#cloud-config
password: centos
chpasswd: { expire: False }
ssh_pwauth: True

sample code

remove the struct under the keys "sriov[1]", "sriov[0].network" and "password" in above yaml sample
lets go and play with playground

package main

import (
    "fmt"
    "strings"

    "github.com/HaesungSeo/yamlconv"
    "gopkg.in/yaml.v2"
)

func main() {
    buf := []string{
        "sriov:",
        "  - network: resource01 # network name",
        "    interface: net1",
        "    ip: 10.10.0.101     # network ip",
        "  - network: resource02 # network name",
        "    interface: net2",
        "    ip: 20.10.0.101     # network ip",
        "gpu:",
        " drivers: video,compute,utility",
        "service:",
        "  type:",
        "    NodePort: 30080",
        "#cloud-config",
        "password: centos",
        "chpasswd: { expire: False }",
        "ssh_pwauth: True",
    }

    data := yaml.MapSlice{}
    yaml.Unmarshal([]byte(strings.Join(buf, "\n")), &data)
    ret, err := yamlconv.Subtract(data, []string{"sriov", "[0]"})
    if err != nil {
        panic(err.Error())
    }
    ret, err = yamlconv.Subtract(ret, []string{"sriov", "[0]", "network"})
    if err != nil {
        panic(err.Error())
    }
    ret, err = yamlconv.Subtract(ret, []string{"password"})
    if err != nil {
        panic(err.Error())
    }
    yamlconv.Print(ret, "  ")
    fmt.Printf("\n")
}

run result

$ ./subtract

M[sriov]
  A[0/1]
    M[interface] Str{net2}
    M[ip] Str{20.10.0.101}
M[gpu]
  M[drivers] Str{video,compute,utility}
M[service]
  M[type]
    M[NodePort] Int{30080}
M[chpasswd]
  M[expire] Bool{false}
M[ssh_pwauth] Bool{true}
$

Documentation

Overview

Package yamlconv implements utility routines for manipulating yaml struct which converted from YAML-encoded string.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFoundError         = errors.New("not found")
	ErrInvalidIndexError     = errors.New("invalid index")
	ErrIndexOutOfRangeError  = errors.New("index out of range")
	ErrSearchKeyTooLongError = errors.New("too many keys")
)

Functions

func MarshalJson

func MarshalJson(data interface{}, keys []string) ([]byte, error)

MarshalJson returns the JSON encoding of the sub yaml struct data. keys are used to filter the match sub yaml struct. a key in kyes must be a form of below: - '[' Unsigned Integer ']', e.g. '[0]', '[10]', etc - any string that can be used as golang map[] key.

func Print

func Print(data interface{}, tab string)

Print prints the YAML-encoded string from the yaml struct data to the standard out. tab is used to spacing the nested yaml structures.

func Search(data interface{}, keys []string) (interface{}, error)

Search returns the match sub-struct of yaml struct data. keys are used to filter the match sub yaml struct. a key in kyes must be a form of below: - '[' Unsigned Integer ']', e.g. '[0]', '[10]', etc - any string that can be used as golang map[] key.

It returns the same yaml struct, if the keys is empty.

An error is returned if there are no match keys or the length of keys are longer than the one of nesting of yaml struct data.

func Subtract added in v1.0.1

func Subtract(data interface{}, keys []string) (interface{}, error)

Subtract returns the modified yaml struct data. keys are used to filter out the matching sub yaml struct. a key in kyes must be a form of below: - '[' Unsigned Integer ']', e.g. '[0]', '[10]', etc - any string that can be used as golang map[] key.

It returns the same yaml struct, if the keys is empty.

An error is returned if there are no match keys or the length of keys are longer than the one of nesting of yaml struct data.

func UnmarshalJson

func UnmarshalJson(data interface{}, keys []string, v any) error

UnmarshalJson parses the yaml struct data and stores the result in the value pointed to by v. keys are used to filter the match sub yaml struct. a key in kyes must be a form of below: - '[' Unsigned Integer ']', e.g. '[0]', '[10]', etc - any string that can be used as golang map[] key.

If v is nil or not a pointer, it returns an InvalidUnmarshalError.

Types

type IndexOutOfRangeError

type IndexOutOfRangeError struct {
	Err error
}

func (*IndexOutOfRangeError) Error

func (e *IndexOutOfRangeError) Error() string

func (*IndexOutOfRangeError) Unwrap added in v1.0.2

func (e *IndexOutOfRangeError) Unwrap() error

type InvalidIndexError

type InvalidIndexError struct {
	Err error
}

func (*InvalidIndexError) Error

func (e *InvalidIndexError) Error() string

func (*InvalidIndexError) Unwrap added in v1.0.2

func (e *InvalidIndexError) Unwrap() error

type NotFoundError

type NotFoundError struct {
	Err error
}

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

func (*NotFoundError) Unwrap added in v1.0.2

func (e *NotFoundError) Unwrap() error

type SearchKeyTooLongError

type SearchKeyTooLongError struct {
	Err error
}

func (*SearchKeyTooLongError) Error

func (e *SearchKeyTooLongError) Error() string

func (*SearchKeyTooLongError) Unwrap added in v1.0.2

func (e *SearchKeyTooLongError) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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