human readable yaml

master
Bel LaPointe 2021-12-15 07:40:36 -07:00
parent 44ce475100
commit aef66625c0
2 changed files with 23 additions and 3 deletions

View File

@ -6,6 +6,8 @@ replace local/storage => ../
replace local/args => ../../args
replace local/logb => ../../logb
require (
local/args v0.0.0-00010101000000-000000000000
local/storage v0.0.0-00010101000000-000000000000

22
yaml.go
View File

@ -11,6 +11,8 @@ import (
"os"
"path"
"path/filepath"
"strings"
"unicode"
yaml "gopkg.in/yaml.v2"
)
@ -83,8 +85,11 @@ func (y *Yaml) GetStream(key string, ns ...string) (io.Reader, error) {
if !ok {
return nil, ErrNotFound
}
b, err := base64.StdEncoding.DecodeString(s)
if strings.HasPrefix("b64://", s) {
b, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(s, "b64://"))
return bytes.NewReader(b), err
}
return strings.NewReader(s), nil
}
func (y *Yaml) Set(key string, value []byte, ns ...string) error {
@ -99,6 +104,15 @@ func (y *Yaml) Del(key string, ns ...string) error {
return y.SetStream(key, nil, ns...)
}
func isASCII(s string) bool {
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
}
func (y *Yaml) SetStream(key string, r io.Reader, ns ...string) error {
namespace := resolve.Namespace(ns)
var v interface{} = nil
@ -107,7 +121,11 @@ func (y *Yaml) SetStream(key string, r io.Reader, ns ...string) error {
if err != nil {
return err
}
v = base64.StdEncoding.EncodeToString(b)
if isASCII(string(b)) {
v = string(b)
} else {
v = "b64://" + base64.StdEncoding.EncodeToString(b)
}
}
m, err := y.getMap()
if err != nil {