diff --git a/automation/service/service.go b/automation/service/service.go index 06671e783..70f957d23 100644 --- a/automation/service/service.go +++ b/automation/service/service.go @@ -113,6 +113,7 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, c Config) &expr.KV{}, &expr.KVV{}, &expr.Reader{}, + &expr.Vars{}, &automation.EmailMessage{}, ) diff --git a/pkg/expr/vars_test.go b/pkg/expr/vars_test.go index 0bb52a269..4627e6555 100644 --- a/pkg/expr/vars_test.go +++ b/pkg/expr/vars_test.go @@ -58,6 +58,22 @@ func TestVars_Decode(t *testing.T) { req.NoError(vars.Decode(dst)) }) + t.Run("vars-in-vars", func(t *testing.T) { + var ( + req = require.New(t) + + dst = &struct { + Vars RVars `var:"vars"` + }{} + + vars = RVars{ + "vars": RVars{"foo": Must(NewString("bar"))}.Vars(), + }.Vars() + ) + + req.NoError(vars.Decode(dst)) + }) + t.Run("int-uint", func(t *testing.T) { var ( req = require.New(t) diff --git a/system/automation/expr_types.gen.go b/system/automation/expr_types.gen.go index ed469963b..70db21ccf 100644 --- a/system/automation/expr_types.gen.go +++ b/system/automation/expr_types.gen.go @@ -224,44 +224,6 @@ func (t *RenderOptions) Assign(val interface{}) error { } } -// RenderVariables is an expression type, wrapper for map[string]interface{} type -type RenderVariables struct{ value map[string]interface{} } - -// NewRenderVariables creates new instance of RenderVariables expression type -func NewRenderVariables(val interface{}) (*RenderVariables, error) { - if c, err := CastToRenderVariables(val); err != nil { - return nil, fmt.Errorf("unable to create RenderVariables: %w", err) - } else { - return &RenderVariables{value: c}, nil - } -} - -// Return underlying value on RenderVariables -func (t RenderVariables) Get() interface{} { return t.value } - -// Return underlying value on RenderVariables -func (t RenderVariables) GetValue() map[string]interface{} { return t.value } - -// Return type name -func (RenderVariables) Type() string { return "RenderVariables" } - -// Convert value to map[string]interface{} -func (RenderVariables) Cast(val interface{}) (TypedValue, error) { - return NewRenderVariables(val) -} - -// Assign new value to RenderVariables -// -// value is first passed through CastToRenderVariables -func (t *RenderVariables) Assign(val interface{}) error { - if c, err := CastToRenderVariables(val); err != nil { - return err - } else { - t.value = c - return nil - } -} - // Role is an expression type, wrapper for *types.Role type type Role struct{ value *types.Role } diff --git a/system/automation/expr_types.yaml b/system/automation/expr_types.yaml index 60832c4b2..f65635620 100644 --- a/system/automation/expr_types.yaml +++ b/system/automation/expr_types.yaml @@ -61,7 +61,5 @@ types: - { name: 'document', exprType: 'Reader', goType: 'io.Reader' } - { name: 'name', exprType: 'string', goType: 'string' } - { name: 'type', exprType: 'string', goType: 'string' } - RenderVariables: - as: 'map[string]interface{}' RenderOptions: as: 'map[string]string' diff --git a/system/automation/templates_handler.gen.go b/system/automation/templates_handler.gen.go index 5cdea4e97..5395797bf 100644 --- a/system/automation/templates_handler.gen.go +++ b/system/automation/templates_handler.gen.go @@ -746,7 +746,7 @@ type ( DocumentType string hasVariables bool - Variables map[string]interface{} + Variables expr.RVars hasOptions bool Options map[string]string @@ -791,7 +791,7 @@ func (h templatesHandler) Render() *atypes.Function { }, { Name: "variables", - Types: []string{"RenderVariables"}, + Types: []string{"Vars"}, }, { Name: "options", diff --git a/system/automation/templates_handler.go b/system/automation/templates_handler.go index b1c7f6bbf..180cd347c 100644 --- a/system/automation/templates_handler.go +++ b/system/automation/templates_handler.go @@ -163,12 +163,9 @@ func (h templatesHandler) recover(ctx context.Context, args *templatesRecoverArg func (h templatesHandler) render(ctx context.Context, args *templatesRenderArgs) (*templatesRenderResults, error) { var err error - vars := make(map[string]interface{}) - if args.hasVariables { - vars, err = cast.ToStringMapE(args.Variables) - if err != nil { - return nil, err - } + vars := args.Variables.Vars().Dict() + if vars == nil { + vars = make(map[string]interface{}) } opts := make(map[string]string) diff --git a/system/automation/templates_handler.yaml b/system/automation/templates_handler.yaml index 1c3d9720c..ffd6e9181 100644 --- a/system/automation/templates_handler.yaml +++ b/system/automation/templates_handler.yaml @@ -153,7 +153,7 @@ functions: - { wf: String } variables: types: - - { wf: RenderVariables } + - { wf: Vars } options: types: - { wf: RenderOptions } diff --git a/system/service/service.go b/system/service/service.go index b3fa1a298..630550343 100644 --- a/system/service/service.go +++ b/system/service/service.go @@ -3,6 +3,8 @@ package service import ( "context" "errors" + "time" + automationService "github.com/cortezaproject/corteza-server/automation/service" "github.com/cortezaproject/corteza-server/pkg/actionlog" intAuth "github.com/cortezaproject/corteza-server/pkg/auth" @@ -19,7 +21,6 @@ import ( "github.com/cortezaproject/corteza-server/system/automation" "github.com/cortezaproject/corteza-server/system/types" "go.uber.org/zap" - "time" ) type ( @@ -173,7 +174,6 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, c Config) automation.User{}, automation.Role{}, automation.Template{}, - automation.RenderVariables{}, automation.RenderOptions{}, automation.Document{}, )