From 8e80ddcafb08b71c92b7a1480cf41374e995a33b Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Thu, 7 Dec 2023 13:37:11 +0200 Subject: [PATCH] Adjust templateParam type and struct --- assets/template.go | 9 ++------- flows/actions/send_msg.go | 19 ++++++++----------- flows/msg.go | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/assets/template.go b/assets/template.go index fdfc845ae..3a72fdf64 100644 --- a/assets/template.go +++ b/assets/template.go @@ -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 diff --git a/flows/actions/send_msg.go b/flows/actions/send_msg.go index 610cb4d8a..688d88681 100644 --- a/flows/actions/send_msg.go +++ b/flows/actions/send_msg.go @@ -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 @@ -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 diff --git a/flows/msg.go b/flows/msg.go index 3ba5c0ae3..45d23d4b7 100644 --- a/flows/msg.go +++ b/flows/msg.go @@ -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 @@ -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,