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 +4 -4
- data/CHANGELOG.md +14 -0
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/camille/basic_type.rb +9 -0
- data/lib/camille/controller_extension.rb +4 -4
- data/lib/camille/generators/install_generator.rb +3 -2
- data/lib/camille/generators/templates/date_time.rb +12 -0
- data/lib/camille/generators/templates/decimal.rb +16 -0
- data/lib/camille/type.rb +5 -0
- data/lib/camille/types/array.rb +14 -5
- data/lib/camille/types/object.rb +22 -6
- data/lib/camille/types/omit.rb +2 -2
- data/lib/camille/types/pick.rb +2 -2
- data/lib/camille/types/tuple.rb +13 -6
- data/lib/camille/types/union.rb +13 -8
- data/lib/camille/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d0df267ba626a69935cbe5da75d90c08bf319b048fd210712953bc2a0e83b45
|
4
|
+
data.tar.gz: f93164ee26857fb30eb3a1c4c77dd8341e7aa6a52b6476134658c4061f030c58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/README.md
CHANGED
data/lib/camille/basic_type.rb
CHANGED
@@ -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.
|
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
|
25
|
-
|
24
|
+
if transformed.is_a? Hash
|
25
|
+
transformed.deep_transform_keys!{|k| k.to_s.camelize(:lower)}
|
26
26
|
end
|
27
|
-
super(json:
|
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
|
14
|
-
copy_file ".
|
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,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
data/lib/camille/types/array.rb
CHANGED
@@ -7,14 +7,23 @@ module Camille
|
|
7
7
|
@content = Camille::Type.instance content
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def transform_and_check value
|
11
11
|
if value.is_a? ::Array
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
|
data/lib/camille/types/object.rb
CHANGED
@@ -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
|
11
|
+
def transform_and_check value
|
11
12
|
if value.is_a? Hash
|
12
|
-
|
13
|
-
|
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
|
-
|
17
|
-
|
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]
|
data/lib/camille/types/omit.rb
CHANGED
data/lib/camille/types/pick.rb
CHANGED
data/lib/camille/types/tuple.rb
CHANGED
@@ -7,17 +7,24 @@ module Camille
|
|
7
7
|
@elements = elements.map{|e| Camille::Type.instance e}
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def transform_and_check value
|
11
11
|
if value.is_a? ::Array
|
12
|
-
|
13
|
-
[
|
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
|
-
|
17
|
-
|
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
|
|
data/lib/camille/types/union.rb
CHANGED
@@ -8,16 +8,21 @@ module Camille
|
|
8
8
|
@right = Camille::Type.instance right
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
13
|
-
if
|
14
|
-
|
15
|
-
if
|
16
|
-
Camille::TypeError.new(
|
17
|
-
'union.left' =>
|
18
|
-
'union.right' =>
|
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
|
|
data/lib/camille/version.rb
CHANGED
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.
|
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-
|
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
|