Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: Replacing the data.Data type with a datasource registry #2083

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.DataSourceReader,
) (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
Loading