babl-json 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/lib/babl.rb +2 -2
  3. data/lib/babl/builder.rb +2 -0
  4. data/lib/babl/builder/chain_builder.rb +1 -2
  5. data/lib/babl/builder/template_base.rb +3 -4
  6. data/lib/babl/nodes.rb +15 -0
  7. data/lib/babl/nodes/create_pin.rb +1 -2
  8. data/lib/babl/nodes/dep.rb +1 -2
  9. data/lib/babl/nodes/each.rb +2 -2
  10. data/lib/babl/nodes/fixed_array.rb +2 -3
  11. data/lib/babl/nodes/goto_pin.rb +1 -2
  12. data/lib/babl/nodes/internal_value.rb +1 -1
  13. data/lib/babl/nodes/merge.rb +14 -35
  14. data/lib/babl/nodes/nav.rb +1 -1
  15. data/lib/babl/nodes/object.rb +5 -4
  16. data/lib/babl/nodes/parent.rb +1 -2
  17. data/lib/babl/nodes/static.rb +2 -3
  18. data/lib/babl/nodes/switch.rb +2 -3
  19. data/lib/babl/nodes/terminal_value.rb +17 -12
  20. data/lib/babl/nodes/typed.rb +2 -2
  21. data/lib/babl/nodes/with.rb +1 -2
  22. data/lib/babl/operators.rb +21 -0
  23. data/lib/babl/operators/array.rb +1 -2
  24. data/lib/babl/operators/dep.rb +1 -1
  25. data/lib/babl/operators/each.rb +1 -1
  26. data/lib/babl/operators/enter.rb +1 -0
  27. data/lib/babl/operators/merge.rb +1 -2
  28. data/lib/babl/operators/nav.rb +1 -1
  29. data/lib/babl/operators/object.rb +1 -2
  30. data/lib/babl/operators/parent.rb +1 -1
  31. data/lib/babl/operators/partial.rb +1 -1
  32. data/lib/babl/operators/pin.rb +2 -3
  33. data/lib/babl/operators/static.rb +3 -3
  34. data/lib/babl/operators/switch.rb +1 -3
  35. data/lib/babl/operators/typed.rb +1 -1
  36. data/lib/babl/operators/with.rb +1 -2
  37. data/lib/babl/rendering.rb +3 -0
  38. data/lib/babl/rendering/compiled_template.rb +2 -2
  39. data/lib/babl/schema.rb +7 -0
  40. data/lib/babl/schema/any_of.rb +43 -38
  41. data/lib/babl/schema/dyn_array.rb +1 -1
  42. data/lib/babl/schema/fixed_array.rb +1 -1
  43. data/lib/babl/schema/object.rb +1 -1
  44. data/lib/babl/schema/static.rb +1 -1
  45. data/lib/babl/schema/typed.rb +1 -1
  46. data/lib/babl/template.rb +2 -23
  47. data/lib/babl/utils.rb +3 -0
  48. data/lib/babl/version.rb +1 -1
  49. metadata +9 -63
  50. data/.gitignore +0 -3
  51. data/.rubocop.yml +0 -201
  52. data/.ruby-version +0 -1
  53. data/.travis.yml +0 -4
  54. data/CHANGELOG.md +0 -36
  55. data/Gemfile +0 -5
  56. data/LICENSE +0 -7
  57. data/README.md +0 -89
  58. data/babl.gemspec +0 -24
  59. data/logo-babl.png +0 -0
  60. data/spec/operators/array_spec.rb +0 -37
  61. data/spec/operators/call_spec.rb +0 -64
  62. data/spec/operators/continue_spec.rb +0 -25
  63. data/spec/operators/default_spec.rb +0 -15
  64. data/spec/operators/dep_spec.rb +0 -15
  65. data/spec/operators/each_spec.rb +0 -42
  66. data/spec/operators/enter_spec.rb +0 -28
  67. data/spec/operators/extends_spec.rb +0 -30
  68. data/spec/operators/merge_spec.rb +0 -124
  69. data/spec/operators/nav_spec.rb +0 -71
  70. data/spec/operators/null_spec.rb +0 -15
  71. data/spec/operators/nullable_spec.rb +0 -29
  72. data/spec/operators/object_spec.rb +0 -30
  73. data/spec/operators/parent_spec.rb +0 -50
  74. data/spec/operators/partial_spec.rb +0 -36
  75. data/spec/operators/pin_spec.rb +0 -172
  76. data/spec/operators/source_spec.rb +0 -33
  77. data/spec/operators/static_spec.rb +0 -27
  78. data/spec/operators/switch_spec.rb +0 -151
  79. data/spec/operators/typed_spec.rb +0 -84
  80. data/spec/operators/with_spec.rb +0 -31
  81. data/spec/spec_helper.rb +0 -2
  82. data/spec/spec_helper/operator_testing.rb +0 -46
  83. data/spec/spec_helper/schema_utils.rb +0 -53
  84. data/spec/utils/value_spec.rb +0 -54
