Skip to content

Commit

Permalink
Add TemplateParam struct and remove unused components field
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Dec 6, 2023
1 parent 47a9985 commit e9d3824
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
21 changes: 8 additions & 13 deletions assets/static/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,22 @@ 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"`
Components_ []map[string]any `json:"components"`
Params_ map[string][]any `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"`
VariableCount_ int `json:"variable_count"`
Params_ map[string][]assets.TemplateParam `json:"params"`
}

// NewTemplateTranslation creates a new template translation
func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string, components []map[string]any, params map[string][]any) *TemplateTranslation {
func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string, params map[string][]assets.TemplateParam) *TemplateTranslation {
return &TemplateTranslation{
Channel_: channel,
Content_: content,
Namespace_: namespace,
Locale_: locale,
VariableCount_: variableCount,
Components_: components,
Params_: params,
}
}
Expand All @@ -75,8 +73,5 @@ 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_ }

// Components returns the components for this template translation
func (t *TemplateTranslation) Components() []map[string]any { return t.Components_ }

// Params returns the params for this template translation
func (t *TemplateTranslation) Params() map[string][]any { return t.Params_ }
func (t *TemplateTranslation) Params() map[string][]assets.TemplateParam { return t.Params_ }

Check warning on line 77 in assets/static/template.go

View check run for this annotation

Codecov / codecov/patch

assets/static/template.go#L77

Added line #L77 was not covered by tests
2 changes: 1 addition & 1 deletion assets/static/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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", []map[string]any{}, map[string][]any{})
translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", map[string][]assets.TemplateParam{})
assert.Equal(t, channel, translation.Channel())
assert.Equal(t, i18n.Locale("eng-US"), translation.Locale())
assert.Equal(t, "Hello {{1}}", translation.Content())
Expand Down
8 changes: 6 additions & 2 deletions assets/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ type Template interface {
Translations() []TemplateTranslation
}

type TemplateParam struct {
Type string `json:"type"`
Value string `json:"value"`
}

// TemplateTranslation represents a single translation for a specific template and channel
type TemplateTranslation interface {
Content() string
Locale() i18n.Locale
Namespace() string
VariableCount() int
Channel() *ChannelReference
Components() []map[string]any
Params() map[string][]any
Params() map[string][]TemplateParam
}

// TemplateReference is used to reference a Template
Expand Down
9 changes: 9 additions & 0 deletions flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
"text": "Hi @contact.name, who's a good boy?",
"templating": {
"uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785",
"params": null,
"template": {
"uuid": "b620b463-8d15-427f-b2e3-4f44f9f071ec",
"name": "missing"
Expand Down Expand Up @@ -419,6 +420,7 @@
"text": "Hi Ryan Lewis, who's a good boy?",
"templating": {
"uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785",
"params": null,
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
Expand All @@ -444,6 +446,7 @@
},
"text": "Hi Ryan Lewis, who's an excellent boy?",
"templating": {
"params": {},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
Expand Down Expand Up @@ -491,6 +494,7 @@
"text": "Hi Ryan Lewis, who's a good boy?",
"templating": {
"uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785",
"params": null,
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
Expand Down Expand Up @@ -525,6 +529,7 @@
},
"text": "Hola Ryan Lewis, quien es un niño excelente?",
"templating": {
"params": {},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
Expand Down Expand Up @@ -573,6 +578,7 @@
"text": "Hi there, it's time to get up!",
"templating": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
"params": null,
"template": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
"name": "wakeup"
Expand All @@ -594,6 +600,7 @@
},
"text": "Hi there, it's time to get up!",
"templating": {
"params": {},
"template": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
"name": "wakeup"
Expand Down Expand Up @@ -631,6 +638,7 @@
"uuid": "4f0510e9-7e41-4f83-aaa3-59b6e9a17e70",
"text": "Hi Ryan, wakeup!",
"templating": {
"params": null,
"uuid": "dce1ab9c-81d7-4e2f-b7d6-33e577c731ba",
"template": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
Expand Down Expand Up @@ -662,6 +670,7 @@
},
"text": "Hi there, it's time to get up!",
"templating": {
"params": {},
"template": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
"name": "wakeup"
Expand Down
10 changes: 5 additions & 5 deletions flows/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]any{}, map[string][]any{}))
tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, len(tc.Variables), "a6a8863e_7879_4487_ad24_5e2ea429027c", map[string][]assets.TemplateParam{}))
result := tt.Substitute(tc.Variables)
assert.Equal(t, tc.Expected, result, "%d: unexpected template substitution", i)
}
Expand All @@ -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]any{}, map[string][]any{})
tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "", []map[string]any{}, map[string][]any{})
tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "", []map[string]any{}, map[string][]any{})
tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "", []map[string]any{}, map[string][]any{})
tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", 1, "", map[string][]assets.TemplateParam{})
tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "", map[string][]assets.TemplateParam{})
tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "", map[string][]assets.TemplateParam{})
tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "", map[string][]assets.TemplateParam{})
template := flows.NewTemplate(static.NewTemplate("c520cbda-e118-440f-aaf6-c0485088384f", "greeting", []*static.TemplateTranslation{tt1, tt2, tt3, tt4}))

tas := flows.NewTemplateAssets([]assets.Template{template})
Expand Down

0 comments on commit e9d3824

Please sign in to comment.