json-model-compiler 2.0b0__py3-none-any.whl

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 (51) hide show
  1. json_model/README.md +32 -0
  2. json_model/__init__.py +18 -0
  3. json_model/analyze.py +409 -0
  4. json_model/clang.py +563 -0
  5. json_model/convert.py +430 -0
  6. json_model/data/clang_entry.c +21 -0
  7. json_model/data/clang_free.c +10 -0
  8. json_model/data/clang_init.c +11 -0
  9. json_model/data/clang_pcre2_fun.c +6 -0
  10. json_model/data/clang_re2_fun.c +5 -0
  11. json_model/data/javascript_exe.js +4 -0
  12. json_model/data/javascript_foot.js +9 -0
  13. json_model/data/javascript_free.js +9 -0
  14. json_model/data/javascript_head.js +1 -0
  15. json_model/data/javascript_init.js +11 -0
  16. json_model/data/javascript_main.js +5 -0
  17. json_model/data/plpgsql_entry.sql +16 -0
  18. json_model/data/python_entry.py +11 -0
  19. json_model/data/python_exe.py +1 -0
  20. json_model/data/python_free.py +6 -0
  21. json_model/data/python_init.py +9 -0
  22. json_model/data/python_main.py +4 -0
  23. json_model/javascript.py +310 -0
  24. json_model/language.py +748 -0
  25. json_model/model.py +1084 -0
  26. json_model/mtypes.py +42 -0
  27. json_model/objmerge.py +165 -0
  28. json_model/optim.py +486 -0
  29. json_model/plpgsql.py +422 -0
  30. json_model/python.py +269 -0
  31. json_model/recurse.py +127 -0
  32. json_model/resolver.py +106 -0
  33. json_model/runtime/META.json +30 -0
  34. json_model/runtime/Makefile +21 -0
  35. json_model/runtime/__init__.py +32 -0
  36. json_model/runtime/json-model.c +1054 -0
  37. json_model/runtime/json-model.h +183 -0
  38. json_model/runtime/json_model--2.0.sql +261 -0
  39. json_model/runtime/json_model.control +2 -0
  40. json_model/runtime/main.c +473 -0
  41. json_model/runtime/support.py +485 -0
  42. json_model/runtime/types.py +16 -0
  43. json_model/script.py +585 -0
  44. json_model/url_cache.py +82 -0
  45. json_model/utils.py +623 -0
  46. json_model/xstatic.py +1444 -0
  47. json_model_compiler-2.0b0.dist-info/METADATA +201 -0
  48. json_model_compiler-2.0b0.dist-info/RECORD +51 -0
  49. json_model_compiler-2.0b0.dist-info/WHEEL +5 -0
  50. json_model_compiler-2.0b0.dist-info/entry_points.txt +2 -0
  51. json_model_compiler-2.0b0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,473 @@