@@ -1,5 +1,4 @@
1
- require 'babl/nodes/with'
2
- require 'babl/nodes/internal_value'
1
+ require 'babl/nodes'
3
2
 
4
3
  module Babl
5
4
  module Operators
@@ -0,0 +1,3 @@
1
+ require 'babl/rendering/compiled_template'
2
+ require 'babl/rendering/noop_preloader'
3
+ require 'babl/rendering/context'
@@ -1,6 +1,6 @@
1
1
  require 'oj'
2
- require 'babl/rendering/context'
3
- require 'babl/utils/value'
2
+ require 'babl/rendering'
3
+ require 'babl/utils'
4
4
 
5
5
  module Babl
6
6
  module Rendering
@@ -0,0 +1,7 @@
1
+ require 'babl/schema/any_of'
2
+ require 'babl/schema/anything'
3
+ require 'babl/schema/dyn_array'
4
+ require 'babl/schema/fixed_array'
5
+ require 'babl/schema/object'
6
+ require 'babl/schema/static'
7
+ require 'babl/schema/typed'
@@ -1,7 +1,6 @@
1
- require 'babl/utils/value'
1
+ require 'babl/utils'
2
+ require 'babl/schema'
2
3
  require 'set'
3
- require 'babl/schema/static'
4
- require 'babl/schema/object'
5
4
 
6
5
  module Babl
7
6
  module Schema
@@ -22,13 +21,13 @@ module Babl
22
21
  # schema.
23
22
  def simplify
24
23
  simplify_single ||
24
+ simplify_anything ||
25
25
  simplify_boolean ||
26
26
  simplify_typed_and_static ||
27
- simplify_nullability ||
28
27
  simplify_empty_array ||
29
28
  simplify_push_down_dyn_array ||
30
29
  simplify_dyn_and_fixed_array ||
31
- simplify_many_objects_only_one_difference ||
30
+ simplify_merge_objects ||
32
31
  self
33
32
  end
34
33
 
@@ -43,12 +42,9 @@ module Babl
43
42
  choices.size == 1 ? choices.first : nil
44
43
  end
45
44
 
46
- # Anything is nullable, so AnyOf[null, Anything] can be replaced by Anything
47
- # (in general, everything can be merged into Anything but I decided not to do it in order
48
- # to retain as much information as possible)
49
- def simplify_nullability
50
- return unless choice_set.include?(Static::NULL) && choice_set.include?(Anything.instance)
51
- AnyOf.canonicalized(choice_set - [Static::NULL])
45
+ # AnyOf[anything, string, number...] always collapse to anything.
46
+ def simplify_anything
47
+ choice_set.include?(Anything.instance) ? Anything.instance : nil
52
48
  end
53
49
 
54
50
  # AnyOf[true, false] is just boolean
@@ -102,38 +98,47 @@ module Babl
102
98
  nil
103
99
  end
104
100
 
