low_type 0.8.5 → 0.8.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40a701625494d482b8d9aa6092f8f924cd84ebc3310186e4dc9c1af38ff67cd4
4
- data.tar.gz: f24a27f11822f27177132eb8b840a9cdc4081867a171e338131a45e3b1a3b787
3
+ metadata.gz: 94ec8cf1ae80a7540248e9a0a97e2e9bbc99fb81724f9a89e3d35de0ccd3dfaa
4
+ data.tar.gz: f1bf339671b5d17ec247814a08ea665aca64df51f404f625dff5d6bf77cf7c2e
5
5
  SHA512:
6
- metadata.gz: 62f84ed471ba858386cba1a1eb8876d62612701bc5760b1e60dd78cb37e10f1741228a4ec7ccbdf0eefbbe94cb13ffca12180ea1d8feedcd7d2c42ed627c4289
7
- data.tar.gz: 02c5577b08fc0ca6ab7e564efb550e11d80ff0521feeaff34aac291332f777507b04c40c9fea038976bacef187c5c769a6ea7a26060ba906cd0d95189b8c9c4c
6
+ metadata.gz: 7a6ff0fa4038f53cd1650f27e7523ec2e0c0ac75c66190d62d976dbaded7887c2dc1900cb514709255af87f1c01545eb7c0748c100cd8133ea947c3d03f0c785
7
+ data.tar.gz: 141e666816a66e0446b0d9559d288dc24b7e838c200537fbfd5b18297533f8812517e37e29d3bcf7388efbef82fffb1aa90a6407037c4803db8bc6a1b26645f8
@@ -51,9 +51,7 @@ module LowType
51
51
  proxy.type_expression.types.each do |type|
52
52
  value = SinatraAdapter.reconstruct_return_value(type:, response:)
53
53
  unless proxy.type_expression.validate(value:, proxy:)
54
- status(500)
55
- body(proxy.error_message(value: value.inspect))
56
- break
54
+ halt 500, {}, proxy.error_message(value: value.inspect)
57
55
  end
58
56
  end
59
57
  end
@@ -72,12 +70,11 @@ module LowType
72
70
  HTML => -> (response) { response.body.first },
73
71
  JSON => -> (response) { response.body.first },
74
72
 
75
- # TODO: Should these be Enumerable[T] instead? How would we match a Module of a class in a hash key?
76
- # NOTE: These keys represent types, not type expressions.
77
- # A type lives inside a type expression and is actually an instance representing that type.
78
- [String] => -> (response) { response.body },
79
- [Integer, String] => -> (response) { [response.status, *response.body] },
80
- [Integer, Hash, String] => -> (response) { [response.status, response.headers, *response.body] },
73
+ [String] => -> (response) { response.body }, # Body is stored internally as an array.
74
+ [Integer, String] => -> (response) { [response.status, response.body.first] },
75
+ [Integer, Hash, String] => -> (response) { [response.status, response.headers, response.body.first] },
76
+
77
+ # TODO: Represent Enumerable[T]. How would we match a Module of a class in a hash key?
81
78
  }
82
79
 
83
80
  raise AllowedTypeError, 'Did you mean "Response.finish"?' if type.to_s == 'Response'
@@ -7,6 +7,7 @@ module LowType
7
7
  attr_reader :types, :default_value
8
8
 
9
9
  def initialize(type: nil, default_value: :LOW_TYPE_UNDEFINED)
10
+ # Types can be instance representations of a structure.
10
11
  @types = []
11
12
  @types << type unless type.nil?
12
13
  @default_value = default_value
@@ -38,9 +39,9 @@ module LowType
38
39
 
39
40
  @types.each do |type|
40
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)
41
43
 
42
44
  # TODO: Shallow validation of enumerables could be made deeper with user config.
43
- return true if type.class == ::Array && value.class == ::Array && type.first == value.first.class
44
45
  if type.class == ::Hash && value.class == ::Hash && type.keys[0] == value.keys[0].class && type.values[0] == value.values[0].class
45
46
  return true
46
47
  end
@@ -56,8 +57,8 @@ module LowType
56
57
  end
57
58
 
58
59
  @types.each do |type|
59
- return true if LowType.type?(type) && type == value.class
60
- return true if type.class == ::Array && value.class == ::Array && array_types_match_values?(types: type, values: value)
60
+ return true if LowType.type?(type) && type == value.class
61
+ return true if ::Array === type && ::Array === value && array_types_match_values?(types: type, values: value)
61
62
 
62
63
  # TODO: Shallow validation of hash could be made deeper with user config.
63
64
  if type.class == ::Hash && value.class == ::Hash && type.keys[0] == value.keys[0].class && type.values[0] == value.values[0].class
@@ -80,10 +81,16 @@ module LowType
80
81
  private
81
82
 
82
83
  def array_types_match_values?(types:, values:)
83
- # TODO: Probably better to use an each that breaks early when types run out.
84
- types.zip(values) do |type, value|
85
- return false unless type === value
84
+ # [T, T, T]
85
+ if types.length > 1
86
+ types.each_with_index do |type, index|
87
+ return false unless type === values[index]
88
+ end
89
+ # [T]
90
+ elsif types.length == 1
91
+ return false unless types.first == values.first.class
86
92
  end
93
+ # TODO: Deep type check (all elements for [T]).
87
94
 
88
95
  true
89
96
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LowType
4
- VERSION = '0.8.5'
4
+ VERSION = '0.8.7'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: low_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.5
4
+ version: 0.8.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi