Skip to content

Commit

Permalink
Merge pull request #30 from antonmashko/28-yaml
Browse files Browse the repository at this point in the history
#28 yaml support
  • Loading branch information
antonmashko authored Sep 23, 2023
2 parents 948e4dd + 40cdcfd commit 65d9813
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions external/yaml/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/antonmashko/envconf/external/yaml

go 1.16

require gopkg.in/yaml.v3 v3.0.1
4 changes: 4 additions & 0 deletions external/yaml/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 13 additions & 0 deletions external/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package yaml

import "gopkg.in/yaml.v3"

type Yaml []byte

func (y Yaml) TagName() string {
return "yaml"
}

func (y Yaml) Unmarshal(v interface{}) error {
return yaml.Unmarshal(y, v)
}
31 changes: 31 additions & 0 deletions external/yaml/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package yaml

import (
"reflect"
"testing"
)

func TestYamlConf_ParseSimple_Ok(t *testing.T) {
var data = `
a: Easy!
b:
c: 2
d: [3, 4]
`
tc := struct {
A string
B struct {
RenamedC int `yaml:"c"`
D []int `yaml:",flow"`
}
}{}
extConf := Yaml([]byte(data))
err := extConf.Unmarshal(&tc)
if err != nil {
t.Error("unexpected error")
}

if tc.A != "Easy!" && tc.B.RenamedC != 2 && !reflect.DeepEqual([]int{3, 4}, tc.B.D) {
t.Errorf("incorrect values: %#v", tc)
}
}

0 comments on commit 65d9813

Please sign in to comment.