3
0

Rework render vars, allow (R)Vars as input arg

This commit is contained in:
Denis Arh
2021-03-30 16:18:34 +02:00
parent 333b2f344f
commit e49f09405b
8 changed files with 25 additions and 51 deletions

View File

@@ -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{},
)

View File

@@ -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)

View File

@@ -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 }

View File

@@ -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'

View File

@@ -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",

View File

@@ -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)

View File

@@ -153,7 +153,7 @@ functions:
- { wf: String }
variables:
types:
- { wf: RenderVariables }
- { wf: Vars }
options:
types:
- { wf: RenderOptions }

View File

@@ -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{},
)