Skip to content

Commit

Permalink
Add env and template tags
Browse files Browse the repository at this point in the history
  • Loading branch information
teodor-pripoae committed Mar 25, 2020
1 parent a6ecf24 commit f76146a
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -13,4 +13,4 @@ go:
- "1.13.x"
- "tip"

go_import_path: gopkg.in/yaml.v3
go_import_path: github.com/flanksource/yaml
13 changes: 11 additions & 2 deletions decode.go
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"math"
"os"
"reflect"
"strconv"
"time"
Expand Down Expand Up @@ -547,6 +548,14 @@ func (d *decoder) scalar(n *Node, out reflect.Value) bool {
failf("!!binary value contains invalid base64 data")
}
resolved = string(data)
} else if tag == envTag {
resolved = os.Getenv(resolved.(string))
} else if tag == templateTag {
r, err := RenderTemplate(resolved.(string))
if err != nil {
failf("!!template value contains invalid template %s", resolved.(string))
}
resolved = r
}
}
if resolved == nil {
Expand All @@ -570,7 +579,7 @@ func (d *decoder) scalar(n *Node, out reflect.Value) bool {
u, ok := out.Addr().Interface().(encoding.TextUnmarshaler)
if ok {
var text []byte
if tag == binaryTag {
if tag == binaryTag || tag == envTag || tag == templateTag {
text = []byte(resolved.(string))
} else {
// We let any value be unmarshaled into TextUnmarshaler.
Expand All @@ -587,7 +596,7 @@ func (d *decoder) scalar(n *Node, out reflect.Value) bool {
}
switch out.Kind() {
case reflect.String:
if tag == binaryTag {
if tag == binaryTag || tag == envTag || tag == templateTag {
out.SetString(resolved.(string))
return true
}
Expand Down
43 changes: 32 additions & 11 deletions decode_test.go
Expand Up @@ -21,12 +21,13 @@ import (
"fmt"
"io"
"math"
"os"
"reflect"
"strings"
"time"

"github.com/flanksource/yaml"
. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"
)

var unmarshalIntTest = 123
Expand Down Expand Up @@ -767,7 +768,7 @@ var unmarshalTests = []struct {
M{"a": 123456e1},
}, {
"a: 123456E1\n",
M{"a": 123456E1},
M{"a": 123456e1},
},
// yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes
{
Expand Down Expand Up @@ -803,6 +804,22 @@ var unmarshalTests = []struct {
},
},

// Env vars
{
"a: !!env TEST_ENV\n",
map[string]string{"a": "value_env"},
},

// Templates
{
"a: !!template '{{ base64.Encode \"hello world\" }}'",
map[string]string{"a": "aGVsbG8gd29ybGQ="},
},

{
"a: !!template '{{ hello \"world\" }}'",
map[string]string{"a": "Hello world"},
},
}

type M map[string]interface{}
Expand All @@ -822,6 +839,8 @@ type inlineD struct {
}

func (s *S) TestUnmarshal(c *C) {
defer os.Setenv("TEST_ENV", "")
os.Setenv("TEST_ENV", "value_env")
for i, item := range unmarshalTests {
c.Logf("test %d: %q", i, item.data)
t := reflect.ValueOf(item.value).Type()
Expand All @@ -846,6 +865,8 @@ func (s *S) TestUnmarshalFullTimestamp(c *C) {
}

func (s *S) TestDecoderSingleDocument(c *C) {
defer os.Setenv("TEST_ENV", "")
os.Setenv("TEST_ENV", "value_env")
// Test that Decoder.Decode works as expected on
// all the unmarshal tests.
for i, item := range unmarshalTests {
Expand Down Expand Up @@ -949,14 +970,14 @@ var unmarshalErrorTests = []struct {
{"a:\n 1:\nb\n 2:", ".*could not find expected ':'"},
{
"a: &a [00,00,00,00,00,00,00,00,00]\n" +
"b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" +
"c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" +
"d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" +
"e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" +
"f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" +
"g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" +
"h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" +
"i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n",
"b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" +
"c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" +
"d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" +
"e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" +
"f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" +
"g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" +
"h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" +
"i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n",
"yaml: document contains excessive aliasing",
},
}
Expand Down Expand Up @@ -1464,7 +1485,7 @@ func (s *S) TestUnmarshalNull(c *C) {
func (s *S) TestUnmarshalPreservesData(c *C) {
var v struct {
A, B int
C int `yaml:"-"`
C int `yaml:"-"`
}
v.A = 42
v.C = 88
Expand Down
2 changes: 1 addition & 1 deletion encode_test.go
Expand Up @@ -26,8 +26,8 @@ import (
"net"
"os"

"github.com/flanksource/yaml"
. "gopkg.in/check.v1"
"gopkg.in/yaml.v3"
)

var marshalIntTest = 123
Expand Down
2 changes: 1 addition & 1 deletion example_embedded_test.go
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"log"

"gopkg.in/yaml.v3"
"github.com/flanksource/yaml"
)

// An example showing how to unmarshal embedded
Expand Down
32 changes: 30 additions & 2 deletions go.mod
@@ -1,5 +1,33 @@
module "gopkg.in/yaml.v3"
module github.com/flanksource/yaml

require (
"gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Shopify/ejson v1.2.1 // indirect
github.com/aws/aws-sdk-go v1.29.25 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/docker/libkv v0.2.1 // indirect
github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/gosimple/slug v1.9.0 // indirect
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/hairyhenderson/gomplate v3.5.0+incompatible
github.com/hairyhenderson/toml v0.3.0 // indirect
github.com/hashicorp/consul/api v1.4.0 // indirect
github.com/hashicorp/vault/api v1.0.4 // indirect
github.com/joho/godotenv v1.3.0 // indirect
github.com/pkg/errors v0.9.1
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/ugorji/go v1.1.7 // indirect
github.com/zealic/xignore v0.3.3 // indirect
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
gopkg.in/hairyhenderson/yaml.v2 v2.0.0-00010101000000-000000000000 // indirect
gopkg.in/yaml.v2 v2.2.8
gotest.tools v2.2.0+incompatible // indirect
k8s.io/client-go v11.0.0+incompatible // indirect
)

go 1.13

replace gopkg.in/hairyhenderson/yaml.v2 => github.com/maxaudron/yaml v0.0.0-20190411130442-27c13492fe3c

0 comments on commit f76146a

Please sign in to comment.