forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Ilhasoft/merge/v5.6.0
merge/v5.6.0
- Loading branch information
Showing
103 changed files
with
3,093 additions
and
3,242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package goflow | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/nyaruka/goflow/assets" | ||
"github.com/nyaruka/goflow/flows" | ||
"github.com/nyaruka/goflow/flows/actions/modifiers" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// MissingAssets is the type for defining missing assets behavior | ||
type MissingAssets int | ||
|
||
// missing assets constants | ||
const ( | ||
IgnoreMissing MissingAssets = 0 | ||
ErrorOnMissing MissingAssets = 1 | ||
) | ||
|
||
// ReadModifiers reads modifiers from the given JSON | ||
func ReadModifiers(sa flows.SessionAssets, data []json.RawMessage, missing MissingAssets) ([]flows.Modifier, error) { | ||
mods := make([]flows.Modifier, 0, len(data)) | ||
for _, m := range data { | ||
mod, err := modifiers.ReadModifier(sa, m, assets.IgnoreMissing) | ||
|
||
// if this modifier turned into a no-op, ignore | ||
if err == modifiers.ErrNoModifier && missing == IgnoreMissing { | ||
continue | ||
} | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "error reading modifier: %s", string(m)) | ||
} | ||
mods = append(mods, mod) | ||
} | ||
return mods, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package goflow_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/nyaruka/mailroom/goflow" | ||
"github.com/nyaruka/mailroom/models" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadModifiers(t *testing.T) { | ||
ctx := testsuite.CTX() | ||
db := testsuite.DB() | ||
|
||
oa, err := models.GetOrgAssets(ctx, db, models.Org1) | ||
assert.NoError(t, err) | ||
|
||
// can read empty list | ||
mods, err := goflow.ReadModifiers(oa.SessionAssets(), []json.RawMessage{}, goflow.IgnoreMissing) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 0, len(mods)) | ||
|
||
// can read non-empty list | ||
mods, err = goflow.ReadModifiers(oa.SessionAssets(), []json.RawMessage{ | ||
[]byte(`{"type": "name", "name": "Bob"}`), | ||
[]byte(`{"type": "field", "field": {"key": "gender", "name": "Gender"}, "value": "M"}`), | ||
[]byte(`{"type": "language", "language": "spa"}`), | ||
}, goflow.IgnoreMissing) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 3, len(mods)) | ||
assert.Equal(t, "name", mods[0].Type()) | ||
assert.Equal(t, "field", mods[1].Type()) | ||
assert.Equal(t, "language", mods[2].Type()) | ||
|
||
// modifier with missing asset can be ignored | ||
mods, err = goflow.ReadModifiers(oa.SessionAssets(), []json.RawMessage{ | ||
[]byte(`{"type": "name", "name": "Bob"}`), | ||
[]byte(`{"type": "field", "field": {"key": "blood_type", "name": "Blood Type"}, "value": "O"}`), | ||
[]byte(`{"type": "language", "language": "spa"}`), | ||
}, goflow.IgnoreMissing) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 2, len(mods)) | ||
assert.Equal(t, "name", mods[0].Type()) | ||
assert.Equal(t, "language", mods[1].Type()) | ||
|
||
// modifier with missing asset or an error if allowMissing is false | ||
mods, err = goflow.ReadModifiers(oa.SessionAssets(), []json.RawMessage{ | ||
[]byte(`{"type": "name", "name": "Bob"}`), | ||
[]byte(`{"type": "field", "field": {"key": "blood_type", "name": "Blood Type"}, "value": "O"}`), | ||
[]byte(`{"type": "language", "language": "spa"}`), | ||
}, goflow.ErrorOnMissing) | ||
assert.EqualError(t, err, `error reading modifier: {"type": "field", "field": {"key": "blood_type", "name": "Blood Type"}, "value": "O"}: no modifier to return because of missing assets`) | ||
|
||
// error if any modifier structurally invalid | ||
mods, err = goflow.ReadModifiers(oa.SessionAssets(), []json.RawMessage{ | ||
[]byte(`{"type": "field", "value": "O"}`), | ||
[]byte(`{"type": "language", "language": "spa"}`), | ||
}, goflow.ErrorOnMissing) | ||
assert.EqualError(t, err, `error reading modifier: {"type": "field", "value": "O"}: field 'field' is required`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.