105
- # If two objects have exactly the same structure, with the exception of only one property
106
- # having a different type, then AnyOf can be pushed down to this property.
107
- def simplify_many_objects_only_one_difference
101
+ # Merge all objects together. This is the only lossy simplification, but it will greatly reduce the size
102
+ # of the generated schema. On top of that, when the JSON-Schema is translated into Typescript, it produces
103
+ # a much more workable type definition (union of anonymous object types is not practical to use)
104
+ def simplify_merge_objects
108
105
  choices.each_with_index { |obj1, index1|
109
106
  next unless Object === obj1
110
107
 
111
108
  choices.each_with_index { |obj2, index2|
112
109
  break if index2 >= index1
113
-
114
- next unless Object === obj2 &&
115
- obj1.additional == obj2.additional &&
116
- obj1.properties.map { |p| [p.name, p.required] }.to_set ==
117
- obj2.properties.map { |p| [p.name, p.required] }.to_set
118
-
119
- diff1 = obj1.properties - obj2.properties
120
- diff2 = obj2.properties - obj1.properties
121
-
122
- next unless diff1.size == 1 && diff2.size == 1 && diff1.first.name == diff2.first.name
123
-
124
- merged = Object.new(
125
- obj1.properties.map { |property|
126
- next property unless property == diff1.first
127
- Object::Property.new(
128
- property.name,
129
- AnyOf.canonicalized([diff1.first.value, diff2.first.value]),
130
- property.required
131
- )
132
- },
133
- obj1.additional
134
- )
135
-
136
- return AnyOf.canonicalized(choice_set - [obj1, obj2] + [merged])
110
+ next unless Object === obj2
111
+
112
+ obj1props = obj1.properties.map { |p| [p.name, p] }.to_h
113
+ obj2props = obj2.properties.map { |p| [p.name, p] }.to_h
114
+
115
+ # Do not merge properties unless a keyset is almost a subset of the other
116
+ next unless (obj1props.keys.to_set - obj2props.keys).size <= 1 ||
117
+ (obj2props.keys.to_set - obj1props.keys).size <= 1
118
+
119
+ # Try to detect a discrimitive property (inspired from Typescript's discriminative union),
120
+ # We will abort the merging process unless all the other properties are exactly the same.
121
+ discriminator = obj1props.find { |name, p1|
122
+ p2 = obj2props[name]
123
+ next name if p2 && Static === p2.value && Static === p1.value && p1.value.value != p2.value.value
124
+ }&.first
125
+
126
+ new_properties = (obj1props.keys + obj2props.keys).uniq.map { |name|
127
+ p1 = obj1props[name]
128
+ p2 = obj2props[name]
129
+
130
+ break if discriminator && discriminator != name && p1 != p2
131
+
132
+ Object::Property.new(
133
+ name,
134
+ AnyOf.canonicalized([p1&.value, p2&.value].compact),
135
+ p1&.required && p2&.required || false
136
+ )
137
+ }
138
+
139
+ next unless new_properties
140
+ new_obj = Object.new(new_properties, obj1.additional || obj2.additional)
141
+ return AnyOf.canonicalized(choice_set - [obj1, obj2] + [new_obj])
137
142
  }
138
143
  }
139
144
 
@@ -1,4 +1,4 @@
1
- require 'babl/utils/value'
1
+ require 'babl/utils'
2
2
 
3
3
  module Babl
4
4
  module Schema
@@ -1,4 +1,4 @@
1
- require 'babl/utils/value'
1
+ require 'babl/utils'
2
2
 
3
3
  module Babl
4
4
  module Schema
@@ -1,4 +1,4 @@
1
- require 'babl/utils/value'
1
+ require 'babl/utils'
2
2
  require 'set'
3
3
 
4
4
  module Babl
@@ -1,4 +1,4 @@
1
- require 'babl/utils/value'
1
+ require 'babl/utils'
2
2
 
3
3
  module Babl
4
4
  module Schema
