Skip to content

Commit

Permalink
chore!: Replacing the data.Data type with a datasource registry
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed May 29, 2024
1 parent 362f058 commit 3ec7071
Show file tree
Hide file tree
Showing 22 changed files with 915 additions and 898 deletions.
28 changes: 14 additions & 14 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"os"
"strings"

"github.com/hairyhenderson/gomplate/v4/data"
"github.com/hairyhenderson/gomplate/v4/internal/datafs"
"github.com/hairyhenderson/gomplate/v4/internal/parsers"
)

// context for templates
Expand All @@ -24,26 +25,25 @@ func (c *tmplctx) Env() map[string]string {
// createTmplContext reads the datasources for the given aliases
func createTmplContext(
ctx context.Context, aliases []string,
//nolint:staticcheck
d *data.Data,
sr datafs.SourceReader,
) (interface{}, error) {
// we need to inject the current context into the Data value, because
// the Datasource method may need it
// TODO: remove this before v4
if d != nil {
d.Ctx = ctx
}

var err error
tctx := &tmplctx{}
for _, a := range aliases {
if a == "." {
return d.Datasource(a)
ct, b, err := sr.ReadSource(ctx, a)
if err != nil {
return nil, err
}
(*tctx)[a], err = d.Datasource(a)

content, err := parsers.ParseData(ct, string(b))
if err != nil {
return nil, err
}

if a == "." {
return content, nil
}

(*tctx)[a] = content
}
return tctx, nil
}
21 changes: 10 additions & 11 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/hairyhenderson/go-fsimpl"
"github.com/hairyhenderson/gomplate/v4/data"
"github.com/hairyhenderson/gomplate/v4/internal/config"
"github.com/hairyhenderson/gomplate/v4/internal/datafs"

Expand All @@ -30,7 +29,10 @@ func TestEnvGetsUpdatedEnvironment(t *testing.T) {

func TestCreateContext(t *testing.T) {
ctx := context.Background()
c, err := createTmplContext(ctx, nil, nil)
reg := datafs.NewRegistry()
sr := datafs.NewSourceReader(reg)

c, err := createTmplContext(ctx, nil, sr)
require.NoError(t, err)
assert.Empty(t, c)

Expand All @@ -43,23 +45,20 @@ func TestCreateContext(t *testing.T) {
barURL := "env:///bar?type=application/yaml"
uf, _ := url.Parse(fooURL)
ub, _ := url.Parse(barURL)
//nolint:staticcheck
d := &data.Data{
Sources: map[string]config.DataSource{
"foo": {URL: uf},
".": {URL: ub},
},
}

reg.Register("foo", config.DataSource{URL: uf})
reg.Register(".", config.DataSource{URL: ub})

t.Setenv("foo", "foo: bar")
c, err = createTmplContext(ctx, []string{"foo"}, d)
c, err = createTmplContext(ctx, []string{"foo"}, sr)
require.NoError(t, err)
assert.IsType(t, &tmplctx{}, c)
tctx := c.(*tmplctx)
ds := ((*tctx)["foo"]).(map[string]interface{})
assert.Equal(t, "bar", ds["foo"])

t.Setenv("bar", "bar: baz")
c, err = createTmplContext(ctx, []string{"."}, d)
c, err = createTmplContext(ctx, []string{"."}, sr)
require.NoError(t, err)
assert.IsType(t, map[string]interface{}{}, c)
ds = c.(map[string]interface{})
Expand Down
Loading

0 comments on commit 3ec7071

Please sign in to comment.