low_type 0.8.8 → 0.8.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/redefiner.rb +0 -2
- data/lib/type_expression.rb +11 -28
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 65c0e662bfe788a1132c9ecf802392285089139cc2d721427a7285d62b87e606
|
|
4
|
+
data.tar.gz: da9f514df1428147990f55e7bc68a343dff87ce3df86d2e7913414ac97c80e15
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 323ed1e9adfe8a07e0e8b6e2ed6fba157caac080611827f32453816db5b0c40cb7631e99c9a004391e1565f05d4ad26f8e1a9ed7b710a13846bf37da1b4290e6
|
|
7
|
+
data.tar.gz: 7b85c07ee7cfe34c2c004a4c62381627d8f258236d891295a073dd9e4c2b304c35ccba6ef6521faf9e1da52558a8610862fb73ca76e184cbf4c19ea362fcc730
|
data/lib/redefiner.rb
CHANGED
data/lib/type_expression.rb
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
require_relative 'proxies/param_proxy'
|
|
2
2
|
|
|
3
3
|
module LowType
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
root_path = File.expand_path(__dir__)
|
|
5
|
+
file_path = File.expand_path(__FILE__)
|
|
6
|
+
adapter_paths = Dir.chdir(root_path) { Dir.glob('adapters/*') }.map { |path| File.join(root_path, path) }
|
|
7
|
+
|
|
8
|
+
HIDDEN_PATHS = [file_path, *adapter_paths, File.join(root_path, 'redefiner.rb')]
|
|
6
9
|
|
|
10
|
+
class TypeExpression
|
|
7
11
|
attr_reader :types, :default_value
|
|
8
12
|
|
|
13
|
+
# @param type - A literal type or an instance representation of a typed structure.
|
|
9
14
|
def initialize(type: nil, default_value: :LOW_TYPE_UNDEFINED)
|
|
10
|
-
# Types can be instance representations of a structure.
|
|
11
15
|
@types = []
|
|
12
16
|
@types << type unless type.nil?
|
|
13
17
|
@default_value = default_value
|
|
@@ -30,26 +34,6 @@ module LowType
|
|
|
30
34
|
@default_value == :LOW_TYPE_UNDEFINED
|
|
31
35
|
end
|
|
32
36
|
|
|
33
|
-
# Called in situations where we want to be inclusive rather than exclusive and pass validation if one of the types is valid.
|
|
34
|
-
def validate(value:, proxy:)
|
|
35
|
-
if value.nil?
|
|
36
|
-
return true if @default_value.nil?
|
|
37
|
-
return false if required?
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
@types.each do |type|
|
|
41
|
-
return true if LowType.type?(type) && type <= value.class # Example: HTML is a subclass of String and should pass as a String.
|
|
42
|
-
return true if ::Array === type && ::Array === value && array_types_match_values?(types: type, values: value)
|
|
43
|
-
|
|
44
|
-
# TODO: Shallow validation of enumerables could be made deeper with user config.
|
|
45
|
-
if type.class == ::Hash && value.class == ::Hash && type.keys[0] == value.keys[0].class && type.values[0] == value.values[0].class
|
|
46
|
-
return true
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
false
|
|
51
|
-
end
|
|
52
|
-
|
|
53
37
|
def validate!(value:, proxy:)
|
|
54
38
|
if value.nil?
|
|
55
39
|
return true if @default_value.nil?
|
|
@@ -68,8 +52,7 @@ module LowType
|
|
|
68
52
|
|
|
69
53
|
raise proxy.error_type, proxy.error_message(value:)
|
|
70
54
|
rescue proxy.error_type => e
|
|
71
|
-
|
|
72
|
-
raise proxy.error_type, e.message, backtrace_with_proxy(file_paths:, backtrace: e.backtrace, proxy:)
|
|
55
|
+
raise proxy.error_type, e.message, backtrace_with_proxy(backtrace: e.backtrace, proxy:)
|
|
73
56
|
end
|
|
74
57
|
|
|
75
58
|
def valid_types
|
|
@@ -95,9 +78,9 @@ module LowType
|
|
|
95
78
|
true
|
|
96
79
|
end
|
|
97
80
|
|
|
98
|
-
def backtrace_with_proxy(
|
|
99
|
-
# Remove LowType file paths from the backtrace.
|
|
100
|
-
filtered_backtrace = backtrace.reject { |line|
|
|
81
|
+
def backtrace_with_proxy(proxy:, backtrace:)
|
|
82
|
+
# Remove LowType defined method file paths from the backtrace.
|
|
83
|
+
filtered_backtrace = backtrace.reject { |line| HIDDEN_PATHS.find { |file_path| line.include?(file_path) } }
|
|
101
84
|
|
|
102
85
|
# Add the proxied file to the backtrace.
|
|
103
86
|
proxy_file_backtrace = "#{proxy.file.path}:#{proxy.file.line}:in '#{proxy.file.scope}'"
|
data/lib/version.rb
CHANGED