@@ -1,4 +1,4 @@
1
- require 'babl/utils/value'
1
+ require 'babl/utils'
2
2
 
3
3
  module Babl
4
4
  module Schema
@@ -1,26 +1,5 @@
1
- require 'babl/operators/array'
2
- require 'babl/operators/call'
3
- require 'babl/operators/continue'
4
- require 'babl/operators/default'
5
- require 'babl/operators/dep'
6
- require 'babl/operators/each'
7
- require 'babl/operators/enter'
8
- require 'babl/operators/extends'
9
- require 'babl/operators/merge'
10
- require 'babl/operators/nav'
11
- require 'babl/operators/null'
12
- require 'babl/operators/nullable'
13
- require 'babl/operators/object'
14
- require 'babl/operators/parent'
15
- require 'babl/operators/partial'
16
- require 'babl/operators/pin'
17
- require 'babl/operators/source'
18
- require 'babl/operators/static'
19
- require 'babl/operators/switch'
20
- require 'babl/operators/typed'
21
- require 'babl/operators/with'
22
-
23
- require 'babl/builder/template_base'
1
+ require 'babl/builder'
2
+ require 'babl/operators'
24
3
 
25
4
  module Babl
26
5
  class Template < Babl::Builder::TemplateBase
@@ -0,0 +1,3 @@
1
+ require 'babl/utils/hash'
2
+ require 'babl/utils/ref'
3
+ require 'babl/utils/value'
@@ -1,3 +1,3 @@
1
1
  module Babl
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babl-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederic Terrazzoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-21 00:00:00.000000000 Z
11
+ date: 2017-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -87,19 +87,12 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - ".gitignore"
91
- - ".rubocop.yml"
92
- - ".ruby-version"
93
- - ".travis.yml"
94
- - CHANGELOG.md
95
- - Gemfile
96
- - LICENSE
97
- - README.md
98
- - babl.gemspec
99
90
  - lib/babl.rb
91
+ - lib/babl/builder.rb
100
92
  - lib/babl/builder/chain_builder.rb
101
93
  - lib/babl/builder/template_base.rb
102
94
  - lib/babl/errors.rb
95
+ - lib/babl/nodes.rb
103
96
  - lib/babl/nodes/create_pin.rb
104
97
  - lib/babl/nodes/dep.rb
105
98
  - lib/babl/nodes/each.rb
@@ -115,6 +108,7 @@ files:
115
108
  - lib/babl/nodes/terminal_value.rb
116
109
  - lib/babl/nodes/typed.rb
117
110
  - lib/babl/nodes/with.rb
111
+ - lib/babl/operators.rb
118
112
  - lib/babl/operators/array.rb
119
113
  - lib/babl/operators/call.rb
120
114
  - lib/babl/operators/continue.rb
@@ -137,9 +131,11 @@ files:
137
131
  - lib/babl/operators/typed.rb
138
132
  - lib/babl/operators/with.rb
139
133
  - lib/babl/railtie.rb
134
+ - lib/babl/rendering.rb
140
135
  - lib/babl/rendering/compiled_template.rb
141
136
  - lib/babl/rendering/context.rb
142
137
  - lib/babl/rendering/noop_preloader.rb
138
+ - lib/babl/schema.rb
143
139
  - lib/babl/schema/any_of.rb
144
140
  - lib/babl/schema/anything.rb
145
141
  - lib/babl/schema/dyn_array.rb
@@ -148,36 +144,11 @@ files:
148
144
  - lib/babl/schema/static.rb
149
145
  - lib/babl/schema/typed.rb
150
146
  - lib/babl/template.rb
147
+ - lib/babl/utils.rb
151
148
  - lib/babl/utils/hash.rb
152
149
  - lib/babl/utils/ref.rb
153
150
  - lib/babl/utils/value.rb
154
151
  - lib/babl/version.rb
