Skip to content

Commit

Permalink
Revert "Rework results so they're keyed by name rather than snakified…
Browse files Browse the repository at this point in the history
… name"

This reverts commit 9a19efe.
  • Loading branch information
rowanseymour committed Dec 10, 2024
1 parent 1414899 commit e79a2c0
Show file tree
Hide file tree
Showing 34 changed files with 124 additions and 123 deletions.
2 changes: 1 addition & 1 deletion flows/engine/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func TestWaitTimeout(t *testing.T) {
require.Equal(t, 2, len(run.Path()))
require.Equal(t, 5, len(run.Events()))

result := run.Results().Get("Favorite Color")
result := run.Results().Get("favorite_color")
require.Equal(t, "Timeout", result.Category)
require.Equal(t, "2018-04-11T13:24:30.123456Z", result.Value)
require.Equal(t, "", result.Input)
Expand Down
20 changes: 10 additions & 10 deletions flows/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,30 @@ func (r Results) Clone() Results {
return clone
}

// Save saves a new result in our map. Returns the old result if it existed.
// Save saves a new result in our map using the snakified name as the key. Returns the old result if it existed.
func (r Results) Save(result *Result) (*Result, bool) {
old := r[result.Name]
r[result.Name] = result
key := utils.Snakify(result.Name)
old := r[key]
r[key] = result

if old == nil || (old.Value != result.Value || old.Category != result.Category) {
return old, true
}
return nil, false
}

// Get returns the result with the given name
func (r Results) Get(name string) *Result {
return r[name]
// Get returns the result with the given key
func (r Results) Get(key string) *Result {
return r[key]
}

// Context returns the properties available in expressions
func (r Results) Context(env envs.Environment) map[string]types.XValue {
entries := make(map[string]types.XValue, len(r)+1)
entries["__default__"] = types.NewXText(r.format())

for name, v := range r {
entries[utils.Snakify(name)] = Context(env, v)
for k, v := range r {
entries[k] = Context(env, v)
}
return entries
}
Expand All @@ -163,7 +164,6 @@ func (r Results) format() string {
}

func (r *Results) UnmarshalJSON(data []byte) error {
// load map which may be keyed by snakified name or name
var m map[string]*Result
if err := json.Unmarshal(data, &m); err != nil {
return err
Expand All @@ -174,7 +174,7 @@ func (r *Results) UnmarshalJSON(data []byte) error {
// we enforce result names being at most 64 chars but old sessions may have longer names
for _, v := range m {
v.Name = strings.TrimSpace(stringsx.Truncate(v.Name, 64))
(*r)[v.Name] = v
(*r)[utils.Snakify(v.Name)] = v
}

return nil
Expand Down
16 changes: 8 additions & 8 deletions flows/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestResults(t *testing.T) {
results.Save(result1)
results.Save(result2)

assert.Equal(t, result1, results.Get("Beer"))
assert.Equal(t, result2, results.Get("Empty"))
assert.Equal(t, result1, results.Get("beer"))
assert.Equal(t, result2, results.Get("empty"))
assert.Nil(t, results.Get("xxx"))

resultsAsContext := flows.Context(env, results)
Expand Down Expand Up @@ -66,8 +66,8 @@ func TestResults(t *testing.T) {
marshaled, err := json.Marshal(results)
assert.NoError(t, err)
assert.JSONEq(t, `{
"Beer": {"category": "Skol", "created_on":"2019-04-05T14:16:30.000123456Z", "name": "Beer", "node_uuid": "26493ebb-a254-4461-a28d-c7761784e276", "value": "skol!"},
"Empty": {"created_on":"2019-04-05T14:16:30.000123456Z", "name": "Empty", "node_uuid": "26493ebb-a254-4461-a28d-c7761784e276", "value": ""}
"beer": {"category": "Skol", "created_on":"2019-04-05T14:16:30.000123456Z", "name": "Beer", "node_uuid": "26493ebb-a254-4461-a28d-c7761784e276", "value": "skol!"},
"empty": {"created_on":"2019-04-05T14:16:30.000123456Z", "name": "Empty", "node_uuid": "26493ebb-a254-4461-a28d-c7761784e276", "value": ""}
}`, string(marshaled))

var unmarshaled flows.Results
Expand All @@ -77,12 +77,12 @@ func TestResults(t *testing.T) {

// test unmarshalling with result names/keys that are too long
err = json.Unmarshal([]byte(`{
"Beer 123456789012345678901234567890123456789012345678901234567890": {"category": "Skol", "created_on":"2019-04-05T14:16:30.000123456Z", "name": "Beer 123456789012345678901234567890123456789012345678901234567890", "node_uuid": "26493ebb-a254-4461-a28d-c7761784e276", "value": "skol!"},
"Empty 123456789012345678901234567890123456789012345678901234567890": {"created_on":"2019-04-05T14:16:30.000123456Z", "name": "Empty 123456789012345678901234567890123456789012345678901234567890", "node_uuid": "26493ebb-a254-4461-a28d-c7761784e276", "value": ""}
"beer_123456789012345678901234567890123456789012345678901234567890": {"category": "Skol", "created_on":"2019-04-05T14:16:30.000123456Z", "name": "Beer 123456789012345678901234567890123456789012345678901234567890", "node_uuid": "26493ebb-a254-4461-a28d-c7761784e276", "value": "skol!"},
"empty_123456789012345678901234567890123456789012345678901234567890": {"created_on":"2019-04-05T14:16:30.000123456Z", "name": "Empty 123456789012345678901234567890123456789012345678901234567890", "node_uuid": "26493ebb-a254-4461-a28d-c7761784e276", "value": ""}
}`), &unmarshaled)
assert.NoError(t, err)
assert.Equal(t, "Skol", unmarshaled.Get("Beer 12345678901234567890123456789012345678901234567890123456789").Category)
assert.Equal(t, "", unmarshaled.Get("Empty 1234567890123456789012345678901234567890123456789012345678").Category)
assert.Equal(t, "Beer 12345678901234567890123456789012345678901234567890123456789", unmarshaled.Get("beer_12345678901234567890123456789012345678901234567890123456789").Name)
assert.Equal(t, "Empty 1234567890123456789012345678901234567890123456789012345678", unmarshaled.Get("empty_1234567890123456789012345678901234567890123456789012345678").Name)
}

func TestResultNameAndCategoryValidation(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion flows/routers/testdata/random.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
]
},
"results": {
"Random Result": {
"random_result": {
"name": "Random Result",
"value": "1",
"category": "No",
Expand Down
8 changes: 4 additions & 4 deletions flows/routers/testdata/switch.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
"default_category_uuid": "78ae8f05-f92e-43b2-a886-406eaea1b8e0"
},
"results": {
"Favorite Color": {
"favorite_color": {
"name": "Favorite Color",
"value": "YES",
"category": "Yes",
Expand Down Expand Up @@ -267,7 +267,7 @@
"default_category_uuid": "78ae8f05-f92e-43b2-a886-406eaea1b8e0"
},
"results": {
"Is Member": {
"is_member": {
"name": "Is Member",
"value": "[]",
"category": "Other",
Expand Down Expand Up @@ -356,7 +356,7 @@
"default_category_uuid": "78ae8f05-f92e-43b2-a886-406eaea1b8e0"
},
"results": {
"In Group": {
"in_group": {
"name": "In Group",
"value": "[]",
"category": "Other",
Expand Down Expand Up @@ -464,7 +464,7 @@
"default_category_uuid": "78ae8f05-f92e-43b2-a886-406eaea1b8e0"
},
"results": {
"Favorite Color": {
"favorite_color": {
"name": "Favorite Color",
"value": "YES",
"category": "Yes",
Expand Down
11 changes: 6 additions & 5 deletions flows/runs/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ func TestSaveResult(t *testing.T) {
assert.Nil(t, prev)
assert.True(t, changed)

assert.Equal(t, "red", run.Results().Get("Response 1").Value)
assert.Equal(t, "Red", run.Results().Get("Response 1").Category)
// name is snaked
assert.Equal(t, "red", run.Results().Get("response_1").Value)
assert.Equal(t, "Red", run.Results().Get("response_1").Category)
assert.Equal(t, time.Date(2020, 4, 20, 12, 39, 30, 123456789, time.UTC), run.ModifiedOn())

prev, changed = run.SaveResult(flows.NewResult("Response 1", "blue", "Blue", "Azul", "6d35528e-cae3-4e30-b842-8fe6ed7d5c02", "I like blue", nil, dates.Now()))
Expand All @@ -307,8 +308,8 @@ func TestSaveResult(t *testing.T) {
assert.True(t, changed)

// result is overwritten
assert.Equal(t, "blue", run.Results().Get("Response 1").Value)
assert.Equal(t, "Blue", run.Results().Get("Response 1").Category)
assert.Equal(t, "blue", run.Results().Get("response_1").Value)
assert.Equal(t, "Blue", run.Results().Get("response_1").Category)
assert.Equal(t, time.Date(2020, 4, 20, 12, 39, 30, 123456789, time.UTC), run.ModifiedOn())

// try saving new result with same value and category again
Expand All @@ -321,7 +322,7 @@ func TestSaveResult(t *testing.T) {
assert.NotNil(t, prev)
assert.True(t, changed)

assert.Equal(t, strings.Repeat("創", 640), run.Results().Get("Response 1").Value)
assert.Equal(t, strings.Repeat("創", 640), run.Results().Get("response_1").Value)
}

func TestTranslation(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/runner/airtime.test_successful_transfer.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@
}
],
"results": {
"Transfer": {
"transfer": {
"category": "Success",
"created_on": "2018-07-06T12:30:10.123456789Z",
"name": "Transfer",
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/runner/all_actions.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@
}
],
"results": {
"Gender": {
"gender": {
"category": "Male",
"created_on": "2018-07-06T12:30:39.123456789Z",
"name": "Gender",
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/runner/brochure.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@
}
],
"results": {
"Name": {
"name": {
"category": "Not Empty",
"created_on": "2018-07-06T12:30:12.123456789Z",
"input": "Ryan Lewis",
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/runner/date_parse.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@
}
],
"results": {
"Birth Date": {
"birth_date": {
"category": "Valid",
"created_on": "2018-07-06T12:30:11.123456789Z",
"input": "I was born on 1977.06.23 at 3:34 pm",
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/runner/default_result.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@
}
],
"results": {
"Contact Name": {
"contact_name": {
"category": "All Responses",
"created_on": "2018-07-06T12:30:12.123456789Z",
"input": "Ryan Lewis",
Expand Down
12 changes: 6 additions & 6 deletions test/testdata/runner/enter_flow_loop.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@
}
],
"results": {
"Command": {
"command": {
"category": "Name",
"created_on": "2018-07-06T12:30:13.123456789Z",
"input": "name",
Expand Down Expand Up @@ -744,7 +744,7 @@
}
],
"results": {
"Command": {
"command": {
"category": "Name",
"created_on": "2018-07-06T12:30:13.123456789Z",
"input": "name",
Expand Down Expand Up @@ -896,7 +896,7 @@
}
],
"results": {
"Command": {
"command": {
"category": "Name",
"created_on": "2018-07-06T12:30:49.123456789Z",
"input": "name",
Expand Down Expand Up @@ -1187,7 +1187,7 @@
}
],
"results": {
"Command": {
"command": {
"category": "Name",
"created_on": "2018-07-06T12:30:13.123456789Z",
"input": "name",
Expand Down Expand Up @@ -1339,7 +1339,7 @@
}
],
"results": {
"Command": {
"command": {
"category": "Name",
"created_on": "2018-07-06T12:30:49.123456789Z",
"input": "name",
Expand Down Expand Up @@ -1469,7 +1469,7 @@
}
],
"results": {
"Command": {
"command": {
"category": "Exit",
"created_on": "2018-07-06T12:31:27.123456789Z",
"input": "exit",
Expand Down
12 changes: 6 additions & 6 deletions test/testdata/runner/expirations.test_input_for_all.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
}
],
"results": {
"First Name": {
"first_name": {
"category": "All Responses",
"created_on": "2018-07-06T12:30:21.123456789Z",
"input": "Dwayne",
Expand Down Expand Up @@ -804,7 +804,7 @@
}
],
"results": {
"Middle Name": {
"middle_name": {
"category": "All Responses",
"created_on": "2018-07-06T12:30:41.123456789Z",
"input": "Douglas",
Expand Down Expand Up @@ -878,7 +878,7 @@
}
],
"results": {
"First Name": {
"first_name": {
"category": "All Responses",
"created_on": "2018-07-06T12:30:21.123456789Z",
"input": "Dwayne",
Expand Down Expand Up @@ -1078,7 +1078,7 @@
}
],
"results": {
"Last Name": {
"last_name": {
"category": "All Responses",
"created_on": "2018-07-06T12:31:01.123456789Z",
"input": "Johnson",
Expand Down Expand Up @@ -1175,7 +1175,7 @@
}
],
"results": {
"Middle Name": {
"middle_name": {
"category": "All Responses",
"created_on": "2018-07-06T12:30:41.123456789Z",
"input": "Douglas",
Expand Down Expand Up @@ -1249,7 +1249,7 @@
}
],
"results": {
"First Name": {
"first_name": {
"category": "All Responses",
"created_on": "2018-07-06T12:30:21.123456789Z",
"input": "Dwayne",
Expand Down
6 changes: 3 additions & 3 deletions test/testdata/runner/extra.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
}
],
"results": {
"Name Check": {
"name_check": {
"category": "Valid",
"created_on": "2018-07-06T12:30:08.123456789Z",
"extra": {
Expand Down Expand Up @@ -631,14 +631,14 @@
}
],
"results": {
"Continue": {
"continue": {
"created_on": "2018-07-06T12:30:36.123456789Z",
"input": "Ryan Lewis",
"name": "Continue",
"node_uuid": "11a772f3-3ca2-4429-8b33-20fdcfc2b69e",
"value": "Ryan Lewis"
},
"Name Check": {
"name_check": {
"category": "Valid",
"created_on": "2018-07-06T12:30:08.123456789Z",
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/runner/initial_wait.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
}
],
"results": {
"Command": {
"command": {
"category": "Ping",
"created_on": "2018-07-06T12:30:04.123456789Z",
"input": "PING",
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/runner/ivr_dial.busy.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
}
],
"results": {
"Redirect": {
"redirect": {
"category": "Busy",
"created_on": "2018-07-06T12:30:09.123456789Z",
"input": "busy",
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/runner/ivr_dial.invalid_phone.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
}
],
"results": {
"Redirect": {
"redirect": {
"category": "Failed",
"created_on": "2018-07-06T12:30:04.123456789Z",
"name": "Redirect",
Expand Down
Loading

0 comments on commit e79a2c0

Please sign in to comment.