2020-02-15 22:13:50 +00:00
|
|
|
// 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
|
|
|
|
|
2020-04-26 20:23:03 +00:00
|
|
|
func (b Blob) String() string {
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(b))
|
|
|
|
}
|
|
|
|
|
2020-02-15 22:13:50 +00:00
|
|
|
// 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
|
|
|
|
}
|