rj_schema 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/rj_schema/rj_schema.cpp +16 -19
- data/lib/rj_schema.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b4465c406c185df39ae9a8c7cb0d27e6e0e0e5516a060482e6c07aee908ccac
|
4
|
+
data.tar.gz: be9ad80bd7a0d1840aa5730e46da6d8a1b839d74df4cab36bc777ec0307bed78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e46aa197637d5b5bb026134b0eadcac0714ad65692b10479d69f93ccf49b10304a4ffdebf42a4925d7f6e9f11a814789f27a9bf48fc75d429e08e77513b2f5a7
|
7
|
+
data.tar.gz: fe08e34c5c1e06826109db6779918da9c386f64c6de040ca642749a73481e9c1e3bd70b797600e9a07da9afc52c28a729f6460755cee532a4190e274e7fd26c8
|
data/ext/rj_schema/rj_schema.cpp
CHANGED
@@ -8,13 +8,13 @@
|
|
8
8
|
#include <rapidjson/filereadstream.h>
|
9
9
|
#include <rapidjson/prettywriter.h>
|
10
10
|
|
11
|
-
typedef std::unordered_map<std::string, rapidjson::SchemaDocument>
|
11
|
+
typedef std::unordered_map<std::string, rapidjson::SchemaDocument> SchemaCollection;
|
12
12
|
|
13
|
-
class
|
14
|
-
|
13
|
+
class SchemaDocumentProvider : public rapidjson::IRemoteSchemaDocumentProvider {
|
14
|
+
SchemaCollection* schema_collection;
|
15
15
|
|
16
16
|
public:
|
17
|
-
|
17
|
+
SchemaDocumentProvider(SchemaCollection* schema_collection) : schema_collection(schema_collection) { }
|
18
18
|
virtual const rapidjson::SchemaDocument* GetRemoteDocument(const char* uri, rapidjson::SizeType length) {
|
19
19
|
auto it = schema_collection->find(std::string(uri, length));
|
20
20
|
if (it == schema_collection->end())
|
@@ -23,10 +23,10 @@ class RemoteSchemaDocumentProvider : public rapidjson::IRemoteSchemaDocumentProv
|
|
23
23
|
}
|
24
24
|
};
|
25
25
|
|
26
|
-
struct
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
struct SchemaManager {
|
27
|
+
SchemaCollection collection;
|
28
|
+
SchemaDocumentProvider provider;
|
29
|
+
SchemaManager() : provider(&collection) { }
|
30
30
|
};
|
31
31
|
|
32
32
|
rapidjson::Document parse_document(VALUE arg) {
|
@@ -39,12 +39,9 @@ rapidjson::Document parse_document(VALUE arg) {
|
|
39
39
|
arg = rb_funcall(arg, rb_intern("to_json"), 0);
|
40
40
|
}
|
41
41
|
|
42
|
-
const char* json_document = StringValuePtr(arg);
|
43
|
-
auto length = RSTRING_LEN(arg);
|
44
|
-
|
45
42
|
rapidjson::Document document;
|
46
43
|
|
47
|
-
if (document.Parse(
|
44
|
+
if (document.Parse(StringValuePtr(arg), RSTRING_LEN(arg)).HasParseError())
|
48
45
|
throw std::invalid_argument("document is not valid JSON");
|
49
46
|
|
50
47
|
return document;
|
@@ -52,8 +49,8 @@ rapidjson::Document parse_document(VALUE arg) {
|
|
52
49
|
|
53
50
|
VALUE perform_validation(VALUE self, VALUE schema_arg, VALUE document_arg, bool with_errors) {
|
54
51
|
try {
|
55
|
-
|
56
|
-
Data_Get_Struct(self,
|
52
|
+
SchemaManager* schema_manager;
|
53
|
+
Data_Get_Struct(self, SchemaManager, schema_manager);
|
57
54
|
auto document = parse_document(document_arg);
|
58
55
|
auto validate = [with_errors, &document](const rapidjson::SchemaDocument& schema) -> VALUE {
|
59
56
|
rapidjson::SchemaValidator validator(schema);
|
@@ -103,17 +100,17 @@ extern "C" VALUE validator_valid(VALUE self, VALUE schema_arg, VALUE document_ar
|
|
103
100
|
return perform_validation(self, schema_arg, document_arg, false);
|
104
101
|
}
|
105
102
|
|
106
|
-
extern "C" void validator_free(
|
103
|
+
extern "C" void validator_free(SchemaManager* schema_manager) {
|
107
104
|
delete schema_manager;
|
108
105
|
}
|
109
106
|
|
110
107
|
extern "C" VALUE validator_alloc(VALUE self) {
|
111
|
-
auto* schema_manager = new
|
108
|
+
auto* schema_manager = new SchemaManager;
|
112
109
|
return Data_Wrap_Struct(self, NULL, validator_free, schema_manager);
|
113
110
|
}
|
114
111
|
|
115
112
|
extern "C" int validator_initialize_load_schema(VALUE key, VALUE value, VALUE input) {
|
116
|
-
auto* schema_manager = reinterpret_cast<
|
113
|
+
auto* schema_manager = reinterpret_cast<SchemaManager*>(input);
|
117
114
|
|
118
115
|
try {
|
119
116
|
schema_manager->collection.emplace(
|
@@ -137,8 +134,8 @@ extern "C" VALUE validator_initialize(int argc, VALUE* argv, VALUE self) {
|
|
137
134
|
if (argc == 0)
|
138
135
|
return self;
|
139
136
|
|
140
|
-
|
141
|
-
Data_Get_Struct(self,
|
137
|
+
SchemaManager* schema_manager;
|
138
|
+
Data_Get_Struct(self, SchemaManager, schema_manager);
|
142
139
|
|
143
140
|
rb_hash_foreach(argv[0], reinterpret_cast<int(*)(...)>(validator_initialize_load_schema), reinterpret_cast<VALUE>(schema_manager));
|
144
141
|
|
data/lib/rj_schema.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rj_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian Semmler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-02-
|
11
|
+
date: 2018-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|