Skip to content

Commit

Permalink
Adjust templateParam type and struct
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Dec 7, 2023
1 parent 54710ac commit 8e80ddc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
9 changes: 2 additions & 7 deletions assets/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,9 @@ type Template interface {
Translations() []TemplateTranslation
}

// TemplateParamUUID is the UUID of a param
type TemplateParamUUID uuids.UUID

// TemplateParam is a parameter for template translation
type TemplateParam struct {
Type string `json:"type"`
UUID TemplateParamUUID `json:"uuid"`
Value string `json:"value" engine:"localized,evaluated"`
type TemplateParam interface {
Type() string
}

// TemplateTranslation represents a single translation for a specific template and channel
Expand Down
19 changes: 8 additions & 11 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ type SendMsgAction struct {

// Templating represents the templating that should be used if possible
type Templating struct {
UUID uuids.UUID `json:"uuid" validate:"required,uuid4"`
Template *assets.TemplateReference `json:"template" validate:"required"`
Variables []string `json:"variables" engine:"localized,evaluated"`
Params map[string][]assets.TemplateParam `json:"params"`
UUID uuids.UUID `json:"uuid" validate:"required,uuid4"`
Template *assets.TemplateReference `json:"template" validate:"required"`
Variables []string `json:"variables" engine:"localized,evaluated"`
Params map[string][]flows.TemplateParam `json:"params"`
}

// LocalizationUUID gets the UUID which identifies this object for localization
Expand Down Expand Up @@ -123,21 +123,18 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
}
evaluatedText = translation.Substitute(evaluatedVariables)

evaluatedParams := make(map[string][]assets.TemplateParam)
evaluatedParams := make(map[string][]flows.TemplateParam)

for compKey, compParams := range a.Templating.Params {
compVariables := make([]assets.TemplateParam, len(compParams))
compVariables := make([]flows.TemplateParam, len(compParams))
for i, templateParam := range compParams {

evaluatedParam := assets.TemplateParam{Type: string(templateParam.Type), UUID: templateParam.UUID}

localizedParamVariables, _ := run.GetTextArray(uuids.UUID(templateParam.UUID), "value", []string{templateParam.Value}, nil)
localizedParamVariables, _ := run.GetTextArray(uuids.UUID(templateParam.UUID()), "value", []string{templateParam.Value()}, nil)
sub, err := run.EvaluateTemplate(localizedParamVariables[0])
if err != nil {
logEvent(events.NewError(err))
}

evaluatedParam.Value = sub
evaluatedParam := flows.NewTemplateParam(templateParam.Type(), templateParam.UUID(), sub)
compVariables[i] = evaluatedParam
}
evaluatedParams[compKey] = compVariables
Expand Down
39 changes: 33 additions & 6 deletions flows/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,39 @@ func (m *MsgOut) Locale() i18n.Locale { return m.Locale_ }
// UnsendableReason returns the reason this message can't be sent (if any)
func (m *MsgOut) UnsendableReason() UnsendableReason { return m.UnsendableReason_ }

// TemplateParamUUID is the UUID of a param
type TemplateParamUUID uuids.UUID

type TemplateParam struct {
Type_ string `json:"type"`
UUID_ TemplateParamUUID `json:"uuid"`
Value_ string `json:"value"`
}

// Type returns the type for this parameter
func (t *TemplateParam) Type() string { return t.Type_ }

// UUID returns the UUID for this parameter
func (t *TemplateParam) UUID() TemplateParamUUID { return t.UUID_ }

// Value returns the value for this parameter
func (t *TemplateParam) Value() string { return t.Value_ }

// NewMsgTemplating creates and returns a new msg template
func NewTemplateParam(paramType string, uuid TemplateParamUUID, value string) TemplateParam {
return TemplateParam{
Type_: paramType,
UUID_: uuid,
Value_: value,
}
}

// MsgTemplating represents any substituted message template that should be applied when sending this message
type MsgTemplating struct {
Template_ *assets.TemplateReference `json:"template"`
Variables_ []string `json:"variables,omitempty"`
Namespace_ string `json:"namespace"`
Params_ map[string][]assets.TemplateParam `json:"params"`
Template_ *assets.TemplateReference `json:"template"`
Variables_ []string `json:"variables,omitempty"`
Namespace_ string `json:"namespace"`
Params_ map[string][]TemplateParam `json:"params"`
}

// Template returns the template this msg template is for
Expand All @@ -186,10 +213,10 @@ func (t MsgTemplating) Variables() []string { return t.Variables_ }
func (t MsgTemplating) Namespace() string { return t.Namespace_ }

// Params returns the params that should be used for the template
func (t MsgTemplating) Params() map[string][]assets.TemplateParam { return t.Params_ }
func (t MsgTemplating) Params() map[string][]TemplateParam { return t.Params_ }

// NewMsgTemplating creates and returns a new msg template
func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string, params map[string][]assets.TemplateParam) *MsgTemplating {
func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string, params map[string][]TemplateParam) *MsgTemplating {
return &MsgTemplating{
Template_: template,
Variables_: variables,
Expand Down

0 comments on commit 8e80ddc

Please sign in to comment.