Carry a seat definition's form and a part's form
A definition and a part gain form, raised at every field their messages add or remove; absent reads 1 and below 1 is refused, so the generator can hold a changed definition to a raised form. Builds: design 47 H2 §780; H-A-352 (1)
This commit is contained in:
+25
-5
@@ -49,10 +49,13 @@ var Domains = []string{"Module", "Config", "Core", "Provisioning", "Identity and
|
|||||||
|
|
||||||
// Definition is one seat's contract.
|
// Definition is one seat's contract.
|
||||||
type Definition struct {
|
type Definition struct {
|
||||||
Format int `json:"format"`
|
Format int `json:"format"`
|
||||||
Seat string `json:"seat"`
|
// Form is the definition's form (design 47 H2 §780; H-A-352): raised at every field its values, events, accepts and
|
||||||
Scope string `json:"scope"`
|
// answers add or remove. Absent reads 1; below 1 is refused.
|
||||||
Grain string `json:"grain"`
|
Form int `json:"form,omitempty"`
|
||||||
|
Seat string `json:"seat"`
|
||||||
|
Scope string `json:"scope"`
|
||||||
|
Grain string `json:"grain"`
|
||||||
// Decision is the record that made it a seat.
|
// Decision is the record that made it a seat.
|
||||||
Decision string `json:"decision"`
|
Decision string `json:"decision"`
|
||||||
// Domain is the glossary domain the seat belongs to, by its heading (ADR 0636 §5): the indexer takes a seat of the
|
// Domain is the glossary domain the seat belongs to, by its heading (ADR 0636 §5): the indexer takes a seat of the
|
||||||
@@ -468,7 +471,9 @@ type Path struct {
|
|||||||
|
|
||||||
// Part is verbs, events and types several definitions include whole.
|
// Part is verbs, events and types several definitions include whole.
|
||||||
type Part struct {
|
type Part struct {
|
||||||
Format int `json:"format"`
|
Format int `json:"format"`
|
||||||
|
// Form is the part's own form, for what it adds (H-A-352): absent reads 1; below 1 is refused.
|
||||||
|
Form int `json:"form,omitempty"`
|
||||||
Part string `json:"part"`
|
Part string `json:"part"`
|
||||||
Description string `json:"description,omitempty"`
|
Description string `json:"description,omitempty"`
|
||||||
Verbs []Verb `json:"verbs,omitempty"`
|
Verbs []Verb `json:"verbs,omitempty"`
|
||||||
@@ -528,9 +533,21 @@ func Parse(raw []byte) (Definition, error) {
|
|||||||
if ps := d.check(); len(ps) > 0 {
|
if ps := d.check(); len(ps) > 0 {
|
||||||
return Definition{}, ps
|
return Definition{}, ps
|
||||||
}
|
}
|
||||||
|
d.Form = formRead(d.Form)
|
||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// formRead is a form as read: absent is 1 (H-A-352).
|
||||||
|
func formRead(n int) int { return max(n, 1) }
|
||||||
|
|
||||||
|
// formProblems refuses a form below 1; absent reads 1.
|
||||||
|
func formProblems(n int) Problems {
|
||||||
|
if n < 0 {
|
||||||
|
return Problems{{"form", fmt.Sprintf("form %d: a form is 1 or more, and absent reads 1", n)}}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ParsePart reads one part.
|
// ParsePart reads one part.
|
||||||
func ParsePart(raw []byte) (Part, error) {
|
func ParsePart(raw []byte) (Part, error) {
|
||||||
var p Part
|
var p Part
|
||||||
@@ -544,11 +561,13 @@ func ParsePart(raw []byte) (Part, error) {
|
|||||||
if !wordName.MatchString(p.Part) {
|
if !wordName.MatchString(p.Part) {
|
||||||
ps = append(ps, Problem{"part", "missing, or not a lower-case name"})
|
ps = append(ps, Problem{"part", "missing, or not a lower-case name"})
|
||||||
}
|
}
|
||||||
|
ps = append(ps, formProblems(p.Form)...)
|
||||||
ps = append(ps, checkVerbs(p.Verbs, true)...)
|
ps = append(ps, checkVerbs(p.Verbs, true)...)
|
||||||
ps = append(ps, checkMessages("events", p.Events, true)...)
|
ps = append(ps, checkMessages("events", p.Events, true)...)
|
||||||
if len(ps) > 0 {
|
if len(ps) > 0 {
|
||||||
return Part{}, ps
|
return Part{}, ps
|
||||||
}
|
}
|
||||||
|
p.Form = formRead(p.Form)
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -633,6 +652,7 @@ func (d Definition) check() Problems {
|
|||||||
if d.Format != Format {
|
if d.Format != Format {
|
||||||
ps = append(ps, Problem{"format", formatProblem(d.Format)})
|
ps = append(ps, Problem{"format", formatProblem(d.Format)})
|
||||||
}
|
}
|
||||||
|
ps = append(ps, formProblems(d.Form)...)
|
||||||
if d.Seat == "" {
|
if d.Seat == "" {
|
||||||
ps = append(ps, Problem{"seat", "missing"})
|
ps = append(ps, Problem{"seat", "missing"})
|
||||||
} else if !seatName.MatchString(d.Seat) {
|
} else if !seatName.MatchString(d.Seat) {
|
||||||
|
|||||||
@@ -173,6 +173,35 @@ func TestADefinitionIsReadStrictlyNamingWhatIsWrong(t *testing.T) {
|
|||||||
`"}`)); !errors.Is(err, ErrTooLarge) {
|
`"}`)); !errors.Is(err, ErrTooLarge) {
|
||||||
t.Errorf("over 256 KiB is ErrTooLarge, got %v", err)
|
t.Errorf("over 256 KiB is ErrTooLarge, got %v", err)
|
||||||
}
|
}
|
||||||
|
// Design 47 H2 §780, H-A-352: a definition and a part carry their form; absent reads 1, below 1 is refused.
|
||||||
|
for _, c := range []struct {
|
||||||
|
raw string
|
||||||
|
form int
|
||||||
|
bad bool
|
||||||
|
}{
|
||||||
|
{whole, 1, false},
|
||||||
|
{strings.Replace(whole, `"seat":`, `"form":3,"seat":`, 1), 3, false},
|
||||||
|
{strings.Replace(whole, `"seat":`, `"form":-1,"seat":`, 1), 0, true},
|
||||||
|
} {
|
||||||
|
d, err := Parse([]byte(c.raw))
|
||||||
|
if c.bad != (err != nil) || (!c.bad && d.Form != c.form) || (c.bad && !strings.Contains(err.Error(), "form")) {
|
||||||
|
t.Errorf("a definition of form %d read as %d, %v", c.form, d.Form, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, c := range []struct {
|
||||||
|
raw string
|
||||||
|
form int
|
||||||
|
bad bool
|
||||||
|
}{
|
||||||
|
{`{"format":1,"part":"registry"}`, 1, false},
|
||||||
|
{`{"format":1,"form":2,"part":"registry"}`, 2, false},
|
||||||
|
{`{"format":1,"form":-2,"part":"registry"}`, 0, true},
|
||||||
|
} {
|
||||||
|
p, err := ParsePart([]byte(c.raw))
|
||||||
|
if c.bad != (err != nil) || (!c.bad && p.Form != c.form) {
|
||||||
|
t.Errorf("a part %s read as form %d, %v", c.raw, p.Form, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// A part is taken in whole, its verbs before the definition's own; a part not given, or a type defined twice,
|
// A part is taken in whole, its verbs before the definition's own; a part not given, or a type defined twice,
|
||||||
// is refused.
|
// is refused.
|
||||||
|
|||||||
Reference in New Issue
Block a user