S4 decoder-sets-aside: an unknown word of a long-form invokes, requires or consumes entry is set aside and named
A consumes entry is a name or {event, ...}, read as its event, every other key set aside.
Builds: design 47 H2b §871; H-A-850
This commit is contained in:
@@ -5,17 +5,25 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 1279/AC4 (ADR 0443 §4): no module is given a way to ask where its consumer starts. A consumes entry is an event's
|
||||
// name; one that carries a key asking for the stream's history is refused by name, so the safe start is the only one.
|
||||
func TestAConsumesEntryAskingWhereItStartsIsRefused(t *testing.T) { // 1279/AC4
|
||||
// 1279/AC4 (ADR 0443 §4), design 47 H2b §871 (H-A-850): no module is given a way to ask where its consumer starts. A consumes entry is an
|
||||
// event's name or {event, …}; a key beside the event (from, start, deliver) is set aside and named with its path, the entry read as
|
||||
// its event alone, so a registration still refuses the manifest by that unknown word; an object with no event is refused by name.
|
||||
func TestAConsumesEntryAskingWhereItStartsIsRefused(t *testing.T) { // 1279/AC4, 1984/AC-871
|
||||
for _, key := range []string{"from", "start", "deliver"} {
|
||||
raw := `{"module":"audit","version":"1","consumes":[{"event":"shop.order.placed","` + key + `":"beginning"}]}`
|
||||
_, err := decodeForTest([]byte(raw))
|
||||
if err == nil {
|
||||
t.Fatalf("a consumes entry with %q was accepted", key)
|
||||
raw := `{"module":"audit","version":"1","consumes":["a.b",{"event":"shop.order.placed","` + key + `":"beginning"}]}`
|
||||
m, err := decodeForTest([]byte(raw))
|
||||
if err != nil {
|
||||
t.Fatalf("a consumes entry with %q was refused: %v", key, err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), `"`+key+`"`) || !strings.Contains(err.Error(), "consumes") {
|
||||
t.Errorf("the refusal for %q does not name the key and the field: %v", key, err)
|
||||
if strings.Join(m.Consumes, " ") != "a.b shop.order.placed" || !strings.Contains(m.SetAside(), "consumes[1]."+key) {
|
||||
t.Errorf("%q: consumes %v, set aside %q", key, m.Consumes, m.SetAside())
|
||||
}
|
||||
if !strings.Contains(m.UnknownField(), `"`+key+`"`) {
|
||||
t.Errorf("%q is not named unknown, which is what a registration refuses by: %q", key, m.UnknownField())
|
||||
}
|
||||
_, err = decodeForTest([]byte(`{"module":"audit","version":"1","consumes":[{"` + key + `":"beginning"}]}`))
|
||||
if err == nil || !strings.Contains(err.Error(), `"`+key+`"`) || !strings.Contains(err.Error(), "consumes") {
|
||||
t.Errorf("the refusal of an entry with %q and no event does not name the key and the field: %v", key, err)
|
||||
}
|
||||
}
|
||||
if _, err := decodeForTest([]byte(`{"module":"audit","version":"1","consumes":["shop.order.placed"]}`)); err != nil {
|
||||
|
||||
+62
-30
@@ -1612,10 +1612,11 @@ func (m *Manifest) UnmarshalJSON(raw []byte) error {
|
||||
// Beside `settings` it is refused by name (MixedFormProblems), never merged into it.
|
||||
delete(keys, ConfigField)
|
||||
}
|
||||
if err := consumesEntryProblem(keys["consumes"]); err != nil {
|
||||
consumesAside, err := readConsumes(keys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
invokeFields, err := readInvokes(keys)
|
||||
invokeFields, invokesAside, err := readInvokes(keys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1705,6 +1706,8 @@ func (m *Manifest) UnmarshalJSON(raw []byte) error {
|
||||
requireShares = read.shares
|
||||
busState, busStateAside = read.busState, read.aside
|
||||
}
|
||||
busStateAside = append(busStateAside, consumesAside...)
|
||||
busStateAside = append(busStateAside, invokesAside...)
|
||||
// The stored form's shares (RequireSharesKey).
|
||||
if stored, ok := keys[RequireSharesKey]; ok {
|
||||
if string(stored) != "null" {
|
||||
@@ -1801,35 +1804,37 @@ type invokeLongForm struct {
|
||||
|
||||
// readInvokes reads `invokes` whose entries are verbs or, in their long form, {verb, fields}: it puts the verbs back in
|
||||
// keys as plain names and answers the fields each long-form entry names.
|
||||
func readInvokes(keys map[string]json.RawMessage) (map[string][]string, error) {
|
||||
// An unknown key of a long-form entry is set aside and said as the path `invokes[i].key` (design 47 H2b §871).
|
||||
func readInvokes(keys map[string]json.RawMessage) (map[string][]string, []string, error) {
|
||||
raw, ok := keys["invokes"]
|
||||
if !ok || string(raw) == "null" {
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
}
|
||||
var entries []json.RawMessage
|
||||
if err := json.Unmarshal(raw, &entries); err != nil {
|
||||
return nil, nil // not a list: the field's own decoder says so
|
||||
return nil, nil, nil // not a list: the field's own decoder says so
|
||||
}
|
||||
var verbs []string
|
||||
var verbs, aside []string
|
||||
var fields map[string][]string
|
||||
for i, e := range entries {
|
||||
trimmed := bytes.TrimSpace(e)
|
||||
if len(trimmed) == 0 || trimmed[0] != '{' {
|
||||
var verb string
|
||||
if err := json.Unmarshal(trimmed, &verb); err != nil {
|
||||
return nil, fmt.Errorf("invokes[%d]: a verb, or {verb, fields}: %w", i, err)
|
||||
return nil, nil, fmt.Errorf("invokes[%d]: a verb, or {verb, fields}: %w", i, err)
|
||||
}
|
||||
verbs = append(verbs, verb)
|
||||
continue
|
||||
}
|
||||
var long invokeLongForm
|
||||
dec := json.NewDecoder(bytes.NewReader(trimmed))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&long); err != nil {
|
||||
return nil, fmt.Errorf("invokes[%d]: a verb, or {verb, fields}: %w", i, err)
|
||||
long, unknown, err := readAside[invokeLongForm](trimmed)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("invokes[%d]: a verb, or {verb, fields}: %w", i, err)
|
||||
}
|
||||
for _, path := range unknown {
|
||||
aside = append(aside, fmt.Sprintf("invokes[%d]%s", i, path))
|
||||
}
|
||||
if long.Verb == "" {
|
||||
return nil, fmt.Errorf("invokes[%d]: a long-form entry names its verb", i)
|
||||
return nil, nil, fmt.Errorf("invokes[%d]: a long-form entry names its verb", i)
|
||||
}
|
||||
verbs = append(verbs, long.Verb)
|
||||
if len(long.Fields) > 0 {
|
||||
@@ -1841,10 +1846,10 @@ func readInvokes(keys map[string]json.RawMessage) (map[string][]string, error) {
|
||||
}
|
||||
plain, err := json.Marshal(verbs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
keys["invokes"] = plain
|
||||
return fields, nil
|
||||
return fields, aside, nil
|
||||
}
|
||||
|
||||
// checkSharesNotContributed refuses a module whose own `contributes` to s3-bucket names `shares` (ADR 0468 §8).
|
||||
@@ -1859,30 +1864,57 @@ func (m Manifest) checkSharesNotContributed() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// consumesEntryProblem refuses a consumes entry that is more than an event's name. **No manifest word asks where a
|
||||
// consumer starts** (novox/hq ADR 0443 §4): the controller makes every module's consumer from now, and the first module
|
||||
// that needs a stream's history brings a decision that adds the word. An entry carrying a key such as from, start or
|
||||
// deliver would otherwise be read as a bare "cannot unmarshal object", which says nothing of why.
|
||||
func consumesEntryProblem(raw json.RawMessage) error {
|
||||
// consumesLongForm is an entry of `consumes` in its long form: the event it consumes (design 47 H2b §871, H-A-850).
|
||||
type consumesLongForm struct {
|
||||
Event string `json:"event"`
|
||||
}
|
||||
|
||||
// readConsumes reads `consumes` whose entries are an event's name or, in the long form, {event, …}: it puts the names back in keys
|
||||
// as plain names, and answers the paths of the words it set aside in a long-form entry (`consumes[i].key`). **No manifest word asks
|
||||
// where a consumer starts** (novox/hq ADR 0443 §4): the controller makes every module's consumer from now, so an object with no
|
||||
// `event` (a key such as from, start or deliver alone) is refused, with the reason, never read as a bare "cannot unmarshal object".
|
||||
func readConsumes(keys map[string]json.RawMessage) ([]string, error) {
|
||||
raw := keys["consumes"]
|
||||
var entries []json.RawMessage
|
||||
if len(raw) == 0 || json.Unmarshal(raw, &entries) != nil {
|
||||
return nil // not an array: the ordinary decoding says so
|
||||
return nil, nil // not an array: the ordinary decoding says so
|
||||
}
|
||||
var aside []string
|
||||
plain := make([]any, 0, len(entries))
|
||||
long := false
|
||||
for i, e := range entries {
|
||||
var obj map[string]json.RawMessage
|
||||
if json.Unmarshal(e, &obj) != nil {
|
||||
var v any
|
||||
_ = json.Unmarshal(e, &v)
|
||||
plain = append(plain, v)
|
||||
continue
|
||||
}
|
||||
keys := make([]string, 0, len(obj))
|
||||
for k := range obj {
|
||||
keys = append(keys, strconv.Quote(k))
|
||||
read, unknown, err := readAside[consumesLongForm](e)
|
||||
if err != nil || read.Event == "" {
|
||||
names := make([]string, 0, len(obj))
|
||||
for k := range obj {
|
||||
names = append(names, strconv.Quote(k))
|
||||
}
|
||||
sort.Strings(names)
|
||||
return nil, fmt.Errorf("consumes entry %d is the long form {event, …} or an event's name, and carries the key %s with no event: "+
|
||||
"no word asks where a consumer starts, because the controller makes every module's consumer from now (novox/hq ADR 0443 §4)",
|
||||
i, strings.Join(names, ", "))
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return fmt.Errorf("consumes entry %d carries the key %s: an entry is an event's name alone, and no word asks where a "+
|
||||
"consumer starts, because the controller makes every module's consumer from now (novox/hq ADR 0443 §4)",
|
||||
i, strings.Join(keys, ", "))
|
||||
for _, path := range unknown {
|
||||
aside = append(aside, fmt.Sprintf("consumes[%d]%s", i, path))
|
||||
}
|
||||
plain = append(plain, read.Event)
|
||||
long = true
|
||||
}
|
||||
return nil
|
||||
if long {
|
||||
b, err := json.Marshal(plain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys["consumes"] = b
|
||||
}
|
||||
return aside, nil
|
||||
}
|
||||
|
||||
// MarshalJSON writes `secrets` and `contributes` back in the shape they were read: single values,
|
||||
@@ -2304,7 +2336,7 @@ const WhileStopped = "while-stopped"
|
||||
// one reader of the field for a part that holds no Manifest. An entry of neither form is an error, never none.
|
||||
func InvokedVerbs(raw json.RawMessage) ([]string, error) {
|
||||
keys := map[string]json.RawMessage{"invokes": raw}
|
||||
if _, err := readInvokes(keys); err != nil {
|
||||
if _, _, err := readInvokes(keys); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var verbs []string
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package catalogue
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
@@ -62,12 +61,13 @@ func readRequires(raw json.RawMessage) (requiresRead, error) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
var full requirementForm
|
||||
dec := json.NewDecoder(bytes.NewReader(e))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&full); err != nil {
|
||||
full, unknown, err := readAside[requirementForm](e)
|
||||
if err != nil {
|
||||
return requiresRead{}, fmt.Errorf("requires[%d]: a name, or {name, optional} (novox/hq ADR 0330): %w", i, err)
|
||||
}
|
||||
for _, path := range unknown {
|
||||
aside = append(aside, fmt.Sprintf("requires[%d]%s", i, path))
|
||||
}
|
||||
if strings.TrimSpace(full.Name) == "" {
|
||||
return requiresRead{}, fmt.Errorf("requires[%d] names no requirement: {name, optional}", i)
|
||||
}
|
||||
|
||||
@@ -29,14 +29,13 @@ func TestARequirementsLongForm(t *testing.T) {
|
||||
t.Fatalf("written and read back, it says %v optional %v", again.Requires, again.OptionalRequires)
|
||||
}
|
||||
|
||||
// The long form {name, optional} refuses what it does not know.
|
||||
for _, raw := range []string{
|
||||
`{"module":"x","version":"1","requires":[{"name":"influxdb-api","optinal":true}]}`,
|
||||
`{"module":"x","version":"1","requires":[{"optional":true}]}`,
|
||||
} {
|
||||
if _, err := decodeForTest([]byte(raw)); err == nil {
|
||||
t.Errorf("accepted %s", raw)
|
||||
}
|
||||
// The long form {name, optional} sets aside a word it does not know and names it (design 47 H2b §871), and refuses an entry with no name.
|
||||
if m, err := decodeForTest([]byte(`{"module":"x","version":"1","requires":[{"name":"influxdb-api","optinal":true}]}`)); err != nil ||
|
||||
!strings.Contains(m.SetAside(), "requires[0].optinal") || strings.Join(m.Requires, " ") != "influxdb-api" || m.IsOptional("influxdb-api") {
|
||||
t.Errorf("an unknown word of a requires entry: %v, set aside %q, %v", err, m.SetAside(), m.Requires)
|
||||
}
|
||||
if _, err := decodeForTest([]byte(`{"module":"x","version":"1","requires":[{"optional":true}]}`)); err == nil {
|
||||
t.Error("a requires entry with no name was accepted")
|
||||
}
|
||||
|
||||
records := BusStateEntry{Name: "records", Storage: "ledger", Template: "ledger",
|
||||
|
||||
@@ -35,8 +35,8 @@ func TestTheReaderReleaseReadsTheManifestWordsGWrites(t *testing.T) {
|
||||
// manifest read whole, or refused naming what is wrong, and what the reader gives of it.
|
||||
func TestTheSecondReaderReleaseReadsGsManifestWords(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name, raw, refused string
|
||||
check func(Manifest) bool
|
||||
name, raw, refused, asideOf string
|
||||
check func(Manifest) bool
|
||||
}{
|
||||
{name: "a state's cap (§469)", raw: `{"module":"m","version":"1.0","state":["plain",{"name":"pipelines","max-mib":256}]}`,
|
||||
check: func(m Manifest) bool { return m.State[0].MaxMiB == 0 && m.State[1].MaxMiB == 256 }},
|
||||
@@ -58,9 +58,15 @@ func TestTheSecondReaderReleaseReadsGsManifestWords(t *testing.T) {
|
||||
len(m.InvokeFields) == 1 && slices.Equal(m.InvokeFields["seat:mesh-catalog.revision"], []string{"none-yet"}) &&
|
||||
strings.Contains(string(out), `"invokes":["*",{"verb":"seat:mesh-catalog.revision","fields":["none-yet"]},"seat:mesh-catalog.entry"]`)
|
||||
}},
|
||||
{name: "a long-form invoke with a word it does not know (§617)",
|
||||
raw: `{"module":"m","version":"1.0","invokes":[{"verb":"seat:mesh-catalog.revision","field":["none-yet"]}]}`,
|
||||
refused: `invokes[0]: a verb, or {verb, fields}`},
|
||||
{name: "a long-form invoke with a word it does not know is read without it and names it (§617, design 47 H2b §871)",
|
||||
raw: `{"module":"m","version":"1.0","invokes":[{"verb":"seat:mesh-catalog.revision","field":["none-yet"],"fields":["a"]}]}`,
|
||||
asideOf: "invokes[0].field",
|
||||
check: func(m Manifest) bool {
|
||||
return slices.Equal(m.Invokes, []string{"seat:mesh-catalog.revision"}) && slices.Equal(m.InvokeFields["seat:mesh-catalog.revision"], []string{"a"})
|
||||
}},
|
||||
{name: "a long-form invoke with no verb",
|
||||
raw: `{"module":"m","version":"1.0","invokes":[{"fields":["a"]}]}`,
|
||||
refused: `invokes[0]: a long-form entry names its verb`},
|
||||
} {
|
||||
m, err := decodeForTest([]byte(c.raw))
|
||||
switch {
|
||||
@@ -68,7 +74,9 @@ func TestTheSecondReaderReleaseReadsGsManifestWords(t *testing.T) {
|
||||
t.Errorf("%s: read as %v, want refused naming %q", c.name, err, c.refused)
|
||||
case c.refused == "" && err != nil:
|
||||
t.Errorf("%s: refused: %v", c.name, err)
|
||||
case c.refused == "" && (len(m.UnknownField()) != 0 || !c.check(m)):
|
||||
case c.asideOf != "" && (!strings.Contains(m.SetAside(), c.asideOf) || !c.check(m)):
|
||||
t.Errorf("%s: read as %+v, set aside %q, want %q named", c.name, m, m.SetAside(), c.asideOf)
|
||||
case c.refused == "" && c.asideOf == "" && (len(m.UnknownField()) != 0 || !c.check(m)):
|
||||
t.Errorf("%s: read as %+v (set aside %v)", c.name, m, m.UnknownField())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,3 +170,45 @@ func removeEvery(v any, key string) int {
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// readAside reads one object of a manifest into a T, setting aside each key T does not know, at any depth, and saying where each
|
||||
// was, as `.key` or `.entry.key` (design 47 H2b §871): the entry reads as it would without the word. Any other fault in it is the
|
||||
// decoder's own and is returned.
|
||||
func readAside[T any](raw json.RawMessage) (T, []string, error) {
|
||||
var zero T
|
||||
var tree any
|
||||
dec := json.NewDecoder(bytes.NewReader(raw))
|
||||
dec.UseNumber()
|
||||
if err := dec.Decode(&tree); err != nil {
|
||||
return zero, nil, err
|
||||
}
|
||||
strict := func(into *T) error {
|
||||
b, err := json.Marshal(tree)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d := json.NewDecoder(bytes.NewReader(b))
|
||||
d.DisallowUnknownFields()
|
||||
return typedUnknown(d.Decode(into))
|
||||
}
|
||||
var aside []string
|
||||
for tries := 0; ; tries++ {
|
||||
var out T
|
||||
err := strict(&out)
|
||||
if err == nil {
|
||||
return out, aside, nil
|
||||
}
|
||||
unknown := asUnknownField(err)
|
||||
if unknown == nil || tries > 64 {
|
||||
return zero, nil, err
|
||||
}
|
||||
path, ok := removalThatHelps(tree, unknown.Field, err.Error(), func() error { var e T; return strict(&e) })
|
||||
if !ok {
|
||||
if removeEvery(tree, unknown.Field) == 0 {
|
||||
return zero, nil, err
|
||||
}
|
||||
path = "." + unknown.Field
|
||||
}
|
||||
aside = append(aside, path)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user