github.com/ijc/docker-app@v0.6.1-0.20181012090447-c7ca8bc483fb/internal/yaml/yaml.go (about)

     1  package yaml
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	"gopkg.in/yaml.v2"
     8  )
     9  
    10  const (
    11  	maxDecodedValues = 1000000
    12  )
    13  
    14  // Unmarshal decodes the first document found within the in byte slice
    15  // and assigns decoded values into the out value.
    16  //
    17  // See gopkg.in/yaml.v2 documentation
    18  func Unmarshal(in []byte, out interface{}) error {
    19  	d := yaml.NewDecoder(bytes.NewBuffer(in), yaml.WithLimitDecodedValuesCount(maxDecodedValues))
    20  	err := d.Decode(out)
    21  	if err == io.EOF {
    22  		return nil
    23  	}
    24  	return err
    25  }
    26  
    27  // Marshal serializes the value provided into a YAML document. The structure
    28  // of the generated document will reflect the structure of the value itself.
    29  // Maps and pointers (to struct, string, int, etc) are accepted as the in value.
    30  //
    31  // See gopkg.in/yaml.v2 documentation
    32  func Marshal(in interface{}) ([]byte, error) {
    33  	return yaml.Marshal(in)
    34  }
    35  
    36  // NewDecoder returns a new decoder that reads from r.
    37  //
    38  // See gopkg.in/yaml.v2 documentation
    39  func NewDecoder(r io.Reader) *yaml.Decoder {
    40  	return yaml.NewDecoder(r, yaml.WithLimitDecodedValuesCount(maxDecodedValues))
    41  }