camille 0.4.2 → 0.4.3

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: 1c6d8ec2d346e2ff02811be47d900bee304a82f5a96e213cbf5e1c9f96f3fe61
4
- data.tar.gz: 1a67d813067334b228caa09c9cc10411fa4a3b56eab57610b415c129616a4533
3
+ metadata.gz: 3d0df267ba626a69935cbe5da75d90c08bf319b048fd210712953bc2a0e83b45
4
+ data.tar.gz: f93164ee26857fb30eb3a1c4c77dd8341e7aa6a52b6476134658c4061f030c58
5
5
  SHA512:
6
- metadata.gz: 0d977978049f5c5589731135c24c360145c3145d3ded0be2d158ea67546eece82d8af7c07674e1895d03a3071418adc9b1a6a597e5246b22b96d0c7abb2123d3
7
- data.tar.gz: b4fd67e56fdd11db3c00d1e1ddee3a775c8e3d87ba2423cbe652729ed3dfb3460591d658a89bd1192385c45f0507fbeae5172a03acac65fcb3ad0880fe3b1aa7
6
+ metadata.gz: 84e73bdb5f359904024658a6ce47e9585851eeb60fb28a093323ec169fa8dc6903b358255380b9b9daafc8f24490628fecd173c6ab97f46f6190bddfe29eaf72
7
+ data.tar.gz: 40e888902aea15a1964e6fddb09476d896c12645717f750869b76c08708529bce2d6baa8eb96dc0adffae2e65a85a70883855788d546d41c9661033378ec2132
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.3
4
+
5
+ ### Fixed
6
+
7
+ * Fixed the error that values not getting transformed in array/tuple/union types
8
+ * Fixed optional fields getting `nil` due to transformation
9
+
3
10
  ## 0.4.2
4
11
 
5
12
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- camille (0.4.2)
4
+ camille (0.4.3)
5
5
  rails (>= 6.1, < 8)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -237,7 +237,7 @@ Camille will print the following error:
237
237
  ```
238
238
  object:
239
239
  array:
240
- [2]: Expected number, got "3".
240
+ array[2]: Expected number, got "3".
241
241
  ```
242
242
 
243
243
  ### Reloading
@@ -7,22 +7,26 @@ module Camille
7
7
  @content = Camille::Type.instance content
8
8
  end
9
9
 
10
- def check value
10
+ def transform_and_check value
11
11
  if value.is_a? ::Array
12
- errors = value.map.with_index{|e, i| ["array[#{i}]", @content.check(e)]}.select{|x| x[1]}
13
- unless errors.empty?
14
- Camille::TypeError.new(**errors.to_h)
12
+ transform_and_check_results = value.map.with_index do |element, index|
13
+ [index, @content.transform_and_check(element)]
14
+ end
15
+ errors = transform_and_check_results.map do |index, (error, transformed)|
16
+ ["array[#{index}]", error]
17
+ end.select{|x| x[1]}
18
+
19
+ if errors.empty?
20
+ transformed = transform_and_check_results.map{|_, (_, transformed)| transformed}
21
+ [nil, transformed]
22
+ else
23
+ [Camille::TypeError.new(**errors.to_h), nil]
15
24
  end
16
25
  else
17
- Camille::TypeError.new("Expected array, got #{value.inspect}.")
26
+ [Camille::TypeError.new("Expected array, got #{value.inspect}."), nil]
18
27
  end
19
28
  end
20
29
 
21
- def transform_and_check value
22
- transformed = value.map{|x| @content.transform x}
23
- [check(transformed), transformed]
24
- end
25
-
26
30
  def literal
27
31
  "#{@content.literal}[]"
28
32
  end
@@ -4,28 +4,20 @@ module Camille
4
4
  attr_reader :fields
5
5
 
6
6
  def initialize fields
7
+ @optional_keys = []
7
8
  @fields = normalize_fields fields
8
9
  end
9
10
 
10
- def check value
11
- if value.is_a? Hash
12
- errors = @fields.map do |key, type|
13
- [key.to_s, type.check(value[key])]
14
- end.select{|x| x[1]}
15
-
16
- unless errors.empty?
17
- Camille::TypeError.new(**errors.to_h)
18
- end
19
- else
20
- Camille::TypeError.new("Expected hash, got #{value.inspect}.")
21
- end
22
- end
23
-
24
11
  def transform_and_check value
25
12
  if value.is_a? Hash
26
13
  transform_and_check_results = @fields.map do |key, type|
27
- [key, type.transform_and_check(value[key])]
28
- end
14
+ error, transformed = type.transform_and_check(value[key])
15
+ if @optional_keys.include?(key) && !error && transformed.nil?
16
+ nil
17
+ else
18
+ [key, type.transform_and_check(value[key])]
19
+ end
20
+ end.compact
29
21
 
30
22
  errors = transform_and_check_results.map do |key, (error, transformed)|
31
23
  [key.to_s, error]
@@ -37,10 +29,10 @@ module Camille
37
29
  end.to_h
38
30
  [nil, transformed]
39
31
  else
