@voxgig/sdkgen 4.3.0 → 4.4.0
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/bin/voxgig-sdkgen +1 -1
- package/package.json +1 -1
- package/project/.sdk/src/cmp/zig/Config_zig.ts +1 -1
- package/project/.sdk/tm/c/tests/primary_corpus_test.c +565 -0
- package/project/.sdk/tm/c/utility/prepare_method.c +3 -1
- package/project/.sdk/tm/c/utility/transform_request.c +4 -3
- package/project/.sdk/tm/clojure/src/sdk/core.clj +55 -5
- package/project/.sdk/tm/clojure/test/sdk/test/primary.clj +171 -1
- package/project/.sdk/tm/clojure/test/sdk/test/struct_corpus.clj +13 -2
- package/project/.sdk/tm/clojure/test/sdk/test_runner.clj +2 -0
- package/project/.sdk/tm/csharp/test/CustomUtilityTest.cs +104 -0
- package/project/.sdk/tm/csharp/utility/MakeOptions.cs +82 -2
- package/project/.sdk/tm/elixir/lib/projectname/utility.ex +50 -2
- package/project/.sdk/tm/elixir/test/primary_utility_test.exs +18 -200
- package/project/.sdk/tm/elixir/test/support/struct_corpus.ex +413 -2
- package/project/.sdk/tm/kotlin/utility/MakeOptions.kt +56 -2
- package/project/.sdk/tm/lua/utility/make_options.lua +22 -2
- package/project/.sdk/tm/ocaml/Makefile +15 -4
- package/project/.sdk/tm/ocaml/sdk_runtime.ml +8 -1
- package/project/.sdk/tm/ocaml/test/corpus_runner.ml +185 -0
- package/project/.sdk/tm/ocaml/test/primary_utility_test.ml +238 -0
- package/project/.sdk/tm/ocaml/test/struct_corpus.ml +5 -160
- package/project/.sdk/tm/scala/Makefile +1 -0
- package/project/.sdk/tm/scala/sdktest/PrimaryCorpus.scala +294 -0
- package/project/.sdk/tm/scala/sdktest/StructCorpus.scala +0 -0
- package/project/.sdk/tm/scala/utility/Make.scala +49 -2
- package/project/.sdk/tm/scala/utility/Prepare.scala +3 -1
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/MakeOptions.swift +21 -3
- package/project/.sdk/tm/ts/src/feature/test/TestFeature.ts +13 -2
- package/project/.sdk/tm/zig/core/utility.zig +8 -1
- package/project/.sdk/tm/zig/test/primary_utility_test.zig +445 -0
- package/project/.sdk/tm/zig/test/struct_runner.zig +238 -0
- package/project/sdkgen-package.json +1 -1
|
@@ -2,6 +2,7 @@ package KOTLINPACKAGE.utility
|
|
|
2
2
|
|
|
3
3
|
import KOTLINPACKAGE.core.Context
|
|
4
4
|
import KOTLINPACKAGE.core.Helpers
|
|
5
|
+
import KOTLINPACKAGE.core.Utility
|
|
5
6
|
import KOTLINPACKAGE.utility.struct.Struct
|
|
6
7
|
|
|
7
8
|
@Suppress("UNCHECKED_CAST")
|
|
@@ -11,11 +12,20 @@ fun makeOptions(ctx: Context): MutableMap<String, Any?> {
|
|
|
11
12
|
options = linkedMapOf()
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
// Merge
|
|
15
|
+
// Merge utility overrides from options onto the utility object.
|
|
15
16
|
// Read from original options before clone for parity with the donors.
|
|
17
|
+
//
|
|
18
|
+
// A key naming a real utility member REPLACES it; anything else is attached
|
|
19
|
+
// as a custom extra. Shelving everything in `custom` - a map nothing reads -
|
|
20
|
+
// made `utility = mapOf("fetcher" to ...)`, the documented transport seam, a
|
|
21
|
+
// silent no-op here while ts honoured it.
|
|
16
22
|
val customUtils = Helpers.toMapAny(options["utility"])
|
|
17
23
|
if (customUtils != null && ctx.utility != null) {
|
|
18
|
-
|
|
24
|
+
for ((key, value) in customUtils) {
|
|
25
|
+
if (!overrideUtil(ctx.utility!!, key, value)) {
|
|
26
|
+
ctx.utility!!.custom[key] = value
|
|
27
|
+
}
|
|
28
|
+
}
|
|
19
29
|
}
|
|
20
30
|
|
|
21
31
|
var opts = Struct.clone(options) as MutableMap<String, Any?>
|
|
@@ -165,3 +175,47 @@ fun makeOptions(ctx: Context): MutableMap<String, Any?> {
|
|
|
165
175
|
|
|
166
176
|
return opts
|
|
167
177
|
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Replaces one utility member from `options.utility`, matching the ts
|
|
182
|
+
* reference: a key naming a real member REPLACES it, and any other key is
|
|
183
|
+
* attached as a custom extra. Returns false when the key names no member or
|
|
184
|
+
* the value is not that member's type, so the caller keeps it in `custom`.
|
|
185
|
+
*
|
|
186
|
+
* REFLECTION, NOT A KEYED SWITCH. The go and java ports list every member by
|
|
187
|
+
* hand and carry a "keep this in step with registerAll" warning, because a
|
|
188
|
+
* utility added to one list and not the other is overridable there and not
|
|
189
|
+
* here. The field set is readable off the class, so the list cannot drift.
|
|
190
|
+
*
|
|
191
|
+
* Kotlin function types erase to FunctionN, so `isInstance` checks arity and
|
|
192
|
+
* not the full signature - the same limit java's port documents. A wrongly
|
|
193
|
+
* shaped value of the right arity is accepted, exactly as the dynamic donors
|
|
194
|
+
* accept whatever they are given.
|
|
195
|
+
*
|
|
196
|
+
* Only a PUBLIC name may replace a member: public utility names are camelCase
|
|
197
|
+
* and carry no underscore, so an underscore means the caller named something
|
|
198
|
+
* of their own rather than a member.
|
|
199
|
+
*/
|
|
200
|
+
internal fun overrideUtil(utility: Utility, key: String, value: Any?): Boolean {
|
|
201
|
+
if (key.isEmpty() || key.contains('_') || "custom" == key) {
|
|
202
|
+
return false
|
|
203
|
+
}
|
|
204
|
+
if (null == value) {
|
|
205
|
+
return false
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
val field = try {
|
|
209
|
+
Utility::class.java.getDeclaredField(key)
|
|
210
|
+
} catch (e: NoSuchFieldException) {
|
|
211
|
+
return false
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (!field.type.isInstance(value)) {
|
|
215
|
+
return false
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
field.isAccessible = true
|
|
219
|
+
field.set(utility, value)
|
|
220
|
+
return true
|
|
221
|
+
}
|
|
@@ -5,13 +5,33 @@ local vs = require("utility.struct.struct")
|
|
|
5
5
|
local function make_options_util(ctx)
|
|
6
6
|
local options = ctx.options or {}
|
|
7
7
|
|
|
8
|
-
-- Merge
|
|
8
|
+
-- Merge utility overrides from options onto the utility object.
|
|
9
|
+
--
|
|
10
|
+
-- A key naming a real utility member REPLACES it; anything else is attached
|
|
11
|
+
-- as a custom extra. Shelving everything in `custom` - a table nothing reads
|
|
12
|
+
-- - made `utility = { fetcher = ... }`, the documented transport seam, a
|
|
13
|
+
-- silent no-op here while ts honoured it.
|
|
14
|
+
--
|
|
15
|
+
-- Option keys are camelCase, as ts spells them; members here are
|
|
16
|
+
-- snake_case. Only a PUBLIC name may replace: public utility names carry no
|
|
17
|
+
-- underscore, so an underscore means the caller named something of their
|
|
18
|
+
-- own - possibly the internal spelling of a real member. `make_error` must
|
|
19
|
+
-- stay an extension, or a non-callable would break the error path on the
|
|
20
|
+
-- next request.
|
|
9
21
|
local custom_utils = vs.getprop(options, "utility")
|
|
10
22
|
if type(custom_utils) == "table" then
|
|
11
23
|
local utility = ctx.utility
|
|
12
24
|
if utility ~= nil then
|
|
13
25
|
for key, val in pairs(custom_utils) do
|
|
14
|
-
|
|
26
|
+
local member = nil
|
|
27
|
+
if type(key) == "string" and not key:find("_") then
|
|
28
|
+
member = key:gsub("(%u)", function(c) return "_" .. c:lower() end)
|
|
29
|
+
end
|
|
30
|
+
if member ~= nil and member ~= "custom" and utility[member] ~= nil then
|
|
31
|
+
utility[member] = val
|
|
32
|
+
else
|
|
33
|
+
utility.custom[key] = val
|
|
34
|
+
end
|
|
15
35
|
end
|
|
16
36
|
end
|
|
17
37
|
end
|
|
@@ -34,11 +34,16 @@ SDK_TEST = $(RUNTIME) sdk_config.ml $(ENTITIES) sdk_client.ml sdk_error.ml \
|
|
|
34
34
|
$(ENTITY_TESTS) $(DOC_TESTS) test/t_main.ml
|
|
35
35
|
|
|
36
36
|
CORPUS_SRC = utility/vregex.ml utility/voxgig_struct.ml sdk_json.ml \
|
|
37
|
-
test/struct_corpus.ml
|
|
37
|
+
test/corpus_runner.ml test/struct_corpus.ml
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
# The primary-utility corpus needs the whole SDK, because it drives the SDK's
|
|
40
|
+
# own request-shaping utilities — not just the struct library.
|
|
41
|
+
PRIMARY_SRC = $(RUNTIME) sdk_config.ml $(ENTITIES) sdk_client.ml sdk_error.ml \
|
|
42
|
+
test/corpus_runner.ml test/primary_utility_test.ml
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
.PHONY: test test-sdk test-corpus test-primary build lint clean
|
|
45
|
+
|
|
46
|
+
test: test-sdk test-corpus test-primary
|
|
42
47
|
|
|
43
48
|
test-sdk: run_sdk_test
|
|
44
49
|
./run_sdk_test
|
|
@@ -50,7 +55,13 @@ run_sdk_test: $(SDK_TEST)
|
|
|
50
55
|
$(OCAMLC) $(INC) $(SDK_TEST) -o run_sdk_test
|
|
51
56
|
|
|
52
57
|
run_struct_corpus: $(CORPUS_SRC)
|
|
53
|
-
$(OCAMLC) -I utility $(CORPUS_SRC) -o run_struct_corpus
|
|
58
|
+
$(OCAMLC) -I utility -I test $(CORPUS_SRC) -o run_struct_corpus
|
|
59
|
+
|
|
60
|
+
test-primary: run_primary_corpus
|
|
61
|
+
./run_primary_corpus $(CORPUS)
|
|
62
|
+
|
|
63
|
+
run_primary_corpus: $(PRIMARY_SRC)
|
|
64
|
+
$(OCAMLC) $(INC) $(PRIMARY_SRC) -o run_primary_corpus
|
|
54
65
|
|
|
55
66
|
# Type-check the SDK library (a clean compile means it is sound).
|
|
56
67
|
build: lint
|
|
@@ -345,7 +345,14 @@ let prepare_method_util (ctx : ctx) : string =
|
|
|
345
345
|
| _ ->
|
|
346
346
|
match ctx.c_op.op_name with
|
|
347
347
|
| "create" -> "POST" | "update" -> "PUT" | "load" -> "GET"
|
|
348
|
-
| "list" -> "GET" | "remove" -> "DELETE" | "patch" -> "PATCH"
|
|
348
|
+
| "list" -> "GET" | "remove" -> "DELETE" | "patch" -> "PATCH"
|
|
349
|
+
(* NO CATCH-ALL GET. The ts reference returns methodMap[key], which is
|
|
350
|
+
* undefined for an op the map does not name — the request is then rejected
|
|
351
|
+
* rather than silently issued. A `| _ -> "GET"` here turned every
|
|
352
|
+
* unrecognised op into a GET, which is both a divergence from the corpus
|
|
353
|
+
* (which expects no method for opname "bad") and the more dangerous of the
|
|
354
|
+
* two behaviours: a mistyped or unsupported op quietly fetched. *)
|
|
355
|
+
| _ -> ""
|
|
349
356
|
|
|
350
357
|
let prepare_headers_util (ctx : ctx) : value =
|
|
351
358
|
let options = client_options_map (cc ctx) in
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
(* Shared corpus harness for the ProjectName SDK.
|
|
2
|
+
*
|
|
3
|
+
* Extracted from struct_corpus.ml so the primary-utility suite can drive the
|
|
4
|
+
* SAME runner over test.json's "primary" section. OCaml runs a module's
|
|
5
|
+
* top-level effects on link, so a harness sitting beside a `let () = ...` main
|
|
6
|
+
* cannot be reused — linking it would run the struct corpus as a side effect.
|
|
7
|
+
* This module deliberately has no main.
|
|
8
|
+
*)
|
|
9
|
+
|
|
10
|
+
(* Test runner for the shared JSON corpus (build/test/test.json).
|
|
11
|
+
* Self-contained: an in-tree JSON reader builds the library's `value` type
|
|
12
|
+
* directly, so the OCaml port is exercised exactly as in production. *)
|
|
13
|
+
|
|
14
|
+
open Voxgig_struct
|
|
15
|
+
|
|
16
|
+
let nullmark = "__NULL__"
|
|
17
|
+
let undefmark = "__UNDEF__"
|
|
18
|
+
let existsmark = "__EXISTS__"
|
|
19
|
+
|
|
20
|
+
(* The JSON reader now lives in the runtime (sdk_json.ml) so the SDK's
|
|
21
|
+
* generated config can use it; the corpus runs against that same function
|
|
22
|
+
* rather than a second copy that could drift from it. *)
|
|
23
|
+
let json_read = Sdk_json.json_read
|
|
24
|
+
|
|
25
|
+
(* ---------------- fixJSON / equality ---------------- *)
|
|
26
|
+
|
|
27
|
+
let rec fix_json v flag_null =
|
|
28
|
+
match v with
|
|
29
|
+
| Noval | Null -> if flag_null then Str nullmark else v
|
|
30
|
+
| Map m -> let o = empty_map () in
|
|
31
|
+
List.iter (fun (k, x) -> ignore (setprop o (Str k) (fix_json x flag_null))) m.entries; o
|
|
32
|
+
| List r -> lst (List.map (fun x -> fix_json x flag_null) !r)
|
|
33
|
+
| _ -> v
|
|
34
|
+
|
|
35
|
+
(* Order-independent deep equality for maps; sequence equality for lists. *)
|
|
36
|
+
let rec eqv a b =
|
|
37
|
+
match a, b with
|
|
38
|
+
| (Noval | Null), (Noval | Null) -> true
|
|
39
|
+
| Bool x, Bool y -> x = y
|
|
40
|
+
| Num x, Num y -> x = y
|
|
41
|
+
| Str x, Str y -> x = y
|
|
42
|
+
| List x, List y -> List.length !x = List.length !y && List.for_all2 eqv !x !y
|
|
43
|
+
| Map x, Map y ->
|
|
44
|
+
omap_len x = omap_len y &&
|
|
45
|
+
List.for_all (fun (k, v) -> match omap_get y k with Some w -> eqv v w | None -> false) x.entries
|
|
46
|
+
| _ -> a == b
|
|
47
|
+
|
|
48
|
+
(* ---------------- match support ---------------- *)
|
|
49
|
+
|
|
50
|
+
let matchval check base =
|
|
51
|
+
let check = if check = Str undefmark || check = Str nullmark then Noval else check in
|
|
52
|
+
if eqv check base then true
|
|
53
|
+
else match check with
|
|
54
|
+
| Str cs ->
|
|
55
|
+
let basestr = stringify base in
|
|
56
|
+
if String.length cs >= 2 && cs.[0] = '/' && cs.[String.length cs - 1] = '/' then
|
|
57
|
+
Vregex.test_str (String.sub cs 1 (String.length cs - 2)) basestr
|
|
58
|
+
else
|
|
59
|
+
let low s = String.lowercase_ascii s in
|
|
60
|
+
let contains hay needle =
|
|
61
|
+
let hl = String.length hay and nl = String.length needle in
|
|
62
|
+
let rec go i = if i + nl > hl then false
|
|
63
|
+
else if String.sub hay i nl = needle then true else go (i + 1) in
|
|
64
|
+
nl = 0 || go 0 in
|
|
65
|
+
contains (low basestr) (low (stringify check))
|
|
66
|
+
| Func _ -> true
|
|
67
|
+
| _ -> false
|
|
68
|
+
|
|
69
|
+
let do_match check base =
|
|
70
|
+
let base = clone base in
|
|
71
|
+
ignore (walk ~before:(fun _k v _p path ->
|
|
72
|
+
(if not (isnode v) then begin
|
|
73
|
+
let baseval = getpath base path in
|
|
74
|
+
if eqv baseval v then ()
|
|
75
|
+
else if v = Str undefmark && is_nullish baseval then ()
|
|
76
|
+
else if v = Str existsmark && not (is_nullish baseval) then ()
|
|
77
|
+
else if not (matchval v baseval) then
|
|
78
|
+
raise (Struct_error (Printf.sprintf "MATCH: %s: [%s] <=> [%s]"
|
|
79
|
+
(String.concat "." (List.map js_string (match path with List r -> !r | _ -> [])))
|
|
80
|
+
(stringify v) (stringify baseval)))
|
|
81
|
+
end);
|
|
82
|
+
v) check)
|
|
83
|
+
|
|
84
|
+
(* ---------------- result tracking ---------------- *)
|
|
85
|
+
|
|
86
|
+
let npass = ref 0
|
|
87
|
+
let nfail = ref 0
|
|
88
|
+
let failures = ref []
|
|
89
|
+
|
|
90
|
+
let record group name ok msg =
|
|
91
|
+
if ok then incr npass
|
|
92
|
+
else (incr nfail; failures := Printf.sprintf "FAIL %s %s - %s" group name msg :: !failures)
|
|
93
|
+
|
|
94
|
+
(* ---------------- per-entry runner ---------------- *)
|
|
95
|
+
|
|
96
|
+
let omap_v kvs =
|
|
97
|
+
let m = empty_map () in
|
|
98
|
+
List.iter (fun (k, v) -> ignore (setprop m (Str k) v)) kvs; m
|
|
99
|
+
|
|
100
|
+
let getprop_raw_pub e k = (match e with Map m -> (match omap_get m k with Some x -> x | None -> Noval) | _ -> Noval)
|
|
101
|
+
let entry_get e k = getprop_raw_pub e k
|
|
102
|
+
let entry_has e k = match e with Map m -> omap_has m k | _ -> false
|
|
103
|
+
let default_injdef_pub () =
|
|
104
|
+
{ d_meta = Noval; d_extra = Noval; d_errs = Noval; d_modify = None; d_handler = None;
|
|
105
|
+
d_base = Noval; d_dparent = Noval; d_dpath = Noval; d_key = Noval }
|
|
106
|
+
|
|
107
|
+
let resolve_args entry =
|
|
108
|
+
if entry_has entry "ctx" then [entry_get entry "ctx"]
|
|
109
|
+
else if entry_has entry "args" then (
|
|
110
|
+
match entry_get entry "args" with
|
|
111
|
+
| List r ->
|
|
112
|
+
(* ts's resolveArgs writes the live first arg back as entry.ctx, so a
|
|
113
|
+
`match: {ctx: ...}` resolves for args-style entries too. Without it
|
|
114
|
+
every such assertion reads null and asserts nothing. *)
|
|
115
|
+
(match !r with
|
|
116
|
+
| (Map _ as first) :: _ -> ignore (setprop entry (Str "ctx") first)
|
|
117
|
+
| _ -> ());
|
|
118
|
+
!r
|
|
119
|
+
| _ -> [])
|
|
120
|
+
else if entry_has entry "in" then [clone (entry_get entry "in")]
|
|
121
|
+
else [Noval]
|
|
122
|
+
|
|
123
|
+
let check_result entry args res =
|
|
124
|
+
let matched = ref false in
|
|
125
|
+
(if entry_has entry "match" then begin
|
|
126
|
+
do_match (entry_get entry "match")
|
|
127
|
+
(omap_v ["in", entry_get entry "in"; "args", lst args;
|
|
128
|
+
"out", entry_get entry "res"; "ctx", entry_get entry "ctx"]);
|
|
129
|
+
matched := true
|
|
130
|
+
end);
|
|
131
|
+
let out = entry_get entry "out" in
|
|
132
|
+
if eqv out res then ()
|
|
133
|
+
else if !matched && (out = Str nullmark || is_nullish out) then ()
|
|
134
|
+
else raise (Struct_error (Printf.sprintf "Expected: %s, got: %s" (stringify out) (stringify res)))
|
|
135
|
+
|
|
136
|
+
let handle_error entry err =
|
|
137
|
+
let msg = (match err with Struct_error m -> m | e -> Printexc.to_string e) in
|
|
138
|
+
if entry_has entry "err" then begin
|
|
139
|
+
let entry_err = entry_get entry "err" in
|
|
140
|
+
if entry_err = Bool true || matchval entry_err (Str msg) then begin
|
|
141
|
+
if entry_has entry "match" then
|
|
142
|
+
do_match (entry_get entry "match")
|
|
143
|
+
(omap_v ["in", entry_get entry "in"; "out", entry_get entry "res";
|
|
144
|
+
(* ts hands do_match the ERROR OBJECT, so a corpus
|
|
145
|
+
`match: {err: {message: ...}}` resolves; a bare string
|
|
146
|
+
leaves err.message reading null. *)
|
|
147
|
+
"ctx", entry_get entry "ctx";
|
|
148
|
+
"err", omap_v ["message", Str msg]])
|
|
149
|
+
end else
|
|
150
|
+
raise (Struct_error (Printf.sprintf "ERROR MATCH: [%s] <=> [%s]" (stringify entry_err) msg))
|
|
151
|
+
end else raise err
|
|
152
|
+
|
|
153
|
+
let run_set ?(flags = []) group node subject =
|
|
154
|
+
let flag_null = (match List.assoc_opt "null" flags with Some b -> b | None -> true) in
|
|
155
|
+
let fixed = fix_json node flag_null in
|
|
156
|
+
let testset = (match getprop fixed (Str "set") with List r -> !r | _ -> []) in
|
|
157
|
+
List.iter (fun entry ->
|
|
158
|
+
let name = js_string (entry_get entry "name") in
|
|
159
|
+
try
|
|
160
|
+
(if not (entry_has entry "out") && flag_null then ignore (setprop entry (Str "out") (Str nullmark)));
|
|
161
|
+
let args = resolve_args entry in
|
|
162
|
+
let res = fix_json (subject args) flag_null in
|
|
163
|
+
ignore (setprop entry (Str "res") res);
|
|
164
|
+
check_result entry args res;
|
|
165
|
+
record group name true ""
|
|
166
|
+
with
|
|
167
|
+
| e ->
|
|
168
|
+
(try handle_error entry e; record group name true ""
|
|
169
|
+
with e2 -> record group name false
|
|
170
|
+
(match e2 with Struct_error m -> m | _ -> Printexc.to_string e2)))
|
|
171
|
+
testset
|
|
172
|
+
|
|
173
|
+
let run_single group node actual_fn =
|
|
174
|
+
try
|
|
175
|
+
let expected = getprop_raw_pub node "out" in
|
|
176
|
+
let actual = actual_fn (getprop_raw_pub node "in") in
|
|
177
|
+
if eqv expected actual then record group "single" true ""
|
|
178
|
+
else record group "single" false (Printf.sprintf "Expected: %s, got: %s" (stringify expected) (stringify actual))
|
|
179
|
+
with e -> record group "single" false (match e with Struct_error m -> m | _ -> Printexc.to_string e)
|
|
180
|
+
|
|
181
|
+
(* ---------------- arg helpers ---------------- *)
|
|
182
|
+
|
|
183
|
+
let arg1 f = fun args -> f (match args with x :: _ -> x | [] -> Noval)
|
|
184
|
+
let vget vin k = match vin with Map m -> (match omap_get m k with Some x -> x | None -> Noval) | _ -> Noval
|
|
185
|
+
let vhas vin k = match vin with Map m -> omap_has m k | _ -> false
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
(* ProjectName SDK primary-utility corpus.
|
|
2
|
+
*
|
|
3
|
+
* Drives the SHARED language-neutral corpus (.sdk/test/test.json -> "primary")
|
|
4
|
+
* through this SDK's request-shaping utilities, so the cases cannot drift from
|
|
5
|
+
* the reference implementation. Each section is looked up with getSpec and
|
|
6
|
+
* executed with runset, as the ts/js reference harness does.
|
|
7
|
+
*
|
|
8
|
+
* ocaml had NO primary-utility suite at all before this: its request-shaping
|
|
9
|
+
* utilities were unverified in every language-neutral sense. The harness is
|
|
10
|
+
* Corpus_runner, shared with the struct corpus.
|
|
11
|
+
*)
|
|
12
|
+
|
|
13
|
+
open Voxgig_struct
|
|
14
|
+
open Sdk_types
|
|
15
|
+
open Sdk_helpers
|
|
16
|
+
open Sdk_runtime
|
|
17
|
+
open Corpus_runner
|
|
18
|
+
|
|
19
|
+
let client () : sdk_client = Sdk_client.test ()
|
|
20
|
+
|
|
21
|
+
(* A client built from a section's DEF.setup.a block. prepare_auth reads the
|
|
22
|
+
* CLIENT's options, as the ts reference does via client.options(), so a
|
|
23
|
+
* section's setup cannot reach it through ctx.options. *)
|
|
24
|
+
let client_for primary name =
|
|
25
|
+
let setup =
|
|
26
|
+
List.fold_left (fun acc k -> getprop_raw_pub acc k) primary [name; "DEF"; "setup"; "a"]
|
|
27
|
+
in
|
|
28
|
+
match setup with
|
|
29
|
+
| Map _ -> Sdk_client.test_with Noval setup
|
|
30
|
+
| _ -> Sdk_client.test ()
|
|
31
|
+
|
|
32
|
+
let getspec root path =
|
|
33
|
+
List.fold_left (fun acc k -> getprop_raw_pub acc k) root path
|
|
34
|
+
|
|
35
|
+
(* A LIVE context from a corpus map. The utilities read and MUTATE spec,
|
|
36
|
+
* result and response through their record types, so a bare value map leaves
|
|
37
|
+
* them nothing to work on and every match reads null. *)
|
|
38
|
+
let ctx_from (cl : sdk_client) (ctxmap : value) : ctx =
|
|
39
|
+
let u = cl.cl_utility in
|
|
40
|
+
(* The corpus names the op — {"ctx": {"opname": "create"}} — and
|
|
41
|
+
* prepare_method reads it. Hardcoding "load" made every method GET. *)
|
|
42
|
+
(* Only when the corpus names one. Defaulting to "load" made the SDK report
|
|
43
|
+
* the wrong operation in error messages the corpus matches on — it expects
|
|
44
|
+
* "unknown operation" where no op is named. *)
|
|
45
|
+
let cs =
|
|
46
|
+
match getprop_raw_pub ctxmap "opname" with
|
|
47
|
+
| Str s -> { (default_ctxspec ()) with cs_opname = Some s }
|
|
48
|
+
| _ -> default_ctxspec ()
|
|
49
|
+
in
|
|
50
|
+
let c = u.u_make_context cs cl.cl_rootctx in
|
|
51
|
+
(match getprop_raw_pub ctxmap "spec" with
|
|
52
|
+
| Map _ as m -> c.c_spec <- Some (new_spec m)
|
|
53
|
+
| _ -> ());
|
|
54
|
+
(match getprop_raw_pub ctxmap "result" with
|
|
55
|
+
| Map _ as m ->
|
|
56
|
+
let rt = new_result m in
|
|
57
|
+
(* new_result hardcodes rt_err = None, so a corpus result carrying an err
|
|
58
|
+
* arrives empty and result_basic has no previous message to prepend —
|
|
59
|
+
* it produced "request: 400: BAD" where the contract says
|
|
60
|
+
* "Foo: request: 400: BAD". The lua and elixir drivers build it too. *)
|
|
61
|
+
(match getprop_raw_pub m "err" with
|
|
62
|
+
| Map _ as em ->
|
|
63
|
+
(match getprop_raw_pub em "message" with
|
|
64
|
+
| Str msg when msg <> "" ->
|
|
65
|
+
rt.rt_err <- Some { err_code = ""; err_msg = msg; err_result = Noval; err_spec = Noval }
|
|
66
|
+
| _ -> ())
|
|
67
|
+
| _ -> ());
|
|
68
|
+
c.c_result <- Some rt
|
|
69
|
+
| _ -> ());
|
|
70
|
+
(match getprop_raw_pub ctxmap "response" with
|
|
71
|
+
| Map _ as m ->
|
|
72
|
+
let r = new_response m in
|
|
73
|
+
(* result_body_util reads response.json and requires it to be CALLABLE;
|
|
74
|
+
* the corpus supplies a plain `body`, so wrap it, as the lua and elixir
|
|
75
|
+
* drivers do. Without this every ctx.result.body match reads empty. *)
|
|
76
|
+
(match getprop_raw_pub m "body" with
|
|
77
|
+
| Noval -> ()
|
|
78
|
+
| b -> r.rs_json <- Func (fun _ _ _ _ -> b));
|
|
79
|
+
(* Header names arrive from the wire in any case and the contract is
|
|
80
|
+
* lowercase; the lua and elixir drivers normalise here rather than in
|
|
81
|
+
* result_headers_util, which copies them verbatim. *)
|
|
82
|
+
(match getprop_raw_pub m "headers" with
|
|
83
|
+
| Map hm ->
|
|
84
|
+
let low = empty_map () in
|
|
85
|
+
List.iter (fun (k, v) -> ignore (setprop low (Str (String.lowercase_ascii k)) v))
|
|
86
|
+
hm.entries;
|
|
87
|
+
r.rs_headers <- low
|
|
88
|
+
| _ -> ());
|
|
89
|
+
c.c_response <- Some r
|
|
90
|
+
| _ -> ());
|
|
91
|
+
(match getprop_raw_pub ctxmap "point" with
|
|
92
|
+
| Map _ as m -> c.c_point <- m
|
|
93
|
+
| _ -> ());
|
|
94
|
+
(match getprop_raw_pub ctxmap "reqdata" with Noval -> () | v -> c.c_reqdata <- v);
|
|
95
|
+
(match getprop_raw_pub ctxmap "reqmatch" with Noval -> () | v -> c.c_reqmatch <- v);
|
|
96
|
+
(match getprop_raw_pub ctxmap "data" with Noval -> () | v -> c.c_data <- v);
|
|
97
|
+
(match getprop_raw_pub ctxmap "match" with Noval -> () | v -> c.c_match <- v);
|
|
98
|
+
c
|
|
99
|
+
|
|
100
|
+
(* The corpus speaks camelCase; this port stores rs_status_text / rt_ok. A
|
|
101
|
+
* neutral-named view is what the match assertions read. *)
|
|
102
|
+
let result_value (r : result option) : value =
|
|
103
|
+
match r with
|
|
104
|
+
| None -> Noval
|
|
105
|
+
| Some rt -> result_to_value rt
|
|
106
|
+
|
|
107
|
+
let ctx_arg args = match args with x :: _ -> x | [] -> Noval
|
|
108
|
+
|
|
109
|
+
(* The harness reports a non-Struct_error exception via Printexc, which renders
|
|
110
|
+
* a branded SDK error as "Sdk_types.Sdk_error_exc(_)" and loses the message the
|
|
111
|
+
* corpus matches on. Convert here rather than teaching the shared harness about
|
|
112
|
+
* SDK types — corpus_runner is struct's, not this SDK's. *)
|
|
113
|
+
let run_guarded f c =
|
|
114
|
+
try f c with
|
|
115
|
+
| Sdk_error_exc e -> raise (Struct_error e.err_msg)
|
|
116
|
+
|
|
117
|
+
(* Publish the MUTATED ctx back onto the corpus map the match reads.
|
|
118
|
+
* check_result matches against entry.ctx, which is the raw corpus map; the
|
|
119
|
+
* utilities mutate the live records beside it, so without this every
|
|
120
|
+
* `match: ctx.spec.*` / `ctx.result.*` assertion reads empty. Neutral names,
|
|
121
|
+
* because the corpus is camelCase and this port stores rs_status_text/rt_ok. *)
|
|
122
|
+
let publish (ctxmap : value) (c : ctx) =
|
|
123
|
+
(match c.c_spec with
|
|
124
|
+
| Some sp -> ignore (setprop ctxmap (Str "spec") (spec_to_value sp))
|
|
125
|
+
| None -> ());
|
|
126
|
+
(match c.c_result with
|
|
127
|
+
| Some _ -> ignore (setprop ctxmap (Str "result") (result_value c.c_result))
|
|
128
|
+
| None -> ());
|
|
129
|
+
(match c.c_response with
|
|
130
|
+
| Some _ -> ignore (setprop ctxmap (Str "response") (Str "__EXISTS__"))
|
|
131
|
+
| None -> ());
|
|
132
|
+
()
|
|
133
|
+
|
|
134
|
+
let () =
|
|
135
|
+
let testfile = if Array.length Sys.argv > 1 then Sys.argv.(1) else "../.sdk/test/test.json" in
|
|
136
|
+
let ic = open_in_bin testfile in
|
|
137
|
+
let len = in_channel_length ic in
|
|
138
|
+
let raw = really_input_string ic len in
|
|
139
|
+
close_in ic;
|
|
140
|
+
let alltests = json_read raw in
|
|
141
|
+
let primary = getprop_raw_pub alltests "primary" in
|
|
142
|
+
|
|
143
|
+
let cl = client () in
|
|
144
|
+
let u = cl.cl_utility in
|
|
145
|
+
|
|
146
|
+
(* Sections configured by their own DEF.setup block get their own client. *)
|
|
147
|
+
let ctx_section_with cl2 primary name f =
|
|
148
|
+
run_set name (getspec primary [name; "basic"])
|
|
149
|
+
(fun args ->
|
|
150
|
+
let ctxmap = ctx_arg args in
|
|
151
|
+
let c = ctx_from cl2 ctxmap in
|
|
152
|
+
let out = run_guarded f c in
|
|
153
|
+
publish ctxmap c;
|
|
154
|
+
out)
|
|
155
|
+
in
|
|
156
|
+
|
|
157
|
+
let ctx_section primary name f =
|
|
158
|
+
run_set name (getspec primary [name; "basic"])
|
|
159
|
+
(fun args ->
|
|
160
|
+
let ctxmap = ctx_arg args in
|
|
161
|
+
let c = ctx_from cl ctxmap in
|
|
162
|
+
let out = run_guarded f c in
|
|
163
|
+
publish ctxmap c;
|
|
164
|
+
out)
|
|
165
|
+
in
|
|
166
|
+
|
|
167
|
+
ctx_section primary "done" (fun c -> u.u_done c);
|
|
168
|
+
ctx_section primary "makeUrl" (fun c -> match make_url_util c with (s, _) -> Str s);
|
|
169
|
+
ctx_section primary "makeRequest"
|
|
170
|
+
(fun c -> ignore (make_request_util c); result_value c.c_result);
|
|
171
|
+
ctx_section primary "makeResponse"
|
|
172
|
+
(fun c -> ignore (make_response_util c); result_value c.c_result);
|
|
173
|
+
ctx_section_with (client_for primary "makeSpec") primary "makeSpec"
|
|
174
|
+
(fun c -> match make_spec_util c with
|
|
175
|
+
| (Some s, _) -> spec_to_value s | _ -> Noval);
|
|
176
|
+
ctx_section_with (client_for primary "prepareAuth") primary "prepareAuth"
|
|
177
|
+
(fun c -> ignore (prepare_auth_util c);
|
|
178
|
+
match c.c_spec with Some s -> spec_to_value s | None -> Noval);
|
|
179
|
+
ctx_section primary "prepareBody" (fun c -> prepare_body_util c);
|
|
180
|
+
ctx_section primary "prepareHeaders" (fun c -> prepare_headers_util c);
|
|
181
|
+
ctx_section primary "prepareMethod"
|
|
182
|
+
(fun c -> match prepare_method_util c with "" -> Noval | m -> Str m);
|
|
183
|
+
ctx_section primary "prepareParams" (fun c -> prepare_params_util c);
|
|
184
|
+
ctx_section primary "preparePath" (fun c -> Str (prepare_path_util c));
|
|
185
|
+
ctx_section primary "prepareQuery" (fun c -> prepare_query_util c);
|
|
186
|
+
ctx_section primary "resultBasic" (fun c -> result_basic_util c; result_value c.c_result);
|
|
187
|
+
ctx_section primary "resultBody" (fun c -> result_body_util c; result_value c.c_result);
|
|
188
|
+
ctx_section primary "resultHeaders" (fun c -> result_headers_util c; result_value c.c_result);
|
|
189
|
+
ctx_section primary "transformRequest" (fun c -> transform_request_util c);
|
|
190
|
+
ctx_section primary "transformResponse" (fun c -> transform_response_util c);
|
|
191
|
+
|
|
192
|
+
(* Sections that take a bare map or explicit args rather than a ctx. *)
|
|
193
|
+
let arg_section primary name f =
|
|
194
|
+
run_set name (getspec primary [name; "basic"]) (fun args -> run_guarded f args)
|
|
195
|
+
in
|
|
196
|
+
|
|
197
|
+
arg_section primary "makeContext" (fun args ->
|
|
198
|
+
let inv = ctx_arg args in
|
|
199
|
+
let c = ctx_from cl inv in
|
|
200
|
+
jo [("op", jo [("entity", Str c.c_op.op_entity); ("name", Str c.c_op.op_name);
|
|
201
|
+
("input", Str c.c_op.op_input); ("points", c.c_op.op_points)])]);
|
|
202
|
+
|
|
203
|
+
arg_section primary "makeOptions" (fun args ->
|
|
204
|
+
let inv = ctx_arg args in
|
|
205
|
+
let c = ctx_from cl (jo []) in
|
|
206
|
+
c.c_config <- getprop_raw_pub inv "config";
|
|
207
|
+
c.c_options <- getprop_raw_pub inv "options";
|
|
208
|
+
make_options_util c);
|
|
209
|
+
|
|
210
|
+
arg_section primary "makeError" (fun args ->
|
|
211
|
+
let a0 = ctx_arg args in
|
|
212
|
+
let a1 = (match args with _ :: y :: _ -> y | _ -> Noval) in
|
|
213
|
+
let c = ctx_from cl a0 in
|
|
214
|
+
let msg = (match getprop_raw_pub a1 "message" with Str m -> m | _ -> "") in
|
|
215
|
+
let e = { err_code = ""; err_msg = msg; err_result = Noval; err_spec = Noval } in
|
|
216
|
+
let out = make_error_util c (if msg = "" then None else Some e) in
|
|
217
|
+
publish a0 c;
|
|
218
|
+
out);
|
|
219
|
+
|
|
220
|
+
arg_section primary "operator" (fun args ->
|
|
221
|
+
let inv = ctx_arg args in
|
|
222
|
+
let op = new_operation inv in
|
|
223
|
+
jo [("entity", Str op.op_entity); ("input", Str op.op_input);
|
|
224
|
+
("name", Str op.op_name); ("points", op.op_points)]);
|
|
225
|
+
|
|
226
|
+
arg_section primary "param" (fun args ->
|
|
227
|
+
let a0 = ctx_arg args in
|
|
228
|
+
let a1 = (match args with _ :: y :: _ -> y | _ -> Noval) in
|
|
229
|
+
let c = ctx_from cl a0 in
|
|
230
|
+
let out = param_util c a1 in
|
|
231
|
+
publish a0 c;
|
|
232
|
+
out);
|
|
233
|
+
|
|
234
|
+
List.iter print_endline (List.rev !failures);
|
|
235
|
+
Printf.printf "\nPRIMARY CORPUS: PASS %d FAIL %d\n" !npass !nfail;
|
|
236
|
+
(* A run that executes nothing is not a pass. *)
|
|
237
|
+
if !npass = 0 then (print_endline "the primary corpus executed no cases"; exit 1);
|
|
238
|
+
if !nfail > 0 then exit 1
|