camille 0.4.1 → 0.4.2
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/lib/camille/basic_type.rb +9 -0
- data/lib/camille/controller_extension.rb +4 -4
- data/lib/camille/custom_types/date.rb +31 -0
- 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 +5 -0
- data/lib/camille/types/object.rb +23 -0
- data/lib/camille/types/omit.rb +4 -0
- data/lib/camille/types/pick.rb +4 -0
- data/lib/camille/types/union.rb +20 -0
- data/lib/camille/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c6d8ec2d346e2ff02811be47d900bee304a82f5a96e213cbf5e1c9f96f3fe61
|
4
|
+
data.tar.gz: 1a67d813067334b228caa09c9cc10411fa4a3b56eab57610b415c129616a4533
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d977978049f5c5589731135c24c360145c3145d3ded0be2d158ea67546eece82d8af7c07674e1895d03a3071418adc9b1a6a597e5246b22b96d0c7abb2123d3
|
7
|
+
data.tar.gz: b4fd67e56fdd11db3c00d1e1ddee3a775c8e3d87ba2423cbe652729ed3dfb3460591d658a89bd1192385c45f0507fbeae5172a03acac65fcb3ad0880fe3b1aa7
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
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.")
|
@@ -0,0 +1,31 @@
|
|
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)
|
@@ -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
data/lib/camille/types/object.rb
CHANGED
@@ -21,6 +21,29 @@ module Camille
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
def transform_and_check value
|
25
|
+
if value.is_a? Hash
|
26
|
+
transform_and_check_results = @fields.map do |key, type|
|
27
|
+
[key, type.transform_and_check(value[key])]
|
28
|
+
end
|
29
|
+
|
30
|
+
errors = transform_and_check_results.map do |key, (error, transformed)|
|
31
|
+
[key.to_s, error]
|
32
|
+
end.select{|x| x[1]}
|
33
|
+
|
34
|
+
if errors.empty?
|
35
|
+
transformed = transform_and_check_results.map do |key, (error, transformed)|
|
36
|
+
[key, transformed]
|
37
|
+
end.to_h
|
38
|
+
[nil, transformed]
|
39
|
+
else
|
40
|
+
Camille::TypeError.new(**errors.to_h)
|
41
|
+
end
|
42
|
+
else
|
43
|
+
Camille::TypeError.new("Expected hash, got #{value.inspect}.")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
24
47
|
def literal
|
25
48
|
"{#{@fields.map{|k,v| "#{ActiveSupport::Inflector.camelize k.to_s, false}: #{v.literal}"}.join(', ')}}"
|
26
49
|
end
|
data/lib/camille/types/omit.rb
CHANGED
data/lib/camille/types/pick.rb
CHANGED
data/lib/camille/types/union.rb
CHANGED
@@ -21,6 +21,26 @@ module Camille
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
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
|
31
|
+
error = Camille::TypeError.new(
|
32
|
+
'union.left' => left_result,
|
33
|
+
'union.right' => right_result
|
34
|
+
)
|
35
|
+
[error, nil]
|
36
|
+
else
|
37
|
+
[nil, right_transformed]
|
38
|
+
end
|
39
|
+
else
|
40
|
+
[nil, left_transformed]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
24
44
|
def literal
|
25
45
|
"#{@left.literal} | #{@right.literal}"
|
26
46
|
end
|
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.2
|
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
|
@@ -52,11 +52,14 @@ 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
|
55
56
|
- lib/camille/endpoint.rb
|
56
57
|
- lib/camille/generators/install_generator.rb
|
57
58
|
- lib/camille/generators/schema_generator.rb
|
58
59
|
- lib/camille/generators/templates/.keep
|
59
60
|
- lib/camille/generators/templates/configuration.rb
|
61
|
+
- lib/camille/generators/templates/date_time.rb
|
62
|
+
- lib/camille/generators/templates/decimal.rb
|
60
63
|
- lib/camille/generators/templates/schema_template.erb
|
61
64
|
- lib/camille/generators/templates/type_template.erb
|
62
65
|
- lib/camille/generators/type_generator.rb
|