diff --git a/template/builder.go b/template/builder.go index 5a2e08cb430dc6ed7ea36f004e6d57125d7066eb..e420bcb30b2b282216693f06ca04bb0dd42170a5 100644 --- a/template/builder.go +++ b/template/builder.go @@ -56,10 +56,7 @@ func NewBuilder(conf *BuilderConfig) *Builder { ctx: context.Background(), data: make(map[string]any), } - builder.funcMap = map[string]any{ - "lookup": getLookup(builder), - "system": getSystem(builder), - } + setFuncMap(builder) return builder } @@ -83,6 +80,7 @@ func (b *Builder) Context() context.Context { func (b *Builder) WithContext(ctx context.Context) *Builder { clone := *b clone.ctx = ctx + setFuncMap(&clone) return &clone } @@ -145,3 +143,10 @@ func (b *Builder) ExecuteMap(patternMap map[string]any, data ...any) (map[string } return b.TextTemplate().ExecuteMap(patternMap, data...) } + +func setFuncMap(b *Builder) { + b.funcMap = map[string]any{ + "lookup": getLookup(b), + "system": getSystem(b), + } +} diff --git a/template/builder_test.go b/template/builder_test.go index 2cd640cd8525671126e84ccfd262a5b19a918364..be97605ae955311aaa277586b35e2d6757ec1209 100644 --- a/template/builder_test.go +++ b/template/builder_test.go @@ -1,6 +1,7 @@ package template import ( + "context" "testing" "git.perx.ru/perxis/perxis-go/pkg/collections" @@ -17,6 +18,8 @@ import ( "github.com/stretchr/testify/require" ) +type testCtxKey struct{} + func TestBuilder(t *testing.T) { t.Run("using default HTML template", func(t *testing.T) { builder := NewBuilder(&BuilderConfig{ @@ -36,6 +39,7 @@ func TestBuilder_Funcs(t *testing.T) { name string pattern string setupContent func(t *testing.T) *content.Content + with func(b *Builder) *Builder spaceID string environmentID string collectionID string @@ -177,6 +181,34 @@ func TestBuilder_Funcs(t *testing.T) { want: "coll_id", assertError: assert.NoError, }, + { + name: "keep builder context", + pattern: "{{ lookup `coll_id.item_id.field` }}", + setupContent: func(t *testing.T) *content.Content { + itemsService := itemsmocks.NewItems(t) + itemsService.On("Get", mock.Anything, "space_id", "env_id", "coll_id", "item_id"). + Return(&items.Item{ + Data: map[string]any{ + "field": "value", + }, + }, nil). + Run(func(args mock.Arguments) { + ctx := args.Get(0).(context.Context) + assert.Equal(t, "bar", ctx.Value(testCtxKey{})) + }). + Once() + return &content.Content{ + Items: itemsService, + } + }, + with: func(b *Builder) *Builder { + return b.WithContext(context.WithValue(context.Background(), testCtxKey{}, "bar")) + }, + spaceID: "space_id", + environmentID: "env_id", + want: "value", + assertError: assert.NoError, + }, } { t.Run(tc.name, func(t *testing.T) { conf := &BuilderConfig{ @@ -188,6 +220,9 @@ func TestBuilder_Funcs(t *testing.T) { conf.Content = tc.setupContent(t) } builder := NewBuilder(conf) + if tc.with != nil { + builder = tc.with(builder) + } got, err := builder.TextTemplate().Execute(tc.pattern) tc.assertError(t, err) assert.Equal(t, tc.want, got) diff --git a/template/funcs.go b/template/funcs.go index 659b9c9dde6d2c5e3789dd71d7466614035c5f23..41dcdc413cbc22ac471d9fd86e0a8f4b7513b7c4 100644 --- a/template/funcs.go +++ b/template/funcs.go @@ -21,7 +21,7 @@ func getLookup(b *Builder) func(string) (any, error) { field := parsedName[2] item, err := b.Content().Items.Get(b.Context(), b.SpaceID(), b.EnvironmentID(), collectionID, itemID) if err != nil { - return nil, errors.Wrapf(err, "failed to get \"%s\"") + return nil, errors.Wrapf(err, "get item %q", itemID) } if len(item.Data) > 0 { diff --git a/template/system.go b/template/system.go index 38ad1a693f758259e516ea749476f382d7eb1f22..dcd82eeb17e7a5df0cc03b04c01d63892b70cea5 100644 --- a/template/system.go +++ b/template/system.go @@ -29,7 +29,8 @@ func (s *System) Environment() (*environments.Environment, error) { return s.builder.environment, nil } - env, err := s.builder.Content().Environments.Get(s.builder.ctx, s.builder.SpaceID(), s.builder.EnvironmentID()) + env, err := s.builder.Content(). + Environments.Get(s.builder.Context(), s.builder.SpaceID(), s.builder.EnvironmentID()) s.builder.environment = env return s.builder.environment, err } @@ -43,7 +44,7 @@ func (s *System) Collection() (*collections.Collection, error) { return s.builder.collection, nil } - coll, err := s.builder.Content().Collections.Get(s.builder.ctx, s.builder.SpaceID(), + coll, err := s.builder.Content().Collections.Get(s.builder.Context(), s.builder.SpaceID(), s.builder.EnvironmentID(), s.builder.CollectionID()) s.builder.collection = coll return s.builder.collection, err