A type added or removed whole changes no reader's value; only a type whose fields changed while the form did not fails the compare. Builds: design 47 H2 §780; H-A-352 (2)
201 lines
7.0 KiB
Go
201 lines
7.0 KiB
Go
package seatgen
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
goregexp "regexp"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
)
|
|
|
|
// The form of a seat's values, events and answers (design 47 H2 §780; H-A-352): one integer per seat definition, and a
|
|
// part's own for what it adds, raised at every field added or removed; left out of a definition while it is 1, and an
|
|
// absent form reads as 1. Every value, event body and verb answer carries it as the wire field `form` (left out while
|
|
// 1), and its generated decoder keeps going past a field it does not know and answers the form it read. Each
|
|
// generated contract carries its forms and a digest of its fields, so the compare step fails a definition whose fields
|
|
// changed while its forms did not.
|
|
|
|
// formOf is a form as a definition gives it: absent reads as 1.
|
|
func formOf(n int) int {
|
|
if n < 1 {
|
|
return 1
|
|
}
|
|
return n
|
|
}
|
|
|
|
// formField is the wire field every value, event and answer carries.
|
|
const formField = "form"
|
|
|
|
// addForms gives each struct type another part reads the field `form`, unless its definition names one already.
|
|
func (tp *typer) addForms() {
|
|
byName := map[string]int{}
|
|
for i, t := range tp.types {
|
|
byName[t.name] = i
|
|
}
|
|
for _, n := range tp.readBy {
|
|
i, ok := byName[n]
|
|
if ok && tp.types[i].t.kind == "ref" {
|
|
// An answer or event that is one of the definition's own types: that type is what travels.
|
|
i, ok = byName[tp.types[i].t.ref]
|
|
}
|
|
if !ok || tp.types[i].t.kind != "struct" {
|
|
continue
|
|
}
|
|
t := tp.types[i].t
|
|
if hasField(t, formField) {
|
|
continue
|
|
}
|
|
f := field{json: formField, desc: "is the form of what this value carries, left out while it is 1 (design 47 H2 §780).",
|
|
t: &typ{kind: "integer"}}
|
|
at := sort.Search(len(t.fields), func(j int) bool { return t.fields[j].json >= formField })
|
|
t.fields = append(t.fields[:at], append([]field{f}, t.fields[at:]...)...)
|
|
}
|
|
}
|
|
|
|
func hasField(t *typ, name string) bool {
|
|
for _, f := range t.fields {
|
|
if f.json == name {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// formsOf is the seat's form and each included part's, in one line: `<seat form>` and ` <part>:<form>` per part in
|
|
// the order included.
|
|
func formsOf(m *Module, d seatdef.Definition) string {
|
|
out := fmt.Sprint(formOf(d.Form))
|
|
for _, p := range m.Raw[d.Seat].Includes {
|
|
out += fmt.Sprintf(" %s:%d", p, formOf(m.Parts[p].Form))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// fieldsDigest is the digest of the names and types of the fields of every value, event, accept and answer of the seat
|
|
// and of every type they reach, one short digest per type (`<type>=<digest>`, sorted, space-separated), so the compare
|
|
// tells a type whose fields changed from one added or removed whole; the field `form` itself is not counted.
|
|
func fieldsDigest(tp *typer) string {
|
|
kinds := map[string]*typ{}
|
|
for _, t := range tp.types {
|
|
kinds[t.name] = t.t
|
|
}
|
|
seen := map[string]bool{}
|
|
lines := map[string][]string{}
|
|
var walk func(t *typ)
|
|
var visit func(name string)
|
|
walk = func(t *typ) {
|
|
if t == nil {
|
|
return
|
|
}
|
|
if t.kind == "ref" {
|
|
visit(t.ref)
|
|
}
|
|
walk(t.elem)
|
|
for _, f := range t.fields {
|
|
walk(f.t)
|
|
}
|
|
}
|
|
visit = func(name string) {
|
|
if seen[name] {
|
|
return
|
|
}
|
|
seen[name] = true
|
|
t, ok := kinds[name]
|
|
if !ok {
|
|
return
|
|
}
|
|
if t.kind != "struct" {
|
|
lines[name] = append(lines[name], "= "+goType(t, kinds, true)+" "+strings.Join(t.enum, "|"))
|
|
} else {
|
|
for _, f := range t.fields {
|
|
if f.json == formField {
|
|
continue
|
|
}
|
|
lines[name] = append(lines[name], fmt.Sprintf("%s %s %v %s", f.json, goType(f.t, kinds, f.required), f.required, strings.Join(f.t.enum, "|")))
|
|
}
|
|
}
|
|
walk(t)
|
|
}
|
|
for _, n := range append(append([]string{}, tp.readBy...), tp.accepts...) {
|
|
visit(n)
|
|
}
|
|
var out []string
|
|
for _, name := range sortedKeys(lines) {
|
|
sum := sha256.Sum256([]byte(strings.Join(lines[name], "\n")))
|
|
out = append(out, name+"="+hex.EncodeToString(sum[:6]))
|
|
}
|
|
return strings.Join(out, " ")
|
|
}
|
|
|
|
// writeForms writes the seat's forms and the digest of its fields.
|
|
func writeForms(b *bytes.Buffer, m *Module, d seatdef.Definition, tp *typer) {
|
|
fmt.Fprintf(b, "// Form is the form of the seat's values, events and answers this contract was generated at (design 47 H2 §780):\n"+
|
|
"// a reader of a value of another form says \"form N, this reader knows M\".\nconst Form = %d\n\n", formOf(d.Form))
|
|
for _, p := range m.Raw[d.Seat].Includes {
|
|
fmt.Fprintf(b, "// Form%s is the form of what the part %s adds.\nconst Form%s = %d\n\n", ident(p), p, ident(p), formOf(m.Parts[p].Form))
|
|
}
|
|
fmt.Fprintf(b, "// The forms and the digest of the fields this contract was generated from: the generator's compare fails a definition\n"+
|
|
"// whose fields changed while its forms did not (design 47 H2 §780).\nconst (\n\tgeneratedForms = %q\n\tgeneratedFieldsDigest = %q\n)\n\n",
|
|
formsOf(m, d), fieldsDigest(tp))
|
|
}
|
|
|
|
// goDecoders writes the tolerant decoder of every event and answer type (a state's values have theirs with its keys),
|
|
// and the reader of the form on the wire.
|
|
func goDecoders(b *bytes.Buffer, tp *typer) {
|
|
if len(tp.readBy) == 0 {
|
|
return
|
|
}
|
|
for _, n := range tp.readBy {
|
|
if tp.values[n] {
|
|
continue
|
|
}
|
|
fmt.Fprintf(b, "// Decode%s reads a %s as another part wrote it: a field this reader does not know is left unread, never refused; the form read is answered, 1 when absent (design 47 H2 §780).\n"+
|
|
"func Decode%s(raw []byte) (%s, int, error) {\n\tvar v %s\n\tif err := json.Unmarshal(raw, &v); err != nil {\n\t\treturn v, 0, err\n\t}\n\treturn v, formRead(raw), nil\n}\n\n",
|
|
n, n, n, n, n)
|
|
}
|
|
b.WriteString("// formRead is the form a value says on the wire: 1 when it says none, or is no object.\nfunc formRead(raw []byte) int {\n" +
|
|
"\tvar f struct {\n\t\tForm int `json:\"form\"`\n\t}\n\tif json.Unmarshal(raw, &f) != nil || f.Form < 1 {\n\t\treturn 1\n\t}\n\treturn f.Form\n}\n\n")
|
|
}
|
|
|
|
var (
|
|
formsLine = goregexp.MustCompile(`generatedForms\s*=\s*"([^"]*)"`)
|
|
digestLine = goregexp.MustCompile(`generatedFieldsDigest\s*=\s*"([^"]*)"`)
|
|
)
|
|
|
|
// formUnraised says whether a contract the tree holds was generated at the same forms as the one written now from a
|
|
// type whose fields differ: a definition whose fields changed with its form unchanged (design 47 H2 §780). A type
|
|
// added or removed whole changes no reader's value. A contract in the tree
|
|
// that carries no digest yet (generated before forms) is not judged.
|
|
func formUnraised(dir, p string, want []byte) (bool, error) {
|
|
got, err := os.ReadFile(filepath.Join(dir, p))
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
of, od := formsLine.FindSubmatch(got), digestLine.FindSubmatch(got)
|
|
nf, nd := formsLine.FindSubmatch(want), digestLine.FindSubmatch(want)
|
|
if of == nil || od == nil || nf == nil || nd == nil {
|
|
return false, nil
|
|
}
|
|
if string(of[1]) != string(nf[1]) {
|
|
return false, nil
|
|
}
|
|
old := map[string]string{}
|
|
for _, kv := range strings.Fields(string(od[1])) {
|
|
k, v, _ := strings.Cut(kv, "=")
|
|
old[k] = v
|
|
}
|
|
for _, kv := range strings.Fields(string(nd[1])) {
|
|
k, v, _ := strings.Cut(kv, "=")
|
|
if was, ok := old[k]; ok && was != v {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|