@voxgig/sdkgen 4.39.1 → 4.40.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.
Files changed (86) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/dist/helpers/testPolicy.d.ts +8 -2
  3. package/dist/helpers/testPolicy.js +18 -0
  4. package/dist/helpers/testPolicy.js.map +1 -1
  5. package/dist/sdkgen.d.ts +4 -4
  6. package/dist/sdkgen.js +4 -3
  7. package/dist/sdkgen.js.map +1 -1
  8. package/dist/tsconfig.tsbuildinfo +1 -1
  9. package/package.json +1 -1
  10. package/project/.sdk/model/target/rust.aontu +1 -0
  11. package/project/.sdk/src/cmp/c/fragment/Main.fragment.c +15 -0
  12. package/project/.sdk/src/cmp/cpp/TestDirect_cpp.ts +2 -0
  13. package/project/.sdk/src/cmp/csharp/TestDirect_csharp.ts +20 -3
  14. package/project/.sdk/src/cmp/csharp/TestEntity_csharp.ts +43 -15
  15. package/project/.sdk/src/cmp/csharp/fragment/Main.fragment.cs +17 -0
  16. package/project/.sdk/src/cmp/elixir/fragment/Main.fragment.ex +10 -0
  17. package/project/.sdk/src/cmp/go/TestDirect_go.ts +23 -4
  18. package/project/.sdk/src/cmp/go/TestEntity_go.ts +37 -12
  19. package/project/.sdk/src/cmp/go/fragment/Main.fragment.go +14 -0
  20. package/project/.sdk/src/cmp/java/TestDirect_java.ts +18 -3
  21. package/project/.sdk/src/cmp/java/TestEntity_java.ts +39 -14
  22. package/project/.sdk/src/cmp/js/TestDirect_js.ts +7 -1
  23. package/project/.sdk/src/cmp/js/fragment/Main.fragment.js +7 -0
  24. package/project/.sdk/src/cmp/kotlin/TestDirect_kotlin.ts +18 -3
  25. package/project/.sdk/src/cmp/kotlin/TestEntity_kotlin.ts +37 -14
  26. package/project/.sdk/src/cmp/lua/TestDirect_lua.ts +13 -3
  27. package/project/.sdk/src/cmp/lua/TestEntity_lua.ts +36 -10
  28. package/project/.sdk/src/cmp/lua/fragment/Main.fragment.lua +14 -0
  29. package/project/.sdk/src/cmp/ocaml/TestDirect_ocaml.ts +3 -1
  30. package/project/.sdk/src/cmp/perl/TestDirect_perl.ts +9 -3
  31. package/project/.sdk/src/cmp/perl/TestEntity_perl.ts +22 -8
  32. package/project/.sdk/src/cmp/perl/fragment/Main.fragment.pm +6 -0
  33. package/project/.sdk/src/cmp/php/TestDirect_php.ts +13 -3
  34. package/project/.sdk/src/cmp/php/TestEntity_php.ts +31 -9
  35. package/project/.sdk/src/cmp/php/fragment/Main.fragment.php +14 -0
  36. package/project/.sdk/src/cmp/py/TestDirect_py.ts +11 -3
  37. package/project/.sdk/src/cmp/py/TestEntity_py.ts +30 -9
  38. package/project/.sdk/src/cmp/py/fragment/Main.fragment.py +6 -0
  39. package/project/.sdk/src/cmp/rb/TestDirect_rb.ts +9 -3
  40. package/project/.sdk/src/cmp/rb/TestEntity_rb.ts +25 -10
  41. package/project/.sdk/src/cmp/rb/fragment/Main.fragment.rb +7 -0
  42. package/project/.sdk/src/cmp/rust/TestDirect_rust.ts +13 -3
  43. package/project/.sdk/src/cmp/rust/TestEntity_rust.ts +48 -27
  44. package/project/.sdk/src/cmp/rust/fragment/Main.fragment.rs +15 -0
  45. package/project/.sdk/src/cmp/swift/fragment/Main.fragment.swift +7 -0
  46. package/project/.sdk/src/cmp/ts/TestDirect_ts.ts +7 -1
  47. package/project/.sdk/src/cmp/ts/TestEntity_ts.ts +8 -2
  48. package/project/.sdk/src/cmp/ts/fragment/Main.fragment.ts +7 -0
  49. package/project/.sdk/src/cmp/zig/TestDirect_zig.ts +1 -0
  50. package/project/.sdk/src/cmp/zig/fragment/Main.fragment.zig +16 -0
  51. package/project/.sdk/tm/c/utility/http.c +256 -0
  52. package/project/.sdk/tm/clojure/src/sdk/client.clj +7 -0
  53. package/project/.sdk/tm/clojure/src/sdk/core.clj +24 -1
  54. package/project/.sdk/tm/cpp/core/types.hpp +14 -0
  55. package/project/.sdk/tm/csharp/test/Runner.cs +49 -3
  56. package/project/.sdk/tm/csharp/utility/Fetcher.cs +5 -1
  57. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +54 -19
  58. package/project/.sdk/tm/go/test/testsupport_test.go +44 -3
  59. package/project/.sdk/tm/go/utility/fetcher.go +40 -0
  60. package/project/.sdk/tm/java/core/SdkClient.java +7 -0
  61. package/project/.sdk/tm/java/test/RunnerSupport.java +45 -2
  62. package/project/.sdk/tm/java/utility/Fetcher.java +67 -1
  63. package/project/.sdk/tm/js/src/utility/MakeResultUtility.js +3 -1
  64. package/project/.sdk/tm/js/test/live-entity.js +7 -3
  65. package/project/.sdk/tm/kotlin/core/SdkClient.kt +7 -0
  66. package/project/.sdk/tm/kotlin/test/RunnerSupport.kt +40 -3
  67. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +52 -1
  68. package/project/.sdk/tm/lua/test/runner.lua +49 -2
  69. package/project/.sdk/tm/lua/utility/fetcher.lua +228 -0
  70. package/project/.sdk/tm/ocaml/sdk_features.ml +8 -0
  71. package/project/.sdk/tm/perl/t/runner.pm +18 -3
  72. package/project/.sdk/tm/perl/utility/fetcher.pm +42 -3
  73. package/project/.sdk/tm/php/test/Runner.php +43 -5
  74. package/project/.sdk/tm/php/utility/Fetcher.php +36 -0
  75. package/project/.sdk/tm/php/utility/MakeResult.php +3 -1
  76. package/project/.sdk/tm/py/test/runner.py +32 -3
  77. package/project/.sdk/tm/rb/test/runner.rb +24 -1
  78. package/project/.sdk/tm/rust/tests/common/mod.rs +53 -5
  79. package/project/.sdk/tm/rust/utility/fetcher.rs +43 -3
  80. package/project/.sdk/tm/scala/core/SdkClient.scala +7 -0
  81. package/project/.sdk/tm/scala/utility/Fetcher.scala +48 -1
  82. package/project/.sdk/tm/ts/src/utility/MakeResultUtility.ts +3 -1
  83. package/project/.sdk/tm/ts/test/live-entity.ts +8 -3
  84. package/project/sdkgen-package.json +1 -1
  85. package/src/helpers/testPolicy.ts +23 -2
  86. package/src/sdkgen.ts +4 -2
