https://github.com/rkosegi/yaml-toolkit
Tools to manipulate data within the YAML files and more
https://github.com/rkosegi/yaml-toolkit
jmespath jsonpath k8s rfc6901 rfc6902 rfc9535 yaml
Last synced: 5 months ago
JSON representation
Tools to manipulate data within the YAML files and more
- Host: GitHub
- URL: https://github.com/rkosegi/yaml-toolkit
- Owner: rkosegi
- License: apache-2.0
- Created: 2023-03-17T05:15:41.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2026-01-13T14:15:57.000Z (5 months ago)
- Last Synced: 2026-01-13T16:56:41.149Z (5 months ago)
- Topics: jmespath, jsonpath, k8s, rfc6901, rfc6902, rfc9535, yaml
- Language: Go
- Homepage:
- Size: 654 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# YAML toolkit
[](https://codecov.io/gh/rkosegi/yaml-toolkit)
[](https://goreportcard.com/report/github.com/rkosegi/yaml-toolkit)
[](https://sonarcloud.io/summary/new_code?id=rkosegi_yaml-toolkit)
[](https://sonarcloud.io/summary/new_code?id=rkosegi_yaml-toolkit)
[](https://sonarcloud.io/summary/new_code?id=rkosegi_yaml-toolkit)
[](https://sonarcloud.io/summary/new_code?id=rkosegi_yaml-toolkit)
[](https://sonarcloud.io/summary/new_code?id=rkosegi_yaml-toolkit)
[](https://pkg.go.dev/github.com/rkosegi/yaml-toolkit)
[](https://github.com/rkosegi/yaml-toolkit/blob/main/LICENSE)
[](https://github.com/rkosegi/yaml-toolkit/security/code-scanning)
[](https://github.com/rkosegi/yaml-toolkit/actions/workflows/ci.yaml)
[](https://scorecard.dev/viewer/?uri=github.com/rkosegi/yaml-toolkit)
Go library to deal with (not only) YAML documents.
## Usage
### Loading configuration file with defaults
given configuration file
`config.yaml`
```yaml
---
listen_address: 0.0.0.0:8080
read_timeout: 15s
```
```go
package main
import (
"time"
"github.com/rkosegi/yaml-toolkit/dom"
"github.com/rkosegi/yaml-toolkit/fluent"
)
type Config struct {
Address string `yaml:"listen_address"`
ReadTimeout time.Duration `yaml:"read_timeout"`
}
var defConfig = &Config{
Address: "0.0.0.0:8081",
}
func main() {
cfg := fluent.NewConfigHelper[Config]().
Add(defConfig). // add defaults
Load("config.yaml"). // override with values loaded from YAML document
Result() // get merged results
// ... use cfg
}
```
### Opening embedded YAML
`example.yaml`
```yaml
---
kind: ConfigMap
metadata:
name: cm1
namespace: default
apiVersion: v1
data:
application.yaml: |
xyz: 456
abc:
def:
leaf1: 123
leaf2: Hello
```
code
```go
package main
import (
ydom "github.com/rkosegi/yaml-toolkit/dom"
yk8s "github.com/rkosegi/yaml-toolkit/k8s"
)
func main() {
d, err := yk8s.YamlDoc("example.yaml", "application.yaml")
if err != nil {
panic(err)
}
print (d.Document().Child("xyz").(ydom.Leaf).Value()) // 456
d.Document().AddValue("another-key", ydom.LeafNode("789")) // add new child node
err = d.Save()
if err != nil {
panic(err)
}
}
```
### Compute difference between 2 documents
LeftRight
left.yamlright.yaml
```yaml
---
root:
sub1:
leaf1: abc
leaf2: 123
list:
- 1
- 2
```
```yaml
---
root:
sub1:
leaf1: def
leaf3: 789
list:
- 3
```
```go
package main
import (
"fmt"
yta "github.com/rkosegi/yaml-toolkit/analytics"
ytc "github.com/rkosegi/yaml-toolkit/common"
ydiff "github.com/rkosegi/yaml-toolkit/diff"
)
func main() {
dp := ytc.DefaultFileDecoderProvider(".yaml")
ds := yta.NewDocumentSet()
err := ds.AddDocumentFromFile("left.yaml", dp, yta.WithTags("left"))
if err != nil {
panic(err)
}
err = ds.AddDocumentFromFile("right.yaml", dp, yta.WithTags("right"))
if err != nil {
panic(err)
}
changes := ydiff.Diff(
ds.TaggedSubset("right").Merged(),
ds.TaggedSubset("left").Merged(),
)
fmt.Printf("All changes: %d\n", len(*changes))
for _, change := range *changes {
fmt.Printf("%s: %s => %v\n", change.Type, change.Path, change.Value)
}
}
```
output:
```
All changes: 5
Delete: root.list =>
Add: root.list[0] => 3
Change: root.sub1.leaf1 => abc
Delete: root.sub1.leaf2 =>
Add: root.sub1.leaf3 => 789
```