From 92981410d856b240a1dc8ba4122fb6902e9d107b Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Mon, 9 Dec 2024 19:51:07 +0000 Subject: [PATCH] Add patch flow migration to fix result references in expressions that need to be truncated --- flows/definition/flow.go | 2 +- flows/definition/migrations/13_x.go | 36 ++++++++ .../migrations/specdata/templates.json | 89 +++++++++++++++++++ .../testdata/migrations/13.6.1.json | 71 +++++++++++++++ ...ChangeLanguage_change_language_to_ara.snap | 2 +- ...ChangeLanguage_change_language_to_kin.snap | 2 +- ...ChangeLanguage_change_language_to_spa.snap | 2 +- .../definition/testdata/change_language.json | 2 +- 8 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 flows/definition/migrations/testdata/migrations/13.6.1.json diff --git a/flows/definition/flow.go b/flows/definition/flow.go index 06e6dd25f..48c1b20de 100644 --- a/flows/definition/flow.go +++ b/flows/definition/flow.go @@ -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 { diff --git a/flows/definition/migrations/13_x.go b/flows/definition/migrations/13_x.go index de76caded..f738de114 100644 --- a/flows/definition/migrations/13_x.go +++ b/flows/definition/migrations/13_x.go @@ -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) @@ -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" diff --git a/flows/definition/migrations/specdata/templates.json b/flows/definition/migrations/specdata/templates.json index 173dbacf2..9b17cc39c 100644 --- a/flows/definition/migrations/specdata/templates.json +++ b/flows/definition/migrations/specdata/templates.json @@ -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": [ diff --git a/flows/definition/migrations/testdata/migrations/13.6.1.json b/flows/definition/migrations/testdata/migrations/13.6.1.json new file mode 100644 index 000000000..2430127f1 --- /dev/null +++ b/flows/definition/migrations/testdata/migrations/13.6.1.json @@ -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" + } + ] + } + ] + } + } +] \ No newline at end of file diff --git a/flows/definition/testdata/TestChangeLanguage_change_language_to_ara.snap b/flows/definition/testdata/TestChangeLanguage_change_language_to_ara.snap index 27eae8a3a..c21bda0b1 100644 --- a/flows/definition/testdata/TestChangeLanguage_change_language_to_ara.snap +++ b/flows/definition/testdata/TestChangeLanguage_change_language_to_ara.snap @@ -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, diff --git a/flows/definition/testdata/TestChangeLanguage_change_language_to_kin.snap b/flows/definition/testdata/TestChangeLanguage_change_language_to_kin.snap index 212441359..9dfcb741c 100644 --- a/flows/definition/testdata/TestChangeLanguage_change_language_to_kin.snap +++ b/flows/definition/testdata/TestChangeLanguage_change_language_to_kin.snap @@ -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, diff --git a/flows/definition/testdata/TestChangeLanguage_change_language_to_spa.snap b/flows/definition/testdata/TestChangeLanguage_change_language_to_spa.snap index 8ef69b280..79269e705 100644 --- a/flows/definition/testdata/TestChangeLanguage_change_language_to_spa.snap +++ b/flows/definition/testdata/TestChangeLanguage_change_language_to_spa.snap @@ -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, diff --git a/flows/definition/testdata/change_language.json b/flows/definition/testdata/change_language.json index 8bf868db8..6be1a4194 100644 --- a/flows/definition/testdata/change_language.json +++ b/flows/definition/testdata/change_language.json @@ -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,