Skip to content

Commit

Permalink
Include template params in template (flow def expressions) inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jan 25, 2024
1 parent 9f700ac commit fe2f3db
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
16 changes: 14 additions & 2 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
)

func init() {
Expand Down Expand Up @@ -57,8 +58,19 @@ type TemplateParams struct {
Values map[string][]string
}

// LocalizationUUID gets the UUID which identifies this object for localization
func (p *TemplateParams) LocalizationUUID() uuids.UUID { return uuids.UUID(p.UUID) }
func (p *TemplateParams) EnumerateTemplates(localization flows.Localization, include func(i18n.Language, string)) {
for _, comp := range utils.SortedKeys(p.Values) {
for _, v := range p.Values[comp] {
include(i18n.NilLanguage, v)
}
for _, lang := range localization.Languages() {
lvals := localization.GetItemTranslation(lang, p.UUID, comp)
for _, v := range lvals {
include(lang, v)
}
}
}
}

func (p *TemplateParams) MarshalJSON() ([]byte, error) {
if p == nil {
Expand Down
17 changes: 16 additions & 1 deletion flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@
"templates": [
"Hi Ryan Lewis, who's a good boy?",
"@contact.name",
"boy",
"@contact.name",
"boy"
],
"localizables": [
Expand Down Expand Up @@ -580,6 +582,10 @@
"@contact.name",
"boy",
"@contact.name",
"niño",
"@contact.name",
"boy",
"@contact.name",
"niño"
],
"localizables": [
Expand Down Expand Up @@ -935,7 +941,16 @@
"Yes",
"No",
"Si",
"No"
"No",
"@contact.name",
"boy",
"@contact.name",
"niño",
"Yeah",
"Sip",
"Nope",
"http://templates.com/red.jpg",
"http://templates.com/rojo.jpg"
],
"localizables": [
"Hey Ryan Lewis, your gender is saved as boy.",
Expand Down
26 changes: 17 additions & 9 deletions flows/inspect/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,26 @@ func Templates(s any, localization flows.Localization, include func(i18n.Languag
}

func templateValues(v reflect.Value, localization flows.Localization, include func(i18n.Language, string)) {
walk(v, nil, func(sv reflect.Value, fv reflect.Value, ef *EngineField) {
if ef.Evaluated {
extractTemplates(fv, i18n.NilLanguage, include)
walk(v,
func(v reflect.Value) {
te, ok := v.Interface().(flows.TemplateEnumerator)
if ok {
te.EnumerateTemplates(localization, include)
}
},
func(sv reflect.Value, fv reflect.Value, ef *EngineField) {
if ef.Evaluated {
extractTemplates(fv, i18n.NilLanguage, include)

// if this field is also localized, each translation is a template and needs to be included
if ef.Localized && localization != nil {
localizable := sv.Interface().(flows.Localizable)
// if this field is also localized, each translation is a template and needs to be included
if ef.Localized && localization != nil {
localizable := sv.Interface().(flows.Localizable)

Translations(localization, localizable.LocalizationUUID(), ef.JSONName, include)
Translations(localization, localizable.LocalizationUUID(), ef.JSONName, include)
}
}
}
})
},
)
}

func Translations(localization flows.Localization, itemUUID uuids.UUID, property string, include func(i18n.Language, string)) {
Expand Down
4 changes: 4 additions & 0 deletions flows/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ type Localizable interface {
LocalizationUUID() uuids.UUID
}

type TemplateEnumerator interface {
EnumerateTemplates(Localization, func(i18n.Language, string))
}

// Flow describes the ordered logic of actions and routers
type Flow interface {
Contextable
Expand Down
2 changes: 1 addition & 1 deletion utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Set[K constraints.Ordered](s []K) map[K]bool {
}

// SortedKeys returns the keys of a set in lexical order
func SortedKeys[K constraints.Ordered](m map[K]bool) []K {
func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K {
keys := maps.Keys(m)
slices.Sort(keys)
return keys
Expand Down

0 comments on commit fe2f3db

Please sign in to comment.