dehub/yamlutil/yamlutil.go

33 lines
740 B
Go
Raw Normal View History

// Package yamlutil contains utility types which are useful for dealing with the
// yaml package.
package yamlutil
import (
"encoding/base64"
)
// Blob encodes and decodes a byte slice as a standard base-64 encoded yaml
// string.
type Blob []byte
// MarshalYAML implements the yaml.Marshaler interface.
func (b Blob) MarshalYAML() (interface{}, error) {
return base64.StdEncoding.EncodeToString([]byte(b)), nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (b *Blob) UnmarshalYAML(unmarshal func(interface{}) error) error {
var b64 string
if err := unmarshal(&b64); err != nil {
return err
}
b64Dec, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return err
}
*b = b64Dec
return nil
}