40
- Camille::TypeError.new(**errors.to_h)
32
+ [Camille::TypeError.new(**errors.to_h), nil]
41
33
  end
42
34
  else
43
- Camille::TypeError.new("Expected hash, got #{value.inspect}.")
35
+ [Camille::TypeError.new("Expected hash, got #{value.inspect}."), nil]
44
36
  end
45
37
  end
46
38
 
@@ -54,6 +46,7 @@ module Camille
54
46
  type = Camille::Type.instance(value)
55
47
  if key.end_with?('?')
56
48
  new_key = remove_question_mark(key)
49
+ @optional_keys << new_key
57
50
  [new_key, type | Camille::Types::Undefined.new]
58
51
  else
59
52
  [key, type]
@@ -1,10 +1,6 @@
1
1
  module Camille
2
2
  module Types
3
3
  class Omit < PickAndOmit
4
- def check value
5
- processed_object.check(value)
6
- end
7
-
8
4
  def transform_and_check value
9
5
  processed_object.transform_and_check(value)
10
6
  end
@@ -1,10 +1,6 @@
1
1
  module Camille
2
2
  module Types
3
3
  class Pick < PickAndOmit
4
- def check value
5
- processed_object.check(value)
6
- end
7
-
8
4
  def transform_and_check value
9
5
  processed_object.transform_and_check(value)
10
6
  end
@@ -7,17 +7,24 @@ module Camille
7
7
  @elements = elements.map{|e| Camille::Type.instance e}
8
8
  end
9
9
 
10
- def check value
10
+ def transform_and_check value
11
11
  if value.is_a? ::Array
12
- errors = @elements.map.with_index do |type, index|
13
- ["tuple[#{index}]", type.check(value[index])]
12
+ transform_and_check_results = @elements.map.with_index do |type, index|
13
+ [index, type.transform_and_check(value[index])]
14
+ end
15
+
16
+ errors = transform_and_check_results.map do |index, (error, transformed)|
17
+ ["tuple[#{index}]", error]
14
18
  end.select{|x| x[1]}
15
19
 
16
- unless errors.empty?
17
- Camille::TypeError.new(**errors.to_h)
20
+ if errors.empty?
21
+ transformed = transform_and_check_results.map{|index, (error, transformed)| transformed}
22
+ [nil, transformed]
23
+ else
24
+ [Camille::TypeError.new(**errors.to_h), nil]
18
25
  end
19
26
  else
20
- Camille::TypeError.new("Expected array, got #{value.inspect}.")
27
+ [Camille::TypeError.new("Expected array, got #{value.inspect}."), nil]
21
28
  end
22
29
  end
23
30
 
@@ -8,29 +8,14 @@ module Camille
8
8
  @right = Camille::Type.instance right
9
9
  end
10
10
 
11
- def check value
12
- left_result = @left.check value
13
- if left_result
14
- right_result = @right.check value
15
- if right_result
16
- Camille::TypeError.new(
17
- 'union.left' => left_result,
18
- 'union.right' => right_result
19
- )
20
- end
21
- end
22
- end
23
-
24
11
  def transform_and_check value
25
- left_transformed = @left.transform value
26
- left_result = @left.check left_transformed
27
- if left_result
28
- right_transformed = @right.transform value
29
- right_result = @right.check right_transformed
30
- if right_result
12
+ left_error, left_transformed = @left.transform_and_check value
13
+ if left_error
14
+ right_error, right_transformed = @right.transform_and_check value
15
+ if right_error
31
16
  error = Camille::TypeError.new(
32
- 'union.left' => left_result,
33
- 'union.right' => right_result
17
+ 'union.left' => right_error,
18
+ 'union.right' => right_error
34
19
  )
35
20
  [error, nil]
36
21
  else
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Camille
4
- VERSION = "0.4.2"
4
+ VERSION = "0.4.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camille
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - 辻彩
@@ -52,7 +52,6 @@ files:
52
52
  - lib/camille/code_generator.rb
53
53
  - lib/camille/configuration.rb
54
54
  - lib/camille/controller_extension.rb
55
- - lib/camille/custom_types/date.rb
56
55
  - lib/camille/endpoint.rb
57
56
  - lib/camille/generators/install_generator.rb
58
57
  - lib/camille/generators/schema_generator.rb
@@ -1,31 +0,0 @@
1
- require 'date'
2
-
3
- # This type is only for testing, to simulate the usage of transform.
4
- module Camille
5
- module CustomTypes
6
- class Date < Camille::Type
7
- include Camille::Types
8
-
9
- alias_of(
10
- type: 'Date',
11
- value: Number
12
- )
13
-
14
- def transform value
15
- case value
16
- when ::Date
17
- timestamp = value.to_time.to_i
18
- when ::Time
19
- timestamp = value.to_i
20
- end
21
- {
22
- type: 'Date',
23
- value: timestamp.to_i * 1000
24
- }
25
- end
26
-
27
- end
28
- end
29
- end
30
-
31
- Camille::Loader.loaded_types.delete(Camille::CustomTypes::Date)