1
+ #include <stdio.h>
2
+ #include <unistd.h>
3
+ #include <assert.h>
4
+ #include <time.h>
5
+ #include <getopt.h>
6
+ #include <math.h>
7
+ #include <errno.h>
8
+ #include <string.h>
9
+
10
+ #include <json-model.h>
11
+
12
+ typedef enum {
13
+ expect_nothing,
14
+ expect_anything,
15
+ expect_fail,
16
+ expect_pass
17
+ } process_mode_t;
18
+
19
+ // return cputime µs
20
+ static double now(void)
21
+ {
22
+ struct timespec ts;
23
+ if (clock_gettime(CLOCK_REALTIME, &ts))
24
+ {
25
+ fprintf(stderr, "cannot get time (%d): %s\n", errno, strerror(errno));
26
+ exit(2);
27
+ }
28
+ return 1000000.0 * ts.tv_sec + 0.001 * ts.tv_nsec;
29
+ }
30
+
31
+ // check value and report if there was some error wrt expectations
32
+ static bool
33
+ process_value(const char *name, const json_t * value,
34
+ const char *fname, size_t index, process_mode_t mode, bool report, int loop)
35
+ {
36
+ // get checker function
37
+ const jm_check_fun_t checker = CHECK_fun(name);
38
+ if (checker == NULL)
39
+ {
40
+ fprintf(stderr, "check function not found for %s\n", name);
41
+ return true;
42
+ }
43
+
44
+ // check value against model, fast (no path nor reasons)
45
+ bool valid = true;
46
+ double empty = 0.0;
47
+
48
+ // performance loop in µs
49
+ if (loop > 1)
50
+ {
51
+ // overhead estimation
52
+ for (int i = loop; i; i--)
53
+ {
54
+ double start = now();
55
+ empty += now() - start;
56
+ }
57
+ empty /= loop;
58
+
59
+ // validation estimation
60
+ double sum = 0.0, sum2 = 0.0;
61
+
62
+ for (int i = loop; i; i--)
63
+ {
64
+ double start = now();
65
+
66
+ valid = checker(value, NULL, NULL);
67
+
68
+ double delay = now() - start - empty;
69
+ sum += delay;
70
+ sum2 += delay * delay;
71
+ }
72
+
73
+ double avg = sum / loop;
74
+ double stdev = sqrt(sum2 / loop - avg * avg);
75
+
76
+ fprintf(stderr, "%s.%s[%zu] nop %s %.03f ± %.03f µs/check (%.03f)\n",
77
+ fname, name, index, valid ? "PASS": "FAIL", avg, stdev, empty);
78
+ }
79
+
80
+ // get result
81
+ valid = checker(value, NULL, NULL);
82
+ bool unexpected = (mode == expect_fail && valid) || (mode == expect_pass && !valid);
83
+
84
+ if (mode == expect_nothing) // just show, no index
85
+ fprintf(stdout, "%s: %s", fname, valid ? "PASS" : "FAIL");
86
+ else if (mode == expect_anything) // just show, including index
87
+ fprintf(stdout, "%s[%zu]: %s", fname, index, valid ? "PASS" : "FAIL");
88
+ else if (unexpected)
89
+ fprintf(stdout, "%s[%zu]: ERROR unexpected %s", fname, index, valid ? "PASS" : "FAIL");
90
+ else // as expected
91
+ fprintf(stdout, "%s[%zu]: %s", fname, index, valid ? "PASS" : "FAIL");
92
+
93
+ // maybe second pass with report collection
94
+ if ((loop > 1 || !valid) && report)
95
+ {
96
+ jm_path_t path = (jm_path_t) { "", 0, NULL, NULL };
97
+ jm_report_t report = (jm_report_t) { NULL };
98
+
99
+ bool valid2 = true;
100
+
101
+ // loop just for measuring performance, including cleanup
102
+ if (loop > 1)
103
+ {
104
+ double sum = 0.0, sum2 = 0.0;
105
+
106
+ for (int i = loop; i; i--)
107
+ {
108
+ double start = now();
109
+
110
+ path = (jm_path_t) { "", 0, NULL, NULL };
111
+ report = (jm_report_t) { NULL };
112
+ valid2 = checker(value, &path, &report);
113
+ jm_report_free_entries(&report);
114
+
115
+ double delay = now() - start - empty;
116
+ sum += delay;
117
+ sum2 += delay * delay;
118
+ }
119
+
120
+ double avg = sum / loop;
121
+ double stdev = sqrt(sum2 / loop - avg * avg);
122
+
123
+ // FIXME could drop name and index in some cases
124
+ fprintf(stderr, "%s.%s[%zu] rep %s %.03f ± %.03f µs/check (%.03f)\n",
125
+ fname, name, index, valid ? "PASS": "FAIL", avg, stdev, empty);
126
+ }
127
+
128
+ // get the result for display
129
+ path = (jm_path_t) { "", 0, NULL, NULL };
130
+ report = (jm_report_t) { NULL };
131
+ valid2 = checker(value, &path, &report);
132
+
133
+ assert(valid == valid2);
134
+
135
+ if (report.entry)
136
+ {
137
+ bool first = true;
138
+ fprintf(stdout, " (");
139
+
140
+ for (jm_report_entry_t *entry = report.entry; entry != NULL; entry = entry->prev)
141
+ {
142
+ if (first)
143
+ first = false;
144
+ else
145
+ fprintf(stdout, "; ");
146
+ fprintf(stdout, "%s: %s", entry->path, entry->message);
147
+ }
148
+
149
+ fprintf(stdout, ")");
150
+ }
151
+
152
+ jm_report_free_entries(&report);
153
+ }
154
+
155
+ fprintf(stdout, "\n");
156
+
157
+ return !unexpected;
158
+ }
159
+
160
+ #if defined(JSONSCHEMA_BENCHMARK)
161
+ // for https://github.com/sourcemeta-research/jsonschema-benchmark
162
+ static void jsonschema_benchmark_run(int argc, char *argv[], const char *name, int loop)
163
+ {
164
+ const jm_check_fun_t checker = CHECK_fun(name);
165
+ if (checker == NULL)
166
+ {
167
+ fprintf(stderr, "no validation function found for %s\n", name);
168
+ exit(2);
169
+ }
170
+
171
+ size_t size = 1024;
172
+ int nvalues = 0;
173
+ json_t **values = (json_t **) malloc(sizeof(json_t *) * size);
174
+
175
+ // load jsonl
176
+ for (int i = optind; i < argc; i++)
177
+ {
178
+ FILE *input = fopen(argv[i], "r");
179
+
180
+ if (input == NULL)
181
+ {
182
+ fprintf(stderr, "%s: ERROR while opening file\n", argv[i]);
183
+ exit(3);
184
+ }
185
+
186
+ json_error_t error;
187
+ json_t *value;
188
+ while ((value = json_loadf(input,
189
+ JSON_DISABLE_EOF_CHECK|JSON_DECODE_ANY|JSON_ALLOW_NUL,
190
+ &error)))
191
+ {
192
+ if (nvalues == size) {
193
+ size *= 2;
194
+ values = (json_t **) realloc(values, sizeof(json_t *) * size);
195
+ }
196
+ values[nvalues++] = value;
197
+ }
198
+ }
199
+
200
+ // evaluate empty run
201
+ int count = 0;
202
+ double empty_start = now();
203
+ for (int i = 0; i < nvalues; i++)
204
+ if (values[i] != NULL)
205
+ count++;
206
+ double empty_delay = now() - empty_start;
207
+
208
+ // run once in for counting
209
+ int npass = 0, nfail = 0;
210
+ double cold_start = now();
211
+ for (int i = 0; i < nvalues; i++)
212
+ if (checker(values[i], NULL, NULL))
213
+ npass++;
214
+ else
215
+ nfail++;
216
+ double cold_delay = now() - cold_start;
217
+
218
+ // collect performance data over a loop
219
+ double sum = 0.0, sum2 = 0.0;
220
+ for (int n = loop; n; n--)
221
+ {
222
+ double start = now();
223
+ for (int i = 0; i < nvalues; i++)
224
+ checker(values[i], NULL, NULL);
225
+ double delay = now() - start;
226
+ sum += delay;
227
+ sum2 += delay * delay;
228
+ }
229
+
230
+ double avg = sum / loop;
231
+ double stdev = sqrt(sum2 / loop - avg * avg);
232
+
233
+ // report
234
+ fprintf(stderr, "C validation: pass=%d fail=%d %.03f ± %.03f µs [%.03f]\n",
235
+ npass, nfail, avg, stdev, empty_delay);
236
+ fprintf(stdout, "%lld,%lld\n",
237
+ (long long int) (1000 * cold_delay), (long long int) (1000 * avg));
238
+
239
+ exit(nfail ? 1 : 0);
240
+ }
241
+ #endif // JSONSCHEMA_BENCHMARK
242
+
243
+ int main(int argc, char* argv[])
244
+ {
245
+ // get options
246
+ int opt;
247
+ char *name = "";
248
+ bool report = false;
249
+ bool test = false;
250
+ bool jsonl = false;
251
+ bool list = false;
252
+ bool jsonschema_benchmark = false;
253
+ int loop = 1;
254
+
255
+ const struct option options[] = {
256
+ { "help", no_argument, NULL, 'h' },
257
+ { "version", no_argument, NULL, 'v' },
258
+ { "list", no_argument, NULL, 'l' },
259
+ { "name", required_argument, NULL, 'n' },
260
+ { "report", no_argument, NULL, 'r' },
261
+ { "test", no_argument, NULL, 't' },
262
+ { "time", required_argument, NULL, 'T' },
263
+ { "fast", no_argument, NULL, 1000 },
264
+ { "slow", no_argument, NULL, 1001 },
265
+ { "jsonl", no_argument, NULL, 1002 },
266
+ { "jsonschema-benchmark", no_argument, NULL, 1003 },
267
+ { NULL, 0, NULL, 0 }
268
+ };
269
+
270
+ while ((opt = getopt_long(argc, argv, "hvln:rtT:", options, &optind)) != -1)
271
+ {
272
+ switch (opt) {
273
+ case 'h': // FIXME expand help!
274
+ fprintf(stdout,
275
+ "Usage: %s [-hvl] [-r] [-n name] [-t] [-T repeat] [files...]\n"
276
+ "Check JSON values validity against a JSON model\n"
277
+ "Generated by jmc (JSON Model Compiler) version %s\n"
278
+ "see https://github.com/clairey-zx81/json-model\n",
279
+ argv[0], jm_version_string);
280
+ return 0;
281
+ case 'l':
282
+ list = true;
283
+ break;
284
+ case 'v':
285
+ fprintf(stdout, "C from JSON Model compiler version %s\n", jm_version_string);
286
+ return 0;
287
+ case 'n':
288
+ name = optarg;
289
+ break;
290
+ case 'r':
291
+ report = true;
292
+ break;
293
+ case 't':
294
+ test = true;
295
+ break;
296
+ case 'T':
297
+ loop = atoi(optarg);
298
+ break;
299
+ case 1000:
300
+ jm_is_valid_regex = jm_is_valid_regex_fast;
301
+ jm_is_valid_date = jm_is_valid_date_fast;
302
+ break;
303
+ case 1001:
304
+ jm_is_valid_regex = jm_is_valid_regex_slow;
305
+ jm_is_valid_date = jm_is_valid_date_slow;
306
+ break;
307
+ case 1002:
308
+ jsonl = true;
309
+ break;
310
+ case 1003:
311
+ jsonl = true;
312
+ jsonschema_benchmark = true;
313
+ break;
314
+ case '?':
315
+ default:
316
+ fprintf(stdout, "unexpected option encountered\n");
317
+ return 1;
318
+ }
319
+ }
320
+
321
+ // option consistency
322
+ if (jsonl && test)
323
+ {
324
+ fprintf(stderr, "Value input format cannot be both jsonl and test\n");
325
+ return 1;
326
+ }
327
+
328
+ // initialization
329
+ const char *error;
330
+ if ((error = CHECK_init()) != NULL)
331
+ {
332
+ fprintf(stderr, "JSON Model initialization error: %s\n", error);
333
+ return 2;
334
+ }
335
+
336
+ // delayed after the initialization
337
+ if (list)
338
+ {
339
+ fprintf(stdout, "JSON Model names (empty for default):");
340
+ for (int i = 0; i < CHECK_map_size; i++)
341
+ fprintf(stdout, "%s ", CHECK_map_tab[i].name);
342
+ fprintf(stdout, "\n");
343
+ return 0;
344
+ }
345
+
346
+ int errors = 0;
347
+
348
+ if (jsonschema_benchmark)
349
+ {
350
+ #if defined(JSONSCHEMA_BENCHMARK)
351
+ jsonschema_benchmark_run(argc, argv, name, loop);
352
+ #else
353
+ fprintf(stderr,
354
+ "jsonschema benchmark unavailable, recompile with -D JSONSCHEMA_BENCHMARK\n");
355
+ return 3;
356
+ #endif // JSONSCHEMA_BENCHMARK
357
+ }
358
+
359
+ for (int i = optind; i < argc; i++)
360
+ {
361
+ json_error_t error;
362
+ json_t *value;
363
+
364
+ if (jsonl)
365
+ {
366
+ FILE *input = fopen(argv[i], "r");
367
+ int count = 1;
368
+
369
+ if (input == NULL)
370
+ {
371
+ fprintf(stderr, "%s: ERROR while opening file\n", argv[i]);
372
+ errors++;
373
+ continue;
374
+ }
375
+
376
+ while ((value = json_loadf(input,
377
+ JSON_DISABLE_EOF_CHECK|JSON_DECODE_ANY|JSON_ALLOW_NUL,
378
+ &error)))
379
+ {
380
+ if (!process_value(name, value, argv[i], count, expect_anything, report, loop))
381
+ errors++;
382
+ count++;
383
+ json_decref(value);
384
+ }
385
+
386
+ if (value == NULL) // if not EOF, it is probably an error
387
+ {
388
+ unsigned char c;
389
+ if (fread(&c, 1, 1, input) != 0)
390
+ {
391
+ fprintf(stdout, "%s: ERROR (%s at %d:%d)\n",
392
+ argv[i], error.text, error.line, error.column);
393
+ errors++;
394
+ continue;
395
+ }
396
+ }
397
+ }
398
+ else
399
+ {
400
+ // read and parse JSON file
401
+ json_t *value = json_load_file(argv[i], JSON_DECODE_ANY|JSON_ALLOW_NUL, &error);
402
+ if (value == NULL)
403
+ {
404
+ fprintf(stdout, "%s: ERROR (%s at %d:%d)\n",
405
+ argv[i], error.text, error.line, error.column);
406
+ errors++;
407
+ continue;
408
+ }
409
+
410
+ if (test)
411
+ {
412
+ if (!json_is_array(value))
413
+ {
414
+ fprintf(stdout, "%s: ERROR not a JSON array in test mode\n", argv[i]);
415
+ errors++;
416
+ continue;
417
+ }
418
+
419
+ size_t _, index = 0;
420
+ json_t *val;
421
+ json_array_foreach(value, _, val)
422
+ {
423
+ if (json_is_string(val))
424
+ // skip string comments
425
+ continue;
426
+
427
+ // else must be an array (size is 0 for non-arrays)
428
+ size_t size = json_array_size(val);
429
+ if (!(size == 2 || size == 3))
430
+ {
431
+ fprintf(stdout,
432
+ "%s[%zu]: ERROR not a 2 or 3-tuple\n",
433
+ argv[i], index);
434
+ errors++;
435
+ continue;
436
+ }
437
+
438
+ const json_t
439
+ *first = json_array_get(val, 0),
440
+ *second = json_array_get(val, 1);
441
+
442
+ if (!json_is_boolean(first) || (size == 3 && !json_is_string(second)))
443
+ {
444
+ fprintf(stdout,
445
+ "%s[%zu]: ERROR first element not boolean or second not string\n",
446
+ argv[i], index);
447
+ errors++;
448
+ continue;
449
+ }
450
+
451
+ const char *tname = size == 3 ? json_string_value(second) : name;
452
+ const json_t *value = size == 3 ? json_array_get(val, 2) : second;
453
+ process_mode_t mode = json_is_true(first) ? expect_pass : expect_fail;
454
+
455
+ if (!process_value(tname, value, argv[i], index, mode, report, loop))
456
+ errors++;
457
+
458
+ index++;
459
+ }
460
+ }
461
+ else
462
+ if (!process_value(name, value, argv[i], 0, expect_nothing, report, loop))
463
+ errors++;
464
+
465
+ // free json value
466
+ json_decref(value);
467
+ }
468
+ }
469
+
470
+ CHECK_free();
471
+
472
+ return errors? 4: 0;
473
+ }