camille 0.4.1 → 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: 2370ceb41fe8fdd701b99671f1d4d82678fc103218e96a10a52e60b9b2d1ad1f
4
- data.tar.gz: ef8fe63ad679a1744fdef8c58971148d3642be0be2c5e23ff53b510dfef2b449
3
+ metadata.gz: 3d0df267ba626a69935cbe5da75d90c08bf319b048fd210712953bc2a0e83b45
4
+ data.tar.gz: f93164ee26857fb30eb3a1c4c77dd8341e7aa6a52b6476134658c4061f030c58
5
5
  SHA512:
6
- metadata.gz: 6b8068692bd238a82cded511bad5e15d3f0993640a487b335cfd7d1592fe3c8cb45f88924753a1b580ae7448a59d58e423c29332842fa9be09472585bc810ea7
7
- data.tar.gz: 71bc242a3a3fc9224c0d2e7d3598d9e4be378ff7b2a028e02b55833927cfb516d6309c44bbc7b2296b0b9b780ffe07458435e40a89a1953d6044a465621e7ef9
6
+ metadata.gz: 84e73bdb5f359904024658a6ce47e9585851eeb60fb28a093323ec169fa8dc6903b358255380b9b9daafc8f24490628fecd173c6ab97f46f6190bddfe29eaf72
7
+ data.tar.gz: 40e888902aea15a1964e6fddb09476d896c12645717f750869b76c08708529bce2d6baa8eb96dc0adffae2e65a85a70883855788d546d41c9661033378ec2132
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
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
+
10
+ ## 0.4.2
11
+
12
+ ### Added
13
+
14
+ * Supported `transform` for custom types
15
+ * Added default DateTime and Decimal type to install generator
16
+
3
17
  ## 0.4.1
4
18
 
5
19
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- camille (0.4.1)
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
@@ -11,6 +11,15 @@ module Camille
11
11
  Camille::Types::Array.new(self)
12
12
  end
13
13
 
14
+ def transform_and_check value
15
+ transformed = transform value
16
+ [check(value), transformed]
17
+ end
18
+
19
+ def transform value
20
+ value
21
+ end
22
+
14
23
  def self.| other
15
24
  Camille::Type.instance(self) | other
16
25
  end
@@ -15,16 +15,16 @@ module Camille
15
15
  if endpoint = camille_endpoint
16
16
  render_options = args.last
17
17
  if value = render_options[:json]
18
- error = endpoint.response_type.check(value)
18
+ error, transformed = endpoint.response_type.transform_and_check(value)
19
19
  if error
20
20
  string_io = StringIO.new
21
21
  Camille::TypeErrorPrinter.new(error).print(string_io)
22
22
  raise TypeError.new("\nType check failed for response.\n#{string_io.string}")
23
23
  else
24
- if value.is_a? Hash
25
- value.deep_transform_keys!{|k| k.to_s.camelize(:lower)}
24
+ if transformed.is_a? Hash
25
+ transformed.deep_transform_keys!{|k| k.to_s.camelize(:lower)}
26
26
  end
27
- super(json: value)
27
+ super(json: transformed)
28
28
  end
29
29
  else
30
30
  raise ArgumentError.new("Expected key :json for `render` call.")
@@ -10,8 +10,9 @@ module Camille
10
10
  copy_file "configuration.rb", "config/camille/configuration.rb"
11
11
  end
12
12
 
13
- def create_types_folder
14
- copy_file ".keep", "config/camille/types/.keep"
13
+ def create_date_time_and_decimal
14
+ copy_file "date_time.rb", "config/camille/types/date_time.rb"
15
+ copy_file "decimal.rb", "config/camille/types/decimal.rb"
15
16
  end
16
17
 
17
18
  def create_schemas_folder
