tsjson 1.0.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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/lib/errors/cant_distinguish_type_error.rb +17 -0
  3. data/lib/errors/index.rb +12 -0
  4. data/lib/errors/list_validation_error.rb +34 -0
  5. data/lib/errors/literal_union_validation_error.rb +18 -0
  6. data/lib/errors/literal_validation_error.rb +16 -0
  7. data/lib/errors/not_enough_discriminators.rb +7 -0
  8. data/lib/errors/object_validation_error.rb +56 -0
  9. data/lib/errors/required_field_error.rb +7 -0
  10. data/lib/errors/scalar_union_validation_error.rb +18 -0
  11. data/lib/errors/scalar_validation_error.rb +16 -0
  12. data/lib/errors/unexpected_field_error.rb +7 -0
  13. data/lib/errors/unexpected_value_error.rb +16 -0
  14. data/lib/errors/validation_error.rb +16 -0
  15. data/lib/language/ast/kind.rb +25 -0
  16. data/lib/language/lexer/lexer.rb +452 -0
  17. data/lib/language/lexer/location.rb +20 -0
  18. data/lib/language/lexer/syntax_error.rb +89 -0
  19. data/lib/language/lexer/token.rb +34 -0
  20. data/lib/language/lexer/token_kind.rb +37 -0
  21. data/lib/language/lexer/utils.rb +32 -0
  22. data/lib/language/parser/parser.rb +437 -0
  23. data/lib/language/source.rb +109 -0
  24. data/lib/schema/schema.rb +48 -0
  25. data/lib/schema/schema_builder.rb +148 -0
  26. data/lib/tsjson.rb +1 -0
  27. data/lib/types/any.rb +15 -0
  28. data/lib/types/base.rb +19 -0
  29. data/lib/types/boolean.rb +17 -0
  30. data/lib/types/discriminator_map.rb +116 -0
  31. data/lib/types/enum.rb +47 -0
  32. data/lib/types/float.rb +17 -0
  33. data/lib/types/index.rb +27 -0
  34. data/lib/types/int.rb +17 -0
  35. data/lib/types/intersection.rb +72 -0
  36. data/lib/types/list.rb +33 -0
  37. data/lib/types/literal.rb +25 -0
  38. data/lib/types/literal_union.rb +48 -0
  39. data/lib/types/merge.rb +21 -0
  40. data/lib/types/null.rb +17 -0
  41. data/lib/types/object.rb +87 -0
  42. data/lib/types/scalar.rb +24 -0
  43. data/lib/types/scalar_union.rb +25 -0
  44. data/lib/types/string.rb +17 -0
  45. data/lib/types/union.rb +61 -0
  46. metadata +85 -0
@@ -0,0 +1,25 @@
1
+ module TSJSON
2
+ class ScalarUnion < Base
3
+ attr_reader :types
4
+
5
+ def initialize(types = [])
6
+ types.each do |t|
7
+ unless t.is_a?(ScalarType)
8
+ raise 'ScalarUnion may contain only Scalar types'
9
+ end
10
+ end
11
+
12
+ @types = types
13
+ end
14
+
15
+ def validate(value)
16
+ @types.each { |type| return true if type.valid?(value) }
17
+
18
+ raise ScalarUnionValidationError.new(
19
+ expected_types: @types.map(&:name),
20
+ received_type: value.class.name,
21
+ received_value: value
22
+ )
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ require_relative './scalar.rb'
2
+
3
+ module TSJSON
4
+ class StringType < ScalarType
5
+ def initialize
6
+ super('String')
7
+ end
8
+
9
+ def validate(value)
10
+ super(value) unless value.is_a?(::String)
11
+
12
+ true
13
+ end
14
+ end
15
+
16
+ TSJSONString = StringType.new
17
+ end
@@ -0,0 +1,61 @@
1
+ require_relative './merge.rb'
2
+ require_relative './discriminator_map.rb'
3
+
4
+ module TSJSON
5
+ class Union < Merge
6
+ def normal_form
7
+ unless @normalized
8
+ @normalized =
9
+ @types.reduce(Union.new) do |buffer, type|
10
+ if type.is_a?(Merge)
11
+ buffer.union_normal(type.normal_form)
12
+ else
13
+ buffer.union_normal(type)
14
+ end
15
+ end
16
+ end
17
+ @normalized
18
+ end
19
+
20
+ def intersect_normal(other)
21
+ # raise_if_not_composite(other)
22
+ @types.reduce(Union.new) do |u, t|
23
+ u.union_normal(t.intersect_normal(other))
24
+ end
25
+ end
26
+
27
+ def union_normal(other)
28
+ # raise_if_not_composite(other)
29
+
30
+ if other.is_a?(Union)
31
+ Union.new(@types.dup.concat(other.types), true)
32
+ else
33
+ Union.new(@types.dup.push(other), true)
34
+ end
35
+ end
36
+
37
+ def validate(object)
38
+ return normal_form.validate(object) unless @is_normal
39
+
40
+ discriminator_map.validate(object)
41
+ end
42
+
43
+ def to_s
44
+ @types.map(&:to_s).join(' | ')
45
+ end
46
+
47
+ def compile
48
+ return if @compiled
49
+ @compiled = true
50
+
51
+ return normal_form.compile unless @is_normal
52
+ discriminator_map
53
+ end
54
+
55
+ private
56
+
57
+ def discriminator_map
58
+ @discriminator_map ||= DiscriminatorMap.new(@types)
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tsjson
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - vitramir
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/errors/cant_distinguish_type_error.rb
20
+ - lib/errors/index.rb
21
+ - lib/errors/list_validation_error.rb
22
+ - lib/errors/literal_union_validation_error.rb
23
+ - lib/errors/literal_validation_error.rb
24
+ - lib/errors/not_enough_discriminators.rb
25
+ - lib/errors/object_validation_error.rb
26
+ - lib/errors/required_field_error.rb
27
+ - lib/errors/scalar_union_validation_error.rb
28
+ - lib/errors/scalar_validation_error.rb
29
+ - lib/errors/unexpected_field_error.rb
30
+ - lib/errors/unexpected_value_error.rb
31
+ - lib/errors/validation_error.rb
32
+ - lib/language/ast/kind.rb
33
+ - lib/language/lexer/lexer.rb
34
+ - lib/language/lexer/location.rb
35
+ - lib/language/lexer/syntax_error.rb
36
+ - lib/language/lexer/token.rb
37
+ - lib/language/lexer/token_kind.rb
38
+ - lib/language/lexer/utils.rb
39
+ - lib/language/parser/parser.rb
40
+ - lib/language/source.rb
41
+ - lib/schema/schema.rb
42
+ - lib/schema/schema_builder.rb
43
+ - lib/tsjson.rb
44
+ - lib/types/any.rb
45
+ - lib/types/base.rb
46
+ - lib/types/boolean.rb
47
+ - lib/types/discriminator_map.rb
48
+ - lib/types/enum.rb
49
+ - lib/types/float.rb
50
+ - lib/types/index.rb
51
+ - lib/types/int.rb
52
+ - lib/types/intersection.rb
53
+ - lib/types/list.rb
54
+ - lib/types/literal.rb
55
+ - lib/types/literal_union.rb
56
+ - lib/types/merge.rb
57
+ - lib/types/null.rb
58
+ - lib/types/object.rb
59
+ - lib/types/scalar.rb
60
+ - lib/types/scalar_union.rb
61
+ - lib/types/string.rb
62
+ - lib/types/union.rb
63
+ homepage:
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.1.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: ruby implementation for tsjson lang
85
+ test_files: []