Documentation
¶
Overview ¶
Package yamlutils provides utilities to work with YAML documents.
- BytesToYAMLDoc to construct a yaml.Node document
- YAMLToJSON to convert a yaml.Node document to JSON bytes
- YAMLMapSlice to serialize and deserialize YAML with the order of keys maintained
Index ¶
- Constants
- func BytesToYAMLDoc(data []byte) (any, error)
- func YAMLToJSON(value any) (json.RawMessage, error)
- type YAMLMapItem
- type YAMLMapSlice
- func (s YAMLMapSlice) MarshalJSON() ([]byte, error)
- func (s YAMLMapSlice) MarshalYAML() (any, error)
- func (s YAMLMapSlice) OrderedItems() iter.Seq2[string, any]
- func (s *YAMLMapSlice) SetOrderedItems(items iter.Seq2[string, any])
- func (s *YAMLMapSlice) UnmarshalJSON(data []byte) error
- func (s *YAMLMapSlice) UnmarshalYAML(node *yaml.Node) error
Examples ¶
Constants ¶
const (
// ErrYAML is an error raised by YAML utilities
ErrYAML yamlError = "yaml error"
)
Variables ¶
This section is empty.
Functions ¶
func BytesToYAMLDoc ¶
BytesToYAMLDoc converts a byte slice into a YAML document.
This function only supports root documents that are objects.
A YAML document is a pointer to a yaml.Node.
func YAMLToJSON ¶
func YAMLToJSON(value any) (json.RawMessage, error)
YAMLToJSON converts a YAML document into JSON bytes.
Note: a YAML document is the output from a yaml.Marshaler, e.g a pointer to a yaml.Node.
YAMLToJSON is typically called after BytesToYAMLDoc.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/go-openapi/swag/yamlutils"
)
func main() {
const doc = `
---
object:
key: x
b: true
n: 1
`
yml, err := yamlutils.BytesToYAMLDoc([]byte(doc))
if err != nil {
panic(err)
}
d, err := yamlutils.YAMLToJSON(yml)
if err != nil {
panic(err)
}
jazon, err := json.MarshalIndent(d, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(jazon))
}
Output: { "object": { "key": "x", "b": true, "n": 1 } }
Types ¶
type YAMLMapItem ¶
type YAMLMapItem = jsonutils.JSONMapItem
YAMLMapItem represents the value of a key in a YAML object held by YAMLMapSlice.
It is entirely equivalent to jsonutils.JSONMapItem, with the same limitation that you should not Marshal or Unmarshal directly this type, outside of a YAMLMapSlice.
type YAMLMapSlice ¶
type YAMLMapSlice []YAMLMapItem
YAMLMapSlice represents a YAML object, with the order of keys maintained.
It is similar to jsonutils.JSONMapSlice and also knows how to marshal and unmarshal YAML.
It behaves like an ordered map, but keys can't be accessed in constant time.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/go-openapi/swag/yamlutils"
)
func main() {
const doc = `
---
object:
key: x
b: true
n: 1
`
ydoc, err := yamlutils.BytesToYAMLDoc([]byte(doc))
if err != nil {
panic(err)
}
jazon, err := yamlutils.YAMLToJSON(ydoc)
if err != nil {
panic(err)
}
var data yamlutils.YAMLMapSlice
err = json.Unmarshal(jazon, &data)
if err != nil {
panic(err)
}
// reconstruct the initial YAML document, preserving the order of keys
// (but not YAML specifics such as anchors, comments, ...).
reconstructed, err := data.MarshalYAML()
if err != nil {
panic(err)
}
fmt.Println(string(reconstructed.([]byte)))
}
Output: object: key: x b: true n: 1
func (YAMLMapSlice) MarshalJSON ¶
func (s YAMLMapSlice) MarshalJSON() ([]byte, error)
MarshalJSON renders this YAML object as JSON bytes.
The difference with standard JSON marshaling is that the order of keys is maintained.
func (YAMLMapSlice) MarshalYAML ¶
func (s YAMLMapSlice) MarshalYAML() (any, error)
MarshalYAML produces a YAML document as bytes
The difference with standard YAML marshaling is that the order of keys is maintained.
It implements yaml.Marshaler.
func (YAMLMapSlice) OrderedItems ¶ added in v0.25.0
func (s YAMLMapSlice) OrderedItems() iter.Seq2[string, any]
func (*YAMLMapSlice) SetOrderedItems ¶ added in v0.25.0
func (s *YAMLMapSlice) SetOrderedItems(items iter.Seq2[string, any])
SetOrderedItems implements ifaces.SetOrdered: it merges keys passed by the iterator argument into the YAMLMapSlice.
func (*YAMLMapSlice) UnmarshalJSON ¶
func (s *YAMLMapSlice) UnmarshalJSON(data []byte) error
UnmarshalJSON builds this YAML object from JSON bytes.
The difference with standard JSON marshaling is that the order of keys is maintained.
func (*YAMLMapSlice) UnmarshalYAML ¶ added in v0.25.0
func (s *YAMLMapSlice) UnmarshalYAML(node *yaml.Node) error
UnmarshalYAML builds a YAMLMapSlice object from a YAML document yaml.Node.
It implements yaml.Unmarshaler.