@@ -0,0 +1,12 @@
1
+ using Camille::Syntax
2
+
3
+ class Camille::Types::DateTime < Camille::Type
4
+ include Camille::Types
5
+
6
+ alias_of(String)
7
+
8
+ # transforms a DateTime into a String of ISO 8601 format
9
+ def transform value
10
+ value.as_json
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ using Camille::Syntax
2
+
3
+ class Camille::Types::Decimal < Camille::Type
4
+ include Camille::Types
5
+
6
+ alias_of(Number)
7
+
8
+ # transforms a BigDecimal into a Float so it fits in Number type
9
+ def transform value
10
+ if value.is_a? BigDecimal
11
+ value.to_f
12
+ else
13
+ value
14
+ end
15
+ end
16
+ end
data/lib/camille/type.rb CHANGED
@@ -21,6 +21,11 @@ module Camille
21
21
  @underlying.check value
22
22
  end
23
23
 
24
+ def transform_and_check value
25
+ transformed = transform value
26
+ @underlying.transform_and_check transformed
27
+ end
28
+
24
29
  def self.klass_name
25
30
  name.gsub(/^Camille::Types::/, '')
26
31
  end
@@ -7,14 +7,23 @@ 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
 
@@ -4,20 +4,35 @@ 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
+ def transform_and_check value
11
12
  if value.is_a? Hash
12
- errors = @fields.map do |key, type|
13
- [key.to_s, type.check(value[key])]
13
+ transform_and_check_results = @fields.map do |key, type|
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
21
+
22
+ errors = transform_and_check_results.map do |key, (error, transformed)|
23
+ [key.to_s, error]
14
24
  end.select{|x| x[1]}
15
25
 
16
- unless errors.empty?
17
- Camille::TypeError.new(**errors.to_h)
26
+ if errors.empty?
27
+ transformed = transform_and_check_results.map do |key, (error, transformed)|
28
+ [key, transformed]
29
+ end.to_h
30
+ [nil, transformed]
31
+ else
32
+ [Camille::TypeError.new(**errors.to_h), nil]
18
33
  end
19
34
  else
20
- Camille::TypeError.new("Expected hash, got #{value.inspect}.")
35
+ [Camille::TypeError.new("Expected hash, got #{value.inspect}."), nil]
21
36
  end
22
37
  end
23
38
 
@@ -31,6 +46,7 @@ module Camille
31
46
  type = Camille::Type.instance(value)
32
47
  if key.end_with?('?')
33
48
  new_key = remove_question_mark(key)
49
+ @optional_keys << new_key
34
50
  [new_key, type | Camille::Types::Undefined.new]
35
51
  else
36
52
  [key, type]
@@ -1,8 +1,8 @@
1
1
  module Camille
2
2
  module Types
3
3
  class Omit < PickAndOmit
4
- def check value
5
- processed_object.check(value)
4
+ def transform_and_check value
5
+ processed_object.transform_and_check(value)
6
6
  end
7
7
 
8
8
  private
@@ -1,8 +1,8 @@
1
1
  module Camille
2
2
  module Types
3
3
  class Pick < PickAndOmit
4
- def check value
5
- processed_object.check(value)
4
+ def transform_and_check value
5
+ processed_object.transform_and_check(value)
6
6
  end
7
7
 
8
8
  private
@@ -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,16 +8,21 @@ 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
11
+ def transform_and_check value
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
16
+ error = Camille::TypeError.new(
17
+ 'union.left' => right_error,
18
+ 'union.right' => right_error
19
19
  )
20
+ [error, nil]
21
+ else
22
+ [nil, right_transformed]
20
23
  end
24
+ else
25
+ [nil, left_transformed]
21
26
  end
22
27
  end
23
28
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Camille
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.3"
5
5
  end
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.4.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - 辻彩
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-17 00:00:00.000000000 Z
11
+ date: 2023-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -57,6 +57,8 @@ files:
57
57
  - lib/camille/generators/schema_generator.rb
58
58
  - lib/camille/generators/templates/.keep
59
59
  - lib/camille/generators/templates/configuration.rb
60
+ - lib/camille/generators/templates/date_time.rb
61
+ - lib/camille/generators/templates/decimal.rb
60
62
  - lib/camille/generators/templates/schema_template.erb
61
63
  - lib/camille/generators/templates/type_template.erb
62
64
  - lib/camille/generators/type_generator.rb