babl-json 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89d572b099ed6dd5b4f7a8d330d62b82f2c614a9
4
- data.tar.gz: a5a54bfc4ea3d28cba05a5285f401faa5708546c
3
+ metadata.gz: 3b3d925354aed5c77de3cf7003b305b6de01b71f
4
+ data.tar.gz: 24ffd681b9f421b971ac5340b1896608cb803313
5
5
  SHA512:
6
- metadata.gz: 80a16645c833210146dd440920895f9d0a53bbea5a06b213ec5c50d396dcf059f3378bbb5da6b23771a035e99abe833c55125a731f92b7d22664216e1883105e
7
- data.tar.gz: 14926b91b5690491bd9ae7d69123950167bfe823bda592ad6ac72e9dccf009dee817ad74f6176cd7bdee85f343bee10928059a804b65ea4701c547d241683c89
6
+ metadata.gz: a935bea9bf8a01a924719526f7d26d1c5bdb016c6e64b3be08ae1040f6126b4aac8cc7dc032c38b7f2f2d1fbfc8ed9a53a091c6f7634b4c4c7b1cd894d7a7f6e
7
+ data.tar.gz: 1fc88f586cd05f30dbcf2a766611b6bb2208006722b14ab53beceeffcacce5b1cb00397cfb3dab048497cd7f41a944810ab1e229f24c0b254bb2ab124355fabe
@@ -12,7 +12,7 @@ module Babl
12
12
  end
13
13
 
14
14
  def dependencies
15
- Babl::Utils::Hash.deep_merge(node.dependencies, node.pinned_dependencies[ref] || {})
15
+ Babl::Utils::Hash.deep_merge(node.dependencies, node.pinned_dependencies[ref] || Utils::Hash::EMPTY)
16
16
  end
17
17
 
18
18
  def pinned_dependencies
@@ -27,9 +27,9 @@ module Babl
27
27
 
28
28
  def canonicalize(path)
29
29
  case path
30
- when ::Array then path.reduce({}) { |a, p| a.merge(canonicalize(p)) }
30
+ when ::Array then path.reduce(Utils::Hash::EMPTY) { |a, p| a.merge(canonicalize(p)) }
31
31
  when ::Hash then path.map { |k, v| [k.to_sym, canonicalize(v)] }.to_h
32
- else { path.to_sym => {} }
32
+ else { path.to_sym => Utils::Hash::EMPTY }
33
33
  end
34
34
  end
35
35
  end
@@ -9,11 +9,11 @@ module Babl
9
9
  end
10
10
 
11
11
  def dependencies
