camille 0.5.8 → 0.5.9

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
  SHA256:
3
- metadata.gz: 60434d249fd905789f0c9fd812d097163cda296655c56fde57569269d8188e39
4
- data.tar.gz: 422d993e72473270985d6ade710ae39e3ada83ef85942e83291176dc8bff9dcc
3
+ metadata.gz: fd8f26b87f7ddf2f3c951a19a24610abfe1bfac4f4da041568883cc371090895
4
+ data.tar.gz: bbecf051c37ec9da1eae0920a6ec42c09d159feef1f2be734beaef2871af7410
5
5
  SHA512:
6
- metadata.gz: 6561b6b4c62f85c22d28b27c9693a1dfe8a3cccaaf95251d3995b50b0457363ea82ef971fa03d10959bcf505ee0188b2189b6de5d3cd984930830bc2a9bdcd3e
7
- data.tar.gz: 4266ecec3ab26f2e1af40b83fe60d337ed81b8fa6f953200c14f6e1dfb2ffe9519ea2d0519882b4790cc34cc1c7f84b18fb8481d0ec3cba88636c73236998761
6
+ metadata.gz: 53bb5fd170e71b21687dadba5ab1ce6c5a81bcb1a4ede09e280103cd81693fb8e9f41a3a15af630b2d48b62f150f29c1f222d8308fff2636135c5653d9ca3da3
7
+ data.tar.gz: 47c9621bfdeb542e625f2115eb96b7d1d14fdecfd5f6cefe62bd6f92be2703d7e3e7518e96df26af3c692fb5887ac08ebe26db38eaeae8a8cfb059eeb07de839
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.9
4
+
5
+ ### Added
6
+
7
+ * Added intersection type
8
+ * Added #print to Camille::TypeError
9
+
10
+ ### Changed
11
+
12
+ * Typecheck will no longer remove fields from a hash if those fields are not presented in type schema
13
+
14
+ ### Fixed
15
+
16
+ * Fixed the wrong literal generated by an array of unions
17
+
3
18
  ## 0.5.8
4
19
 
5
20
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- camille (0.5.8)
4
+ camille (0.5.9)
5
5
  rails (>= 6.1, < 8)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -153,6 +153,8 @@ params(
153
153
  }[]
154
154
  # a union type is two types connected by '|'
155
155
  union: Number | String,
156
+ # an intersection type is two types connected by '&'
157
+ intersection: { id: Number } & { name: String },
156
158
  # a tuple type is several types put inside '[]'
157
159
  tuple: [Number, String, Boolean],
158
160
  # a field followed by '?' is optional, the same as in TypeScript
@@ -7,6 +7,10 @@ module Camille
7
7
  Camille::Types::Union.new(self, other)
8
8
  end
9
9
 
10
+ def & other
11
+ Camille::Types::Intersection.new(self, other)
12
+ end
13
+
10
14
  def []
11
15
  Camille::Types::Array.new(self)
12
16
  end
@@ -24,6 +28,10 @@ module Camille
24
28
  Camille::Type.instance(self) | other
25
29
  end
26
30
 
31
+ def self.& other
32
+ Camille::Type.instance(self) & other
33
+ end
34
+
27
35
  def self.[]
28
36
  Camille::Type.instance(self)[]
29
37
  end
@@ -17,5 +17,9 @@ module Camille
17
17
  def basic?
18
18
  !!@message
19
19
  end
20
+
21
+ def print io = STDOUT
22
+ Camille::TypeErrorPrinter.new(self).print io
23
+ end
20
24
  end
21
25
  end
@@ -0,0 +1,39 @@
1
+ module Camille
2
+ module Types
3
+ class Intersection < Camille::BasicType
4
+ class ArgumentError < ::ArgumentError; end
5
+
6
+ attr_reader :left, :right
7
+
8
+ def initialize left, right
9
+ @left = Camille::Type.instance left
10
+ @right = Camille::Type.instance right
11
+ end
12
+
13
+ def transform_and_check value
14
+ left_error, left_transformed = @left.transform_and_check value
15
+ if left_error
16
+ error = Camille::TypeError.new(
17
+ 'intersection.left' => left_error
18
+ )
19
+ [error, nil]
20
+ else
21
+ right_error, right_transformed = @right.transform_and_check left_transformed
22
+ if right_error
23
+ error = Camille::TypeError.new(
24
+ 'intersection.right' => right_error
25
+ )
26
+ [error, nil]
27
+ else
28
+ [nil, right_transformed]
29
+ end
30
+ end
31
+ end
32
+
33
+ def literal
34
+ "(#{@left.literal} & #{@right.literal})"
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -11,12 +11,17 @@ module Camille
11
11
 
12
12
  def transform_and_check value
13
13
  if value.is_a? Hash
14
- transform_and_check_results = @fields.map do |key, type|
15
- error, transformed = type.transform_and_check(value[key])
16
- if @optional_keys.include?(key) && !error && transformed.nil?
17
- nil
14
+ keys = (@fields.keys + value.keys).uniq
15
+ transform_and_check_results = keys.map do |key|
16
+ if type = @fields[key]
17
+ error, transformed = type.transform_and_check(value[key])
18
+ if @optional_keys.include?(key) && !error && transformed.nil?
19
+ nil
20
+ else
21
+ [key, [error, transformed]]
22
+ end
18
23
  else
19
- [key, type.transform_and_check(value[key])]
24
+ [key, [nil, value[key]]]
20
25
  end
21
26
  end.compact
22
27
 
@@ -63,7 +68,7 @@ module Camille
63
68
  def check_case_conversion_safe sym
64
69
  str = sym.to_s
65
70
  if str != str.camelize.underscore
66
- raise ArgumentError.new("Only keys satisfying `key.to_s == key.to_s.caamelize.underscore` can be used.")
71
+ raise ArgumentError.new("Only keys satisfying `key.to_s == key.to_s.camelize.underscore` can be used.")
67
72
  end
68
73
  end
69
74
  end
@@ -27,7 +27,7 @@ module Camille
27
27
  end
28
28
 
29
29
  def literal
30
- "#{@left.literal} | #{@right.literal}"
30
+ "(#{@left.literal} | #{@right.literal})"
31
31
  end
32
32
  end
33
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Camille
4
- VERSION = "0.5.8"
4
+ VERSION = "0.5.9"
5
5
  end
data/lib/camille.rb CHANGED
@@ -13,6 +13,7 @@ require_relative "camille/types/object"
13
13
  require_relative "camille/types/null"
14
14
  require_relative "camille/types/undefined"
15
15
  require_relative "camille/types/union"
16
+ require_relative "camille/types/intersection"
16
17
  require_relative "camille/types/tuple"
17
18
  require_relative "camille/types/any"
18
19
  require_relative "camille/types/number_literal"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camille
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.5.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - 辻彩
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-26 00:00:00.000000000 Z
11
+ date: 2023-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -78,6 +78,7 @@ files:
78
78
  - lib/camille/types/array.rb
79
79
  - lib/camille/types/boolean.rb
80
80
  - lib/camille/types/boolean_literal.rb
81
+ - lib/camille/types/intersection.rb
81
82
  - lib/camille/types/null.rb
82
83
  - lib/camille/types/number.rb
83
84
  - lib/camille/types/number_literal.rb