babl-json 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/babl.rb +1 -0
- data/lib/babl/builder.rb +1 -0
- data/lib/babl/builder/chain_builder.rb +1 -0
- data/lib/babl/builder/template_base.rb +1 -0
- data/lib/babl/errors.rb +1 -0
- data/lib/babl/nodes.rb +1 -0
- data/lib/babl/nodes/create_pin.rb +1 -0
- data/lib/babl/nodes/dep.rb +1 -0
- data/lib/babl/nodes/each.rb +1 -0
- data/lib/babl/nodes/fixed_array.rb +1 -0
- data/lib/babl/nodes/goto_pin.rb +1 -0
- data/lib/babl/nodes/internal_value.rb +1 -0
- data/lib/babl/nodes/merge.rb +5 -4
- data/lib/babl/nodes/nav.rb +7 -6
- data/lib/babl/nodes/object.rb +1 -0
- data/lib/babl/nodes/parent.rb +1 -0
- data/lib/babl/nodes/static.rb +7 -1
- data/lib/babl/nodes/switch.rb +1 -0
- data/lib/babl/nodes/terminal_value.rb +1 -0
- data/lib/babl/nodes/typed.rb +1 -0
- data/lib/babl/nodes/with.rb +6 -2
- data/lib/babl/operators.rb +1 -0
- data/lib/babl/operators/array.rb +1 -0
- data/lib/babl/operators/call.rb +1 -0
- data/lib/babl/operators/continue.rb +1 -0
- data/lib/babl/operators/default.rb +1 -0
- data/lib/babl/operators/dep.rb +1 -0
- data/lib/babl/operators/each.rb +1 -0
- data/lib/babl/operators/enter.rb +1 -0
- data/lib/babl/operators/extends.rb +1 -0
- data/lib/babl/operators/merge.rb +1 -0
- data/lib/babl/operators/nav.rb +1 -0
- data/lib/babl/operators/null.rb +1 -0
- data/lib/babl/operators/nullable.rb +4 -1
- data/lib/babl/operators/object.rb +1 -0
- data/lib/babl/operators/parent.rb +1 -0
- data/lib/babl/operators/partial.rb +1 -0
- data/lib/babl/operators/pin.rb +1 -0
- data/lib/babl/operators/source.rb +1 -0
- data/lib/babl/operators/static.rb +1 -0
- data/lib/babl/operators/switch.rb +1 -0
- data/lib/babl/operators/typed.rb +1 -0
- data/lib/babl/operators/with.rb +1 -0
- data/lib/babl/railtie.rb +1 -0
- data/lib/babl/rendering.rb +1 -0
- data/lib/babl/rendering/compiled_template.rb +1 -0
- data/lib/babl/rendering/context.rb +1 -10
- data/lib/babl/rendering/noop_preloader.rb +1 -0
- data/lib/babl/schema.rb +1 -0
- data/lib/babl/schema/any_of.rb +15 -17
- data/lib/babl/schema/anything.rb +1 -0
- data/lib/babl/schema/dyn_array.rb +1 -0
- data/lib/babl/schema/fixed_array.rb +1 -0
- data/lib/babl/schema/object.rb +6 -8
- data/lib/babl/schema/static.rb +6 -6
- data/lib/babl/schema/typed.rb +6 -5
- data/lib/babl/template.rb +1 -0
- data/lib/babl/utils.rb +1 -0
- data/lib/babl/utils/hash.rb +1 -0
- data/lib/babl/utils/ref.rb +1 -0
- data/lib/babl/utils/value.rb +1 -0
- data/lib/babl/version.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ba3e05ba09552694ee2869f3105feb91ec3459c
|
4
|
+
data.tar.gz: 1c5c24a986f264dc33db0d48a5f0885621c56793
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4014ee582da9da899725759eb4b3f0242929c8870f7326de59a5e65416e17b92469dab7cbb7c960ab4d783f696e6aa5d0840cbde14ec0fc7f951de0012e915fd
|
7
|
+
data.tar.gz: e3d4615978a94bd19c48f7089fa0722613b10dc44411f32d9f27cd8dbdf27c18f6dd62f59a9a0880fc22fb74991afbfcb111bb099e090d03f4f2c4523366dd63
|
data/lib/babl.rb
CHANGED
data/lib/babl/builder.rb
CHANGED
data/lib/babl/errors.rb
CHANGED
data/lib/babl/nodes.rb
CHANGED
data/lib/babl/nodes/dep.rb
CHANGED
data/lib/babl/nodes/each.rb
CHANGED
data/lib/babl/nodes/goto_pin.rb
CHANGED
data/lib/babl/nodes/merge.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/errors'
|
2
3
|
require 'babl/utils'
|
3
4
|
require 'babl/schema'
|
@@ -59,9 +60,9 @@ module Babl
|
|
59
60
|
|
60
61
|
case
|
61
62
|
when Schema::AnyOf === doc1
|
62
|
-
Schema::AnyOf.canonicalized(doc1.
|
63
|
+
Schema::AnyOf.canonicalized(doc1.choice_set.map { |c| merge_doc(c, doc2) })
|
63
64
|
when Schema::AnyOf === doc2
|
64
|
-
Schema::AnyOf.canonicalized(doc2.
|
65
|
+
Schema::AnyOf.canonicalized(doc2.choice_set.map { |c| merge_doc(doc1, c) })
|
65
66
|
when Schema::Object === doc1 && Schema::Object === doc2
|
66
67
|
merge_object(doc1, doc2)
|
67
68
|
else raise Errors::InvalidTemplate, 'Only objects can be merged'
|
@@ -73,8 +74,8 @@ module Babl
|
|
73
74
|
additional = doc1.additional || doc2.additional
|
74
75
|
|
75
76
|
properties = (
|
76
|
-
doc1.
|
77
|
-
doc2.
|
77
|
+
doc1.property_set.map { |property| doc2.additional ? anything_property(property) : property } +
|
78
|
+
doc2.property_set.to_a
|
78
79
|
).each_with_object({}) { |property, acc| acc[property.name] = property }.values
|
79
80
|
|
80
81
|
Schema::Object.new(properties, additional)
|
data/lib/babl/nodes/nav.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/utils'
|
2
3
|
|
3
4
|
module Babl
|
@@ -16,17 +17,17 @@ module Babl
|
|
16
17
|
end
|
17
18
|
|
18
19
|
def render(ctx)
|
19
|
-
|
20
|
+
value = begin
|
21
|
+
::Hash === ctx.object ? ctx.object.fetch(through) : ctx.object.send(through)
|
22
|
+
rescue StandardError => e
|
23
|
+
raise Errors::RenderingError, "#{e.message}\n" + ctx.formatted_stack(through), e.backtrace
|
24
|
+
end
|
25
|
+
node.render(ctx.move_forward(value, through))
|
20
26
|
end
|
21
27
|
|
22
28
|
private
|
23
29
|
|
24
30
|
def navigate(object)
|
25
|
-
if ::Hash === object
|
26
|
-
object.fetch(through)
|
27
|
-
else
|
28
|
-
object.send(through)
|
29
|
-
end
|
30
31
|
end
|
31
32
|
end
|
32
33
|
end
|
data/lib/babl/nodes/object.rb
CHANGED
data/lib/babl/nodes/parent.rb
CHANGED
data/lib/babl/nodes/static.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/schema'
|
2
3
|
require 'babl/utils'
|
3
4
|
|
@@ -5,7 +6,12 @@ module Babl
|
|
5
6
|
module Nodes
|
6
7
|
class Static < Utils::Value.new(:value)
|
7
8
|
def schema
|
8
|
-
|
9
|
+
case value
|
10
|
+
when ::NilClass then Schema::Static::NULL
|
11
|
+
when ::TrueClass then Schema::Static::TRUE
|
12
|
+
when ::FalseClass then Schema::Static::FALSE
|
13
|
+
else Schema::Static.new(value)
|
14
|
+
end
|
9
15
|
end
|
10
16
|
|
11
17
|
def render(_ctx)
|
data/lib/babl/nodes/switch.rb
CHANGED
data/lib/babl/nodes/typed.rb
CHANGED
data/lib/babl/nodes/with.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/utils'
|
2
3
|
|
3
4
|
module Babl
|
@@ -19,9 +20,12 @@ module Babl
|
|
19
20
|
|
20
21
|
def render(ctx)
|
21
22
|
values = nodes.map { |n| n.render(ctx) }
|
22
|
-
|
23
|
+
value = begin
|
23
24
|
block.arity.zero? ? ctx.object.instance_exec(&block) : block.call(*values)
|
24
|
-
|
25
|
+
rescue StandardError => e
|
26
|
+
raise Errors::RenderingError, "#{e.message}\n" + ctx.formatted_stack(:__block__), e.backtrace
|
27
|
+
end
|
28
|
+
node.render(ctx.move_forward(value, :__block__))
|
25
29
|
end
|
26
30
|
end
|
27
31
|
end
|
data/lib/babl/operators.rb
CHANGED
data/lib/babl/operators/array.rb
CHANGED
data/lib/babl/operators/call.rb
CHANGED
data/lib/babl/operators/dep.rb
CHANGED
data/lib/babl/operators/each.rb
CHANGED
data/lib/babl/operators/enter.rb
CHANGED
data/lib/babl/operators/merge.rb
CHANGED
data/lib/babl/operators/nav.rb
CHANGED
data/lib/babl/operators/null.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Babl
|
2
3
|
module Operators
|
3
4
|
module Nullable
|
4
5
|
module DSL
|
6
|
+
IS_NIL = ->(val) { ::NilClass === val }
|
7
|
+
|
5
8
|
# Nullify the current construction if
|
6
9
|
# the current element is Nil.
|
7
10
|
def nullable
|
8
11
|
source {
|
9
12
|
switch(
|
10
|
-
nav(
|
13
|
+
nav(&IS_NIL) => nil,
|
11
14
|
default => continue
|
12
15
|
)
|
13
16
|
}
|
data/lib/babl/operators/pin.rb
CHANGED
data/lib/babl/operators/typed.rb
CHANGED
data/lib/babl/operators/with.rb
CHANGED
data/lib/babl/railtie.rb
CHANGED
data/lib/babl/rendering.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/errors'
|
2
3
|
require 'babl/utils'
|
3
4
|
|
@@ -43,16 +44,6 @@ module Babl
|
|
43
44
|
Context.new(object, key, parent, (pins || Utils::Hash::EMPTY).merge(ref => self))
|
44
45
|
end
|
45
46
|
|
46
|
-
# Wrapper around #move_forward navigating into the return value of
|
47
|
-
# the block. However, if an error occurs, it is wrapped in a
|
48
|
-
# RenderingError and the navigation stack trace is added
|
49
|
-
# to the error message.
|
50
|
-
def move_forward_block(key)
|
51
|
-
move_forward(yield, key)
|
52
|
-
rescue StandardError => e
|
53
|
-
raise Errors::RenderingError, "#{e.message}\n" + formatted_stack(key), e.backtrace
|
54
|
-
end
|
55
|
-
|
56
47
|
def formatted_stack(*additional_stack_items)
|
57
48
|
stack_trace = ([:__root__] + stack + additional_stack_items).join('.')
|
58
49
|
"BABL @ #{stack_trace}"
|
data/lib/babl/schema.rb
CHANGED
data/lib/babl/schema/any_of.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/utils'
|
2
3
|
require 'babl/schema'
|
3
4
|
require 'set'
|
@@ -5,16 +6,13 @@ require 'set'
|
|
5
6
|
module Babl
|
6
7
|
module Schema
|
7
8
|
class AnyOf < Utils::Value.new(:choice_set)
|
8
|
-
attr_reader :choices
|
9
|
-
|
10
9
|
def initialize(choices)
|
11
|
-
flattened_choices = choices.flat_map { |doc| AnyOf === doc ? doc.
|
12
|
-
|
13
|
-
super(flattened_choices.to_set)
|
10
|
+
flattened_choices = choices.flat_map { |doc| AnyOf === doc ? doc.choice_set.to_a : [doc] }.uniq
|
11
|
+
super(flattened_choices.to_set.freeze)
|
14
12
|
end
|
15
13
|
|
16
14
|
def json
|
17
|
-
{ anyOf:
|
15
|
+
{ anyOf: choice_set.map(&:json) }
|
18
16
|
end
|
19
17
|
|
20
18
|
# Perform simple transformations in order to reduce the size of the generated
|
@@ -39,7 +37,7 @@ module Babl
|
|
39
37
|
|
40
38
|
# We can completely get rid of the AnyOf element of there is only one possible schema.
|
41
39
|
def simplify_single
|
42
|
-
|
40
|
+
choice_set.size == 1 ? choice_set.first : nil
|
43
41
|
end
|
44
42
|
|
45
43
|
# AnyOf[anything, string, number...] always collapse to anything.
|
@@ -58,9 +56,9 @@ module Babl
|
|
58
56
|
# AnyOf[number, 2, 3.1] is just number
|
59
57
|
# AnyOf[integer, 2, 1] is just integer
|
60
58
|
def simplify_typed_and_static
|
61
|
-
|
59
|
+
choice_set.each do |typed|
|
62
60
|
next unless Typed === typed
|
63
|
-
instances =
|
61
|
+
instances = choice_set.select { |instance|
|
64
62
|
Static === instance && typed.classes.any? { |clazz| clazz === instance.value }
|
65
63
|
}
|
66
64
|
next if instances.empty?
|
@@ -84,10 +82,10 @@ module Babl
|
|
84
82
|
# If the static array is an instance of another dyn array, then the fixed array can be
|
85
83
|
# removed.
|
86
84
|
def simplify_dyn_and_fixed_array
|
87
|
-
fixed_arrays =
|
85
|
+
fixed_arrays = choice_set.select { |s| FixedArray === s && s.items.uniq.size == 1 }
|
88
86
|
return if fixed_arrays.empty?
|
89
87
|
|
90
|
-
|
88
|
+
choice_set.each do |dyn|
|
91
89
|
next unless DynArray === dyn
|
92
90
|
fixed_arrays.each do |fixed|
|
93
91
|
new_dyn = DynArray.new(dyn.item)
|
@@ -102,15 +100,15 @@ module Babl
|
|
102
100
|
# of the generated schema. On top of that, when the JSON-Schema is translated into Typescript, it produces
|
103
101
|
# a much more workable type definition (union of anonymous object types is not practical to use)
|
104
102
|
def simplify_merge_objects
|
105
|
-
|
103
|
+
choice_set.each_with_index { |obj1, index1|
|
106
104
|
next unless Object === obj1
|
107
105
|
|
108
|
-
|
106
|
+
choice_set.each_with_index { |obj2, index2|
|
109
107
|
break if index2 >= index1
|
110
108
|
next unless Object === obj2
|
111
109
|
|
112
|
-
obj1props = obj1.
|
113
|
-
obj2props = obj2.
|
110
|
+
obj1props = obj1.property_set.map { |p| [p.name, p] }.to_h
|
111
|
+
obj2props = obj2.property_set.map { |p| [p.name, p] }.to_h
|
114
112
|
|
115
113
|
# Do not merge properties unless a keyset is almost a subset of the other
|
116
114
|
next unless (obj1props.keys.to_set - obj2props.keys).size <= 1 ||
|
@@ -147,9 +145,9 @@ module Babl
|
|
147
145
|
|
148
146
|
# Push down the AnyOf to the item if all outputs are of type DynArray
|
149
147
|
def simplify_push_down_dyn_array
|
150
|
-
|
148
|
+
choice_set.each_with_index { |arr1, index1|
|
151
149
|
next unless DynArray === arr1
|
152
|
-
|
150
|
+
choice_set.each_with_index { |arr2, index2|
|
153
151
|
break if index2 >= index1
|
154
152
|
next unless DynArray === arr2
|
155
153
|
new_arr = DynArray.new(AnyOf.canonicalized([arr1.item, arr2.item]))
|
data/lib/babl/schema/anything.rb
CHANGED
data/lib/babl/schema/object.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/utils'
|
2
3
|
require 'set'
|
3
4
|
|
4
5
|
module Babl
|
5
6
|
module Schema
|
6
7
|
class Object < Utils::Value.new(:property_set, :additional)
|
7
|
-
attr_reader :properties
|
8
|
-
|
9
8
|
def initialize(properties, additional)
|
10
|
-
|
11
|
-
super(properties.to_set, additional)
|
9
|
+
super(properties.to_set.freeze, additional)
|
12
10
|
end
|
13
11
|
|
14
12
|
EMPTY = new([], false)
|
@@ -22,12 +20,12 @@ module Babl
|
|
22
20
|
|
23
21
|
def json
|
24
22
|
{ type: 'object' }.tap { |out|
|
25
|
-
next if
|
26
|
-
out[:properties] =
|
23
|
+
next if property_set.empty?
|
24
|
+
out[:properties] = property_set.map { |property| [property.name, property.value.json] }.to_h
|
27
25
|
out[:additionalProperties] = additional
|
28
|
-
required_properties =
|
26
|
+
required_properties = property_set.select(&:required)
|
29
27
|
next if required_properties.empty?
|
30
|
-
out[:required] =
|
28
|
+
out[:required] = property_set.select(&:required).map(&:name).map(&:to_s)
|
31
29
|
}
|
32
30
|
end
|
33
31
|
end
|
data/lib/babl/schema/static.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/utils'
|
2
3
|
|
3
4
|
module Babl
|
4
5
|
module Schema
|
5
|
-
class Static < Utils::Value.new(:value)
|
6
|
+
class Static < Utils::Value.new(:value, :json)
|
7
|
+
def initialize(value)
|
8
|
+
super(value, ::NilClass === value ? { type: 'null' } : { enum: [value] })
|
9
|
+
end
|
10
|
+
|
6
11
|
NULL = new(nil)
|
7
12
|
TRUE = new(true)
|
8
13
|
FALSE = new(false)
|
9
|
-
|
10
|
-
def json
|
11
|
-
return { type: 'null' } if value.nil?
|
12
|
-
{ enum: [value] }
|
13
|
-
end
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
data/lib/babl/schema/typed.rb
CHANGED
@@ -1,16 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'babl/utils'
|
2
3
|
|
3
4
|
module Babl
|
4
5
|
module Schema
|
5
|
-
class Typed < Utils::Value.new(:type, :classes)
|
6
|
+
class Typed < Utils::Value.new(:type, :classes, :json)
|
7
|
+
def initialize(type, classes)
|
8
|
+
super(type.freeze, classes.freeze, { type: type }.freeze)
|
9
|
+
end
|
10
|
+
|
6
11
|
INTEGER = new('integer', [::Integer])
|
7
12
|
BOOLEAN = new('boolean', [::TrueClass, ::FalseClass])
|
8
13
|
NUMBER = new('number', [::Numeric])
|
9
14
|
STRING = new('string', [::String])
|
10
|
-
|
11
|
-
def json
|
12
|
-
{ type: type }
|
13
|
-
end
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
data/lib/babl/template.rb
CHANGED
data/lib/babl/utils.rb
CHANGED
data/lib/babl/utils/hash.rb
CHANGED
data/lib/babl/utils/ref.rb
CHANGED
data/lib/babl/utils/value.rb
CHANGED
data/lib/babl/version.rb
CHANGED
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.
|
4
|
+
version: 0.2.6
|
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-08-
|
11
|
+
date: 2017-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|