12
- nodes.map(&:dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
12
+ nodes.map(&:dependencies).reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
13
13
  end
14
14
 
15
15
  def pinned_dependencies
16
- nodes.map(&:pinned_dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
16
+ nodes.map(&:pinned_dependencies).reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
17
17
  end
18
18
 
19
19
  def render(ctx)
@@ -4,7 +4,7 @@ module Babl
4
4
  module Nodes
5
5
  class GotoPin < Utils::Value.new(:node, :ref)
6
6
  def dependencies
7
- {}
7
+ Utils::Hash::EMPTY
8
8
  end
9
9
 
10
10
  def pinned_dependencies
@@ -1,4 +1,5 @@
1
1
  require 'babl/errors'
2
+ require 'babl/utils'
2
3
  require 'singleton'
3
4
 
4
5
  module Babl
@@ -16,11 +17,11 @@ module Babl
16
17
  end
17
18
 
18
19
  def dependencies
19
- {}
20
+ Utils::Hash::EMPTY
20
21
  end
21
22
 
22
23
  def pinned_dependencies
23
- {}
24
+ Utils::Hash::EMPTY
24
25
  end
25
26
 
26
27
  def render(ctx)
@@ -6,11 +6,11 @@ module Babl
6
6
  module Nodes
7
7
  class Merge < Utils::Value.new(:nodes)
8
8
  def dependencies
9
- nodes.map(&:dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
9
+ nodes.map(&:dependencies).reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
10
10
  end
11
11
 
12
12
  def pinned_dependencies
13
- nodes.map(&:pinned_dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
13
+ nodes.map(&:pinned_dependencies).reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
14
14
  end
15
15
 
16
16
  def schema
@@ -24,31 +24,47 @@ module Babl
24
24
  }
25
25
  end
26
26
 
27
+ def self.build(nodes)
28
+ return new(nodes) if nodes.empty?
29
+ return nodes.first if nodes.size == 1 && Object === nodes.first
30
+
31
+ expanded = nodes.flat_map { |node| Merge === node ? node.nodes : [node] }
32
+ out = []
33
+ op1 = nil
34
+ op2 = expanded[0]
35
+ expanded.drop(1).each do |elm|
36
+ op1 = op2
37
+ op2 = elm
38
+ if Object === op1 && Object === op2
39
+ op2 = Object.new(op1.nodes.merge(op2.nodes))
40
+ op1 = nil
41
+ end
42
+ out << op1 if op1
43
+ end
44
+ out << op2 if op2
45
+ new(out)
46
+ end
47
+
27
48
  private
28
49
 
50
+ AS_OBJECT_MAPPING = {
51
+ Schema::Anything.instance => Schema::Object::EMPTY_WITH_ADDITIONAL,
52
+ Schema::Static::NULL => Schema::Object::EMPTY
53
+ }
54
+
29
55
  # Merge two documentations together
30
56
  def merge_doc(doc1, doc2)
57
+ doc1 = AS_OBJECT_MAPPING[doc1] || doc1
58
+ doc2 = AS_OBJECT_MAPPING[doc2] || doc2
59
+
31
60
  case
32
- when Schema::Static::NULL == doc1
33
- merge_doc(Schema::Object::EMPTY, doc2)
34
- when Schema::Static::NULL == doc2
35
- merge_doc(doc1, Schema::Object::EMPTY)
36
61
  when Schema::AnyOf === doc1
37
- Schema::AnyOf.canonicalized(doc1.choices.map { |c|
38
- merge_doc(c, doc2)
39
- })
62
+ Schema::AnyOf.canonicalized(doc1.choices.map { |c| merge_doc(c, doc2) })
40
63
  when Schema::AnyOf === doc2
41
- Schema::AnyOf.canonicalized(doc2.choices.map { |c|
42
- merge_doc(doc1, c)
43
- })
64
+ Schema::AnyOf.canonicalized(doc2.choices.map { |c| merge_doc(doc1, c) })
44
65
  when Schema::Object === doc1 && Schema::Object === doc2
45
66
  merge_object(doc1, doc2)
46
- when Schema::Object === doc1 && Schema::Anything === doc2
47
- merge_object(doc1, Schema::Object::EMPTY_WITH_ADDITIONAL)
48
- when Schema::Anything === doc1 && Schema::Object === doc2
49
- merge_object(Schema::Object::EMPTY_WITH_ADDITIONAL, doc2)
50
- else
51
- raise Errors::InvalidTemplate, 'Only objects can be merged'
67
+ else raise Errors::InvalidTemplate, 'Only objects can be merged'
52
68
  end
53
69
  end
54
70
 
@@ -57,7 +73,7 @@ module Babl
57
73
  additional = doc1.additional || doc2.additional
58
74
 
59
75
  properties = (
60
- doc1.properties.map { |property| doc2.additional ? allow_anything(property) : property } +
76
+ doc1.properties.map { |property| doc2.additional ? anything_property(property) : property } +
61
77
  doc2.properties
62
78
  ).each_with_object({}) { |property, acc| acc[property.name] = property }.values
63
79
 
@@ -65,12 +81,8 @@ module Babl
65
81
  end
66
82
 
67
83
  # Rewrite a property to allow Schema::Anything as value
68
- def allow_anything(property)
69
- Schema::Object::Property.new(
70
- property.name,
71
- Schema::AnyOf.canonicalized([property.value, Schema::Anything.instance]),
72
- property.required
73
- )
84
+ def anything_property(property)
85
+ Schema::Object::Property.new(property.name, Schema::Anything.instance, property.required)
74
86
  end
75
87
  end
76
88
  end
@@ -5,11 +5,12 @@ module Babl
5
5
  module Nodes
6
6
  class Object < Utils::Value.new(:nodes)
7
7
  def dependencies
8
- nodes.values.map(&:dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
8
+ nodes.values.map(&:dependencies).reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
9
9
  end
10
10
 
11
11
  def pinned_dependencies
12
- nodes.values.map(&:pinned_dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
12
+ nodes.values.map(&:pinned_dependencies)
13
+ .reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
13
14
  end
14
15
 
15
16
  def schema
@@ -32,12 +32,12 @@ module Babl
32
32
  end
33
33
 
34
34
  def backpropagate_dependencies_one_level(deps)
35
- deps.reduce({}) do |out, (k, v)|
35
+ deps.reduce(Utils::Hash::EMPTY) do |out, (k, v)|
36
36
  next out if k == PARENT_MARKER
37
37
 
38
38
  Babl::Utils::Hash.deep_merge(
39
39
  Babl::Utils::Hash.deep_merge(out, k => backpropagate_dependencies_one_level(v)),
40
- v[PARENT_MARKER] || {}
40
+ v[PARENT_MARKER] || Utils::Hash::EMPTY
41
41
  )
42
42
  end
43
43
  end
@@ -13,11 +13,11 @@ module Babl
13
13
  end
14
14
 
15
15
  def dependencies
16
- {}
16
+ Utils::Hash::EMPTY
17
17
  end
18
18
 
19
19
  def pinned_dependencies
20
- {}
20
+ Utils::Hash::EMPTY
21
21
  end
22
22
 
23
23
  private
@@ -7,12 +7,12 @@ module Babl
7
7
  class Switch < Utils::Value.new(:nodes)
8
8
  def dependencies
9
9
  (nodes.values + nodes.keys).map(&:dependencies)
10
- .reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
10
+ .reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
11
11
  end
12
12
 
13
13
  def pinned_dependencies
14
14
  (nodes.values + nodes.keys).map(&:pinned_dependencies)
15
- .reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
15
+ .reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
16
16
  end
17
17
 
18
18
  def schema
@@ -1,5 +1,6 @@
1
1
  require 'babl/schema'
2
2
  require 'babl/errors'
3
+ require 'babl/utils'
3
4
  require 'singleton'
4
5
 
5
6
  module Babl
@@ -15,11 +16,11 @@ module Babl
15
16
  end
16
17
 
17
18
  def dependencies
18
- {}
19
+ Utils::Hash::EMPTY
19
20
  end
20
21
 
21
22
  def pinned_dependencies
22
- {}
23
+ Utils::Hash::EMPTY
23
24
  end
24
25
 
25
26
  def render(ctx)
@@ -3,40 +3,25 @@ require 'babl/schema'
3
3
 
4
4
  module Babl
5
5
  module Nodes
6
- class Typed < Utils::Value.new(:schema, :node)
7
- CURRIED = method(:new).curry(2)
8
-
9
- BOOLEAN = CURRIED.call(Schema::Typed::BOOLEAN)
10
- INTEGER = CURRIED.call(Schema::Typed::INTEGER)
11
- NUMBER = CURRIED.call(Schema::Typed::NUMBER)
12
- STRING = CURRIED.call(Schema::Typed::STRING)
6
+ class Typed < Utils::Value.new(:schema)
7
+ BOOLEAN = new(Schema::Typed::BOOLEAN)
8
+ INTEGER = new(Schema::Typed::INTEGER)
9
+ NUMBER = new(Schema::Typed::NUMBER)
10
+ STRING = new(Schema::Typed::STRING)
13
11
 
14
12
  def dependencies
15
- node.dependencies
13
+ Utils::Hash::EMPTY
16
14
  end
17
15
 
18
16
  def pinned_dependencies
19
- node.pinned_dependencies
17
+ Utils::Hash::EMPTY
20
18
  end
21
19
 
22
20
  def render(ctx)
23
- value = node.render(ctx)
21
+ value = ctx.object
24
22
  return value if schema.classes.any? { |clazz| clazz === value }
25
23
  raise Errors::RenderingError, "Expected type '#{schema.type}': #{value}\n#{ctx.formatted_stack}"
26
24
  end
27
-
28
- private
29
-
30
- def initialize(*)
31
- super
32
- check_type_compatibility
33
- end
34
-
35
- def check_type_compatibility
36
- return if schema == node.schema
37
- return if node.schema == Schema::Anything.instance
38
- raise Errors::InvalidTemplate, "Type cannot be '#{schema.type}' in this context"
39
- end
40
25
  end
41
26
  end
42
27
  end
@@ -9,11 +9,12 @@ module Babl
9
9
 
10
10
  def dependencies
11
11
  # Dependencies of 'node' are explicitely ignored
12
- nodes.map(&:dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
12
+ nodes.map(&:dependencies).reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
13
13
  end
14
14
 
15
15
  def pinned_dependencies
16
- (nodes + [node]).map(&:pinned_dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
16
+ (nodes + [node]).map(&:pinned_dependencies)
17
+ .reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
17
18
  end
18
19
 
19
20
  def render(ctx)
@@ -1,4 +1,5 @@
1
1
  require 'babl/nodes'
2
+ require 'babl/utils'
2
3
 
3
4
  module Babl
4
5
  module Operators
@@ -6,10 +7,10 @@ module Babl
6
7
  module DSL
7
8
  # Merge multiple JSON objects (non-deep)
8
9
  def merge(*templates)
9
- return call({}) if templates.empty?
10
+ return call(Utils::Hash::EMPTY) if templates.empty?
10
11
 
11
12
  construct_terminal { |context|
12
- Nodes::Merge.new(
13
+ Nodes::Merge.build(
13
14
  templates.map { |t|
14
15
  unscoped.call(t).builder.precompile(
15
16
  Nodes::TerminalValue.instance,
@@ -1,11 +1,12 @@
1
1
  require 'babl/nodes'
2
+ require 'babl/utils'
2
3
 
3
4
  module Babl
4
5
  module Operators
5
6
  module Switch
6
7
  module DSL
7
8
  # Conditional switching between different templates
8
- def switch(conds = {})
9
+ def switch(conds = Utils::Hash::EMPTY)
9
10
  construct_node(continue: nil) { |node, context|
10
11
  nodes = conds.map { |cond, value|
11
12
  cond_node = unscoped.call(cond).builder
@@ -5,19 +5,19 @@ module Babl
5
5
  module Typed
6
6
  module DSL
7
7
  def integer
8
- construct_node(continue: nil) { |node| Nodes::Typed::INTEGER.call(node) }
8
+ construct_terminal { Nodes::Typed::INTEGER }
9
9
  end
10
10
 
11
11
  def number
12
- construct_node(continue: nil) { |node| Nodes::Typed::NUMBER.call(node) }
12
+ construct_terminal { Nodes::Typed::NUMBER }
13
13
  end
14
14
 
15
15
  def string
16
- construct_node(continue: nil) { |node| Nodes::Typed::STRING.call(node) }
16
+ construct_terminal { Nodes::Typed::STRING }
17
17
  end
18
18
 
19
19
  def boolean
20
- construct_node(continue: nil) { |node| Nodes::Typed::BOOLEAN.call(node) }
20
+ construct_terminal { Nodes::Typed::BOOLEAN }
21
21
  end
22
22
  end
23
23
  end
@@ -1,4 +1,5 @@
1
1
  require 'babl/errors'
2
+ require 'babl/utils'
2
3
 
3
4
  module Babl
4
5
  module Rendering
@@ -34,12 +35,12 @@ module Babl
34
35
  def goto_pin(ref)
35
36
  pin = pins&.[](ref)
36
37
  raise Errors::RenderingError, 'Pin reference cannot be used here' unless pin
37
- Context.new(pin.object, pin.key, pin.parent, (pin.pins || {}).merge(pins))
38
+ Context.new(pin.object, pin.key, pin.parent, (pin.pins || Utils::Hash::EMPTY).merge(pins))
38
39
  end
39
40
 
40
41
  # Associate a pin to current context
41
42
  def create_pin(ref)
42
- Context.new(object, key, parent, (pins || {}).merge(ref => self))
43
+ Context.new(object, key, parent, (pins || Utils::Hash::EMPTY).merge(ref => self))
43
44
  end
44
45
 
45
46
  # Wrapper around #move_forward navigating into the return value of
@@ -1,3 +1,4 @@
1
+ require 'babl/utils'
1
2
  require 'singleton'
2
3
 
3
4
  module Babl
@@ -6,7 +7,7 @@ module Babl
6
7
  include Singleton
7
8
 
8
9
  def json
9
- {}
10
+ Utils::Hash::EMPTY
10
11
  end
11
12
  end
12
13
  end
@@ -1,6 +1,8 @@
1
1
  module Babl
2
2
  module Utils
3
3
  class Hash
4
+ EMPTY = {}.freeze
5
+
4
6
  # Source: http://stackoverflow.com/a/9381776/1434017 (Jon M)
5
7
  def self.deep_merge(first, second)
6
8
  merger = proc { |_key, v1, v2| ::Hash === v1 && ::Hash === v2 ? v1.merge(v2, &merger) : v2 }
@@ -1,3 +1,5 @@
1
+ require 'babl/utils'
2
+
1
3
  module Babl
2
4
  module Utils
3
5
  # Construct deeply immutable value objects
@@ -25,7 +27,7 @@ module Babl
25
27
  eql?(other)
26
28
  end
27
29
 
28
- def self.with(hash = {})
30
+ def self.with(hash = Utils::Hash::EMPTY)
29
31
  raise ::ArgumentError unless ::Hash === hash && (hash.keys - FIELDS).empty?
30
32
  new(*FIELDS.map { |f| hash.fetch(f) })
31
33
  end
data/lib/babl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Babl
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
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.4
4
+ version: 0.2.5
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-17 00:00:00.000000000 Z
11
+ date: 2017-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry