Skip to content

Commit

Permalink
Add UUID field templateParam so we can properly support localization …
Browse files Browse the repository at this point in the history
…excellent variables
  • Loading branch information
norkans7 committed Dec 6, 2023
1 parent 80a02c5 commit b27d1e4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 32 deletions.
7 changes: 5 additions & 2 deletions assets/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ type Template interface {
Translations() []TemplateTranslation
}

// TemplateParam is the UUID of a param
type TemplateParamUUID uuids.UUID
type TemplateParam struct {
Type string `json:"type"`
Value string `json:"value"`
Type string `json:"type"`
UUID TemplateParamUUID `json:"uuid"`
Value string `json:"value" engine:"localized,evaluated"`
}

// TemplateTranslation represents a single translation for a specific template and channel
Expand Down
38 changes: 18 additions & 20 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][]map[string]string `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][]assets.TemplateParam `json:"params"`
}

// LocalizationUUID gets the UUID which identifies this object for localization
Expand Down Expand Up @@ -121,31 +121,29 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
}
evaluatedVariables[i] = sub
}
evaluatedText = translation.Substitute(evaluatedVariables)

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

for compKey, compParams := range a.Templating.Params {
compVariables := make([]any, len(compParams))
for _, variableMap := range compParams {
evaluatedMap := make(map[string]string)
for k, v := range variableMap {
if k == "value" {
sub, err := run.EvaluateTemplate(variableMap[k])
if err != nil {
logEvent(events.NewError(err))
}
evaluatedMap[k] = sub
} else {
evaluatedMap[k] = v
}
compVariables := make([]assets.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)
sub, err := run.EvaluateTemplate(localizedParamVariables[0])
if err != nil {
logEvent(events.NewError(err))

Check warning on line 137 in flows/actions/send_msg.go

View check run for this annotation

Codecov / codecov/patch

flows/actions/send_msg.go#L137

Added line #L137 was not covered by tests
}
compVariables = append(compVariables, evaluatedMap)

evaluatedParam.Value = sub
compVariables[i] = evaluatedParam
}
evaluatedParams[compKey] = compVariables

}

evaluatedText = translation.Substitute(evaluatedVariables)
templating = flows.NewMsgTemplating(a.Templating.Template, evaluatedVariables, translation.Namespace(), evaluatedParams)
locale = translation.Locale()
}
Expand Down
66 changes: 62 additions & 4 deletions flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,20 @@
"text": "Hi Ryan Lewis, who's a good boy?",
"templating": {
"uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785",
"params": {"body": [{"type": "text", "value": "@contact.name"}, {"type": "text", "value": "boy"}]},
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "@contact.name"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "boy"
}
]
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
Expand All @@ -446,7 +459,20 @@
},
"text": "Hi Ryan Lewis, who's an excellent boy?",
"templating": {
"params": {"body": [{"type": "text", "value": "Ryan Lewis"}, {"type": "text", "value": "boy"}]},
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "Ryan Lewis"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "boy"
}
]
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
Expand Down Expand Up @@ -494,7 +520,20 @@
"text": "Hi Ryan Lewis, who's a good boy?",
"templating": {
"uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785",
"params": {"body": [{"type": "text", "value": "@contact.name"}, {"type": "text", "value": "boy"}]},
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "@contact.name"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "boy"
}
]
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
Expand All @@ -512,6 +551,12 @@
"@contact.name",
"niño"
]
},
"f383e94d-0598-4fd9-81d7-f0aea397d63b": {
"value": ["niño"]
},
"74d59988-18c0-4919-9cb4-9f9e3847ed50": {
"value": ["@contact.name"]
}
}
},
Expand All @@ -529,7 +574,20 @@
},
"text": "Hola Ryan Lewis, quien es un niño excelente?",
"templating": {
"params": {"body": [{"type": "text", "value": "Ryan Lewis"}, {"type": "text", "value": "niño"}]},
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "Ryan Lewis"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "niño"
}
]
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
Expand Down
12 changes: 6 additions & 6 deletions flows/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ func (m *MsgOut) UnsendableReason() UnsendableReason { return m.UnsendableReason

// 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][]any `json:"params"`
Template_ *assets.TemplateReference `json:"template"`
Variables_ []string `json:"variables,omitempty"`
Namespace_ string `json:"namespace"`
Params_ map[string][]assets.TemplateParam `json:"params"`
}

// Template returns the template this msg template is for
Expand All @@ -186,10 +186,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][]any { return t.Params_ }
func (t MsgTemplating) Params() map[string][]assets.TemplateParam { return t.Params_ }

Check warning on line 189 in flows/msg.go

View check run for this annotation

Codecov / codecov/patch

flows/msg.go#L189

Added line #L189 was not covered by tests

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

0 comments on commit b27d1e4

Please sign in to comment.