From a4424ddde8a3e1794f48776428aac4955bb80955 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Wed, 17 Jan 2024 19:34:38 +0200 Subject: [PATCH 1/2] Load template translations params --- assets/static/template.go | 40 ++++++++++++++++++++++++----- assets/static/template_test.go | 6 ++++- assets/template.go | 6 +++++ flows/actions/testdata/_assets.json | 24 +++++++++++++++-- flows/template_test.go | 10 ++++---- 5 files changed, 72 insertions(+), 14 deletions(-) diff --git a/assets/static/template.go b/assets/static/template.go index f2a511cd2..b6ff989a8 100644 --- a/assets/static/template.go +++ b/assets/static/template.go @@ -38,21 +38,23 @@ func (t *Template) Translations() []assets.TemplateTranslation { // TemplateTranslation represents a single template translation type TemplateTranslation struct { - Channel_ *assets.ChannelReference `json:"channel" validate:"required"` - Content_ string `json:"content" validate:"required"` - Locale_ i18n.Locale `json:"locale" validate:"required"` - Namespace_ string `json:"namespace"` - VariableCount_ int `json:"variable_count"` + Channel_ *assets.ChannelReference `json:"channel" validate:"required"` + Content_ string `json:"content" validate:"required"` + Locale_ i18n.Locale `json:"locale" validate:"required"` + Namespace_ string `json:"namespace"` + VariableCount_ int `json:"variable_count"` + Params_ map[string][]TemplateParam `json:"params"` } // NewTemplateTranslation creates a new template translation -func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string) *TemplateTranslation { +func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string, params map[string][]TemplateParam) *TemplateTranslation { return &TemplateTranslation{ Channel_: channel, Content_: content, Namespace_: namespace, Locale_: locale, VariableCount_: variableCount, + Params_: params, } } @@ -70,3 +72,29 @@ func (t *TemplateTranslation) VariableCount() int { return t.VariableCount_ } // Channel returns the channel this template translation is for func (t *TemplateTranslation) Channel() *assets.ChannelReference { return t.Channel_ } + +// Params returns the params for this template translation +func (t *TemplateTranslation) Params() map[string][]assets.TemplateParam { + prs := make(map[string][]assets.TemplateParam, len(t.Params_)) + for k, v := range t.Params_ { + compParams := make([]assets.TemplateParam, len(v)) + for i, pr := range v { + compParams[i] = (assets.TemplateParam)(&pr) + } + prs[k] = compParams + } + return prs +} + +// TemplateParam represents a single parameter for a template translation +type TemplateParam struct { + Type_ string `json:"type"` +} + +// Type returns the type for this parameter +func (t *TemplateParam) Type() string { return t.Type_ } + +// NewTemplateParam creates a new template param +func NewTemplateParam(paramType string) TemplateParam { + return TemplateParam{Type_: paramType} +} diff --git a/assets/static/template_test.go b/assets/static/template_test.go index 1136bc8bb..20efb52f3 100644 --- a/assets/static/template_test.go +++ b/assets/static/template_test.go @@ -12,12 +12,16 @@ import ( func TestTemplate(t *testing.T) { channel := assets.NewChannelReference("Test Channel", "ffffffff-9b24-92e1-ffff-ffffb207cdb4") - translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b") + tp1 := NewTemplateParam("text") + assert.Equal(t, "text", tp1.Type()) + + translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", map[string][]TemplateParam{"body": {tp1}}) assert.Equal(t, channel, translation.Channel()) assert.Equal(t, i18n.Locale("eng-US"), translation.Locale()) assert.Equal(t, "Hello {{1}}", translation.Content()) assert.Equal(t, 1, translation.VariableCount()) assert.Equal(t, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", translation.Namespace()) + assert.Equal(t, map[string][]assets.TemplateParam{"body": {(assets.TemplateParam)(&tp1)}}, translation.Params()) template := NewTemplate(assets.TemplateUUID("8a9c1f73-5059-46a0-ba4a-6390979c01d3"), "hello", []*TemplateTranslation{translation}) assert.Equal(t, assets.TemplateUUID("8a9c1f73-5059-46a0-ba4a-6390979c01d3"), template.UUID()) diff --git a/assets/template.go b/assets/template.go index d30bf608a..3a72fdf64 100644 --- a/assets/template.go +++ b/assets/template.go @@ -42,6 +42,11 @@ type Template interface { Translations() []TemplateTranslation } +// TemplateParam is a parameter for template translation +type TemplateParam interface { + Type() string +} + // TemplateTranslation represents a single translation for a specific template and channel type TemplateTranslation interface { Content() string @@ -49,6 +54,7 @@ type TemplateTranslation interface { Namespace() string VariableCount() int Channel() *ChannelReference + Params() map[string][]TemplateParam } // TemplateReference is used to reference a Template diff --git a/flows/actions/testdata/_assets.json b/flows/actions/testdata/_assets.json index 8231c750c..ce93326c1 100644 --- a/flows/actions/testdata/_assets.json +++ b/flows/actions/testdata/_assets.json @@ -257,7 +257,17 @@ "name": "My Android Phone" }, "locale": "eng-US", - "content": "Hi {{1}}, who's an excellent {{2}}?" + "content": "Hi {{1}}, who's an excellent {{2}}?", + "params": { + "body": [ + { + "type": "text" + }, + { + "type": "text" + } + ] + } }, { "channel": { @@ -265,7 +275,17 @@ "name": "My Android Phone" }, "locale": "spa", - "content": "Hola {{1}}, quien es un {{2}} excelente?" + "content": "Hola {{1}}, quien es un {{2}} excelente?", + "params": { + "body": [ + { + "type": "text" + }, + { + "type": "text" + } + ] + } } ] }, diff --git a/flows/template_test.go b/flows/template_test.go index 8bc6f4187..c5c398448 100644 --- a/flows/template_test.go +++ b/flows/template_test.go @@ -26,7 +26,7 @@ func TestTemplateTranslation(t *testing.T) { channel := assets.NewChannelReference("0bce5fd3-c215-45a0-bcb8-2386eb194175", "Test Channel") for i, tc := range tcs { - tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, len(tc.Variables), "a6a8863e_7879_4487_ad24_5e2ea429027c")) + tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, len(tc.Variables), "a6a8863e_7879_4487_ad24_5e2ea429027c", map[string][]static.TemplateParam{})) result := tt.Substitute(tc.Variables) assert.Equal(t, tc.Expected, result, "%d: unexpected template substitution", i) } @@ -39,10 +39,10 @@ func TestTemplate(t *testing.T) { channel1Ref := assets.NewChannelReference(channel1.UUID(), channel1.Name()) channel2Ref := assets.NewChannelReference(channel2.UUID(), channel2.Name()) - tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", 1, "") - tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "") - tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "") - tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "") + tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) + tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) + tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) + tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) template := flows.NewTemplate(static.NewTemplate("c520cbda-e118-440f-aaf6-c0485088384f", "greeting", []*static.TemplateTranslation{tt1, tt2, tt3, tt4})) tas := flows.NewTemplateAssets([]assets.Template{template}) From 1d62cfe1918fa24c5971d4f913ab6e19348e3b23 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Mon, 22 Jan 2024 16:38:59 +0200 Subject: [PATCH 2/2] Remove template translation variable count --- assets/static/template.go | 27 +++++++++++---------------- assets/static/template_test.go | 3 +-- assets/template.go | 1 - flows/template_test.go | 10 +++++----- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/assets/static/template.go b/assets/static/template.go index b6ff989a8..af55d76e1 100644 --- a/assets/static/template.go +++ b/assets/static/template.go @@ -38,23 +38,21 @@ func (t *Template) Translations() []assets.TemplateTranslation { // TemplateTranslation represents a single template translation type TemplateTranslation struct { - Channel_ *assets.ChannelReference `json:"channel" validate:"required"` - Content_ string `json:"content" validate:"required"` - Locale_ i18n.Locale `json:"locale" validate:"required"` - Namespace_ string `json:"namespace"` - VariableCount_ int `json:"variable_count"` - Params_ map[string][]TemplateParam `json:"params"` + Channel_ *assets.ChannelReference `json:"channel" validate:"required"` + Content_ string `json:"content" validate:"required"` + Locale_ i18n.Locale `json:"locale" validate:"required"` + Namespace_ string `json:"namespace"` + Params_ map[string][]TemplateParam `json:"params"` } // NewTemplateTranslation creates a new template translation -func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string, params map[string][]TemplateParam) *TemplateTranslation { +func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, namespace string, params map[string][]TemplateParam) *TemplateTranslation { return &TemplateTranslation{ - Channel_: channel, - Content_: content, - Namespace_: namespace, - Locale_: locale, - VariableCount_: variableCount, - Params_: params, + Channel_: channel, + Content_: content, + Namespace_: namespace, + Locale_: locale, + Params_: params, } } @@ -67,9 +65,6 @@ func (t *TemplateTranslation) Namespace() string { return t.Namespace_ } // Language returns the locale this translation is in func (t *TemplateTranslation) Locale() i18n.Locale { return t.Locale_ } -// VariableCount returns the number of variables in this template -func (t *TemplateTranslation) VariableCount() int { return t.VariableCount_ } - // Channel returns the channel this template translation is for func (t *TemplateTranslation) Channel() *assets.ChannelReference { return t.Channel_ } diff --git a/assets/static/template_test.go b/assets/static/template_test.go index 20efb52f3..cddb506e4 100644 --- a/assets/static/template_test.go +++ b/assets/static/template_test.go @@ -15,11 +15,10 @@ func TestTemplate(t *testing.T) { tp1 := NewTemplateParam("text") assert.Equal(t, "text", tp1.Type()) - translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", map[string][]TemplateParam{"body": {tp1}}) + translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", map[string][]TemplateParam{"body": {tp1}}) assert.Equal(t, channel, translation.Channel()) assert.Equal(t, i18n.Locale("eng-US"), translation.Locale()) assert.Equal(t, "Hello {{1}}", translation.Content()) - assert.Equal(t, 1, translation.VariableCount()) assert.Equal(t, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", translation.Namespace()) assert.Equal(t, map[string][]assets.TemplateParam{"body": {(assets.TemplateParam)(&tp1)}}, translation.Params()) diff --git a/assets/template.go b/assets/template.go index 3a72fdf64..6889bca83 100644 --- a/assets/template.go +++ b/assets/template.go @@ -52,7 +52,6 @@ type TemplateTranslation interface { Content() string Locale() i18n.Locale Namespace() string - VariableCount() int Channel() *ChannelReference Params() map[string][]TemplateParam } diff --git a/flows/template_test.go b/flows/template_test.go index c5c398448..eb724c095 100644 --- a/flows/template_test.go +++ b/flows/template_test.go @@ -26,7 +26,7 @@ func TestTemplateTranslation(t *testing.T) { channel := assets.NewChannelReference("0bce5fd3-c215-45a0-bcb8-2386eb194175", "Test Channel") for i, tc := range tcs { - tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, len(tc.Variables), "a6a8863e_7879_4487_ad24_5e2ea429027c", map[string][]static.TemplateParam{})) + tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, "a6a8863e_7879_4487_ad24_5e2ea429027c", map[string][]static.TemplateParam{})) result := tt.Substitute(tc.Variables) assert.Equal(t, tc.Expected, result, "%d: unexpected template substitution", i) } @@ -39,10 +39,10 @@ func TestTemplate(t *testing.T) { channel1Ref := assets.NewChannelReference(channel1.UUID(), channel1.Name()) channel2Ref := assets.NewChannelReference(channel2.UUID(), channel2.Name()) - tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) - tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) - tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) - tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) + tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) + tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) + tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) + tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", "", map[string][]static.TemplateParam{"body": {{Type_: "text"}}}) template := flows.NewTemplate(static.NewTemplate("c520cbda-e118-440f-aaf6-c0485088384f", "greeting", []*static.TemplateTranslation{tt1, tt2, tt3, tt4})) tas := flows.NewTemplateAssets([]assets.Template{template})