diff --git a/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient/client.go b/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient/client.go index e1702ce10..b33015629 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient/client.go +++ b/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient/client.go @@ -105,3 +105,35 @@ func StepEnded(data []byte) (seat.StepEndedEvent, error) { } return e, nil } + +// MoverAdded decodes a mover-added body, refusing one that names none or more than one of from, caller and take-in +// (design 47 H2 H-A-594 (4), H-A-609 (3), H-A-645). +func MoverAdded(data []byte) (seat.MoverAddedEvent, error) { + var e seat.MoverAddedEvent + if err := json.Unmarshal(data, &e); err != nil { + return e, fmt.Errorf("meshpipelinerunnerclient: mover-added: %w", err) + } + named := 0 + for _, set := range []bool{e.From != "", e.Caller != "", e.TakeIn != 0} { + if set { + named++ + } + } + if named != 1 { + return e, fmt.Errorf("meshpipelinerunnerclient: mover-added names exactly one of from, caller and take-in, not %d", named) + } + return e, nil +} + +// PlaceUnfinished decodes the input of place-unfinished, refusing one that names none or both of key and all +// (design 47 H2 H-A-644). +func PlaceUnfinished(data []byte) (seat.PlaceUnfinishedInput, error) { + var in seat.PlaceUnfinishedInput + if err := json.Unmarshal(data, &in); err != nil { + return in, fmt.Errorf("meshpipelinerunnerclient: place-unfinished: %w", err) + } + if in.All == (in.Key != "") { + return in, fmt.Errorf("meshpipelinerunnerclient: place-unfinished names exactly one of key and all") + } + return in, nil +} diff --git a/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient/client_test.go b/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient/client_test.go index f76ef5116..84f0cd243 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient/client_test.go +++ b/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient/client_test.go @@ -51,4 +51,23 @@ func TestSubjectsAndBodies(t *testing.T) { if e, err := StepEnded([]byte(`{"v":1,"pipeline":"main.3589","module":"nats","branch":"main","commit":"c1","result":{"step":"validate","status":"required","outcome":"passed"}}`)); err != nil || e.Result.Step != "validate" { t.Errorf("step-ended: %+v, %v", e, err) } + // design 47 H2 H-A-645, H-A-644: a mover-added names exactly one of from, caller and take-in; place-unfinished exactly one of key and all. + base := `"fingerprint":"fp","module":"m","pipeline":"main.1","revision":1` + for _, c := range []struct { + given string + ok bool + }{{`{` + base + `,"from":"main.0"}`, true}, {`{` + base + `,"caller":"x"}`, true}, {`{` + base + `,"take-in":2}`, true}, + {`{` + base + `}`, false}, {`{` + base + `,"from":"main.0","take-in":2}`, false}, {`{` + base + `,"caller":"x","from":"main.0","take-in":2}`, false}} { + if _, err := MoverAdded([]byte(c.given)); (err == nil) != c.ok { + t.Errorf("mover-added %s: %v, want ok %v", c.given, err, c.ok) + } + } + for _, c := range []struct { + given string + ok bool + }{{`{"key":"main.m","why":"x"}`, true}, {`{"all":true,"why":"x"}`, true}, {`{"why":"x"}`, false}, {`{"all":true,"key":"main.m","why":"x"}`, false}} { + if _, err := PlaceUnfinished([]byte(c.given)); (err == nil) != c.ok { + t.Errorf("place-unfinished %s: %v, want ok %v", c.given, err, c.ok) + } + } } diff --git a/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner/contract_test.go b/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner/contract_test.go index 993780cf8..25aef886c 100644 --- a/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner/contract_test.go +++ b/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner/contract_test.go @@ -183,20 +183,4 @@ func TestFixtures(t *testing.T) { t.Errorf("%s: subject %q is not the contract's %s", c.name, c.subject, c.subjectIs) } } - - // readers refuse a body naming none or two of the exclusive fields (H-A-645, H-A-644) - for _, r := range []struct { - name, given string - decode func([]byte) error - }{ - {"a mover added naming none of from, caller and take-in", `{"fingerprint":"fp","module":"m","pipeline":"main.1","revision":1}`, func(b []byte) error { _, _, err := DecodeMoverAddedEvent(b); return err }}, - {"a mover added naming from and take-in", `{"fingerprint":"fp","from":"main.0","module":"m","pipeline":"main.1","revision":1,"take-in":2}`, func(b []byte) error { _, _, err := DecodeMoverAddedEvent(b); return err }}, - {"a mover added naming caller and from", `{"caller":"x","fingerprint":"fp","from":"main.0","module":"m","pipeline":"main.1","revision":1}`, func(b []byte) error { _, _, err := DecodeMoverAddedEvent(b); return err }}, - {"a mover added naming all three", `{"caller":"x","fingerprint":"fp","from":"main.0","module":"m","pipeline":"main.1","revision":1,"take-in":2}`, func(b []byte) error { var v MoverAdded; return json.Unmarshal(b, &v) }}, - {"place-unfinished naming key and all", `{"all":true,"key":"main.m","why":"x"}`, func(b []byte) error { var v PlaceUnfinishedInput; return json.Unmarshal(b, &v) }}, - } { - if err := r.decode([]byte(r.given)); err == nil { - t.Errorf("%s: read, want refused", r.name) - } - } } diff --git a/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner/readers.go b/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner/readers.go deleted file mode 100644 index da5825901..000000000 --- a/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner/readers.go +++ /dev/null @@ -1,42 +0,0 @@ -package meshpipelinerunner - -import ( - "encoding/json" - "errors" -) - -// UnmarshalJSON reads a mover-added body and refuses one naming none or two of from, caller and take-in -// (design 47 H2 H-A-594 (4), H-A-609 (3), H-A-645). -func (m *MoverAdded) UnmarshalJSON(raw []byte) error { - type plain MoverAdded - var v plain - if err := json.Unmarshal(raw, &v); err != nil { - return err - } - named := 0 - for _, set := range []bool{v.From != "", v.Caller != "", v.TakeIn != 0} { - if set { - named++ - } - } - if named != 1 { - return errors.New("mover-added names exactly one of from, caller and take-in") - } - *m = MoverAdded(v) - return nil -} - -// UnmarshalJSON reads a place-unfinished input and refuses one naming both key and all -// (design 47 H2 H-A-644). -func (p *PlaceUnfinishedInput) UnmarshalJSON(raw []byte) error { - type plain PlaceUnfinishedInput - var v plain - if err := json.Unmarshal(raw, &v); err != nil { - return err - } - if v.All && v.Key != "" { - return errors.New("place-unfinished names exactly one of key and all") - } - *p = PlaceUnfinishedInput(v) - return nil -}