behavior-contracts 0.12.1 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -2
- package/dist/commands/ts-extract.d.ts.map +1 -1
- package/dist/commands/ts-extract.js +9 -0
- package/dist/commands/ts-extract.js.map +1 -1
- package/go/api_test.go +675 -0
- package/go/behavior.go +1202 -0
- package/go/cmd/conformance/main.go +40 -0
- package/go/codec.go +170 -0
- package/go/coderuntime/behaviorcore.go +93 -0
- package/go/coderuntime/canonical.go +287 -0
- package/go/coderuntime/exprcore.go +334 -0
- package/go/coderuntime/plan.go +422 -0
- package/go/coderuntime/primitives.go +383 -0
- package/go/coderuntime/rawabi.go +281 -0
- package/go/coderuntime/spec.go +17 -0
- package/go/coderuntime/template.go +139 -0
- package/go/coderuntime/value.go +138 -0
- package/go/coderuntime/version.go +9 -0
- package/go/compat.go +289 -0
- package/go/conformance.go +1046 -0
- package/go/envelope.go +77 -0
- package/go/expr.go +347 -0
- package/go/fingerprint.go +147 -0
- package/go/fingerprint_test.go +94 -0
- package/go/go.mod +3 -0
- package/go/guard.go +538 -0
- package/go/jsonord.go +151 -0
- package/go/plan_parallel_test.go +279 -0
- package/go/primitives_test.go +227 -0
- package/package.json +3 -1
package/go/guard.go
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"fmt"
|
|
6
|
+
"math"
|
|
7
|
+
"regexp"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// #192: 宣言型名(obj の `name`)の検証: 非空・有効 identifier(TS guard NOMINAL_NAME と同一・cross-lang parity)。
|
|
11
|
+
var nominalNameRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
|
12
|
+
|
|
13
|
+
// PortabilityError reports a non-portable (non-JSON-serializable) value in an IR.
|
|
14
|
+
type PortabilityError struct {
|
|
15
|
+
Path string
|
|
16
|
+
Msg string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
func (e *PortabilityError) Error() string {
|
|
20
|
+
return fmt.Sprintf("Portability Guard at %s: %s", e.Path, e.Msg)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// AssertPortable asserts that a runtime Value is portable (JSON-serializable).
|
|
24
|
+
// The only non-portable scalar in the Go value model is a non-finite float.
|
|
25
|
+
func AssertPortable(value Value) error {
|
|
26
|
+
return assertPortableAt(value, "$")
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
func assertPortableAt(value Value, path string) error {
|
|
30
|
+
switch t := value.(type) {
|
|
31
|
+
case float64:
|
|
32
|
+
if math.IsNaN(t) || math.IsInf(t, 0) {
|
|
33
|
+
return &PortabilityError{Path: path, Msg: fmt.Sprintf("non-finite float %v cannot be placed in portable IR", t)}
|
|
34
|
+
}
|
|
35
|
+
return nil
|
|
36
|
+
case []Value:
|
|
37
|
+
for i, v := range t {
|
|
38
|
+
if err := assertPortableAt(v, fmt.Sprintf("%s[%d]", path, i)); err != nil {
|
|
39
|
+
return err
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return nil
|
|
43
|
+
case *Obj:
|
|
44
|
+
for _, k := range t.Keys {
|
|
45
|
+
if err := assertPortableAt(t.Vals[k], path+"."+k); err != nil {
|
|
46
|
+
return err
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return nil
|
|
50
|
+
default:
|
|
51
|
+
return nil
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// PortableExprOperators is the closed set of Expression-IR operators allowed in a
|
|
56
|
+
// portable IR (expression-ir.md §3).
|
|
57
|
+
var PortableExprOperators = map[string]bool{
|
|
58
|
+
"int": true, "float": true, "ref": true, "refOpt": true, "obj": true, "arr": true,
|
|
59
|
+
"add": true, "sub": true, "mul": true, "neg": true, "div": true, "mod": true,
|
|
60
|
+
"concat": true, "eq": true, "ne": true, "lt": true, "le": true, "gt": true, "ge": true,
|
|
61
|
+
"and": true, "or": true, "not": true, "coalesce": true, "cond": true, "len": true,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// PortableScalarTypes is the closed set of scalar types in the portable type
|
|
65
|
+
// notation (scp-ir-architecture.md §5.2, bc#44 B0). Compound types are the
|
|
66
|
+
// single-key objects {opt|arr|map|obj: ...}.
|
|
67
|
+
var PortableScalarTypes = map[string]bool{
|
|
68
|
+
"string": true, "int": true, "float": true, "bool": true, "null": true,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// PortableValueType is the generic opaque-value literal (bc#156): the declared type of a driver
|
|
72
|
+
// boundary's generic bound value (WireValue). Not a concrete scalar, but in the type notation it
|
|
73
|
+
// appears as the leaf string "value" ({arr:"value"} = a list of bound values) and is accepted with
|
|
74
|
+
// the same strength as a scalar (a DECLARED opaque type, not an "any" escape hatch). Same as the TS
|
|
75
|
+
// guard's PORTABLE_VALUE_TYPE.
|
|
76
|
+
const PortableValueType = "value"
|
|
77
|
+
|
|
78
|
+
// PortableCompoundKinds is the closed set of compound type constructors (same as the TS guard's
|
|
79
|
+
// PORTABLE_COMPOUND_KINDS).
|
|
80
|
+
var PortableCompoundKinds = map[string]bool{"opt": true, "arr": true, "map": true, "obj": true}
|
|
81
|
+
|
|
82
|
+
// portableTypeHasValue reports whether the portable type carries the opaque wire value "value"
|
|
83
|
+
// ANYWHERE (recursive). The COUNTERPART of wirePassthroughOutTypeOk: an output type that contains
|
|
84
|
+
// `value` without BEING the legal passthrough shape (nested in a record field / map value / nested
|
|
85
|
+
// array) has no de-box contract. Same rule as the TS portableTypeHasValue (behavior.ts).
|
|
86
|
+
func portableTypeHasValue(t JNode) bool {
|
|
87
|
+
if s, ok := t.(string); ok {
|
|
88
|
+
return s == PortableValueType
|
|
89
|
+
}
|
|
90
|
+
o, ok := t.(*JObj)
|
|
91
|
+
if !ok {
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
for _, k := range []string{"opt", "arr", "map"} {
|
|
95
|
+
if v, has := o.Get(k); has {
|
|
96
|
+
return portableTypeHasValue(v)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if fieldsN, has := o.Get("obj"); has {
|
|
100
|
+
if fields, ok := fieldsN.(*JObj); ok {
|
|
101
|
+
for _, k := range fields.Keys {
|
|
102
|
+
if portableTypeHasValue(fields.Vals[k]) {
|
|
103
|
+
return true
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return false
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// wirePassthroughOutTypeOk reports whether an output-passthrough node's outType is the legal opaque
|
|
112
|
+
// shape ("value" / {arr:"value"}). Passthrough only covers "the whole output" and "the DIRECT element
|
|
113
|
+
// of an output array" — a `value` anywhere else has no de-box contract. Same rule as the TS
|
|
114
|
+
// wirePassthroughOutTypeOk (behavior.ts).
|
|
115
|
+
// wirePassthroughOutTypeOk reports whether the declared type carries an opaque position (= the
|
|
116
|
+
// passthrough flag is meaningful, #221). De-box is DECLARED-TYPE driven, so `value` may sit at any
|
|
117
|
+
// position; there is no shape whitelist.
|
|
118
|
+
func wirePassthroughOutTypeOk(outType JNode, has bool) bool {
|
|
119
|
+
return has && portableTypeHasValue(outType)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// assertPortableTypeNotation validates a portable type-notation node (§5.2) with
|
|
123
|
+
// the same fail-closed strength as the Expression IR guard. Closed set:
|
|
124
|
+
// - scalar: one of PortableScalarTypes (string only; no "any" escape hatch);
|
|
125
|
+
// - {opt: T} / {arr: T}: single-key object whose value is a type T (recursive);
|
|
126
|
+
// - {obj: {field: T, ...}}: single-key object whose value is a field->type map
|
|
127
|
+
// (the __proto__ own key is forbidden — prototype-pollution discipline).
|
|
128
|
+
func assertPortableTypeNotation(node JNode, path string) error {
|
|
129
|
+
switch t := node.(type) {
|
|
130
|
+
case string:
|
|
131
|
+
if !PortableScalarTypes[t] && t != PortableValueType {
|
|
132
|
+
return &PortabilityError{Path: path, Msg: fmt.Sprintf("unknown scalar type '%s' (fail-closed)", t)}
|
|
133
|
+
}
|
|
134
|
+
return nil
|
|
135
|
+
case *JObj:
|
|
136
|
+
// #192: obj のみ optional な `name`(宣言型名)を許す。opt/arr はきっかり 1 キー(既存語彙は不変)。
|
|
137
|
+
kind := ""
|
|
138
|
+
for _, k := range t.Keys {
|
|
139
|
+
if PortableCompoundKinds[k] {
|
|
140
|
+
kind = k
|
|
141
|
+
break
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if kind == "" || (kind != "obj" && t.Len() != 1) {
|
|
145
|
+
return &PortabilityError{Path: path, Msg: fmt.Sprintf("compound type must be a single-key object (opt|arr|map|obj; obj may carry an optional 'name'), got %d keys (fail-closed)", t.Len())}
|
|
146
|
+
}
|
|
147
|
+
arg := t.Vals[kind]
|
|
148
|
+
switch kind {
|
|
149
|
+
case "opt", "arr", "map":
|
|
150
|
+
return assertPortableTypeNotation(arg, path+"."+kind)
|
|
151
|
+
case "obj":
|
|
152
|
+
for _, k := range t.Keys {
|
|
153
|
+
if k != "obj" && k != "name" {
|
|
154
|
+
return &PortabilityError{Path: path, Msg: fmt.Sprintf("{obj} type allows only 'obj' and optional 'name', got '%s' (fail-closed)", k)}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if nameV, has := t.Vals["name"]; has {
|
|
158
|
+
name, ok := nameV.(string)
|
|
159
|
+
if !ok || !nominalNameRe.MatchString(name) {
|
|
160
|
+
return &PortabilityError{Path: path + ".name", Msg: "obj type 'name' must be a non-empty identifier (fail-closed)"}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
fields, ok := arg.(*JObj)
|
|
164
|
+
if !ok {
|
|
165
|
+
return &PortabilityError{Path: path + ".obj", Msg: "{obj: ...} type expects a field->type object (fail-closed)"}
|
|
166
|
+
}
|
|
167
|
+
for _, k := range fields.Keys {
|
|
168
|
+
if k == ForbiddenObjectKey {
|
|
169
|
+
return &PortabilityError{Path: path + ".obj", Msg: fmt.Sprintf("object type key %q is forbidden (fail-closed)", ForbiddenObjectKey)}
|
|
170
|
+
}
|
|
171
|
+
if err := assertPortableTypeNotation(fields.Vals[k], path+".obj."+k); err != nil {
|
|
172
|
+
return err
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return nil
|
|
176
|
+
default:
|
|
177
|
+
return &PortabilityError{Path: path, Msg: fmt.Sprintf("unknown compound type kind '%s' (opt|arr|map|obj) (fail-closed)", kind)}
|
|
178
|
+
}
|
|
179
|
+
default:
|
|
180
|
+
return &PortabilityError{Path: path, Msg: "type notation must be a scalar-type string or a single-key {opt|arr|map|obj} object (fail-closed)"}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// AssertPortableComponentGraph is the Portability Guard for a component-graph IR
|
|
185
|
+
// (scp-ir-architecture.md §5), operating on the raw parsed JNode IR (P0-2). It
|
|
186
|
+
// enforces, fail-closed: (1) component (componentRef / map) is a string catalog
|
|
187
|
+
// reference only; (2) every Expression-IR node in ports / output / map.over / cond
|
|
188
|
+
// uses only the known operator closed set (PortableExprOperators); (3) any type
|
|
189
|
+
// annotation (outType / outputType, bc#44 B0) matches the portable type-notation
|
|
190
|
+
// closed set (§5.2). Annotations are additive: an IR without them validates
|
|
191
|
+
// identically to before B0.
|
|
192
|
+
func AssertPortableComponentGraph(ir JNode) error {
|
|
193
|
+
return assertPortableCGAt(ir, "$")
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
func assertPortableCGAt(ir JNode, path string) error {
|
|
197
|
+
o, ok := ir.(*JObj)
|
|
198
|
+
if !ok {
|
|
199
|
+
return &PortabilityError{Path: path, Msg: "IR root must be an object"}
|
|
200
|
+
}
|
|
201
|
+
// #173: v2 carries portSchemas. #192: v3 adds optional nominal type names on obj types (additive —
|
|
202
|
+
// absent = anonymous, so v2 IR stays valid). Portable versions are 2 | 3. Numbers from
|
|
203
|
+
// ParseJSONOrdered are json.Number (UseNumber), so decode the integral value.
|
|
204
|
+
verN, _ := o.Get("irVersion")
|
|
205
|
+
ver := int64(-1)
|
|
206
|
+
if num, isNum := verN.(json.Number); isNum {
|
|
207
|
+
ver, _ = num.Int64()
|
|
208
|
+
}
|
|
209
|
+
if ver != 2 && ver != 3 {
|
|
210
|
+
return &PortabilityError{Path: path + ".irVersion", Msg: "irVersion must be 2 or 3 (v3 adds optional nominal type names) (fail-closed)"}
|
|
211
|
+
}
|
|
212
|
+
compsN, has := o.Get("components")
|
|
213
|
+
comps, isArr := compsN.([]JNode)
|
|
214
|
+
if !has || !isArr {
|
|
215
|
+
return &PortabilityError{Path: path + ".components", Msg: "IR.components must be an array"}
|
|
216
|
+
}
|
|
217
|
+
for ci, cN := range comps {
|
|
218
|
+
if err := assertPortableComponent(cN, fmt.Sprintf("%s.components[%d]", path, ci)); err != nil {
|
|
219
|
+
return err
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return nil
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
func assertPortableComponent(cN JNode, path string) error {
|
|
226
|
+
c, ok := cN.(*JObj)
|
|
227
|
+
if !ok {
|
|
228
|
+
return &PortabilityError{Path: path, Msg: "component must be an object"}
|
|
229
|
+
}
|
|
230
|
+
if nm, ok := c.Get("name"); !ok {
|
|
231
|
+
return &PortabilityError{Path: path + ".name", Msg: "component.name must be a string"}
|
|
232
|
+
} else if _, isStr := nm.(string); !isStr {
|
|
233
|
+
return &PortabilityError{Path: path + ".name", Msg: "component.name must be a string"}
|
|
234
|
+
}
|
|
235
|
+
bodyN, ok := c.Get("body")
|
|
236
|
+
body, isArr := bodyN.([]JNode)
|
|
237
|
+
if !ok || !isArr {
|
|
238
|
+
return &PortabilityError{Path: path + ".body", Msg: "component.body must be an array"}
|
|
239
|
+
}
|
|
240
|
+
for ni, nN := range body {
|
|
241
|
+
if err := assertPortableBodyNode(nN, fmt.Sprintf("%s.body[%d]", path, ni)); err != nil {
|
|
242
|
+
return err
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
outputN, _ := c.Get("output")
|
|
246
|
+
if err := assertPortableExpr(outputN, path+".output"); err != nil {
|
|
247
|
+
return err
|
|
248
|
+
}
|
|
249
|
+
// Portable type notation (bc#44 B0): `outputType` is additive/optional.
|
|
250
|
+
if otN, has := c.Get("outputType"); has {
|
|
251
|
+
if err := assertPortableTypeNotation(otN, path+".outputType"); err != nil {
|
|
252
|
+
return err
|
|
253
|
+
}
|
|
254
|
+
// #221: an output contract MAY carry opaque positions — de-box is declared-type driven, so a
|
|
255
|
+
// `value` in the contract IS the contract ("opaque here; the receiver de-boxes once").
|
|
256
|
+
}
|
|
257
|
+
return nil
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
func assertPortableBodyNode(nN JNode, path string) error {
|
|
261
|
+
if err := assertPortableBodyNodeKind(nN, path); err != nil {
|
|
262
|
+
return err
|
|
263
|
+
}
|
|
264
|
+
n, ok := nN.(*JObj)
|
|
265
|
+
if !ok {
|
|
266
|
+
return nil
|
|
267
|
+
}
|
|
268
|
+
// Portable type notation (bc#44 B0): a body node's `outType` is additive/optional
|
|
269
|
+
// (validated for every node kind; the runner is unchanged — additive over `vectors`).
|
|
270
|
+
otN, hasOut := n.Get("outType")
|
|
271
|
+
if hasOut {
|
|
272
|
+
if err := assertPortableTypeNotation(otN, path+".outType"); err != nil {
|
|
273
|
+
return err
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
// Output wire passthrough (bc#164, additive/optional): a bool only, and the flag and the output type
|
|
277
|
+
// are EQUIVALENT (#205) — "declared opaque" and "passes through" are two faces of one fact, so both
|
|
278
|
+
// directions are checked: (1) flag => opaque shape, (2) an output type carrying `value` => opaque
|
|
279
|
+
// shape AND flag. Either half alone lets a raw IR through with no de-box contract for the caller.
|
|
280
|
+
passthrough := false
|
|
281
|
+
if wpN, has := n.Get("wirePassthrough"); has {
|
|
282
|
+
b, isBool := wpN.(bool)
|
|
283
|
+
if !isBool {
|
|
284
|
+
return &PortabilityError{Path: path + ".wirePassthrough", Msg: "'wirePassthrough' must be a boolean"}
|
|
285
|
+
}
|
|
286
|
+
passthrough = b
|
|
287
|
+
}
|
|
288
|
+
if passthrough && !wirePassthroughOutTypeOk(otN, hasOut) {
|
|
289
|
+
return &PortabilityError{Path: path + ".outType", Msg: `an output-passthrough node (wirePassthrough:true) must declare outType 'value' or {arr:"value"} (the opaque-wire shape) (fail-closed)`}
|
|
290
|
+
}
|
|
291
|
+
// A pure-expression map (#222) CONSTRUCTS its value — it never crosses the wire, so this rule does not
|
|
292
|
+
// apply: a `value` position in its declared type is D4b's INPUT-side opaque (the downstream port boxes it
|
|
293
|
+
// once), not an output without a de-box contract.
|
|
294
|
+
constructsValue := false
|
|
295
|
+
if mN, ok := n.Get("map"); ok {
|
|
296
|
+
if m, ok2 := mN.(*JObj); ok2 {
|
|
297
|
+
_, hasComp := m.Get("component")
|
|
298
|
+
_, hasTransform := m.Get("transform")
|
|
299
|
+
constructsValue = !hasComp && hasTransform
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if !constructsValue && hasOut && portableTypeHasValue(otN) && !passthrough {
|
|
303
|
+
return &PortabilityError{Path: path + ".outType", Msg: `an output type carrying the opaque wire value must BE the passthrough shape ('value' or {arr:"value"}) and the node must declare wirePassthrough:true — a nested/unflagged opaque output gives the caller no de-box contract (strict-typing-and-debox.md §D4b, fail-closed)`}
|
|
304
|
+
}
|
|
305
|
+
return nil
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
func assertPortableBodyNodeKind(nN JNode, path string) error {
|
|
309
|
+
n, ok := nN.(*JObj)
|
|
310
|
+
if !ok {
|
|
311
|
+
return &PortabilityError{Path: path, Msg: "body node must be an object"}
|
|
312
|
+
}
|
|
313
|
+
if mN, ok := n.Get("map"); ok {
|
|
314
|
+
m, _ := mN.(*JObj)
|
|
315
|
+
// The element body has two EXCLUSIVE forms: a Component call (`component` + `ports`) or a pure
|
|
316
|
+
// element expression (`transform`, #222). Iteration belongs to the SCP structural layer, so the
|
|
317
|
+
// Expression IR carries no iteration operator (expression-ir.md §3).
|
|
318
|
+
transformN, isTransform := m.Get("transform")
|
|
319
|
+
_, hasComp := m.Get("component")
|
|
320
|
+
_, hasPorts := m.Get("ports")
|
|
321
|
+
if isTransform && (hasComp || hasPorts) {
|
|
322
|
+
return &PortabilityError{Path: path + ".map.transform", Msg: "'transform' (a pure element expression) and 'component'/'ports' (a Component call) are exclusive"}
|
|
323
|
+
}
|
|
324
|
+
if isTransform {
|
|
325
|
+
if err := assertPortableExpr(transformN, path+".map.transform"); err != nil {
|
|
326
|
+
return err
|
|
327
|
+
}
|
|
328
|
+
for _, k := range []string{"into", "batched", "elementPolicy", "policy", "portSchemas", "leafSymbol"} {
|
|
329
|
+
if _, present := m.Get(k); present {
|
|
330
|
+
return &PortabilityError{Path: path + ".map." + k, Msg: "this key has no meaning on a pure-expression map — no Component is called, so there is no per-element outcome"}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
} else {
|
|
334
|
+
if err := requireStringComponent(m, path+".map.component"); err != nil {
|
|
335
|
+
return err
|
|
336
|
+
}
|
|
337
|
+
portsN, _ := m.Get("ports")
|
|
338
|
+
if err := assertPortablePorts(portsN, path+".map.ports"); err != nil {
|
|
339
|
+
return err
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
overN, _ := m.Get("over")
|
|
343
|
+
if err := assertPortableExpr(overN, path+".map.over"); err != nil {
|
|
344
|
+
return err
|
|
345
|
+
}
|
|
346
|
+
// v2 vocabulary: `when` is an Expression IR node (unknown operators rejected
|
|
347
|
+
// fail-closed); `into` must be a string key; `batched` a bool.
|
|
348
|
+
if whenN, ok := m.Get("when"); ok {
|
|
349
|
+
if err := assertPortableExpr(whenN, path+".map.when"); err != nil {
|
|
350
|
+
return err
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if intoN, ok := m.Get("into"); ok {
|
|
354
|
+
if _, isStr := intoN.(string); !isStr {
|
|
355
|
+
return &PortabilityError{Path: path + ".map.into", Msg: "'into' must be a string key"}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if bN, ok := m.Get("batched"); ok {
|
|
359
|
+
if _, isBool := bN.(bool); !isBool {
|
|
360
|
+
return &PortabilityError{Path: path + ".map.batched", Msg: "'batched' must be a boolean"}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// Element Error Policy Kind (scp-error.md): closed set error|skip. skip needs a per-element
|
|
364
|
+
// Failure, so a batched map (one outcome for the whole batch) rejects it.
|
|
365
|
+
if epN, ok := m.Get("elementPolicy"); ok {
|
|
366
|
+
eps, _ := epN.(string)
|
|
367
|
+
if eps != "error" && eps != "skip" {
|
|
368
|
+
return &PortabilityError{Path: path + ".map.elementPolicy", Msg: "'elementPolicy' must be \"error\" or \"skip\""}
|
|
369
|
+
}
|
|
370
|
+
if bN, hasB := m.Get("batched"); eps == "skip" && hasB {
|
|
371
|
+
if b, _ := bN.(bool); b {
|
|
372
|
+
return &PortabilityError{Path: path + ".map.elementPolicy", Msg: "'elementPolicy' \"skip\" needs a per-element Failure, but a batched map takes ONE outcome for the whole batch"}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
// A pure-expression map has no ports, hence no port type contract (#222).
|
|
377
|
+
if isTransform {
|
|
378
|
+
return nil
|
|
379
|
+
}
|
|
380
|
+
return assertPortSchemasPresent(m, path+".map") // #173
|
|
381
|
+
}
|
|
382
|
+
if foN, ok := n.Get("fanout"); ok {
|
|
383
|
+
// fanout (the behaviorVersion 3 first-class node kind — same rules as the TS guard): `over` is an
|
|
384
|
+
// Expression IR node, `component` a catalog name, `ports` Expression IR nodes; `as`/`dedupeKey` are
|
|
385
|
+
// non-empty strings, `drop` is "dangling"|"none", `implicitSource` a string or absent, and
|
|
386
|
+
// `relationKind` is fixed to "connection".
|
|
387
|
+
fo, _ := foN.(*JObj)
|
|
388
|
+
if err := requireStringComponent(fo, path+".fanout.component"); err != nil {
|
|
389
|
+
return err
|
|
390
|
+
}
|
|
391
|
+
overN, _ := fo.Get("over")
|
|
392
|
+
if err := assertPortableExpr(overN, path+".fanout.over"); err != nil {
|
|
393
|
+
return err
|
|
394
|
+
}
|
|
395
|
+
portsN, _ := fo.Get("ports")
|
|
396
|
+
if err := assertPortablePorts(portsN, path+".fanout.ports"); err != nil {
|
|
397
|
+
return err
|
|
398
|
+
}
|
|
399
|
+
for _, f := range []struct{ field, label string }{{"as", "an element-binding"}, {"dedupeKey", "a dedupe key"}} {
|
|
400
|
+
vN, _ := fo.Get(f.field)
|
|
401
|
+
v, isStr := vN.(string)
|
|
402
|
+
if !isStr || v == "" {
|
|
403
|
+
return &PortabilityError{Path: path + ".fanout." + f.field, Msg: fmt.Sprintf("fanout '%s' must be a non-empty %s string", f.field, f.label)}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
dropN, _ := fo.Get("drop")
|
|
407
|
+
if drop, _ := dropN.(string); drop != "dangling" && drop != "none" {
|
|
408
|
+
return &PortabilityError{Path: path + ".fanout.drop", Msg: `fanout 'drop' must be "dangling" or "none" (fail-closed)`}
|
|
409
|
+
}
|
|
410
|
+
if isN, has := fo.Get("implicitSource"); has {
|
|
411
|
+
if _, isStr := isN.(string); !isStr {
|
|
412
|
+
return &PortabilityError{Path: path + ".fanout.implicitSource", Msg: "fanout 'implicitSource' must be a string or absent"}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
rkN, _ := fo.Get("relationKind")
|
|
416
|
+
if rk, _ := rkN.(string); rk != "connection" {
|
|
417
|
+
return &PortabilityError{Path: path + ".fanout.relationKind", Msg: `fanout 'relationKind' must be "connection" (fail-closed)`}
|
|
418
|
+
}
|
|
419
|
+
return assertPortSchemasPresent(fo, path+".fanout") // #173
|
|
420
|
+
}
|
|
421
|
+
if cN, ok := n.Get("cond"); ok {
|
|
422
|
+
c, _ := cN.(*JObj)
|
|
423
|
+
ifN, _ := c.Get("if")
|
|
424
|
+
thenN, _ := c.Get("then")
|
|
425
|
+
elseN, _ := c.Get("else")
|
|
426
|
+
if err := assertPortableExpr(ifN, path+".cond.if"); err != nil {
|
|
427
|
+
return err
|
|
428
|
+
}
|
|
429
|
+
if err := assertPortableExpr(thenN, path+".cond.then"); err != nil {
|
|
430
|
+
return err
|
|
431
|
+
}
|
|
432
|
+
return assertPortableExpr(elseN, path+".cond.else")
|
|
433
|
+
}
|
|
434
|
+
if _, ok := n.Get("component"); ok {
|
|
435
|
+
if err := requireStringComponent(n, path+".component"); err != nil {
|
|
436
|
+
return err
|
|
437
|
+
}
|
|
438
|
+
portsN, _ := n.Get("ports")
|
|
439
|
+
if err := assertPortablePorts(portsN, path+".ports"); err != nil {
|
|
440
|
+
return err
|
|
441
|
+
}
|
|
442
|
+
return assertPortSchemasPresent(n, path) // #173
|
|
443
|
+
}
|
|
444
|
+
return &PortabilityError{Path: path, Msg: "unknown body node kind (not componentRef/map/cond/fanout)"}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// assertPortSchemasPresent — #173: irVersion 2 carries the per-node port type contract. The guard
|
|
448
|
+
// requires the field's PRESENCE (an object) on the port-bearing node kinds for cross-language parity
|
|
449
|
+
// (the TS guard additionally parses its structure, since only TS codegen consumes it). Missing /
|
|
450
|
+
// non-object portSchemas is a malformed v2 IR → fail-closed.
|
|
451
|
+
func assertPortSchemasPresent(holder *JObj, path string) error {
|
|
452
|
+
psN, ok := holder.Get("portSchemas")
|
|
453
|
+
if !ok {
|
|
454
|
+
return &PortabilityError{Path: path + ".portSchemas", Msg: "portSchemas is required (irVersion 2 carries the port type contract) and must be an object"}
|
|
455
|
+
}
|
|
456
|
+
if _, isObj := psN.(*JObj); !isObj {
|
|
457
|
+
return &PortabilityError{Path: path + ".portSchemas", Msg: "portSchemas is required (irVersion 2 carries the port type contract) and must be an object"}
|
|
458
|
+
}
|
|
459
|
+
return nil
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
func requireStringComponent(o *JObj, path string) error {
|
|
463
|
+
if o == nil {
|
|
464
|
+
return &PortabilityError{Path: path, Msg: "'component' must be a string catalog reference"}
|
|
465
|
+
}
|
|
466
|
+
cN, _ := o.Get("component")
|
|
467
|
+
if _, isStr := cN.(string); !isStr {
|
|
468
|
+
return &PortabilityError{Path: path, Msg: "'component' must be a string catalog reference"}
|
|
469
|
+
}
|
|
470
|
+
return nil
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
func assertPortablePorts(portsN JNode, path string) error {
|
|
474
|
+
ports, ok := portsN.(*JObj)
|
|
475
|
+
if !ok {
|
|
476
|
+
return &PortabilityError{Path: path, Msg: "ports must be an object"}
|
|
477
|
+
}
|
|
478
|
+
for _, k := range ports.Keys {
|
|
479
|
+
if err := assertPortableExpr(ports.Vals[k], path+"."+k); err != nil {
|
|
480
|
+
return err
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return nil
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
func assertPortableExpr(node JNode, path string) error {
|
|
487
|
+
switch t := node.(type) {
|
|
488
|
+
case nil, bool, string, json.Number:
|
|
489
|
+
return nil
|
|
490
|
+
case []JNode:
|
|
491
|
+
for i, e := range t {
|
|
492
|
+
if err := assertPortableExpr(e, fmt.Sprintf("%s[%d]", path, i)); err != nil {
|
|
493
|
+
return err
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return nil
|
|
497
|
+
case *JObj:
|
|
498
|
+
if t.Len() == 1 {
|
|
499
|
+
op := t.Keys[0]
|
|
500
|
+
if !PortableExprOperators[op] {
|
|
501
|
+
return &PortabilityError{Path: path, Msg: fmt.Sprintf("unknown operator '%s' (fail-closed)", op)}
|
|
502
|
+
}
|
|
503
|
+
// obj は「任意の data key → 子式」の構築ノード(expression-ir.md §3)。arg の key は
|
|
504
|
+
// operator ではなく data であり、値だけを式として再帰する(単一 data key の obj が
|
|
505
|
+
// 未知 operator と誤判定されないように — evaluate の意味論と一致させる)。
|
|
506
|
+
if op == "obj" {
|
|
507
|
+
fields, ok := t.Vals[op].(*JObj)
|
|
508
|
+
if !ok {
|
|
509
|
+
return &PortabilityError{Path: path + ".obj", Msg: "{obj: ...} expects an object"}
|
|
510
|
+
}
|
|
511
|
+
for _, k := range fields.Keys {
|
|
512
|
+
// 静的 fail-closed(defense-in-depth): own key "__proto__" は evaluator が
|
|
513
|
+
// 実行時に FORBIDDEN_KEY で拒否する(expression-ir.md §2.3/§8)。guard でも
|
|
514
|
+
// 静的に拒否する(言語間発散 / prototype pollution 対策)。
|
|
515
|
+
if k == ForbiddenObjectKey {
|
|
516
|
+
return &PortabilityError{
|
|
517
|
+
Path: path + ".obj",
|
|
518
|
+
Msg: fmt.Sprintf("object key %q is forbidden (fail-closed)", ForbiddenObjectKey),
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if err := assertPortableExpr(fields.Vals[k], path+".obj."+k); err != nil {
|
|
522
|
+
return err
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return nil
|
|
526
|
+
}
|
|
527
|
+
return assertPortableExpr(t.Vals[op], path+"."+op)
|
|
528
|
+
}
|
|
529
|
+
for _, k := range t.Keys {
|
|
530
|
+
if err := assertPortableExpr(t.Vals[k], path+"."+k); err != nil {
|
|
531
|
+
return err
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return nil
|
|
535
|
+
default:
|
|
536
|
+
return &PortabilityError{Path: path, Msg: "invalid expression node"}
|
|
537
|
+
}
|
|
538
|
+
}
|
package/go/jsonord.go
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
package dslcontracts
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"encoding/json"
|
|
6
|
+
"fmt"
|
|
7
|
+
"io"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// JNode is a JSON value with object key insertion order preserved. It is used
|
|
11
|
+
// both as the parsed IR (expression trees, vectors) and as the intermediate for
|
|
12
|
+
// decoding wire values. The concrete dynamic types are:
|
|
13
|
+
//
|
|
14
|
+
// nil, bool, string -> as-is
|
|
15
|
+
// json.Number -> number (distinguishes int vs float by textual form)
|
|
16
|
+
// []JNode -> array
|
|
17
|
+
// *JObj -> object (ordered)
|
|
18
|
+
type JNode interface{}
|
|
19
|
+
|
|
20
|
+
// JObj is an insertion-ordered JSON object.
|
|
21
|
+
type JObj struct {
|
|
22
|
+
Keys []string
|
|
23
|
+
Vals map[string]JNode
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
func newJObj() *JObj { return &JObj{Vals: map[string]JNode{}} }
|
|
27
|
+
|
|
28
|
+
func (o *JObj) set(k string, v JNode) {
|
|
29
|
+
if _, ok := o.Vals[k]; !ok {
|
|
30
|
+
o.Keys = append(o.Keys, k)
|
|
31
|
+
}
|
|
32
|
+
o.Vals[k] = v
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Get returns the value for k and whether it is present.
|
|
36
|
+
func (o *JObj) Get(k string) (JNode, bool) {
|
|
37
|
+
v, ok := o.Vals[k]
|
|
38
|
+
return v, ok
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// JObjOf constructs an ordered JSON object from alternating key/value pairs
|
|
42
|
+
// ("k1", v1, "k2", v2, …), preserving the given key order. It is the literal
|
|
43
|
+
// constructor used by generated codegen modules (bc#13 common Generator), which
|
|
44
|
+
// embed the portable IR as native JNode composite literals. Malformed pairs
|
|
45
|
+
// (odd count / non-string key) panic — generated code is machine-produced, so
|
|
46
|
+
// this is a fail-closed programming-error guard, not a runtime data path.
|
|
47
|
+
func JObjOf(pairs ...JNode) *JObj {
|
|
48
|
+
if len(pairs)%2 != 0 {
|
|
49
|
+
panic("JObjOf: odd number of arguments (want alternating key/value pairs)")
|
|
50
|
+
}
|
|
51
|
+
o := newJObj()
|
|
52
|
+
for i := 0; i < len(pairs); i += 2 {
|
|
53
|
+
k, ok := pairs[i].(string)
|
|
54
|
+
if !ok {
|
|
55
|
+
panic(fmt.Sprintf("JObjOf: key at position %d must be a string, got %T", i, pairs[i]))
|
|
56
|
+
}
|
|
57
|
+
o.set(k, pairs[i+1])
|
|
58
|
+
}
|
|
59
|
+
return o
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Len returns the number of keys.
|
|
63
|
+
func (o *JObj) Len() int { return len(o.Keys) }
|
|
64
|
+
|
|
65
|
+
// ParseJSONOrdered parses JSON preserving object key order and int/float
|
|
66
|
+
// distinction (numbers become json.Number).
|
|
67
|
+
func ParseJSONOrdered(data []byte) (JNode, error) {
|
|
68
|
+
dec := json.NewDecoder(bytes.NewReader(data))
|
|
69
|
+
dec.UseNumber()
|
|
70
|
+
n, err := parseValue(dec)
|
|
71
|
+
if err != nil {
|
|
72
|
+
return nil, err
|
|
73
|
+
}
|
|
74
|
+
// ensure no trailing garbage
|
|
75
|
+
if dec.More() {
|
|
76
|
+
return nil, fmt.Errorf("trailing data after JSON value")
|
|
77
|
+
}
|
|
78
|
+
return n, nil
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func parseValue(dec *json.Decoder) (JNode, error) {
|
|
82
|
+
tok, err := dec.Token()
|
|
83
|
+
if err != nil {
|
|
84
|
+
return nil, err
|
|
85
|
+
}
|
|
86
|
+
return parseFromToken(dec, tok)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
func parseFromToken(dec *json.Decoder, tok json.Token) (JNode, error) {
|
|
90
|
+
switch t := tok.(type) {
|
|
91
|
+
case json.Delim:
|
|
92
|
+
switch t {
|
|
93
|
+
case '{':
|
|
94
|
+
return parseObject(dec)
|
|
95
|
+
case '[':
|
|
96
|
+
return parseArray(dec)
|
|
97
|
+
default:
|
|
98
|
+
return nil, fmt.Errorf("unexpected delimiter %q", t)
|
|
99
|
+
}
|
|
100
|
+
case nil:
|
|
101
|
+
return nil, nil
|
|
102
|
+
case bool:
|
|
103
|
+
return t, nil
|
|
104
|
+
case string:
|
|
105
|
+
return t, nil
|
|
106
|
+
case json.Number:
|
|
107
|
+
return t, nil
|
|
108
|
+
default:
|
|
109
|
+
return nil, fmt.Errorf("unexpected token %T", tok)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
func parseObject(dec *json.Decoder) (*JObj, error) {
|
|
114
|
+
obj := newJObj()
|
|
115
|
+
for dec.More() {
|
|
116
|
+
keyTok, err := dec.Token()
|
|
117
|
+
if err != nil {
|
|
118
|
+
return nil, err
|
|
119
|
+
}
|
|
120
|
+
key, ok := keyTok.(string)
|
|
121
|
+
if !ok {
|
|
122
|
+
return nil, fmt.Errorf("object key not a string: %T", keyTok)
|
|
123
|
+
}
|
|
124
|
+
val, err := parseValue(dec)
|
|
125
|
+
if err != nil {
|
|
126
|
+
return nil, err
|
|
127
|
+
}
|
|
128
|
+
obj.set(key, val)
|
|
129
|
+
}
|
|
130
|
+
// consume closing '}'
|
|
131
|
+
if _, err := dec.Token(); err != nil && err != io.EOF {
|
|
132
|
+
return nil, err
|
|
133
|
+
}
|
|
134
|
+
return obj, nil
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
func parseArray(dec *json.Decoder) ([]JNode, error) {
|
|
138
|
+
arr := []JNode{}
|
|
139
|
+
for dec.More() {
|
|
140
|
+
val, err := parseValue(dec)
|
|
141
|
+
if err != nil {
|
|
142
|
+
return nil, err
|
|
143
|
+
}
|
|
144
|
+
arr = append(arr, val)
|
|
145
|
+
}
|
|
146
|
+
// consume closing ']'
|
|
147
|
+
if _, err := dec.Token(); err != nil && err != io.EOF {
|
|
148
|
+
return nil, err
|
|
149
|
+
}
|
|
150
|
+
return arr, nil
|
|
151
|
+
}
|