forked from rainycape/gondola
/
yaml.go
66 lines (61 loc) · 2.12 KB
/
yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package yaml provides functions for encoding/decoding YAML.
//
// This package is a small wrapper for other packages or packages
// which do the heavy lifting. The purpose of this package is providing
// a stable and easy to find YAML API in Gondola, so users don't have
// to search and evaluate the miriad of different alternatives available
// when it comes to parsing YAML in Go.
package yaml
import (
"gopkg.in/yaml.v1"
"io"
"io/ioutil"
"os"
)
// Marshal returns the YAML encoding of the in argument. Unexported field names are ignored,
// while exported files are serialized using their name in lowercase as the key. The "yaml"
// struct tag might be used to control the serialization, using the following format:
//
// `yaml:"[name][,flag1][,flag2][,flagn]"
//
// Supported flags are:
//
// - omitempty Ignore the field if it's not set the zero for its type,
// or if it's an empty slice or map
//
// - flow Marshal using a flow style, using explicit indicatos rather than
// indentation to denote scope (this only applies for structs, slices,
// arrays and maps).
//
// - inline Inline the field it's applied to, so its fields
// are processed as if they were part of the outer struct.
//
// Additionally, if the name of the field is set to "-", the field will be skipped.
func Marshal(in interface{}) ([]byte, error) {
return yaml.Marshal(in)
}
// Unmarshal parses the YAML-encoded in data and decodes it into the value pointed by out.
// See Marshal for the available struct field tags.
func Unmarshal(in []byte, out interface{}) error {
return yaml.Unmarshal(in, out)
}
// UnmarshalReader works like Unmarshal, but accepts an io.Reader rather
// than the encoded YAML data.
func UnmarshalReader(r io.Reader, out interface{}) error {
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
return Unmarshal(data, out)
}
// UnmarshalFile works like Unmarshal, but accepts a filename rather
// than the encoded YAML data.
func UnmarshalFile(filename string, out interface{}) error {
f, err := os.Open(filename)
if err != nil {
return err
}
err = UnmarshalReader(f, out)
f.Close()
return err
}