paramore 3.9.0 → 3.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/paramore/cast_parameters.rb +54 -43
- data/lib/paramore/errors.rb +8 -4
- data/lib/paramore/types.rb +9 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '048043c9ea2f99c0f8233046a6afb9f6710610a5e1bb691f0f4e8e03479e0b20'
|
4
|
+
data.tar.gz: 605bc5998bc57401f3b39db585cae073b39254dd11c8cefa98a3ad53b029dc38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78715e9f134b17502cc9eacac1c6d597cc2e29f4550f223ff2c80eb41a0e50f8e0968830c1542301fb4867a9b0cc89a2dac2e87e16a0fd55b2b5df6e1a70b1f7
|
7
|
+
data.tar.gz: bd0ebc180f12c23d3e02860d8f5fbafa9215d3091a08c7fa44b3c92655a1289ee1a8235cd11940bab3a80379b3b87e45bd4f4208b596b9708a541b6120e503e9
|
@@ -15,81 +15,92 @@ module Paramore
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def run
|
18
|
-
cast(root_field, data,
|
18
|
+
cast(root_field, data, nil)
|
19
19
|
end
|
20
20
|
|
21
|
-
def cast(field, value,
|
21
|
+
def cast(field, value, key, namespace = nil)
|
22
|
+
full_name = [namespace, key].select(&:present?).join('.')
|
23
|
+
|
22
24
|
if value.nil?
|
23
|
-
if field.nullable? || field.default?
|
24
|
-
|
25
|
-
|
26
|
-
raise Paramore::NilParameter, name
|
27
|
-
end
|
25
|
+
return field.default if field.nullable? || field.default?
|
26
|
+
|
27
|
+
raise Paramore::NilParameter, full_name
|
28
28
|
elsif value == ''
|
29
|
-
if field.allow_empty?
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
return
|
35
|
-
else
|
36
|
-
raise Paramore::NilParameter, name
|
37
|
-
end
|
29
|
+
return typecast_value(field.type, '', full_name) if field.allow_empty?
|
30
|
+
return field.default if field.default?
|
31
|
+
return if field.nullable?
|
32
|
+
|
33
|
+
raise Paramore::NilParameter, full_name
|
38
34
|
end
|
39
35
|
|
40
36
|
case field.type
|
41
37
|
when Hash
|
42
|
-
typecast_hash(field, value || {},
|
38
|
+
typecast_hash(field, value || {}, full_name)
|
43
39
|
when Array
|
44
|
-
typecast_array(field, value,
|
40
|
+
typecast_array(field, value, full_name)
|
45
41
|
else
|
46
|
-
typecast_value(field.type, value,
|
42
|
+
typecast_value(field.type, value, full_name)
|
47
43
|
end
|
48
44
|
end
|
49
45
|
|
50
|
-
def typecast_hash(field, hash,
|
51
|
-
raise Paramore::HashExpected.new(
|
46
|
+
def typecast_hash(field, hash, full_name)
|
47
|
+
raise Paramore::HashExpected.new(full_name, hash) unless hash.is_a?(Hash)
|
52
48
|
|
53
49
|
result =
|
54
50
|
if field.wildly_keyed_hash?
|
55
|
-
|
56
|
-
key_type = field.type.keys.first
|
57
|
-
hash.to_h do |name, value|
|
58
|
-
[typecast_value(key_type, name, nil), cast(value_field, value, name)]
|
59
|
-
end
|
51
|
+
cast_wild_hash(field, hash, full_name)
|
60
52
|
else
|
61
|
-
|
62
|
-
extra = hash.keys - field.type.keys
|
63
|
-
raise "Found extra keys in `name`: #{extra}!"
|
64
|
-
end
|
65
|
-
|
66
|
-
field
|
67
|
-
.type
|
68
|
-
.reject { |name, value_field| missing_and_optional?(hash, name, value_field) }
|
69
|
-
.to_h { |name, value_field| [name, cast(value_field, hash[name], name)] }
|
53
|
+
cast_regular_hash(field, hash, full_name)
|
70
54
|
end
|
71
55
|
|
72
56
|
field.compact? ? result.compact : result
|
73
57
|
end
|
74
58
|
|
75
|
-
def typecast_array(field, array,
|
76
|
-
raise Paramore::ArrayExpected.new(
|
59
|
+
def typecast_array(field, array, full_name)
|
60
|
+
raise Paramore::ArrayExpected.new(full_name, array) unless array.is_a?(Array)
|
77
61
|
|
78
|
-
result =
|
62
|
+
result =
|
63
|
+
array
|
79
64
|
.reject { |unit| unit.to_s == '' && field.compact? }
|
80
|
-
.map { |unit| cast(Paramore.field(field.type.first, null: true), unit) }
|
65
|
+
.map { |unit| cast(Paramore.field(field.type.first, null: true), unit, nil, full_name) }
|
81
66
|
|
82
67
|
field.compact? ? result.compact : result
|
83
68
|
end
|
84
69
|
|
85
|
-
def typecast_value(type, value,
|
70
|
+
def typecast_value(type, value, full_name)
|
86
71
|
type.send(Paramore.configuration.type_method_name, value)
|
87
72
|
rescue StandardError => e
|
88
|
-
raise "
|
73
|
+
raise "Tried casting #{Paramore::ErrorFieldName(full_name)}, but \"#{e}\" was raised!"
|
74
|
+
end
|
75
|
+
|
76
|
+
def cast_wild_hash(field, hash, full_name)
|
77
|
+
value_field = field.type.values.first
|
78
|
+
key_type = field.type.keys.first
|
79
|
+
|
80
|
+
hash.to_h do |inner_key, value|
|
81
|
+
[
|
82
|
+
typecast_value(key_type, inner_key, nil),
|
83
|
+
cast(value_field, value, inner_key, full_name)
|
84
|
+
]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def cast_regular_hash(field, hash, full_name)
|
89
|
+
if options[:no_extra_keys]
|
90
|
+
extra = hash.keys - field.type.keys
|
91
|
+
raise "Found extra keys in #{Paramore::ErrorFieldName(full_name)}: #{extra}!" if extra.any?
|
92
|
+
end
|
93
|
+
|
94
|
+
field
|
95
|
+
.type
|
96
|
+
.reject { |inner_key, value_field| missing_and_optional?(hash, inner_key, value_field) }
|
97
|
+
.to_h do |inner_key, value_field|
|
98
|
+
[inner_key, cast(value_field, hash[inner_key], inner_key, full_name)]
|
99
|
+
end
|
89
100
|
end
|
90
101
|
|
91
|
-
def missing_and_optional?(hash,
|
92
|
-
!hash.key?(
|
102
|
+
def missing_and_optional?(hash, key, field)
|
103
|
+
!hash.key?(key) && !field.required?
|
93
104
|
end
|
94
105
|
end
|
95
106
|
end
|
data/lib/paramore/errors.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
class Paramore::NilParameter < StandardError
|
2
2
|
def initialize(param_name)
|
3
|
-
super("Received a nil
|
3
|
+
super("Received a nil #{Paramore::ErrorFieldName(param_name)}, but its type is non nullable!")
|
4
4
|
end
|
5
5
|
end
|
6
6
|
|
7
7
|
class Paramore::NonField < StandardError
|
8
8
|
def initialize(param_name, type)
|
9
|
-
super("
|
9
|
+
super("#{Paramore::ErrorFieldName(param_name)} defined as a `#{type.class}`, expected a call of `Paramore.field()`! Perhaps you declared a plain hash instead of Paramore.field({})?")
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
class Paramore::HashExpected < StandardError
|
14
14
|
def initialize(param_name, param)
|
15
|
-
super("Expected
|
15
|
+
super("Expected #{Paramore::ErrorFieldName(param_name)} to be a hash, received #{param.class} instead!")
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
class Paramore::ArrayExpected < StandardError
|
20
20
|
def initialize(param_name, param)
|
21
|
-
super("Expected
|
21
|
+
super("Expected #{Paramore::ErrorFieldName(param_name)} to be an array, received #{param.class} instead!")
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -27,3 +27,7 @@ class Paramore::HashTooWild < StandardError
|
|
27
27
|
super("A hash field with a type as key may not contain any more entries! (so, eg.: { String => field } is ok, but { String => field, user_id: field } is not)")
|
28
28
|
end
|
29
29
|
end
|
30
|
+
|
31
|
+
def Paramore::ErrorFieldName(name)
|
32
|
+
name.present? ? "`#{name}`" : 'root field'
|
33
|
+
end
|
data/lib/paramore/types.rb
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
module Paramore
|
2
|
+
module TypeRegexes
|
3
|
+
INTEGER_REGEX = /^-{0,1}\d+$/
|
4
|
+
FLOAT_REGEX = /^-{0,1}\d+(\.\d*){0,1}$/
|
5
|
+
end
|
6
|
+
|
2
7
|
module BigDecimal
|
3
8
|
module_function
|
4
9
|
def [](input)
|
10
|
+
raise ArgumentError "#{input} is not a BigDecimal!" unless input.to_s.match?(TypeRegexes::FLOAT_REGEX)
|
11
|
+
|
5
12
|
BigDecimal(input)
|
6
13
|
end
|
7
14
|
end
|
@@ -18,7 +25,7 @@ module Paramore
|
|
18
25
|
module Float
|
19
26
|
module_function
|
20
27
|
def [](input)
|
21
|
-
raise ArgumentError "#{input} is not a
|
28
|
+
raise ArgumentError "#{input} is not a Float!" unless input.to_s.match?(TypeRegexes::FLOAT_REGEX)
|
22
29
|
|
23
30
|
input.to_f
|
24
31
|
end
|
@@ -27,7 +34,7 @@ module Paramore
|
|
27
34
|
module Int
|
28
35
|
module_function
|
29
36
|
def [](input)
|
30
|
-
raise ArgumentError "#{input} is not an
|
37
|
+
raise ArgumentError, "#{input} is not an Integer!" unless input.to_s.match?(TypeRegexes::INTEGER_REGEX)
|
31
38
|
|
32
39
|
input.to_i
|
33
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paramore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.9.
|
4
|
+
version: 3.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lukas Kairevičius
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-12-
|
11
|
+
date: 2021-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -115,7 +115,7 @@ licenses:
|
|
115
115
|
- MIT
|
116
116
|
metadata: {}
|
117
117
|
post_install_message: |
|
118
|
-
Thank you for installing Paramore 3.9.
|
118
|
+
Thank you for installing Paramore 3.9.4 !
|
119
119
|
From the command line you can run `paramore` to generate a configuration file
|
120
120
|
|
121
121
|
More details here : https://github.com/lumzdas/paramore/blob/master/README.md
|