Skip to content

Commit

Permalink
Merge branch 'master' into contact_create
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jul 2, 2020
2 parents 5bfa51a + 540379b commit 97bdd00
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
13 changes: 11 additions & 2 deletions goflow/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ import (
"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, allowMissing bool) ([]flows.Modifier, error) {
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 && allowMissing {
if err == modifiers.ErrNoModifier && missing == IgnoreMissing {
continue
}
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions goflow/modifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestReadModifiers(t *testing.T) {
assert.NoError(t, err)

// can read empty list
mods, err := goflow.ReadModifiers(oa.SessionAssets(), []json.RawMessage{}, true)
mods, err := goflow.ReadModifiers(oa.SessionAssets(), []json.RawMessage{}, goflow.IgnoreMissing)
assert.NoError(t, err)
assert.Equal(t, 0, len(mods))

Expand All @@ -28,7 +28,7 @@ func TestReadModifiers(t *testing.T) {
[]byte(`{"type": "name", "name": "Bob"}`),
[]byte(`{"type": "field", "field": {"key": "gender", "name": "Gender"}, "value": "M"}`),
[]byte(`{"type": "language", "language": "spa"}`),
}, true)
}, goflow.IgnoreMissing)
assert.NoError(t, err)
assert.Equal(t, 3, len(mods))
assert.Equal(t, "name", mods[0].Type())
Expand All @@ -40,7 +40,7 @@ func TestReadModifiers(t *testing.T) {
[]byte(`{"type": "name", "name": "Bob"}`),
[]byte(`{"type": "field", "field": {"key": "blood_type", "name": "Blood Type"}, "value": "O"}`),
[]byte(`{"type": "language", "language": "spa"}`),
}, true)
}, goflow.IgnoreMissing)
assert.NoError(t, err)
assert.Equal(t, 2, len(mods))
assert.Equal(t, "name", mods[0].Type())
Expand All @@ -51,13 +51,13 @@ func TestReadModifiers(t *testing.T) {
[]byte(`{"type": "name", "name": "Bob"}`),
[]byte(`{"type": "field", "field": {"key": "blood_type", "name": "Blood Type"}, "value": "O"}`),
[]byte(`{"type": "language", "language": "spa"}`),
}, false)
}, 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"}`),
}, false)
}, goflow.ErrorOnMissing)
assert.EqualError(t, err, `error reading modifier: {"type": "field", "value": "O"}: field 'field' is required`)
}
2 changes: 1 addition & 1 deletion web/contact/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func handleModify(ctx context.Context, s *web.Server, r *http.Request) (interfac
}

// read the modifiers from the request
mods, err := goflow.ReadModifiers(org.SessionAssets(), request.Modifiers, false)
mods, err := goflow.ReadModifiers(oa.SessionAssets(), request.Modifiers, goflow.ErrorOnMissing)
if err != nil {
return nil, http.StatusBadRequest, err
}
Expand Down
8 changes: 1 addition & 7 deletions web/surveyor/surveyor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import (

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
<<<<<<< HEAD

"github.com/nyaruka/goflow/flows/actions/modifiers"

=======
>>>>>>> modifier_read_fix
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/engine"
"github.com/nyaruka/goflow/flows/events"
Expand Down Expand Up @@ -89,7 +83,7 @@ func handleSubmit(ctx context.Context, s *web.Server, r *http.Request) (interfac
}

// and our modifiers
mods, err := goflow.ReadModifiers(org.SessionAssets(), request.Modifiers, true)
mods, err := goflow.ReadModifiers(org.SessionAssets(), request.Modifiers, goflow.IgnoreMissing)
if err != nil {
return nil, http.StatusBadRequest, err
}
Expand Down

0 comments on commit 97bdd00

Please sign in to comment.