https://github.com/jdockerty/yaml-stream
Go library for parsing multiple YAML documents in a single file, a YAML stream.
https://github.com/jdockerty/yaml-stream
golang yaml yaml-deserialization yaml-library yaml-parser
Last synced: 24 days ago
JSON representation
Go library for parsing multiple YAML documents in a single file, a YAML stream.
- Host: GitHub
- URL: https://github.com/jdockerty/yaml-stream
- Owner: jdockerty
- Created: 2022-12-11T16:59:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-23T11:40:43.000Z (over 3 years ago)
- Last Synced: 2025-01-23T13:29:30.433Z (over 1 year ago)
- Topics: golang, yaml, yaml-deserialization, yaml-library, yaml-parser
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# YAML Stream
A Go library to simplify the handling of YAML streams. These are multiple YAML files
which you will see delimited with `---`, for example
```yaml
items:
- 'apples'
- 'peaches'
language: 'go'
---
name: 'jack'
message: 'hello world'
```
### Usage
Using the example YAML file provided in [`testdata/simple_stream.yaml`](testdata/simple_stream.yaml).
Shown below are some use cases of the library, for further details you can view the tests provided within the
`stream_test.go` file.
```go
package main
import (
"fmt"
"github.com/jdockerty/yaml-stream"
)
func main() {
ys := yamlstream.New()
ys.ReadWithOpen("testdata/simple_stream.yaml")
firstDocument := ys.Get(0)
fmt.Println(firstDocument.String())
// Outputs the YAML first document in the file
// ---
// stream_number: 1
var myMap map[string]int
firstDocument.Unmarshal(&myMap)
fmt.Println(myMap["stream_number"]) // Outputs '1'
var secondValue map[string]int
ys.GetUnmarshal(1, &secondValue)
fmt.Println(secondValue["stream_number"]) // Outputs '2'
}
```
When dealing with complex types, it is necessary to instead use a `map[string]interface{}` type or
unmarshal into a `struct` when the schema is known in advance.
#### CLI
Using this library, you can also utilise the CLI tool, available in the `cmd/ys` package, to print out the YAML document
which is contained within a stream.
For example
```
go install -v github.com/jdockerty/yaml-stream/cmd/ys@latest
ys -filename testdata/simple_stream.yaml
ys -filename testdata/simple_stream.yaml -index 2
```
By default, the first document is printed at index 0. The YAML stream is treated as an array of documents.