teptris 0.3.19.0 → 0.3.20.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.
- checksums.yaml +4 -4
- data/Steepfile +1 -0
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/schema.h +107 -0
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.c +440 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.h +34 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.c +1090 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.h +25 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema.c +601 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema_internal.h +179 -0
- data/ext/teptris_ext/engine/src/teptris/schema/validate.c +863 -0
- data/ext/teptris_ext/materialize.c +104 -10
- data/lib/teptris/schema.rb +64 -0
- data/lib/teptris/version.rb +1 -1
- data/lib/teptris.rb +1 -0
- data/sig/teptris.rbs +32 -0
- metadata +10 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#include <ruby.h>
|
|
2
2
|
#include <ruby/thread.h>
|
|
3
3
|
#include <ruby/encoding.h>
|
|
4
|
+
#include "teptris/schema.h"
|
|
4
5
|
#include "teptris/teptris.h"
|
|
5
6
|
#include "teptris/plan.h"
|
|
6
7
|
|
|
@@ -9,7 +10,7 @@
|
|
|
9
10
|
#define FMT_FORBID_TIME 2
|
|
10
11
|
#define FMT_FORBID_DATE 4
|
|
11
12
|
|
|
12
|
-
static VALUE eParseError, eError, cDate, cDoc;
|
|
13
|
+
static VALUE eParseError, eError, cDate, cDoc, cSchema, gregorian_start;
|
|
13
14
|
/* rb_raise's varargs formatting segfaults in its format dispatch on
|
|
14
15
|
* windows x64-mingw-ucrt ruby 4.0 (diag #158/#160: the identical raise
|
|
15
16
|
* crashes across PGO on/off while direct exception construction runs
|
|
@@ -239,8 +240,9 @@ static VALUE obj_from_node(const teptris_node *n, unsigned flags,
|
|
|
239
240
|
if (flags & FMT_FORBID_DATE)
|
|
240
241
|
raise_teptris("value materializes Date, which is not in "
|
|
241
242
|
"permitted_classes (use datetime_policy: :string)");
|
|
242
|
-
return rb_funcall(cDate, rb_intern("new"),
|
|
243
|
-
INT2FIX(d.year), INT2FIX(d.month),
|
|
243
|
+
return rb_funcall(cDate, rb_intern("new"), 4,
|
|
244
|
+
INT2FIX(d.year), INT2FIX(d.month),
|
|
245
|
+
INT2FIX(d.day), gregorian_start);
|
|
244
246
|
}
|
|
245
247
|
return dt_string(&d, kind); /* local time: canonical string */
|
|
246
248
|
}}
|
|
@@ -477,10 +479,16 @@ static void dump_scalar(teptris_builder *b, const char *key, size_t klen,
|
|
|
477
479
|
}
|
|
478
480
|
if (rb_obj_is_kind_of(v, cDate)) {
|
|
479
481
|
/* DateTime is a Date subclass: dumps its date part, as the
|
|
480
|
-
* Ruby-era dump writers did.
|
|
482
|
+
* Ruby-era dump writers did. The civil fields come
|
|
483
|
+
* DIRECTLY off the Date: Date#jd is interpreted with the
|
|
484
|
+
* date's calendar reform (default ITALY), so the old
|
|
485
|
+
* jd -> civil crossing shifted pre-1582 dates by the
|
|
486
|
+
* reform offset (0001-01-01 roundtripped as 0000-12-30).
|
|
487
|
+
* year/month/day are exact under every reform. */
|
|
481
488
|
teptris_datetime dt = {0};
|
|
482
|
-
|
|
483
|
-
|
|
489
|
+
dt.year = (int32_t)NUM2LL(rb_funcall(v, rb_intern("year"), 0));
|
|
490
|
+
dt.month = (uint8_t)NUM2LL(rb_funcall(v, rb_intern("month"), 0));
|
|
491
|
+
dt.day = (uint8_t)NUM2LL(rb_funcall(v, rb_intern("day"), 0));
|
|
484
492
|
dump_check(teptris_builder_put_datetime(b, key, klen,
|
|
485
493
|
TEPTRIS_DATE_LOCAL, &dt));
|
|
486
494
|
return;
|
|
@@ -651,9 +659,9 @@ static VALUE asm_rows(const teptris_plan *p, const teptris_plan_result *res,
|
|
|
651
659
|
break;
|
|
652
660
|
case TEPTRIS_DATE_LOCAL:
|
|
653
661
|
if (teptris_plan_result_datetime_at(res, i, &d) == TEPTRIS_OK) {
|
|
654
|
-
val = rb_funcall(cDate, rb_intern("new"),
|
|
662
|
+
val = rb_funcall(cDate, rb_intern("new"), 4,
|
|
655
663
|
INT2FIX(d.year), INT2FIX(d.month),
|
|
656
|
-
INT2FIX(d.day));
|
|
664
|
+
INT2FIX(d.day), gregorian_start);
|
|
657
665
|
}
|
|
658
666
|
break;
|
|
659
667
|
case TEPTRIS_TIME_LOCAL:
|
|
@@ -722,10 +730,10 @@ static VALUE asm_rows(const teptris_plan *p, const teptris_plan_result *res,
|
|
|
722
730
|
val,
|
|
723
731
|
teptris_plan_result_array_value_kind_at(
|
|
724
732
|
res, i, j) == TEPTRIS_DATE_LOCAL
|
|
725
|
-
? rb_funcall(cDate, rb_intern("new"),
|
|
733
|
+
? rb_funcall(cDate, rb_intern("new"), 4,
|
|
726
734
|
INT2FIX(d.year),
|
|
727
735
|
INT2FIX(d.month),
|
|
728
|
-
INT2FIX(d.day))
|
|
736
|
+
INT2FIX(d.day), gregorian_start)
|
|
729
737
|
: dt_string(&d, TEPTRIS_TIME_LOCAL));
|
|
730
738
|
}
|
|
731
739
|
break;
|
|
@@ -1339,6 +1347,9 @@ static VALUE ext_load_batch(int argc, VALUE *argv, VALUE self) {
|
|
|
1339
1347
|
return out;
|
|
1340
1348
|
}
|
|
1341
1349
|
|
|
1350
|
+
static VALUE ext_schema_compile(VALUE self, VALUE str);
|
|
1351
|
+
static VALUE ext_schema_validate(VALUE self, VALUE rsch, VALUE str);
|
|
1352
|
+
|
|
1342
1353
|
void Init_teptris_ext(void) {
|
|
1343
1354
|
/* every module static is written once here and read-only after;
|
|
1344
1355
|
* call state (key cache, arenas, types) is per-object or per-call */
|
|
@@ -1355,6 +1366,10 @@ void Init_teptris_ext(void) {
|
|
|
1355
1366
|
rb_define_module_function(m, "load_batch", ext_load_batch, -1);
|
|
1356
1367
|
rb_define_module_function(m, "load_slice", ext_load_slice, -1);
|
|
1357
1368
|
rb_define_module_function(m, "load_lazy_batch", ext_load_lazy_batch, 1);
|
|
1369
|
+
cSchema = rb_define_class_under(m, "Schema", rb_cObject);
|
|
1370
|
+
rb_undef_alloc_func(cSchema);
|
|
1371
|
+
rb_define_module_function(m, "schema_compile", ext_schema_compile, 1);
|
|
1372
|
+
rb_define_module_function(m, "schema_validate", ext_schema_validate, 2);
|
|
1358
1373
|
cLazyOwner = rb_define_class_under(m, "LazyOwner", rb_cObject);
|
|
1359
1374
|
cLazyValue = rb_define_class_under(m, "LazyValue", rb_cObject);
|
|
1360
1375
|
rb_define_method(cLazyValue, "kind", lazy_kind, 0);
|
|
@@ -1379,6 +1394,7 @@ void Init_teptris_ext(void) {
|
|
|
1379
1394
|
eParseError = rb_const_get(t, rb_intern("ParseError"));
|
|
1380
1395
|
rb_funcall(rb_mKernel, rb_intern("require"), 1, rb_str_new_cstr("date"));
|
|
1381
1396
|
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
|
|
1397
|
+
gregorian_start = rb_const_get(cDate, rb_intern("GREGORIAN"));
|
|
1382
1398
|
id_utc_offset = rb_intern("utc_offset");
|
|
1383
1399
|
id_to_s = rb_intern("to_s");
|
|
1384
1400
|
id_to_i = rb_intern("to_i");
|
|
@@ -1397,3 +1413,81 @@ void Init_teptris_ext(void) {
|
|
|
1397
1413
|
rb_gc_register_address(&cLazyOwner);
|
|
1398
1414
|
rb_gc_register_address(&cLazyValue);
|
|
1399
1415
|
}
|
|
1416
|
+
|
|
1417
|
+
/* ---------------- schema validation (engine 0.3.20) ----------------
|
|
1418
|
+
* compile once, validate many: the compiled schema is an immutable,
|
|
1419
|
+
* arena-backed handle wrapped as TypedData (freed by GC). validate
|
|
1420
|
+
* parses via the same parse step as load (so ParseError carries
|
|
1421
|
+
* line/column) and hands back the engine's JSON array string
|
|
1422
|
+
* ("[]" = valid); the Ruby wrapper decodes it. */
|
|
1423
|
+
|
|
1424
|
+
typedef struct {
|
|
1425
|
+
teptris_schema *sch;
|
|
1426
|
+
} schema_owner;
|
|
1427
|
+
|
|
1428
|
+
static void schema_owner_free(void *p) {
|
|
1429
|
+
schema_owner *o = (schema_owner *)p;
|
|
1430
|
+
if (o->sch != NULL) teptris_schema_free(o->sch);
|
|
1431
|
+
xfree(o);
|
|
1432
|
+
}
|
|
1433
|
+
static size_t schema_owner_size(const void *p) { return sizeof(schema_owner); }
|
|
1434
|
+
static const rb_data_type_t schema_owner_type = {
|
|
1435
|
+
"TeptrisExt/Schema",
|
|
1436
|
+
{0, schema_owner_free, schema_owner_size, NULL, 0},
|
|
1437
|
+
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY};
|
|
1438
|
+
|
|
1439
|
+
static VALUE ext_schema_compile(VALUE self, VALUE str) {
|
|
1440
|
+
StringValue(str);
|
|
1441
|
+
long len = RSTRING_LEN(str);
|
|
1442
|
+
char *cbuf = ALLOC_N(char, (size_t)len + 1);
|
|
1443
|
+
memcpy(cbuf, RSTRING_PTR(str), (size_t)len);
|
|
1444
|
+
teptris_schema *sch = NULL;
|
|
1445
|
+
char *diag = NULL;
|
|
1446
|
+
size_t diag_len = 0;
|
|
1447
|
+
teptris_status st =
|
|
1448
|
+
teptris_schema_compile(cbuf, (size_t)len, &sch, &diag, &diag_len);
|
|
1449
|
+
xfree(cbuf);
|
|
1450
|
+
if (st != TEPTRIS_OK || sch == NULL) {
|
|
1451
|
+
VALUE msg = diag != NULL ? rb_str_new(diag, (long)diag_len)
|
|
1452
|
+
: rb_str_new_cstr("invalid schema");
|
|
1453
|
+
teptris_diag_free(diag);
|
|
1454
|
+
if (sch != NULL) teptris_schema_free(sch);
|
|
1455
|
+
rb_raise(eError, "schema rejected: %s", StringValueCStr(msg));
|
|
1456
|
+
}
|
|
1457
|
+
schema_owner *o;
|
|
1458
|
+
VALUE obj = TypedData_Make_Struct(cSchema, schema_owner,
|
|
1459
|
+
&schema_owner_type, o);
|
|
1460
|
+
o->sch = sch;
|
|
1461
|
+
return obj;
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
static VALUE ext_schema_validate(VALUE self, VALUE rsch, VALUE str) {
|
|
1465
|
+
schema_owner *o;
|
|
1466
|
+
TypedData_Get_Struct(rsch, schema_owner, &schema_owner_type, o);
|
|
1467
|
+
StringValue(str);
|
|
1468
|
+
long len = RSTRING_LEN(str);
|
|
1469
|
+
char *cbuf = ALLOC_N(char, (size_t)len + 1);
|
|
1470
|
+
memcpy(cbuf, RSTRING_PTR(str), (size_t)len);
|
|
1471
|
+
teptris_document *doc = NULL;
|
|
1472
|
+
struct gvl_parse_arg arg = {cbuf, (size_t)len, &doc, TEPTRIS_OK};
|
|
1473
|
+
rb_thread_call_without_gvl(parse_without_gvl, &arg, RUBY_UBF_IO, NULL);
|
|
1474
|
+
if (arg.st != TEPTRIS_OK) {
|
|
1475
|
+
const teptris_error *e = teptris_document_error(doc);
|
|
1476
|
+
VALUE ex = rb_exc_new(eParseError, e->message, (long)strlen(e->message));
|
|
1477
|
+
rb_iv_set(ex, "@line", SIZET2NUM(e->line));
|
|
1478
|
+
rb_iv_set(ex, "@column", SIZET2NUM(e->column));
|
|
1479
|
+
xfree(cbuf);
|
|
1480
|
+
teptris_document_free(doc);
|
|
1481
|
+
rb_exc_raise(ex);
|
|
1482
|
+
}
|
|
1483
|
+
char *buf = NULL;
|
|
1484
|
+
size_t out_len = 0;
|
|
1485
|
+
teptris_status st =
|
|
1486
|
+
teptris_schema_validate_doc(o->sch, doc, &buf, &out_len);
|
|
1487
|
+
teptris_document_free(doc);
|
|
1488
|
+
xfree(cbuf);
|
|
1489
|
+
dump_check(st);
|
|
1490
|
+
VALUE out = rb_str_new(buf, (long)out_len);
|
|
1491
|
+
free(buf);
|
|
1492
|
+
return out;
|
|
1493
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
# Schema validation against the JSON Schema 2020-12 practical
|
|
6
|
+
# subset (engine SCHEMAS.md). Compile once, validate many: the
|
|
7
|
+
# compiled schema is an immutable, arena-backed handle held by the
|
|
8
|
+
# native extension (freed with the wrapper by GC).
|
|
9
|
+
#
|
|
10
|
+
# schema = Teptris::Schema.compile('{"properties":{"port":{"type":"integer"}}}')
|
|
11
|
+
# schema.validate("port = \"x\"\n")
|
|
12
|
+
# # => [{path: "port", schema_path: "#/properties.port",
|
|
13
|
+
# # message: "expected a matching type, found string"}]
|
|
14
|
+
# schema.valid?("port = 8080\n") # => true
|
|
15
|
+
#
|
|
16
|
+
# Compile failures raise Teptris::Error carrying the engine's diag
|
|
17
|
+
# JSON - unsupported constructs are named, never silently ignored.
|
|
18
|
+
# Documents that do not parse raise Teptris::ParseError with
|
|
19
|
+
# line/column.
|
|
20
|
+
class Teptris::Schema
|
|
21
|
+
# The violation shape: where in the document (+path+), where in
|
|
22
|
+
# the schema (+schema_path+), and why (+message+).
|
|
23
|
+
class Violation
|
|
24
|
+
attr_reader :path, :schema_path, :message
|
|
25
|
+
|
|
26
|
+
def initialize(path, schema_path, message)
|
|
27
|
+
@path = path
|
|
28
|
+
@schema_path = schema_path
|
|
29
|
+
@message = message
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def to_h
|
|
33
|
+
{ path: path, schema_path: schema_path, message: message }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class << self
|
|
38
|
+
def compile(schema_json)
|
|
39
|
+
new(TeptrisExt.schema_compile(schema_json))
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def initialize(handle)
|
|
44
|
+
@handle = handle
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Validate raw TOML; an empty array = valid. Raises
|
|
48
|
+
# Teptris::ParseError when the document does not parse.
|
|
49
|
+
def validate(toml)
|
|
50
|
+
raw = JSON.parse(TeptrisExt.schema_validate(@handle, toml))
|
|
51
|
+
raw.map do |v|
|
|
52
|
+
Violation.new(v["path"], v["schemaPath"], v["message"])
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# True when the document parses with no violations.
|
|
57
|
+
def valid?(toml)
|
|
58
|
+
validate(toml).empty?
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def inspect
|
|
62
|
+
"#<#{self.class.name}>"
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/teptris/version.rb
CHANGED
data/lib/teptris.rb
CHANGED
|
@@ -6,6 +6,7 @@ require_relative "teptris/version"
|
|
|
6
6
|
require_relative "teptris/error"
|
|
7
7
|
require_relative "teptris/descriptor"
|
|
8
8
|
require_relative "teptris/toml"
|
|
9
|
+
require_relative "teptris/schema"
|
|
9
10
|
|
|
10
11
|
# Fat-gem layout on EVERY platform (nokogiri's shape): one extension
|
|
11
12
|
# per ruby minor under lib/teptris/<minor>/ — ucrt rubies bind a
|
data/sig/teptris.rbs
CHANGED
|
@@ -17,6 +17,28 @@ module Teptris
|
|
|
17
17
|
def initialize: (String message, ?Integer line, ?Integer column) -> void
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
# A compiled JSON Schema (the 2020-12 practical subset). Compile
|
|
21
|
+
# once, validate many: Violation carries the materialized TOML
|
|
22
|
+
# path, the schema path, and the message.
|
|
23
|
+
class Schema
|
|
24
|
+
class Violation
|
|
25
|
+
attr_reader path: String
|
|
26
|
+
attr_reader schema_path: String
|
|
27
|
+
attr_reader message: String
|
|
28
|
+
|
|
29
|
+
def initialize: (String path, String schema_path, String message) -> void
|
|
30
|
+
|
|
31
|
+
def to_h: () -> { path: String, schema_path: String, message: String }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.compile: (String schema_json) -> Schema
|
|
35
|
+
|
|
36
|
+
def initialize: (untyped handle) -> void
|
|
37
|
+
def validate: (String toml) -> Array[Violation]
|
|
38
|
+
def valid?: (String toml) -> bool
|
|
39
|
+
def inspect: () -> String
|
|
40
|
+
end
|
|
41
|
+
|
|
20
42
|
module TOML
|
|
21
43
|
VERSION: String
|
|
22
44
|
|
|
@@ -149,4 +171,14 @@ module TeptrisExt
|
|
|
149
171
|
Array[Integer] first_row) -> untyped
|
|
150
172
|
|
|
151
173
|
def self.plan_emit: (untyped handle, String toml) -> Hash[String, untyped]
|
|
174
|
+
|
|
175
|
+
# schema validation (engine 0.3.20): the compiled schema is an
|
|
176
|
+
# opaque TypedData handle freed by GC
|
|
177
|
+
def self.schema_compile: (String schema_json) -> TeptrisExt::Schema
|
|
178
|
+
|
|
179
|
+
def self.schema_validate: (TeptrisExt::Schema schema, String toml) -> String
|
|
180
|
+
|
|
181
|
+
class Schema
|
|
182
|
+
# opaque; owns the native handle
|
|
183
|
+
end
|
|
152
184
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: teptris
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.20.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- teptris contributors
|
|
@@ -22,6 +22,7 @@ files:
|
|
|
22
22
|
- ext/teptris_ext/engine/LICENSE.md
|
|
23
23
|
- ext/teptris_ext/engine/README.md
|
|
24
24
|
- ext/teptris_ext/engine/src/include/teptris/plan.h
|
|
25
|
+
- ext/teptris_ext/engine/src/include/teptris/schema.h
|
|
25
26
|
- ext/teptris_ext/engine/src/include/teptris/teptris.h
|
|
26
27
|
- ext/teptris_ext/engine/src/include/teptris/version.h
|
|
27
28
|
- ext/teptris_ext/engine/src/teptris/api.c
|
|
@@ -52,12 +53,20 @@ files:
|
|
|
52
53
|
- ext/teptris_ext/engine/src/teptris/parse/parser.c
|
|
53
54
|
- ext/teptris_ext/engine/src/teptris/parse/scalars.c
|
|
54
55
|
- ext/teptris_ext/engine/src/teptris/plan/plan.c
|
|
56
|
+
- ext/teptris_ext/engine/src/teptris/schema/json_read.c
|
|
57
|
+
- ext/teptris_ext/engine/src/teptris/schema/json_read.h
|
|
58
|
+
- ext/teptris_ext/engine/src/teptris/schema/regex.c
|
|
59
|
+
- ext/teptris_ext/engine/src/teptris/schema/regex.h
|
|
60
|
+
- ext/teptris_ext/engine/src/teptris/schema/schema.c
|
|
61
|
+
- ext/teptris_ext/engine/src/teptris/schema/schema_internal.h
|
|
62
|
+
- ext/teptris_ext/engine/src/teptris/schema/validate.c
|
|
55
63
|
- ext/teptris_ext/engine/src/teptris/version.c
|
|
56
64
|
- ext/teptris_ext/extconf.rb
|
|
57
65
|
- ext/teptris_ext/materialize.c
|
|
58
66
|
- lib/teptris.rb
|
|
59
67
|
- lib/teptris/descriptor.rb
|
|
60
68
|
- lib/teptris/error.rb
|
|
69
|
+
- lib/teptris/schema.rb
|
|
61
70
|
- lib/teptris/toml.rb
|
|
62
71
|
- lib/teptris/version.rb
|
|
63
72
|
- sig/teptris.rbs
|