Skip to content

Commit

Permalink
Changed yaml loader API
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnes committed Aug 2, 2023
1 parent 451ab0f commit e93ac80
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 17 deletions.
27 changes: 22 additions & 5 deletions examples/xload/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"os"
"time"

"github.com/gojekfarm/xtools/xload"
Expand All @@ -23,16 +24,24 @@ func main() {
// from yaml, and then from environment variables
cfg := newAppConfig()

yamlLoader, _ := yaml.NewFileLoader("application.yaml", "_")
f, err := os.Open("application.yaml")
if err != nil {
panic(err)
}

yamlLoader, err := yaml.NewLoader(f, "_")
if err != nil {
panic(err)
}

err := xload.Load(
err = xload.Load(
ctx,
cfg,
xload.FieldTagName("env"),
xload.WithLoader(
xload.SerialLoader(
yamlLoader,
xload.OSLoader(),
xload.OSLoader(), // OSLoader values take precedence over yaml
),
),
)
Expand All @@ -50,7 +59,12 @@ func main() {
// middleware.
rcfg := newRequestConfig()

reqYamlLoader, err := yaml.NewFileLoader("request.default.yaml", "_")
rf, err := os.Open("request.default.yaml")
if err != nil {
panic(err)
}

reqYamlLoader, err := yaml.NewLoader(rf, "_")
if err != nil {
panic(err)
}
Expand All @@ -62,7 +76,7 @@ func main() {
xload.WithLoader(
xload.SerialLoader(
reqYamlLoader,
businessConfigLoader(),
businessConfigLoader(), // businessConfigLoader values take precedence over yaml
),
),
)
Expand All @@ -71,6 +85,9 @@ func main() {
}

prettyPrint(rcfg)

_ = rf.Close()
_ = f.Close()
}

func prettyPrint(v interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion xload/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLoadEnv(t *testing.T) {
Host string `config:"XLOAD_HOST"`
}{}

os.Setenv("XLOAD_HOST", "localhost")
_ = os.Setenv("XLOAD_HOST", "localhost")

err := LoadEnv(context.Background(), cfg)
require.NoError(t, err)
Expand Down
10 changes: 9 additions & 1 deletion xload/providers/yaml/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ require (
gopkg.in/yaml.v3 v3.0.1
)

require github.com/spf13/cast v1.5.1
require (
github.com/spf13/cast v1.5.1
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)
3 changes: 3 additions & 0 deletions xload/providers/yaml/go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/gotidy/ptr v1.4.0 h1:7++suUs+HNHMnyz6/AW3SE+4EnBhupPSQTSI7QNijVc=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
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=
Expand Down
15 changes: 5 additions & 10 deletions xload/providers/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@
package yaml

import (
"os"
"io"

"github.com/spf13/cast"
"gopkg.in/yaml.v3"

"github.com/gojekfarm/xtools/xload"
)

// NewFileLoader reads YAML file and returns a xload.Loader
func NewFileLoader(filename string, delim string) (xload.Loader, error) {
b, err := os.ReadFile(filename)
// NewLoader reads YAML from the given io.Reader and returns a xload.Loader
func NewLoader(r io.Reader, delim string) (xload.Loader, error) {
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}

return NewBytesLoader(b, delim)
}

// NewBytesLoader reads YAML bytes and returns a xload.Loader
func NewBytesLoader(b []byte, delim string) (xload.Loader, error) {
var out map[string]interface{}

err := yaml.Unmarshal(b, &out)
err = yaml.Unmarshal(b, &out)
if err != nil {
return nil, err
}
Expand Down
69 changes: 69 additions & 0 deletions xload/providers/yaml/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Package yaml provides a YAML loader for xload.
package yaml

import (
"bytes"
"context"
"errors"
"io"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewLoader(t *testing.T) {
yaml := `
NAME: "Bombay Bob"
AGE: 30
ADDRESS:
STREET: "123 Main St"
CITY: "Bombay"
`

b := bytes.NewBufferString(yaml)

l, err := NewLoader(b, "_")
assert.NoError(t, err)
assert.NotNil(t, l)

ctx := context.Background()

name, err := l.Load(ctx, "NAME")
assert.NoError(t, err)
assert.Equal(t, "Bombay Bob", name)

street, err := l.Load(ctx, "ADDRESS_STREET")
assert.NoError(t, err)
assert.Equal(t, "123 Main St", street)

phone, err := l.Load(ctx, "PHONE")
assert.NoError(t, err)
assert.Equal(t, "", phone)
}

func TestNewLoader_InvalidYAML(t *testing.T) {
yaml := `a
b`

b := bytes.NewBufferString(yaml)

l, err := NewLoader(b, "_")
assert.Error(t, err)
assert.Nil(t, l)
}

type errReader struct{}

func (e errReader) Read(p []byte) (n int, err error) {
return 0, errors.New("simulated error")
}

func TestNewLoader_ReaderError(t *testing.T) {
b := errReader{}

_, _ = io.ReadAll(b)

l, err := NewLoader(b, "_")
assert.Error(t, err)
assert.Nil(t, l)
}

0 comments on commit e93ac80

Please sign in to comment.