teptris 0.3.19.1-aarch64-linux → 0.3.20.0-aarch64-linux

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.
@@ -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, gregorian_start;
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
@@ -1346,6 +1347,9 @@ static VALUE ext_load_batch(int argc, VALUE *argv, VALUE self) {
1346
1347
  return out;
1347
1348
  }
1348
1349
 
1350
+ static VALUE ext_schema_compile(VALUE self, VALUE str);
1351
+ static VALUE ext_schema_validate(VALUE self, VALUE rsch, VALUE str);
1352
+
1349
1353
  void Init_teptris_ext(void) {
1350
1354
  /* every module static is written once here and read-only after;
1351
1355
  * call state (key cache, arenas, types) is per-object or per-call */
@@ -1362,6 +1366,10 @@ void Init_teptris_ext(void) {
1362
1366
  rb_define_module_function(m, "load_batch", ext_load_batch, -1);
1363
1367
  rb_define_module_function(m, "load_slice", ext_load_slice, -1);
1364
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);
1365
1373
  cLazyOwner = rb_define_class_under(m, "LazyOwner", rb_cObject);
1366
1374
  cLazyValue = rb_define_class_under(m, "LazyValue", rb_cObject);
1367
1375
  rb_define_method(cLazyValue, "kind", lazy_kind, 0);
@@ -1405,3 +1413,81 @@ void Init_teptris_ext(void) {
1405
1413
  rb_gc_register_address(&cLazyOwner);
1406
1414
  rb_gc_register_address(&cLazyValue);
1407
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
+ }
Binary file
Binary file
Binary file
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Teptris
4
- VERSION = "0.3.19.1"
4
+ VERSION = "0.3.20.0"
5
5
  end
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.19.1
4
+ version: 0.3.20.0
5
5
  platform: aarch64-linux
6
6
  authors:
7
7
  - teptris contributors
@@ -21,6 +21,7 @@ files:
21
21
  - ext/teptris_ext/engine/LICENSE.md
22
22
  - ext/teptris_ext/engine/README.md
23
23
  - ext/teptris_ext/engine/src/include/teptris/plan.h
24
+ - ext/teptris_ext/engine/src/include/teptris/schema.h
24
25
  - ext/teptris_ext/engine/src/include/teptris/teptris.h
25
26
  - ext/teptris_ext/engine/src/include/teptris/version.h
26
27
  - ext/teptris_ext/engine/src/teptris/api.c
@@ -51,6 +52,13 @@ files:
51
52
  - ext/teptris_ext/engine/src/teptris/parse/parser.c
52
53
  - ext/teptris_ext/engine/src/teptris/parse/scalars.c
53
54
  - ext/teptris_ext/engine/src/teptris/plan/plan.c
55
+ - ext/teptris_ext/engine/src/teptris/schema/json_read.c
56
+ - ext/teptris_ext/engine/src/teptris/schema/json_read.h
57
+ - ext/teptris_ext/engine/src/teptris/schema/regex.c
58
+ - ext/teptris_ext/engine/src/teptris/schema/regex.h
59
+ - ext/teptris_ext/engine/src/teptris/schema/schema.c
60
+ - ext/teptris_ext/engine/src/teptris/schema/schema_internal.h
61
+ - ext/teptris_ext/engine/src/teptris/schema/validate.c
54
62
  - ext/teptris_ext/engine/src/teptris/version.c
55
63
  - ext/teptris_ext/extconf.rb
56
64
  - ext/teptris_ext/materialize.c
@@ -60,6 +68,7 @@ files:
60
68
  - lib/teptris/4.0/teptris_ext.so
61
69
  - lib/teptris/descriptor.rb
62
70
  - lib/teptris/error.rb
71
+ - lib/teptris/schema.rb
63
72
  - lib/teptris/toml.rb
64
73
  - lib/teptris/version.rb
65
74
  - sig/teptris.rbs