155
- - logo-babl.png
156
- - spec/operators/array_spec.rb
157
- - spec/operators/call_spec.rb
158
- - spec/operators/continue_spec.rb
159
- - spec/operators/default_spec.rb
160
- - spec/operators/dep_spec.rb
161
- - spec/operators/each_spec.rb
162
- - spec/operators/enter_spec.rb
163
- - spec/operators/extends_spec.rb
164
- - spec/operators/merge_spec.rb
165
- - spec/operators/nav_spec.rb
166
- - spec/operators/null_spec.rb
167
- - spec/operators/nullable_spec.rb
168
- - spec/operators/object_spec.rb
169
- - spec/operators/parent_spec.rb
170
- - spec/operators/partial_spec.rb
171
- - spec/operators/pin_spec.rb
172
- - spec/operators/source_spec.rb
173
- - spec/operators/static_spec.rb
174
- - spec/operators/switch_spec.rb
175
- - spec/operators/typed_spec.rb
176
- - spec/operators/with_spec.rb
177
- - spec/spec_helper.rb
178
- - spec/spec_helper/operator_testing.rb
179
- - spec/spec_helper/schema_utils.rb
180
- - spec/utils/value_spec.rb
181
152
  homepage: https://github.com/getbannerman/babl
182
153
  licenses:
183
154
  - MIT
@@ -202,29 +173,4 @@ rubygems_version: 2.6.11
202
173
  signing_key:
203
174
  specification_version: 4
204
175
  summary: JSON templating on steroids
