Skip to content

Commit

Permalink
Add patch flow migration to fix result references in expressions that…
Browse files Browse the repository at this point in the history
… need to be truncated
  • Loading branch information
rowanseymour committed Dec 9, 2024
1 parent 6fe3809 commit 9298141
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 5 deletions.
2 changes: 1 addition & 1 deletion flows/definition/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// CurrentSpecVersion is the flow spec version supported by this library
var CurrentSpecVersion = semver.MustParse("13.6.0")
var CurrentSpecVersion = semver.MustParse("13.6.1")

// IsVersionSupported checks the given version is supported
func IsVersionSupported(v *semver.Version) bool {
Expand Down
36 changes: 36 additions & 0 deletions flows/definition/migrations/13_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/stringsx"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/excellent"
"github.com/nyaruka/goflow/excellent/refactor"
)

func init() {
registerMigration(semver.MustParse("13.6.1"), Migrate13_6_1)
registerMigration(semver.MustParse("13.6.0"), Migrate13_6)
registerMigration(semver.MustParse("13.5.0"), Migrate13_5)
registerMigration(semver.MustParse("13.4.0"), Migrate13_4)
Expand All @@ -19,6 +21,40 @@ func init() {
registerMigration(semver.MustParse("13.1.0"), Migrate13_1)
}

// Migrate13_6_1 fixes result lookups that need to be truncated.
//
// @version 13_6_1 "13.6.1"
func Migrate13_6_1(f Flow, cfg *Config) (Flow, error) {
const maxResultRef = 64

RewriteTemplates(f, GetTemplateCatalog(semver.MustParse("13.6.0")), func(s string) string {
// refactor any @result.* or @(...) template to find result lookups that need to be truncated
refactored, _ := refactor.Template(s, []string{"results"}, func(exp excellent.Expression) bool {
changed := false

exp.Visit(func(e excellent.Expression) {
switch typed := e.(type) {
case *excellent.DotLookup:
if asRef, isRef := typed.Container.(*excellent.ContextReference); isRef {
if asRef.Name == "results" {
old := typed.Lookup
typed.Lookup = stringsx.Truncate(old, maxResultRef)
if typed.Lookup != old {
changed = true
}
}
}
}
})

return changed
})

return refactored
})
return f, nil
}

// Migrate13_6 ensures that names of results and categories respect definition limits.
//
// @version 13_6 "13.6"
Expand Down
89 changes: 89 additions & 0 deletions flows/definition/migrations/specdata/templates.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,93 @@
{
"13.6.1": {
"actions": {
"add_contact_groups": [
".groups[*].name_match"
],
"add_contact_urn": [
".path"
],
"add_input_labels": [
".labels[*].name_match"
],
"call_classifier": [
".input"
],
"call_resthook": [],
"call_webhook": [
".body",
".headers.*",
".url"
],
"enter_flow": [],
"open_ticket": [
".assignee.email_match",
".body"
],
"play_audio": [
".audio_url"
],
"remove_contact_groups": [
".groups[*].name_match"
],
"request_optin": [],
"say_msg": [
".text"
],
"send_broadcast": [
".attachments[*]",
".contact_query",
".groups[*].name_match",
".legacy_vars[*]",
".quick_replies[*]",
".text"
],
"send_email": [
".addresses[*]",
".body",
".subject"
],
"send_msg": [
".attachments[*]",
".quick_replies[*]",
".template_variables[*]",
".text"
],
"set_contact_channel": [],
"set_contact_field": [
".value"
],
"set_contact_language": [
".language"
],
"set_contact_name": [
".name"
],
"set_contact_status": [],
"set_contact_timezone": [
".timezone"
],
"set_run_result": [
".value"
],
"start_session": [
".contact_query",
".groups[*].name_match",
".legacy_vars[*]"
],
"transfer_airtime": []
},
"routers": {
"random": [
".operand",
".cases[*].arguments[*]"
],
"switch": [
".operand",
".cases[*].arguments[*]"
]
}
},
"13.6.0": {
"actions": {
"add_contact_groups": [
Expand Down
71 changes: 71 additions & 0 deletions flows/definition/migrations/testdata/migrations/13.6.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[
{
"description": "flow with localization",
"original": {
"uuid": "25a2d8b2-ae7c-4fed-964a-506fb8c3f0c0",
"name": "Test Flow",
"spec_version": "13.6.0",
"language": "eng",
"type": "messaging",
"localization": {
"spa": {
"9d9290a7-3713-4c22-8821-4af0a64c0821": {
"text": [
"Hola @results.this_single_variable_is_too_long_and_needs_to_be_truncated_to_match_the_flow @(if(results.this_in_a_complex_expression_is_also_too_long_and_needs_to_be_truncated.value))"
]
}
}
},
"nodes": [
{
"uuid": "32bc60ad-5c86-465e-a6b8-049c44ecce49",
"actions": [
{
"uuid": "9d9290a7-3713-4c22-8821-4af0a64c0821",
"type": "send_msg",
"text": "Hi @results.this_single_variable_is_too_long_and_needs_to_be_truncated_to_match_the_flow @(if(results.this_in_a_complex_expression_is_also_too_long_and_needs_to_be_truncated.value))"
}
],
"exits": [
{
"uuid": "2d481ce6-efcf-4898-a825-f76208e32f2a"
}
]
}
]
},
"migrated": {
"uuid": "25a2d8b2-ae7c-4fed-964a-506fb8c3f0c0",
"name": "Test Flow",
"spec_version": "13.6.1",
"language": "eng",
"type": "messaging",
"localization": {
"spa": {
"9d9290a7-3713-4c22-8821-4af0a64c0821": {
"text": [
"Hola @results.this_single_variable_is_too_long_and_needs_to_be_truncated_to_ma @(if(results.this_in_a_complex_expression_is_also_too_long_and_needs_to_be_tr.value))"
]
}
}
},
"nodes": [
{
"uuid": "32bc60ad-5c86-465e-a6b8-049c44ecce49",
"actions": [
{
"uuid": "9d9290a7-3713-4c22-8821-4af0a64c0821",
"type": "send_msg",
"text": "Hi @results.this_single_variable_is_too_long_and_needs_to_be_truncated_to_ma @(if(results.this_in_a_complex_expression_is_also_too_long_and_needs_to_be_tr.value))"
}
],
"exits": [
{
"uuid": "2d481ce6-efcf-4898-a825-f76208e32f2a"
}
]
}
]
}
}
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"uuid": "19cad1f2-9110-4271-98d4-1b968bf19410",
"name": "Change Language",
"spec_version": "13.6.0",
"spec_version": "13.6.1",
"language": "ara",
"type": "messaging",
"revision": 16,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"uuid": "19cad1f2-9110-4271-98d4-1b968bf19410",
"name": "Change Language",
"spec_version": "13.6.0",
"spec_version": "13.6.1",
"language": "kin",
"type": "messaging",
"revision": 16,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"uuid": "19cad1f2-9110-4271-98d4-1b968bf19410",
"name": "Change Language",
"spec_version": "13.6.0",
"spec_version": "13.6.1",
"language": "spa",
"type": "messaging",
"revision": 16,
Expand Down
2 changes: 1 addition & 1 deletion flows/definition/testdata/change_language.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"uuid": "19cad1f2-9110-4271-98d4-1b968bf19410",
"name": "Change Language",
"spec_version": "13.6.0",
"spec_version": "13.6.1",
"language": "eng",
"type": "messaging",
"revision": 16,
Expand Down

0 comments on commit 9298141

Please sign in to comment.