babl-json 0.5.1 → 0.5.2

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: e01613db4c4f669f2f64a0bdbc324bfe38074b44
4
- data.tar.gz: 62b10c1b72ad3f2773f3e655099d90453d3f6b1d
3
+ metadata.gz: f5116d890ae716b94f6fcb09fd90591313beb68d
4
+ data.tar.gz: 4bba953360594c5f46ad6160870a2ab469a932af
5
5
  SHA512:
6
- metadata.gz: 3ca13bb367880694af386e52197753e1e4a1a85aa624bc59c05af691fe98227e840ea5c54e0124eb5401988aa01393657c90c92abd278c3230664046ff638406
7
- data.tar.gz: bcbecaa3869e2c8f2c04c4fc565eef473ae5219a39d92c2d6c5dd950eaeaba2d03f66017df98859ecef1ee48e6da9ff919452a76965644ddad32324035553960
6
+ metadata.gz: 4467e3975a1f2f3130f68dc8fed5327b608838f2bcf8eebe61fae29fd5608ecc9de328192a2a2c5797ad54cce7ea68b6e9dc684c7c11ccf1c4a479f0147c54b9
7
+ data.tar.gz: effc3f204316f2d67f0e74a8f6704fedae840b03ff41129697316c06f169d3a7363a2ccd3f0bfa9042f9398e5fe450b209290d7a1b7c38b1620aff426d086634
@@ -12,12 +12,7 @@ module Babl
12
12
  end
13
13
 
14
14
  def json
15
- primitives, complex = choice_set.partition { |choice| Primitive === choice }
16
-
17
- simple_enum = { enum: primitives.map(&:value) }
18
- return simple_enum if complex.empty?
19
-
20
- { anyOf: complex.map(&:json) + (primitives.empty? ? [] : [simple_enum]) }
15
+ json_only_primitives || json_coalesced_types || json_general_case
21
16
  end
22
17
 
23
18
  # Perform simple transformations in order to reduce the size of the generated
@@ -42,6 +37,34 @@ module Babl
42
37
 
43
38
  private
44
39
 
40
+ def json_only_primitives
41
+ return { enum: choice_set.map(&:value) } if choice_set.all? { |c| Primitive === c }
42
+ end
43
+
44
+ def json_coalesced_types
45
+ remaining = choice_set.dup
46
+ json_types = []
47
+
48
+ [Primitive::NULL, Typed::INTEGER, Typed::BOOLEAN, Typed::NUMBER, Typed::STRING].each do |coalescible|
49
+ if remaining.include?(coalescible)
50
+ json_types << coalescible.json[:type]
51
+ remaining.delete(coalescible)
52
+ end
53
+ end
54
+
55
+ # Note: ideally, we would like to turn :
56
+ # {"anyOf": [{"type": "null"},{"type": "object", ...}]}
57
+ # into
58
+ # {"type": ["null", "object"], ...}
59
+ # but https://github.com/bcherny/json-schema-to-typescript has trouble converting
60
+ # from the latter format and that's an issue for us.
61
+ return { type: json_types.uniq } if remaining.empty?
62
+ end
63
+
64
+ def json_general_case
65
+ { anyOf: choice_set.map(&:json) }
66
+ end
67
+
45
68
  def simplify_integer_is_number
46
69
  return unless choice_set.include?(Typed::INTEGER) && choice_set.include?(Typed::NUMBER)
47
70
  AnyOf.canonicalized(choice_set - [Typed::INTEGER])
data/lib/babl/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Babl
3
- VERSION = '0.5.1'
3
+ VERSION = '0.5.2'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babl-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederic Terrazzoni