205
- test_files:
206
- - spec/operators/array_spec.rb
207
- - spec/operators/call_spec.rb
208
- - spec/operators/continue_spec.rb
209
- - spec/operators/default_spec.rb
210
- - spec/operators/dep_spec.rb
211
- - spec/operators/each_spec.rb
212
- - spec/operators/enter_spec.rb
213
- - spec/operators/extends_spec.rb
214
- - spec/operators/merge_spec.rb
215
- - spec/operators/nav_spec.rb
216
- - spec/operators/null_spec.rb
217
- - spec/operators/nullable_spec.rb
218
- - spec/operators/object_spec.rb
219
- - spec/operators/parent_spec.rb
220
- - spec/operators/partial_spec.rb
221
- - spec/operators/pin_spec.rb
222
- - spec/operators/source_spec.rb
223
- - spec/operators/static_spec.rb
224
- - spec/operators/switch_spec.rb
225
- - spec/operators/typed_spec.rb
226
- - spec/operators/with_spec.rb
227
- - spec/spec_helper.rb
228
- - spec/spec_helper/operator_testing.rb
229
- - spec/spec_helper/schema_utils.rb
230
- - spec/utils/value_spec.rb
176
+ test_files: []
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- .DS_Store
2
- Gemfile.lock
3
- *.gem
@@ -1,201 +0,0 @@
1
- AllCops:
2
- Include:
3
- - Gemfile
4
- - '**/*.babl'
5
- TargetRubyVersion: 2.3
6
-
7
- # Begin -- Keep the following
8
- CaseIndentation:
9
- EnforcedStyle: end
10
-
11
- ParameterLists:
12
- Max: 5
13
- CountKeywordArgs: false
14
-
15
- Rails:
16
- Enabled: False
17
-
18
- Style/AndOr:
19
- EnforcedStyle: conditionals
20
-
21
- Style/BlockDelimiters:
22
- Enabled: False
23
-
24
- Style/CollectionMethods:
25
- Enabled: True
26
-
27
- Style/Documentation:
28
- Enabled: False
29
-
30
- Style/Lambda:
31
- Enabled: False
32
-
33
- Style/MultilineIfModifier:
34
- Enabled: False
35
-
36
- Style/NumericLiterals:
37
- Enabled: False
38
-
39
- Style/SignalException:
40
- EnforcedStyle: only_raise
41
-
42
- Layout/AlignParameters:
43
- EnforcedStyle: with_fixed_indentation
44
-
45
- Layout/ExtraSpacing:
46
- Enabled: True
47
-
48
- Layout/IndentationWidth:
49
- Width: 4
50
-
51
- Layout/IndentHash:
52
- EnforcedStyle: consistent
53
-
54
- Layout/MultilineOperationIndentation:
55
- EnforcedStyle: indented
56
-
57
- Layout/EmptyLineAfterMagicComment:
58
- Enabled: False
59
-
60
- Layout/IndentArray:
61
- Enabled: False
62
-
63
- Layout/IndentHeredoc:
64
- Enabled: False
65
-
66
- Layout/MultilineMethodCallIndentation:
67
- Enabled: False
68
-
69
- Lint/AmbiguousBlockAssociation:
70
- Enabled: False
71
-
72
- Lint/EmptyWhen:
73
- Enabled: False
74
-
75
- Lint/EndAlignment:
76
- Enabled: False
77
-
78
- Lint/InheritException:
79
- Enabled: False
80
-
81
- Lint/NestedMethodDefinition:
82
- Enabled: False
83
-
84
- Lint/SafeNavigationChain:
85
- Enabled: True
86
-
87
- Lint/UnneededSplatExpansion:
88
- Enabled: False
89
-
90
- Lint/UselessAccessModifier:
91
- Enabled: False
92
-
93
- Metrics/LineLength:
94
- Max: 130
95
-
96
- Metrics/PerceivedComplexity:
97
- Max: 12
98
-
99
- Metrics/AbcSize:
100
- Enabled: False
101
- Max: 30
102
-
103
- Metrics/BlockLength:
104
- Enabled: False
105
- Max: 25
106
-
107
- Metrics/ClassLength:
108
- Enabled: False
109
- Max: 200
110
-
111
- Metrics/CyclomaticComplexity:
112
- Enabled: False
113
- Max: 10
114
-
115
- Metrics/MethodLength:
116
- Enabled: False
117
- Max: 20
118
-
119
- Metrics/ModuleLength:
120
- Enabled: False
121
-
122
- Performance/Casecmp:
123
- Enabled: False
124
-
125
- Rails/TimeZone:
126
- Enabled: False
127
-
128
- Security/YAMLLoad:
129
- Enabled: False
130
-
131
- Style/Alias:
132
- Enabled: False
133
-
134
- Style/ConditionalAssignment:
135
- Enabled: False
136
-
137
- Style/ClassVars:
138
- Enabled: False
139
-
140
- Style/EmptyCaseCondition:
141
- Enabled: False
142
-
143
- Style/EmptyMethod:
144
- Enabled: False
145
-
146
- Style/FileName:
147
- Enabled: False
148
-
149
- Style/FrozenStringLiteralComment:
150
- Enabled: False
151
-
152
- Style/GuardClause:
153
- Enabled: False
154
-
155
- Style/MixinGrouping:
156
- Enabled: False
157
-
158
- Style/MultilineBlockChain:
159
- Enabled: False
160
-
161
- Style/MultilineMemoization:
162
- Enabled: False
163
-
164
- Style/MutableConstant:
165
- Enabled: False
166
-
167
- Style/NestedParenthesizedCalls:
168
- Enabled: False
169
-
170
- Style/Next:
171
- Enabled: False
172
-
173
- Style/NumericPredicate:
174
- Enabled: False
175
-
176
- Style/ParallelAssignment:
177
- Enabled: False
178
-
179
- Style/RedundantParentheses:
180
- Enabled: False
181
-
182
- Style/RedundantSelf:
183
- Enabled: False
184
-
185
- Style/SafeNavigation:
186
- Enabled: False
187
-
188
- Style/SingleLineBlockParams:
189
- Enabled: False
190
-
191
- Style/CaseEquality:
192
- Enabled: False
193
-
194
- Style/StringLiterals:
195
- Enabled: False
196
-
197
- Style/SymbolArray:
198
- Enabled: False
199
-
200
- Style/VariableNumber:
201
- Enabled: False