GEN1: the seat generator refuses a definition whose callers entry is no form of the format
The generator checks each verb's and accepted kind's callers in a definition and a part before reading it, naming the file, the verb and the entry; text is refused as one entry; the contracts it writes carry no caller check. Builds: design 47 G §336, §339, §392 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package seatgen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
re "regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// callerName is a seat's or a module's name inside a caller entry.
|
||||
var callerName = re.MustCompile(`^[a-z][a-z0-9]*(-[a-z0-9]+)*$`)
|
||||
|
||||
// callerClasses are the entries that take no name (design 47 G §336, §392).
|
||||
var callerClasses = map[string]bool{"operator": true, "healers": true, "persons": true, "sessions": true, "invokers": true}
|
||||
|
||||
// callerForm says whether an entry is one of the forms of design 47 G §336 as §392 writes them: `operator`, `healers`,
|
||||
// `persons`, `sessions`, `invokers`, `seat:<seat>`, `module:<module>` and `holder:<seat>@same-node`.
|
||||
func callerForm(entry string) bool {
|
||||
switch {
|
||||
case callerClasses[entry]:
|
||||
return true
|
||||
case strings.HasPrefix(entry, "seat:"):
|
||||
return callerName.MatchString(strings.TrimPrefix(entry, "seat:"))
|
||||
case strings.HasPrefix(entry, "module:"):
|
||||
return callerName.MatchString(strings.TrimPrefix(entry, "module:"))
|
||||
case strings.HasPrefix(entry, "holder:") && strings.HasSuffix(entry, "@same-node"):
|
||||
return callerName.MatchString(strings.TrimSuffix(strings.TrimPrefix(entry, "holder:"), "@same-node"))
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// checkCallers refuses a definition or part whose verb or accepted kind names a `callers` that is no list of the
|
||||
// forms of §336, naming the entry (design 47 G §339: the generator reads `callers` and makes nothing of it; the
|
||||
// grants part reads them from the definition). Text, the old form, is refused as one entry.
|
||||
func checkCallers(file string, raw []byte) error {
|
||||
var doc struct {
|
||||
Verbs []callerHolder `json:"verbs"`
|
||||
Accepts []callerHolder `json:"accepts"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &doc); err != nil {
|
||||
return nil // the definition reader names a malformed file
|
||||
}
|
||||
for _, group := range []struct {
|
||||
field string
|
||||
items []callerHolder
|
||||
}{{"verbs", doc.Verbs}, {"accepts", doc.Accepts}} {
|
||||
for _, it := range group.items {
|
||||
if len(it.Callers) == 0 {
|
||||
continue
|
||||
}
|
||||
var list []string
|
||||
if err := json.Unmarshal(it.Callers, &list); err != nil {
|
||||
var text string
|
||||
if json.Unmarshal(it.Callers, &text) != nil {
|
||||
return fmt.Errorf("%s: %s %q: callers is neither a list nor text", file, group.field, it.Name)
|
||||
}
|
||||
list = []string{text}
|
||||
}
|
||||
for _, e := range list {
|
||||
if !callerForm(e) {
|
||||
return fmt.Errorf("%s: %s %q: caller %q is no form of the format (operator, healers, persons, sessions, invokers, seat:<seat>, module:<module>, holder:<seat>@same-node)", file, group.field, it.Name, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type callerHolder struct {
|
||||
Name string `json:"name"`
|
||||
Callers json.RawMessage `json:"callers"`
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
re "regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -177,3 +178,57 @@ func TestEachSeatDefinedWholeIsAGoModuleOfItsOwn(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCallersAreTheFormsOfTheFormat is design 47 G §339's check: the generator refuses a definition (or a part)
|
||||
// whose verb or accepted kind names a caller that is no form of §336, naming the entry, and the contracts it writes
|
||||
// carry no caller check.
|
||||
func TestCallersAreTheFormsOfTheFormat(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, callers, refused string
|
||||
}{
|
||||
{"every form", `["operator","healers","persons","sessions","invokers","seat:mesh-wants","module:gitea","holder:node-backup@same-node"]`, ""},
|
||||
{"text", `"any caller whose role grants it"`, "any caller whose role grants it"},
|
||||
{"a bare seat name", `["mesh-wants"]`, "mesh-wants"},
|
||||
{"holder without the node", `["holder:node-backup"]`, "holder:node-backup"},
|
||||
{"seat with no name", `["seat:"]`, "seat:"},
|
||||
} {
|
||||
for _, place := range []string{"verbs", "accepts"} {
|
||||
t.Run(tc.name+"/"+place, func(t *testing.T) {
|
||||
raw := []byte(`{"format":1,"seat":"x","` + place + `":[{"name":"v","callers":` + tc.callers + `}]}`)
|
||||
err := checkCallers("seats/x/seat.json", raw)
|
||||
switch {
|
||||
case tc.refused == "" && err != nil:
|
||||
t.Fatalf("refused: %v", err)
|
||||
case tc.refused != "" && (err == nil || !strings.Contains(err.Error(), tc.refused) || !strings.Contains(err.Error(), "seats/x/seat.json")):
|
||||
t.Fatalf("error %v, want one naming %q", err, tc.refused)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
// Load refuses an unknown entry in a definition before it reads it.
|
||||
dir := t.TempDir()
|
||||
if err := os.MkdirAll(dir+"/seats/x", 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
def := `{"format":1,"seat":"x","scope":"mesh","grain":"seat-list","verbs":[{"name":"v","callers":["somebody"]}]}`
|
||||
if err := os.WriteFile(dir+"/seats/x/seat.json", []byte(def), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Load(dir, "", ""); err == nil || !strings.Contains(err.Error(), `"somebody"`) {
|
||||
t.Fatalf("Load error %v, want one naming the entry", err)
|
||||
}
|
||||
// The generated contracts of the testdata hold no caller check.
|
||||
m, err := Load("testdata/contracts", "example.org/contracts/go", "@example/contracts")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files, err := Generate(m)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for p, b := range files {
|
||||
if strings.Contains(strings.ToLower(string(b)), "callers") && strings.HasSuffix(p, ".go") {
|
||||
t.Errorf("%s carries a caller check", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,9 @@ func Load(dir, goModule, tsPackage string) (*Module, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := checkCallers(rel(dir, p), raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
part, err := seatdef.ParsePart(raw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", rel(dir, p), err)
|
||||
@@ -66,6 +69,9 @@ func Load(dir, goModule, tsPackage string) (*Module, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := checkCallers(rel(dir, p), raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d, err := seatdef.Parse(raw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", rel(dir, p), err)
|
||||
|
||||
Reference in New Issue
Block a user