DC2 step-runner-delivery-forms: the readers of mover-added and place-unfinished refuse a body naming none or two of from, caller and take-in, or both key and all
Builds: design 47 H2 H-A-645, H-A-644 (AC-DC2c, AC-DC2d; review round 1)
This commit is contained in:
@@ -183,4 +183,20 @@ 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user