Repository navigation
Expand file tree
/
Copy pathassertion_env.go
More file actions
372 lines (329 loc) · 9.52 KB
/
Copy pathassertion_env.go
File metadata and controls
372 lines (329 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package uir
import (
"encoding/json"
"fmt"
"strings"
)
// celKeyAliases re-exposes JSON keys that collide with CEL reserved words under
// a name a CEL expression can select.
var celKeyAliases = map[string]string{
"package": "pkg",
"type": "clazz",
"string": "str",
}
// ToMap returns the UIR as its generic JSON map, with CEL-reserved keys aliased.
func (uir UIR) ToMap() (map[string]any, error) {
b, err := json.Marshal(uir)
if err != nil {
return nil, fmt.Errorf("json marshal: %w", err)
}
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil {
return nil, fmt.Errorf("json unmarshal: %w", err)
}
m = UnwrapSlots(m).(map[string]any)
addCELAliases(m)
return m, nil
}
// UnwrapSlots rewrites a generic JSON value of UIR so every tagged interface
// slot, {"$<kind>": <value>}, reads as the inline value it holds, stamped as the
// legacy layout stamps it: statement_type is the registered kind and a refined
// Type travels under statement_refinement. Assertions and other map walkers see
// one shape whichever layout the JSON was written in.
func UnwrapSlots(v any) any {
switch val := v.(type) {
case map[string]any:
if kind, inner, ok := taggedSlot(val); ok {
if carried, isString := inner[statementTypeField].(string); isString && carried != kind {
inner[statementRefinementField] = carried
inner[statementTypeField] = kind
}
return UnwrapSlots(inner)
}
for k, child := range val {
val[k] = UnwrapSlots(child)
}
return val
case []any:
for i, item := range val {
val[i] = UnwrapSlots(item)
}
return val
}
return v
}
func taggedSlot(m map[string]any) (string, map[string]any, bool) {
if len(m) != 1 {
return "", nil, false
}
for key, value := range m {
kind, tagged := strings.CutPrefix(key, slotKeyPrefix)
inner, isObject := value.(map[string]any)
if tagged && isObject {
return kind, inner, true
}
}
return "", nil, false
}
// AssertionEnv returns a CEL assertion environment with stable UIR lookup helpers.
func (uir UIR) AssertionEnv() (map[string]any, error) {
env, err := uir.ToMap()
if err != nil {
return nil, err
}
AddAssertionIndex(env)
return env, nil
}
func addCELAliases(v any) {
switch val := v.(type) {
case map[string]any:
for k, child := range val {
addCELAliases(child)
if alias, ok := celKeyAliases[k]; ok {
val[alias] = child
}
}
case []any:
for _, item := range val {
addCELAliases(item)
}
}
}
// AddAssertionIndex adds path-indexed UIR symbols to env under "symbols" (and
// "__uir_index"), returning the existing index when env already carries one.
func AddAssertionIndex(env map[string]any) map[string]any {
if existing, ok := env["__uir_index"].(map[string]any); ok {
return existing
}
index := map[string]any{}
for _, fn := range collectAssertionFunctions(env) {
if len(fn) == 0 {
continue
}
key := functionAssertionKey(fn)
if key == "" {
continue
}
params := indexByName(fn["params"], "name", "field")
returns := indexByName(fn["returns"], "name", "field")
annotations := indexByName(fn["annotations"], "clazz", "type")
properties := asMap(fn["properties"])
statements := collectStatementMaps(fn)
statementMap := indexStatements(statements)
calls := indexCalls(statements)
fn["_params"] = params
fn["_returns"] = returns
fn["_annotations"] = annotations
fn["_properties"] = properties
fn["_statements"] = statementMap
fn["_calls"] = calls
uniqueKey := addIndexedValue(index, key, fn)
addIndexedParts(index, uniqueKey, params, returns, annotations, properties, statementMap, calls)
if method := asString(fn["method"]); method != "" {
uniqueMethodKey := addIndexedValue(index, "method."+method, fn)
addIndexedParts(index, uniqueMethodKey, params, returns, annotations, properties, statementMap, calls)
}
if root := asString(properties["rootElement"]); root != "" {
uniqueRootKey := addIndexedValue(index, "root."+root, fn)
addIndexedParts(index, uniqueRootKey, params, returns, annotations, properties, statementMap, calls)
}
}
env["__uir_index"] = index
env["symbols"] = index
return index
}
func collectAssertionFunctions(env map[string]any) []map[string]any {
out := make([]map[string]any, 0)
collectFunctionMaps(asList(env["functions"]), "", "", &out)
collectTypeFunctions(asList(env["types"]), "", &out)
collectPackageFunctions(asList(env["packages"]), &out)
return out
}
func collectPackageFunctions(packages []any, out *[]map[string]any) {
for _, raw := range packages {
pkg := asMap(raw)
if len(pkg) == 0 {
continue
}
pkgName := firstString(pkg["pkg"], pkg["package"])
collectFunctionMaps(asList(pkg["functions"]), pkgName, "", out)
collectTypeFunctions(asList(pkg["types"]), pkgName, out)
}
}
func collectTypeFunctions(types []any, parentPkg string, out *[]map[string]any) {
for _, raw := range types {
typ := asMap(raw)
if len(typ) == 0 {
continue
}
pkgName := firstString(typ["pkg"], typ["package"], parentPkg)
typeName := firstString(typ["clazz"], typ["_name"], typ["type"])
collectFunctionMaps(asList(typ["methods"]), pkgName, typeName, out)
collectFunctionMap(asMap(typ["constructor"]), pkgName, typeName, out)
collectFunctionMap(asMap(typ["constructor_"]), pkgName, typeName, out)
collectFunctionMap(asMap(typ["destructor"]), pkgName, typeName, out)
collectTypeFunctions(asList(typ["types"]), pkgName, out)
}
}
func collectFunctionMaps(items []any, pkg, clazz string, out *[]map[string]any) {
for _, raw := range items {
collectFunctionMap(asMap(raw), pkg, clazz, out)
}
}
func collectFunctionMap(fn map[string]any, pkg, clazz string, out *[]map[string]any) {
if len(fn) == 0 {
return
}
if pkgName := firstString(fn["pkg"], fn["package"], pkg); pkgName != "" {
if _, ok := fn["package"]; !ok {
fn["package"] = pkgName
}
if _, ok := fn["pkg"]; !ok {
fn["pkg"] = pkgName
}
}
if typeName := firstString(fn["clazz"], fn["type"], clazz); typeName != "" {
if _, ok := fn["type"]; !ok {
fn["type"] = typeName
}
if _, ok := fn["clazz"]; !ok {
fn["clazz"] = typeName
}
}
*out = append(*out, fn)
}
func functionAssertionKey(fn map[string]any) string {
pkg := firstString(fn["pkg"], fn["package"])
clazz := firstString(fn["clazz"], fn["type"])
method := asString(fn["method"])
if clazz == "" {
clazz = method
}
return strings.Join(nonEmpty(pkg, clazz, method), ".")
}
func addIndexedParts(index map[string]any, key string, params, returns, annotations, properties, statements, calls map[string]any) {
index[key+"._params"] = params
index[key+"._returns"] = returns
index[key+"._annotations"] = annotations
index[key+"._properties"] = properties
index[key+"._statements"] = statements
index[key+"._calls"] = calls
}
func addIndexedValue(index map[string]any, key string, value any) string {
instancesKey := key + "._instances"
instances, _ := index[instancesKey].([]any)
index[instancesKey] = append(instances, value)
if _, exists := index[key]; !exists {
index[key] = value
return key
}
for i := 2; ; i++ {
candidate := fmt.Sprintf("%s#%d", key, i)
if _, exists := index[candidate]; !exists {
index[candidate] = value
return candidate
}
}
}
func indexByName(raw any, names ...string) map[string]any {
out := map[string]any{}
for _, item := range asList(raw) {
m := asMap(item)
for _, name := range names {
if key := asString(m[name]); key != "" {
out[key] = m
break
}
}
}
return out
}
func collectStatementMaps(fn map[string]any) []map[string]any {
var out []map[string]any
body := asMap(fn["body"])
collectStatements(asList(body["children"]), &out)
return out
}
func collectStatements(items []any, out *[]map[string]any) {
for _, item := range items {
stmt := asMap(item)
if len(stmt) == 0 {
continue
}
*out = append(*out, stmt)
collectStatements(asList(stmt["children"]), out)
collectBlockStatements(stmt["body"], out)
collectBlockStatements(stmt["then"], out)
collectBlockStatements(stmt["else"], out)
collectBlockStatements(stmt["catch"], out)
for _, rawCase := range asList(stmt["cases"]) {
collectBlockStatements(asMap(rawCase)["body"], out)
}
}
}
func collectBlockStatements(raw any, out *[]map[string]any) {
if block := asMap(raw); len(block) > 0 {
collectStatements(asList(block["children"]), out)
}
}
func indexStatements(statements []map[string]any) map[string]any {
out := map[string]any{}
for i, stmt := range statements {
key := firstString(stmt["signature"], stmt["statement_type"], stmt["type"])
if key == "" {
key = fmt.Sprintf("statement_%d", i)
}
addIndexedValue(out, key, stmt)
}
return out
}
func indexCalls(statements []map[string]any) map[string]any {
out := map[string]any{}
for _, stmt := range statements {
if asString(stmt["statement_type"]) != "call" {
continue
}
method := asMap(stmt["Method"])
key := strings.Join(nonEmpty(firstString(method["pkg"], method["package"]), asString(method["method"])), ".")
if key == "" {
continue
}
addIndexedValue(out, key, stmt)
}
return out
}
func asMap(raw any) map[string]any {
if m, ok := raw.(map[string]any); ok {
return m
}
return map[string]any{}
}
func asList(raw any) []any {
if list, ok := raw.([]any); ok {
return list
}
return nil
}
func asString(raw any) string {
if value, ok := raw.(string); ok {
return value
}
return ""
}
func firstString(values ...any) string {
for _, value := range values {
if s := asString(value); s != "" {
return s
}
}
return ""
}
func nonEmpty(values ...string) []string {
out := make([]string, 0, len(values))
for _, value := range values {
if value != "" {
out = append(out, value)
}
}
return out
}