@@ -129,9 +129,18 @@ fn default_http_fetch(fullurl: &str, fetchdef: &Value) -> Result<Value, ProjectN
129
129
  }
130
130
  }
131
131
 
132
- let body_txt = resp
133
- .into_string()
134
- .map_err(|e| ProjectNameError::new("fetch_body", &format!("{}", e)))?;
132
+ // ureq decodes gzip and no deflate, which a server may send whatever the
133
+ // request asked for.
134
+ let deflate = resp
135
+ .header("content-encoding")
136
+ .map(|v| v.trim().eq_ignore_ascii_case("deflate"))
137
+ .unwrap_or(false);
138
+ let body_txt = if deflate {
139
+ read_deflate(resp.into_reader())?
140
+ } else {
141
+ resp.into_string()
142
+ .map_err(|e| ProjectNameError::new("fetch_body", &format!("{}", e)))?
143
+ };
135
144
 
136
145
  let parsed = if body_txt.trim().is_empty() {
137
146
  Ok(Value::Noval)
@@ -151,6 +160,37 @@ fn default_http_fetch(fullurl: &str, fetchdef: &Value) -> Result<Value, ProjectN
151
160
  ]))
152
161
  }
153
162
 
163
+ // A deflate body as text, bounded as ureq's into_string bounds one. Deflate is
164
+ // zlib-wrapped, or raw as some servers send it.
165
+ fn read_deflate(reader: impl std::io::Read) -> Result<String, ProjectNameError> {
166
+ use std::io::Read;
167
+ const LIMIT: usize = 10 * 1_024 * 1_024;
168
+ let fail = |e: std::io::Error| {
169
+ ProjectNameError::new("fetch_body", &format!("response body is not valid deflate data: {}", e))
170
+ };
171
+ let mut raw: Vec<u8> = Vec::new();
172
+ reader.take((LIMIT + 1) as u64).read_to_end(&mut raw).map_err(fail)?;
173
+ let zlib = raw.len() > 1
174
+ && raw[0] & 0x0f == 8
175
+ && ((u16::from(raw[0]) << 8) | u16::from(raw[1])) % 31 == 0;
176
+ let mut out: Vec<u8> = Vec::new();
177
+ if zlib {
178
+ flate2::read::ZlibDecoder::new(&raw[..])
179
+ .take((LIMIT + 1) as u64)
180
+ .read_to_end(&mut out)
181
+ .map_err(fail)?;
182
+ } else {
183
+ flate2::read::DeflateDecoder::new(&raw[..])
184
+ .take((LIMIT + 1) as u64)
185
+ .read_to_end(&mut out)
186
+ .map_err(fail)?;
187
+ }
188
+ if out.len() > LIMIT {
189
+ return Err(ProjectNameError::new("fetch_body", "response too big for into_string"));
190
+ }
191
+ Ok(String::from_utf8_lossy(&out).to_string())
192
+ }
193
+
154
194
  pub fn fetcher_util(
155
195
  ctx: &Rc<Context>,
156
196
  fullurl: &str,
@@ -142,6 +142,13 @@ abstract class SdkClient(options0: JMap[String, Object]) {
142
142
  val uheaders = Helpers.toMapAny(Struct.getprop(fetchargs, "headers"))
143
143
  if (uheaders != null) ctx.spec.headers.putAll(uheaders)
144
144
 
145
+ // A raw request has no point to declare its response type, so it asks
146
+ // for the JSON it reads: an API that negotiates on Accept otherwise
147
+ // answers in its own default format. A caller's own accept wins.
148
+ if (!ctx.spec.headers.keySet().stream().anyMatch(k => "accept".equalsIgnoreCase(k))) {
149
+ ctx.spec.headers.put("accept", "application/json")
150
+ }
151
+
145
152
  utility.prepareAuth(ctx)
146
153
 
147
154
  utility.makeFetchDef(ctx)
@@ -1,7 +1,10 @@
1
1
  package SCALAPACKAGE.utility
2
2
 
3
+ import java.io.{ByteArrayInputStream, IOException, UncheckedIOException}
3
4
  import java.net.{InetSocketAddress, ProxySelector, URI}
4
5
  import java.net.http.{HttpClient, HttpRequest, HttpResponse}
6
+ import java.nio.charset.{Charset, StandardCharsets}
7
+ import java.util.zip.{GZIPInputStream, Inflater, InflaterInputStream}
5
8
  import java.util.{LinkedHashMap, List => JList, Map => JMap}
6
9
  import java.util.concurrent.ConcurrentHashMap
7
10
  import java.util.function.{BiFunction, Function => JFunction, Supplier}
@@ -99,7 +102,7 @@ object Fetcher {
99
102
  }
100
103
 
101
104
  val resp =
102
- try clientFor(fetchdef).send(reqb.build(), HttpResponse.BodyHandlers.ofString())
105
+ try clientFor(fetchdef).send(reqb.build(), decodedText())
103
106
  catch { case e: Exception => throw new RuntimeException("fetch: " + e.getMessage, e) }
104
107
 
105
108
  val headers = new LinkedHashMap[String, Object]()
@@ -129,6 +132,50 @@ object Fetcher {
129
132
  out
130
133
  }
131
134
 
135
+ // The body as text, as BodyHandlers.ofString reads it, once a gzip or
136
+ // deflate Content-Encoding is undone: HttpClient decodes none, and a server
137
+ // may compress its answer whatever the request asked for.
138
+ def decodedText(): HttpResponse.BodyHandler[String] = (info: HttpResponse.ResponseInfo) => {
139
+ val encoding = info.headers().firstValue("content-encoding").orElse("").trim.toLowerCase
140
+ val charset = charsetOf(info.headers().firstValue("content-type").orElse(""))
141
+ HttpResponse.BodySubscribers.mapping[Array[Byte], String](
142
+ HttpResponse.BodySubscribers.ofByteArray(),
143
+ (bytes: Array[Byte]) =>
144
+ try new String(decodeBody(bytes, encoding), charset)
145
+ catch {
146
+ case e: IOException =>
147
+ throw new UncheckedIOException(
148
+ "response body is not valid " + encoding + " data: " + e.getMessage, e)
149
+ })
150
+ }
151
+
152
+ // Deflate is zlib-wrapped, or raw as some servers send it.
153
+ def decodeBody(bytes: Array[Byte], encoding: String): Array[Byte] = {
154
+ if (bytes == null || bytes.isEmpty) return if (bytes == null) Array.emptyByteArray else bytes
155
+ if ("gzip" == encoding || "x-gzip" == encoding) {
156
+ val in = new GZIPInputStream(new ByteArrayInputStream(bytes))
157
+ try return in.readAllBytes() finally in.close()
158
+ }
159
+ if ("deflate" == encoding) {
160
+ val zlib = bytes.length > 1 && (bytes(0) & 0x0f) == 8 &&
161
+ (((bytes(0) & 0xff) << 8) | (bytes(1) & 0xff)) % 31 == 0
162
+ val in = new InflaterInputStream(new ByteArrayInputStream(bytes), new Inflater(!zlib))
163
+ try return in.readAllBytes() finally in.close()
164
+ }
165
+ bytes
166
+ }
167
+
168
+ def charsetOf(contentType: String): Charset = {
169
+ for (part <- contentType.split(";")) {
170
+ val kv = part.trim.split("=", 2)
171
+ if (kv.length == 2 && "charset".equalsIgnoreCase(kv(0).trim)) {
172
+ return try Charset.forName(kv(1).trim.replace("\"", ""))
173
+ catch { case _: RuntimeException => StandardCharsets.UTF_8 }
174
+ }
175
+ }
176
+ StandardCharsets.UTF_8
177
+ }
178
+
132
179
  def statusText(status: Int): String = status match {
133
180
  case 200 => "OK"
134
181
  case 201 => "Created"
@@ -33,7 +33,9 @@ function makeResult(ctx: Context): Result | Error {
33
33
  const resdata = result.resdata
34
34
  result.resdata = []
35
35
 
36
- if (null != resdata && 0 < resdata.length) {
36
+ // Only a list holds records: a string has a length too, and an object
37
+ // (a page envelope the transform did not unwrap) holds none.
38
+ if (Array.isArray(resdata) && 0 < resdata.length) {
37
39
  for (let entry of resdata) {
38
40
  const ent = entity.make()
39
41
  ent.data(entry)
@@ -8,6 +8,9 @@ const routeOf = (point: any) => String(point.m).toUpperCase() + ' ' + point.o
8
8
 
9
9
  const scalarOf = (value: any) => ['string', 'number', 'boolean'].includes(typeof value) ? value : undefined
10
10
 
11
+ // A route parameter's value is a string where the record may hold a number.
12
+ const sameId = (a: any, b: any) => null != a && null != b && String(a) === String(b)
13
+
11
14
  // The offline flow keeps its deterministic fixture assertions. Live flows
12
15
  // resolve real prerequisites per operation and collect failures until done.
13
16
  // `excluded` names, by step id, the steps this run leaves out, and why.
@@ -27,6 +30,8 @@ async function runLiveEntity(
27
30
  ...Object.values(entity.fields || {}).map((field: any) => field?.n).filter(Boolean)])
28
31
  const marks = new Map<string, { name: string, value: any }>()
29
32
  const idField = entity.id?.field || 'id'
33
+ const declared = new Set(Object.values(entity.fields || {}).map((field: any) => field?.n))
34
+ const repeatsId = null != entity.id && (declared.has(idField) || declared.has('id'))
30
35
  const copy = (value: any) => JSON.parse(JSON.stringify(value ?? {}))
31
36
  const hasCreate = steps.some(step => step.o === 'create')
32
37
  const report = await runLiveSteps(steps.map((step, index) => {
@@ -146,7 +151,7 @@ async function runLiveEntity(
146
151
  const previous = created.get(validation.def?.ref)
147
152
  const id = previous?.[idField] ?? previous?.id
148
153
  if (undefined === id) continue
149
- const found = data.some((item: any) => (item?.[idField] ?? item?.id) === id)
154
+ const found = data.some((item: any) => sameId(item?.[idField] ?? item?.id, id))
150
155
  if (validation.apply === 'ItemExists') assert(found, 'Created resource missing from list')
151
156
  if (validation.apply === 'ItemNotExists') assert(!found, 'Removed resource still in list')
152
157
  }
@@ -160,8 +165,8 @@ async function runLiveEntity(
160
165
  if (op === 'create') created.set(ref, data)
161
166
  context.publish(data)
162
167
  if (op === 'create' && entity.id) assert(null != (data[idField] ?? data.id), 'Missing created identity')
163
- if (['load', 'update'].includes(op) && input.id != null && entity.id) {
164
- assert.equal(data[idField] ?? data.id, input.id, 'Response identity mismatch')
168
+ if (['load', 'update'].includes(op) && input.id != null && repeatsId) {
169
+ assert.equal(String(data[idField] ?? data.id), String(input.id), 'Response identity mismatch')
165
170
  }
166
171
  const mark = marks.get(ref)
167
172
  if (mark && ['update', 'load'].includes(op)) {
@@ -3,7 +3,7 @@
3
3
  "package": 1
4
4
  },
5
5
  "name": "@voxgig/sdkgen",
6
- "version": "4.39.1",
6
+ "version": "4.40.0",
7
7
  "provides": {
8
8
  "target": [
9
9
  "c",
@@ -4,6 +4,7 @@ import { flowSteps } from './buildIdNames'
4
4
  import { primaryPoint } from './graphqlPoint'
5
5
  import { getMatchEntries } from './getMatchEntries'
6
6
  import { pointFacts } from './resolved'
7
+ import { entityRecordIdField } from './opShape'
7
8
 
8
9
 
9
10
  function liveStrict(model: any, target?: string): boolean {
@@ -215,6 +216,26 @@ function liveRecordFound(entity: any, flow: any): boolean {
215
216
  }
216
217
 
217
218
 
219
+ type LiveIdentity = { field: string, key: string, ex?: Scalar }
220
+
221
+
222
+ // Where a create-less flow's load reads the identity of the record it finds,
223
+ // and what a record lacking one takes instead: the *_ENTID value under `key`,
224
+ // else `ex`, the example of the id parameter of the load's route.
225
+ function liveIdentity(entity: any, flow: any): LiveIdentity | undefined {
226
+ const load = flowSteps(flow).find((step: any) => 'load' === step.o)
227
+ if (null == load || null == entity?.id) return undefined
228
+ const hasId = (point: any) => (point.g?.params || []).some((arg: any) => 'id' === arg.n)
229
+ const point = flowLoadRoute(entity, load)?.point ??
230
+ plainPoints(entity, 'load').find((point) => !isAction(point) && hasId(point))
231
+ const ex = scalarExample((point?.g?.params || []).find((arg: any) => 'id' === arg.n))
232
+ return {
233
+ field: entityRecordIdField(entity), key: loadIdKey(entity, load),
234
+ ...(undefined === ex ? {} : { ex }),
235
+ }
236
+ }
237
+
238
+
218
239
  // The idmap key a live load of no known record reads its id from.
219
240
  function loadIdKey(entity: any, step: any): string {
220
241
  const binding = getMatchEntries(step).find(([name]) => 'id' === name)?.[1]
@@ -517,10 +538,10 @@ function liveExcludedSummary(plan: { excluded: LiveExcluded[] }): string {
517
538
  }
518
539
 
519
540
 
520
- export type { LiveFlowNeeds, LiveExcluded, LiveGetOnly, LiveGate, LiveKeyExcluded, FlowRouteArg, FlowLoadRoute }
541
+ export type { LiveFlowNeeds, LiveIdentity, LiveExcluded, LiveGetOnly, LiveGate, LiveKeyExcluded, FlowRouteArg, FlowLoadRoute }
521
542
 
522
543
  export {
523
- liveStrict, liveStrictNote, liveFlowNeeds,
544
+ liveStrict, liveStrictNote, liveFlowNeeds, liveIdentity,
524
545
  liveIdKey, liveExampleIds, liveQueryExamples, liveRouteExamples, liveStepExamples,
525
546
  flowLoadRoute, flowLoadReadsRecord, liveFlowRoutes,
526
547
  liveEntity, LIVE_GETONLY_ENV, liveFlowSteps, liveStepId, liveGetOnly, liveGetOnlyReason,
package/src/sdkgen.ts CHANGED
@@ -82,14 +82,14 @@ import {
82
82
  } from './helpers/opExample'
83
83
  import type { ExampleLang, LiteralLang } from './helpers/opExample'
84
84
  import {
85
- liveStrict, liveStrictNote, liveFlowNeeds,
85
+ liveStrict, liveStrictNote, liveFlowNeeds, liveIdentity,
86
86
  liveIdKey, liveExampleIds, liveQueryExamples, liveRouteExamples, liveStepExamples,
87
87
  flowLoadRoute, flowLoadReadsRecord, liveFlowRoutes,
88
88
  liveEntity, LIVE_GETONLY_ENV, liveFlowSteps, liveStepId, liveGetOnly, liveGetOnlyReason,
89
89
  liveFlowPlan, liveGates,
90
90
  liveDirectGetOnly, liveExcludedLines, liveExcludedSummary,
91
91
  } from './helpers/testPolicy'
92
- import type { LiveFlowNeeds, LiveExcluded, LiveGetOnly, LiveGate, LiveKeyExcluded, FlowRouteArg, FlowLoadRoute } from './helpers/testPolicy'
92
+ import type { LiveFlowNeeds, LiveIdentity, LiveExcluded, LiveGetOnly, LiveGate, LiveKeyExcluded, FlowRouteArg, FlowLoadRoute } from './helpers/testPolicy'
93
93
  import { pointSegments, pointParts, pointSlash, pointPlaceholders, pointTerminalParam, pointPathKey }
94
94
  from './helpers/pointPath'
95
95
  import { pointIsGraphql, ownPointFirst, primaryPoint, graphqlDirectSkip } from './helpers/graphqlPoint'
@@ -1003,6 +1003,7 @@ export type {
1003
1003
  McpTool,
1004
1004
  DepEntry,
1005
1005
  LiveFlowNeeds,
1006
+ LiveIdentity,
1006
1007
  LiveExcluded,
1007
1008
  LiveGetOnly,
1008
1009
  LiveGate,
@@ -1221,6 +1222,7 @@ export {
1221
1222
  liveStrict,
1222
1223
  liveStrictNote,
1223
1224
  liveFlowNeeds,
1225
+ liveIdentity,
1224
1226
  liveIdKey,
1225
1227
  liveExampleIds,
1226
1228
  liveQueryExamples,