Documentation
¶
Overview ¶
Package yaml reads and writes YAML documents.
Marshal and Unmarshal cover the ordinary case, as in the standard library's encoding packages, and take no option. Anything else lives a layer down:
- codec holds Encoder and Decoder, the twenty-five options that steer them, MapSlice, RawMessage, and the comment types. Use codec.NewDecoder to read a stream and codec.UnmarshalWithOptions to pass an option.
- github.com/go-openapi/go-yaml/ast holds the document as a tree.
- github.com/go-openapi/go-yaml/parser builds that tree from a source, and github.com/go-openapi/go-yaml/parser/scanner hands it the tokens.
- github.com/go-openapi/go-yaml/errors declares the failure every one of them reports, with the position it happened at.
- github.com/go-openapi/go-yaml/expressions navigates a document by path.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal serializes v into a YAML document.
See codec.Marshal for how a Go value is mapped onto YAML and what the struct tags mean.
Example ¶
package main
import (
"bytes"
"fmt"
"strconv"
"github.com/go-openapi/go-yaml"
"github.com/go-openapi/go-yaml/codec"
)
type SlowMarshaler struct {
A string
B int
}
type FastMarshaler struct {
A string
B int
}
type TextMarshaler int64
type TextMarshalerContainer struct {
Field TextMarshaler `yaml:"field"`
}
func (v SlowMarshaler) MarshalYAML() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("tags:\n")
buf.WriteString("- slow-marshaler\n")
buf.WriteString("a: " + v.A + "\n")
buf.WriteString("b: " + strconv.FormatInt(int64(v.B), 10) + "\n")
return buf.Bytes(), nil
}
func (v FastMarshaler) MarshalYAML() (interface{}, error) {
return codec.MapSlice{
{Key: "tags", Value: []string{"fast-marshaler"}},
{Key: "a", Value: v.A},
{Key: "b", Value: v.B},
}, nil
}
func (t TextMarshaler) MarshalText() ([]byte, error) {
return []byte(strconv.FormatInt(int64(t), 8)), nil
}
func main() {
var slow SlowMarshaler
slow.A = "Hello slow poke"
slow.B = 100
buf, err := yaml.Marshal(slow)
if err != nil {
panic(err.Error())
}
fmt.Println(string(buf))
var fast FastMarshaler
fast.A = "Hello speed demon"
fast.B = 100
buf, err = yaml.Marshal(fast)
if err != nil {
panic(err.Error())
}
fmt.Println(string(buf))
text := TextMarshalerContainer{
Field: 11,
}
buf, err = yaml.Marshal(text)
if err != nil {
panic(err.Error())
}
fmt.Println(string(buf))
}
Output: tags: - slow-marshaler a: Hello slow poke b: 100 tags: - fast-marshaler a: Hello speed demon b: 100 field: "13"
Example (ExplicitAnchorAlias) ¶
package main
import (
"fmt"
"github.com/go-openapi/go-yaml"
)
func main() {
type T struct {
A int
B string
}
var v struct {
C *T `yaml:"c,anchor=x"`
D *T `yaml:"d,alias=x"`
}
v.C = &T{A: 1, B: "hello"}
v.D = v.C
bytes, err := yaml.Marshal(v)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
Output: c: &x a: 1 b: hello d: *x
Example (ImplicitAnchorAlias) ¶
package main
import (
"fmt"
"github.com/go-openapi/go-yaml"
)
func main() {
type T struct {
I int
S string
}
var v struct {
A *T `yaml:"a,anchor"`
B *T `yaml:"b,anchor"`
C *T `yaml:"c"`
D *T `yaml:"d"`
}
v.A = &T{I: 1, S: "hello"}
v.B = &T{I: 2, S: "world"}
v.C = v.A // C has same pointer address to A
v.D = v.B // D has same pointer address to B
bytes, err := yaml.Marshal(v)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
Output: a: &a i: 1 s: hello b: &b i: 2 s: world c: *a d: *b
Example (Node) ¶
package main
import (
"fmt"
"github.com/go-openapi/go-yaml"
"github.com/go-openapi/go-yaml/ast"
"github.com/go-openapi/go-yaml/codec"
)
func main() {
type T struct {
Text ast.Node `yaml:"text"`
}
stringNode, err := codec.ValueToNode("node example")
if err != nil {
panic(err)
}
bytes, err := yaml.Marshal(T{Text: stringNode})
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
Output: text: node example
func Unmarshal ¶
Unmarshal decodes the YAML document data into the value pointed to by v.
See codec.Unmarshal for how a YAML document is mapped onto a Go value.
Example (JSONTags) ¶
package main
import (
"fmt"
"log"
"github.com/go-openapi/go-yaml"
)
func main() {
yml := `---
foo: 1
bar: c
`
var v struct {
A int `json:"foo"`
B string `json:"bar"`
}
if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
log.Fatal(err)
}
fmt.Println(v.A)
fmt.Println(v.B)
}
Output: 1 c
Example (YAMLTags) ¶
package main
import (
"fmt"
"log"
"github.com/go-openapi/go-yaml"
)
func main() {
yml := `---
foo: 1
bar: c
A: 2
B: d
`
var v struct {
A int `yaml:"foo" json:"A"`
B string `yaml:"bar" json:"B"`
}
if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
log.Fatal(err)
}
fmt.Println(v.A)
fmt.Println(v.B)
}
Output: 1 c
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package conformance measures this parser against the YAML Test Suite.
|
Package conformance measures this parser against the YAML Test Suite. |
|
Package errors reports what went wrong reading or writing a YAML document, with the position it happened at and the lines of source around it.
|
Package errors reports what went wrong reading or writing a YAML document, with the position it happened at and the lines of source around it. |
|
internal
|
|
|
corpus
Package corpus generates the synthetic documents the benchmarks run on.
|
Package corpus generates the synthetic documents the benchmarks run on. |
|
fuzzseeds
Package fuzzseeds supplies the shared seed corpus for the fuzz targets.
|
Package fuzzseeds supplies the shared seed corpus for the fuzz targets. |
|
lab
Package lab holds parsers we are experimenting on.
|
Package lab holds parsers we are experimenting on. |
|
nocopy
Package nocopy makes a string that shares a byte slice's memory.
|
Package nocopy makes a string that shares a byte slice's memory. |
|
probe
Package probe counts what the library did, for tests that ask a question no output answers.
|
Package probe counts what the library did, for tests that ask a question no output answers. |
|
refparser
Package refparser is the parser this library shipped before the token tape, kept as the yardstick the current one is measured against.
|
Package refparser is the parser this library shipped before the token tape, kept as the yardstick the current one is measured against. |
|
scanner
Package scanner is the internal YAML scanner.
|
Package scanner is the internal YAML scanner. |
|
swar
Package swar scans eight bytes of a document at a time, in one register.
|
Package swar scans eight bytes of a document at a time, in one register. |
|
tokenarena
Package tokenarena holds the tokens of a parse in chunks it can reuse.
|
Package tokenarena holds the tokens of a parse in chunks it can reuse. |
|
analysis
module
|
|
|
This source inspired by https://github.com/fatih/color.
|
This source inspired by https://github.com/fatih/color. |
Click to show internal directories.
Click to hide internal directories.