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 +4 -4
- data/CHANGELOG.md +7 -0
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/camille/types/array.rb +14 -10
- data/lib/camille/types/object.rb +11 -18
- data/lib/camille/types/omit.rb +0 -4
- data/lib/camille/types/pick.rb +0 -4
- data/lib/camille/types/tuple.rb +13 -6
- data/lib/camille/types/union.rb +6 -21
- data/lib/camille/version.rb +1 -1
- metadata +1 -2
- data/lib/camille/custom_types/date.rb +0 -31
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
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
data/lib/camille/types/array.rb
CHANGED
@@ -7,22 +7,26 @@ 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
|
|
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
|
data/lib/camille/types/object.rb
CHANGED
@@ -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
|
-
|
28
|
-
|
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]
|
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,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.
|
26
|
-
|
27
|
-
|
28
|
-
|
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' =>
|
33
|
-
'union.right' =>
|
17
|
+
'union.left' => right_error,
|
18
|
+
'union.right' => right_error
|
34
19
|
)
|
35
20
|
[error, nil]
|
36
21
|
else
|
data/lib/camille/version.rb
CHANGED
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.
|
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)
|