shale 0.3.1 → 0.4.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/CHANGELOG.md +15 -0
- data/README.md +105 -14
- data/exe/shaleb +79 -31
- data/lib/shale/attribute.rb +6 -0
- data/lib/shale/error.rb +6 -0
- data/lib/shale/mapper.rb +6 -4
- data/lib/shale/schema/json_compiler/boolean.rb +21 -0
- data/lib/shale/schema/json_compiler/date.rb +21 -0
- data/lib/shale/schema/json_compiler/float.rb +21 -0
- data/lib/shale/schema/json_compiler/integer.rb +21 -0
- data/lib/shale/schema/json_compiler/object.rb +85 -0
- data/lib/shale/schema/json_compiler/property.rb +70 -0
- data/lib/shale/schema/json_compiler/string.rb +21 -0
- data/lib/shale/schema/json_compiler/time.rb +21 -0
- data/lib/shale/schema/json_compiler/utils.rb +52 -0
- data/lib/shale/schema/json_compiler/value.rb +13 -0
- data/lib/shale/schema/json_compiler.rb +333 -0
- data/lib/shale/schema/{json → json_generator}/base.rb +2 -2
- data/lib/shale/schema/{json → json_generator}/boolean.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/collection.rb +2 -2
- data/lib/shale/schema/{json → json_generator}/date.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/float.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/integer.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/object.rb +5 -2
- data/lib/shale/schema/{json → json_generator}/ref.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/schema.rb +6 -4
- data/lib/shale/schema/{json → json_generator}/string.rb +1 -1
- data/lib/shale/schema/{json → json_generator}/time.rb +1 -1
- data/lib/shale/schema/json_generator/value.rb +23 -0
- data/lib/shale/schema/{json.rb → json_generator.rb} +36 -36
- data/lib/shale/schema/{xml → xml_generator}/attribute.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/complex_type.rb +5 -2
- data/lib/shale/schema/{xml → xml_generator}/element.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/import.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/ref_attribute.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/ref_element.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/schema.rb +5 -5
- data/lib/shale/schema/{xml → xml_generator}/typed_attribute.rb +1 -1
- data/lib/shale/schema/{xml → xml_generator}/typed_element.rb +1 -1
- data/lib/shale/schema/{xml.rb → xml_generator.rb} +22 -23
- data/lib/shale/schema.rb +28 -5
- data/lib/shale/type/composite.rb +14 -20
- data/lib/shale/version.rb +1 -1
- metadata +36 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26877841702bfce542b42206f519ffd1b03e2f0d3db1e70a42e37836ff0f3bff
|
4
|
+
data.tar.gz: 23fddba7256bc38b5d64309cf56833f47fd636ed0e9f6ec61f2a076764dad2bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e98bd0bbb20fbbaf4a8bdbfb5e2f83ba5d7bae78ad66a8597a1fe28c0c13d9799bc3ade785105ea7261147f55590ff530e85c31da459fe8c9171ee5b68f02977
|
7
|
+
data.tar.gz: 54be0efe36f4d330c551f1bd8994a36b360ecec3696aafe12382408a0656e50a181b91ff54ec9f39758908d15fcf51eafc4810540cd1aeb00bda7b552382bc80
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
## [0.4.0] - 2022-05-30
|
2
|
+
|
3
|
+
### Added
|
4
|
+
- Allow to add title to JSON Schema
|
5
|
+
- Map Shale::Type::Value to "anyType" XML Schema type
|
6
|
+
- Map Shale::Type::Value to "any" JSON Schema type
|
7
|
+
- Allow to generate Shale model from JSON Schema
|
8
|
+
|
9
|
+
### Changed
|
10
|
+
- Performance improvements
|
11
|
+
- Reformat README a little bit and fix typos
|
12
|
+
|
13
|
+
### Fixed
|
14
|
+
- Fix stack overflow caused by circular dependency when generating JSON and XML schemas
|
15
|
+
|
1
16
|
## [0.3.1] - 2022-04-29
|
2
17
|
|
3
18
|
### Changed
|
data/README.md
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
# Shale
|
2
2
|
|
3
|
-
Shale is a object mapper and serializer for JSON, YAML and XML.
|
3
|
+
Shale is a Ruby object mapper and serializer for JSON, YAML and XML.
|
4
|
+
It allows you to parse JSON, YAML and XML data and convert it into Ruby data structures,
|
5
|
+
as well as serialize data structures into JSON, YAML or XML.
|
6
|
+
|
7
|
+
## Features
|
8
|
+
|
9
|
+
* Convert JSON, YAML and XML to Ruby data model
|
10
|
+
* Convert Ruby data model to JSON, YAML and XML
|
11
|
+
* Generate JSON and XML Schema from Ruby models
|
12
|
+
* Compile JSON Schema into Ruby models
|
13
|
+
* Out of the box support for JSON, YAML, Nokogiri, REXML and Ox parsers
|
14
|
+
* Support for custom adapters
|
4
15
|
|
5
16
|
## Installation
|
6
17
|
|
@@ -47,6 +58,7 @@ $ gem install shale
|
|
47
58
|
* [Writing your own type](#writing-your-own-type)
|
48
59
|
* [Adapters](#adapters)
|
49
60
|
* [Generating JSON Schema](#generating-json-schema)
|
61
|
+
* [Compiling JSON Schema into Shale model](#compiling-json-schema-into-shale-model)
|
50
62
|
* [Generating XML Schema](#generating-xml-schema)
|
51
63
|
|
52
64
|
## Usage
|
@@ -126,6 +138,7 @@ DATA
|
|
126
138
|
|
127
139
|
```ruby
|
128
140
|
person.to_json
|
141
|
+
|
129
142
|
# =>
|
130
143
|
#
|
131
144
|
# {
|
@@ -164,6 +177,7 @@ DATA
|
|
164
177
|
|
165
178
|
```ruby
|
166
179
|
person.to_yaml
|
180
|
+
|
167
181
|
# =>
|
168
182
|
#
|
169
183
|
# ---
|
@@ -201,6 +215,7 @@ person = Person.from_hash(
|
|
201
215
|
|
202
216
|
```ruby
|
203
217
|
person.to_hash
|
218
|
+
|
204
219
|
# =>
|
205
220
|
#
|
206
221
|
# {
|
@@ -237,6 +252,7 @@ DATA
|
|
237
252
|
|
238
253
|
```ruby
|
239
254
|
person.to_xml
|
255
|
+
|
240
256
|
# =>
|
241
257
|
#
|
242
258
|
# <person>
|
@@ -256,7 +272,7 @@ person.to_xml
|
|
256
272
|
|
257
273
|
### Mapping JSON keys to object attributes
|
258
274
|
|
259
|
-
By default keys are named the same as attributes. To use custom
|
275
|
+
By default keys are named the same as attributes. To use custom keys use:
|
260
276
|
|
261
277
|
```ruby
|
262
278
|
class Person < Shale::Mapper
|
@@ -300,7 +316,7 @@ end
|
|
300
316
|
|
301
317
|
### Mapping XML elements and attributes to object attributes
|
302
318
|
|
303
|
-
XML is more
|
319
|
+
XML is more complicated format than JSON or YAML. To map elements, attributes and content use:
|
304
320
|
|
305
321
|
```ruby
|
306
322
|
class Address < Shale::Mapper
|
@@ -356,8 +372,6 @@ DATA
|
|
356
372
|
|
357
373
|
### Using XML namespaces
|
358
374
|
|
359
|
-
:warning: **Ox doesn't support XML namespaces**
|
360
|
-
|
361
375
|
To map namespaced elements and attributes use `namespace` and `prefix` properties on
|
362
376
|
`map_element` and `map_attribute`
|
363
377
|
|
@@ -385,7 +399,7 @@ DATA
|
|
385
399
|
```
|
386
400
|
|
387
401
|
To define default namespace for all elements use `namespace` declaration
|
388
|
-
(this will define namespace
|
402
|
+
(this will define namespace on elements only, if you want to define namespace on an attribute
|
389
403
|
explicitly declare it on `map_attribute`).
|
390
404
|
|
391
405
|
```ruby
|
@@ -590,6 +604,8 @@ Shale.yaml_adapter = MyYamlAdapter
|
|
590
604
|
|
591
605
|
Shale provides adapters for most popular Ruby XML parsers:
|
592
606
|
|
607
|
+
:warning: **Ox doesn't support XML namespaces**
|
608
|
+
|
593
609
|
```ruby
|
594
610
|
require 'shale'
|
595
611
|
|
@@ -611,14 +627,19 @@ Shale.xml_adapter = Shale::Adapter::Ox
|
|
611
627
|
|
612
628
|
### Generating JSON Schema
|
613
629
|
|
614
|
-
|
630
|
+
:warning: Only **[Draft 2020-12](https://json-schema.org/draft/2020-12/schema)** JSON Schema is supported
|
615
631
|
|
616
|
-
|
632
|
+
To generate JSON Schema from your Shale data model use:
|
617
633
|
|
618
634
|
```ruby
|
619
635
|
require 'shale/schema'
|
620
636
|
|
621
|
-
Shale::Schema.to_json(
|
637
|
+
Shale::Schema.to_json(
|
638
|
+
Person,
|
639
|
+
id: 'http://foo.bar/schema/person',
|
640
|
+
description: 'My description',
|
641
|
+
pretty: true
|
642
|
+
)
|
622
643
|
|
623
644
|
# =>
|
624
645
|
#
|
@@ -663,7 +684,7 @@ Shale::Schema.to_json(Person, id: 'http://foo.bar/schema/person', description: '
|
|
663
684
|
You can also use a command line tool to do it:
|
664
685
|
|
665
686
|
```
|
666
|
-
$ shaleb -i data_model.rb -
|
687
|
+
$ shaleb -i data_model.rb -r Person -p
|
667
688
|
```
|
668
689
|
|
669
690
|
If you want to convert your own types to JSON Schema types use:
|
@@ -676,13 +697,83 @@ class MyEmailType < Shale::Type::Value
|
|
676
697
|
...
|
677
698
|
end
|
678
699
|
|
679
|
-
class MyEmailJSONType < Shale::Schema::
|
700
|
+
class MyEmailJSONType < Shale::Schema::JSONGenerator::Base
|
680
701
|
def as_type
|
681
702
|
{ 'type' => 'string', 'format' => 'email' }
|
682
703
|
end
|
683
704
|
end
|
684
705
|
|
685
|
-
Shale::Schema::
|
706
|
+
Shale::Schema::JSONGenerator.register_json_type(MyEmailType, MyEmailJSONType)
|
707
|
+
```
|
708
|
+
|
709
|
+
### Compiling JSON Schema into Shale model
|
710
|
+
|
711
|
+
:warning: Only **[Draft 2020-12](https://json-schema.org/draft/2020-12/schema)** JSON Schema is supported
|
712
|
+
|
713
|
+
To generate Shale data model from JSON Schema use:
|
714
|
+
|
715
|
+
```ruby
|
716
|
+
require 'shale/schema'
|
717
|
+
|
718
|
+
schema = <<~SCHEMA
|
719
|
+
{
|
720
|
+
"type": "object",
|
721
|
+
"properties": {
|
722
|
+
"firstName": { "type": "string" },
|
723
|
+
"lastName": { "type": "string" },
|
724
|
+
"address": {
|
725
|
+
"type": "object",
|
726
|
+
"properties": {
|
727
|
+
"street": { "type": "string" },
|
728
|
+
"city": { "type": "string" }
|
729
|
+
}
|
730
|
+
}
|
731
|
+
}
|
732
|
+
}
|
733
|
+
SCHEMA
|
734
|
+
|
735
|
+
Shale::Schema.from_json([schema], root_name: 'Person')
|
736
|
+
|
737
|
+
# =>
|
738
|
+
#
|
739
|
+
# {
|
740
|
+
# "address" => "
|
741
|
+
# require 'shale'
|
742
|
+
#
|
743
|
+
# class Address < Shale::Mapper
|
744
|
+
# attribute :street, Shale::Type::String
|
745
|
+
# attribute :city, Shale::Type::String
|
746
|
+
#
|
747
|
+
# json do
|
748
|
+
# map 'street', to: :street
|
749
|
+
# map 'city', to: :city
|
750
|
+
# end
|
751
|
+
# end
|
752
|
+
# ",
|
753
|
+
# "person" => "
|
754
|
+
# require 'shale'
|
755
|
+
#
|
756
|
+
# require_relative 'address'
|
757
|
+
#
|
758
|
+
# class Person < Shale::Mapper
|
759
|
+
# attribute :first_name, Shale::Type::String
|
760
|
+
# attribute :last_name, Shale::Type::String
|
761
|
+
# attribute :address, Address
|
762
|
+
#
|
763
|
+
# json do
|
764
|
+
# map 'firstName', to: :first_name
|
765
|
+
# map 'lastName', to: :last_name
|
766
|
+
# map 'address', to: :address
|
767
|
+
# end
|
768
|
+
# end
|
769
|
+
# "
|
770
|
+
# }
|
771
|
+
```
|
772
|
+
|
773
|
+
You can also use a command line tool to do it:
|
774
|
+
|
775
|
+
```
|
776
|
+
$ shaleb -c -i schema.json -r Person
|
686
777
|
```
|
687
778
|
|
688
779
|
### Generating XML Schema
|
@@ -735,7 +826,7 @@ Shale::Schema.to_xml(Person, pretty: true)
|
|
735
826
|
You can also use a command line tool to do it:
|
736
827
|
|
737
828
|
```
|
738
|
-
$ shaleb -i data_model.rb -
|
829
|
+
$ shaleb -i data_model.rb -r Person -p -f xml
|
739
830
|
```
|
740
831
|
|
741
832
|
If you want to convert your own types to XML Schema types use:
|
@@ -748,7 +839,7 @@ class MyEmailType < Shale::Type::Value
|
|
748
839
|
...
|
749
840
|
end
|
750
841
|
|
751
|
-
Shale::Schema::
|
842
|
+
Shale::Schema::XMLGenerator.register_xml_type(MyEmailType, 'myEmailXMLType')
|
752
843
|
```
|
753
844
|
|
754
845
|
## Contributing
|
data/exe/shaleb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
+
require 'fileutils'
|
4
5
|
require 'optparse'
|
5
6
|
|
6
7
|
base_path = File.expand_path('../lib', __dir__)
|
@@ -16,11 +17,17 @@ params = {}
|
|
16
17
|
ARGV << '-h' if ARGV.empty?
|
17
18
|
|
18
19
|
OptionParser.new do |opts|
|
19
|
-
opts.banner =
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
opts.banner = <<~BANNER
|
21
|
+
Usage: shaleb [options]
|
22
|
+
example generate schema from Shale model: shaleb -g -i data_model.rb -c MyRoot
|
23
|
+
example generate Shale model from schema: shaleb -c -i schema1.json,schema2.json -c MyRoot
|
24
|
+
BANNER
|
25
|
+
|
26
|
+
opts.on('-g', '--generate', 'generate schema from Shale model')
|
27
|
+
opts.on('-c', '--compile', 'compile schema into Shale model')
|
28
|
+
opts.on('-i INPUT', '--input', Array, 'Input file')
|
29
|
+
opts.on('-o OUTPUT', '--output', 'Output (defaults to STDOUT)')
|
30
|
+
opts.on('-r ROOT', '--root ROOT', 'Shale model class name')
|
24
31
|
opts.on('-f FORMAT', '--format FORMAT', 'Schema format: JSON (default), XML')
|
25
32
|
opts.on('-p', '--pretty', 'Pretty print generated schema')
|
26
33
|
|
@@ -30,46 +37,87 @@ OptionParser.new do |opts|
|
|
30
37
|
end
|
31
38
|
end.parse!(into: params)
|
32
39
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
40
|
+
if params[:compile]
|
41
|
+
unless params[:input]
|
42
|
+
puts 'Input file is required: shaleb -c -i schema1.json,schema2.json'
|
43
|
+
exit
|
44
|
+
end
|
39
45
|
|
40
|
-
|
41
|
-
|
42
|
-
exit
|
43
|
-
end
|
46
|
+
schemas = params[:input].map do |file|
|
47
|
+
path = File.expand_path(file, Dir.pwd)
|
44
48
|
|
45
|
-
|
49
|
+
if File.exist?(path)
|
50
|
+
File.read(path)
|
51
|
+
else
|
52
|
+
puts "File '#{path}' does not exist"
|
53
|
+
exit
|
54
|
+
end
|
55
|
+
end
|
46
56
|
|
47
|
-
|
57
|
+
models = Shale::Schema.from_json(schemas, root_name: params[:root])
|
48
58
|
|
49
|
-
if params[:format] == 'xml'
|
50
59
|
if params[:output]
|
51
|
-
|
52
|
-
|
60
|
+
dir = File.expand_path(params[:output], Dir.pwd)
|
61
|
+
FileUtils.mkdir_p(dir) unless File.directory?(dir)
|
53
62
|
|
54
|
-
|
55
|
-
File.
|
63
|
+
models.each do |name, model|
|
64
|
+
output_path = File.join(dir, "#{name}.rb")
|
65
|
+
File.write(output_path, model)
|
56
66
|
end
|
57
67
|
else
|
58
|
-
|
59
|
-
|
60
|
-
output = schemas.map do |name, xml|
|
61
|
-
"<!-- #{name} -->\n#{xml}\n"
|
68
|
+
output = models.map do |name, model|
|
69
|
+
"# --- #{name}.rb ---\n#{model}\n"
|
62
70
|
end.join("\n")
|
63
71
|
|
64
72
|
puts output
|
65
73
|
end
|
66
74
|
else
|
67
|
-
|
75
|
+
unless params[:input]
|
76
|
+
puts 'Input file is required: shaleb -i model.rb -r MyClass'
|
77
|
+
exit
|
78
|
+
end
|
68
79
|
|
69
|
-
|
70
|
-
|
71
|
-
|
80
|
+
input_path = File.expand_path(params[:input][0], Dir.pwd)
|
81
|
+
|
82
|
+
unless File.exist?(input_path)
|
83
|
+
puts "File '#{input_path}' does not exist"
|
84
|
+
exit
|
85
|
+
end
|
86
|
+
|
87
|
+
unless params[:root]
|
88
|
+
puts 'Model class is required: shaleb -i model.rb -r MyClass'
|
89
|
+
exit
|
90
|
+
end
|
91
|
+
|
92
|
+
require input_path
|
93
|
+
|
94
|
+
klass = Object.const_get(params[:root])
|
95
|
+
|
96
|
+
if params[:format] == 'xml'
|
97
|
+
if params[:output]
|
98
|
+
base_name = File.basename(params[:output], File.extname(params[:output]))
|
99
|
+
schemas = Shale::Schema.to_xml(klass, base_name, pretty: params[:pretty])
|
100
|
+
|
101
|
+
schemas.map do |name, xml|
|
102
|
+
File.write(File.expand_path(name, Dir.pwd), xml)
|
103
|
+
end
|
104
|
+
else
|
105
|
+
schemas = Shale::Schema.to_xml(klass, pretty: params[:pretty])
|
106
|
+
|
107
|
+
output = schemas.map do |name, xml|
|
108
|
+
"<!-- #{name} -->\n#{xml}\n"
|
109
|
+
end.join("\n")
|
110
|
+
|
111
|
+
puts output
|
112
|
+
end
|
72
113
|
else
|
73
|
-
|
114
|
+
schema = Shale::Schema.to_json(klass, pretty: params[:pretty])
|
115
|
+
|
116
|
+
if params[:output]
|
117
|
+
output_path = File.expand_path(params[:output], Dir.pwd)
|
118
|
+
File.write(output_path, schema)
|
119
|
+
else
|
120
|
+
puts schema
|
121
|
+
end
|
74
122
|
end
|
75
123
|
end
|
data/lib/shale/attribute.rb
CHANGED
@@ -20,6 +20,11 @@ module Shale
|
|
20
20
|
# @api private
|
21
21
|
attr_reader :default
|
22
22
|
|
23
|
+
# Return setter name
|
24
|
+
#
|
25
|
+
# @api private
|
26
|
+
attr_reader :setter
|
27
|
+
|
23
28
|
# Initialize Attribute object
|
24
29
|
#
|
25
30
|
# @param [Symbol] name Name of the attribute
|
@@ -30,6 +35,7 @@ module Shale
|
|
30
35
|
# @api private
|
31
36
|
def initialize(name, type, collection, default)
|
32
37
|
@name = name
|
38
|
+
@setter = "#{name}="
|
33
39
|
@type = type
|
34
40
|
@collection = collection
|
35
41
|
@default = collection ? -> { [] } : default
|
data/lib/shale/error.rb
CHANGED
data/lib/shale/mapper.rb
CHANGED
@@ -272,9 +272,11 @@ module Shale
|
|
272
272
|
def initialize(**props)
|
273
273
|
super()
|
274
274
|
|
275
|
-
props.
|
276
|
-
|
277
|
-
|
275
|
+
unless props.empty?
|
276
|
+
unknown_attributes = props.keys - self.class.attributes.keys
|
277
|
+
|
278
|
+
unless unknown_attributes.empty?
|
279
|
+
raise UnknownAttributeError.new(self.class.to_s, unknown_attributes[0].to_s)
|
278
280
|
end
|
279
281
|
end
|
280
282
|
|
@@ -285,7 +287,7 @@ module Shale
|
|
285
287
|
value = attribute.default.call
|
286
288
|
end
|
287
289
|
|
288
|
-
|
290
|
+
send(attribute.setter, value)
|
289
291
|
end
|
290
292
|
end
|
291
293
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Shale
|
4
|
+
module Schema
|
5
|
+
class JSONCompiler
|
6
|
+
# Class that maps Schema type to Shale Boolean type
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Boolean
|
10
|
+
# Return name of the Shale type
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
def name
|
16
|
+
'Shale::Type::Boolean'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Shale
|
4
|
+
module Schema
|
5
|
+
class JSONCompiler
|
6
|
+
# Class that maps Schema type to Shale Date type
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Date
|
10
|
+
# Return name of the Shale type
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
def name
|
16
|
+
'Shale::Type::Date'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Shale
|
4
|
+
module Schema
|
5
|
+
class JSONCompiler
|
6
|
+
# Class that maps Schema type to Shale Float type
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Float
|
10
|
+
# Return name of the Shale type
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
def name
|
16
|
+
'Shale::Type::Float'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Shale
|
4
|
+
module Schema
|
5
|
+
class JSONCompiler
|
6
|
+
# Class that maps Schema type to Shale Integer type
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Integer
|
10
|
+
# Return name of the Shale type
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
#
|
14
|
+
# @api private
|
15
|
+
def name
|
16
|
+
'Shale::Type::Integer'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'utils'
|
4
|
+
|
5
|
+
module Shale
|
6
|
+
module Schema
|
7
|
+
class JSONCompiler
|
8
|
+
# Class representing Shale's comosite type
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
class Object
|
12
|
+
# Return id
|
13
|
+
#
|
14
|
+
# @return [String]
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
attr_reader :id
|
18
|
+
|
19
|
+
# Return properties
|
20
|
+
#
|
21
|
+
# @return [Array<Shale::Schema::JSONCompiler::Property>]
|
22
|
+
#
|
23
|
+
# @api private
|
24
|
+
attr_reader :properties
|
25
|
+
|
26
|
+
# Name setter
|
27
|
+
#
|
28
|
+
# @param [String] val
|
29
|
+
#
|
30
|
+
# @api private
|
31
|
+
attr_writer :name
|
32
|
+
|
33
|
+
# Initialize object
|
34
|
+
#
|
35
|
+
# @param [String] id
|
36
|
+
# @param [String] name
|
37
|
+
#
|
38
|
+
# @api private
|
39
|
+
def initialize(id, name)
|
40
|
+
@id = id
|
41
|
+
@name = name
|
42
|
+
@properties = []
|
43
|
+
end
|
44
|
+
|
45
|
+
# Return name
|
46
|
+
#
|
47
|
+
# @return [String]
|
48
|
+
#
|
49
|
+
# @api private
|
50
|
+
def name
|
51
|
+
Utils.classify(@name)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Return file name
|
55
|
+
#
|
56
|
+
# @return [String]
|
57
|
+
#
|
58
|
+
# @api private
|
59
|
+
def file_name
|
60
|
+
Utils.snake_case(@name)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Return references
|
64
|
+
#
|
65
|
+
# @return [Array<Shale::Schema::JSONCompiler::Property>]
|
66
|
+
#
|
67
|
+
# @api private
|
68
|
+
def references
|
69
|
+
@properties
|
70
|
+
.filter { |e| e.type.is_a?(self.class) && e.type != self }
|
71
|
+
.uniq { |e| e.type.id }
|
72
|
+
end
|
73
|
+
|
74
|
+
# Add property to Object
|
75
|
+
#
|
76
|
+
# @param [Shale::Schema::JSONCompiler::Property] property
|
77
|
+
#
|
78
|
+
# @api private
|
79
|
+
def add_property(property)
|
80
|
+
@properties << property
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|