thrift-validator 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f917d062bb6cba06853a570d6e390e66ddc210d
4
- data.tar.gz: 5e41fa2667cea5cc6e360fa3a593de38cf19faf1
3
+ metadata.gz: 7d2072d19fcac9ac4d9e44efc3d1a9b360fde931
4
+ data.tar.gz: 861205ed86104587b4a8fe319f5771c95f9733a4
5
5
  SHA512:
6
- metadata.gz: bbbf1149166c7616571275d48b3f78be4470c69662426f63cb046eea30e3134722430fdc2f3fe2069fb711103cc4a7c264b966fb373ee6f90b90a614b4c87cd9
7
- data.tar.gz: 3d1c69299e2b8a9432b37a6e6d2e807f4227353c77d501abaafa9cc133cc2546b0b7cfe9026b99f24a48cc663af2fb28d961a5d433c105de81e91f9fb357d834
6
+ metadata.gz: b038a575a75faeb0b986f76ce8dfb265d8a31bcdc45d80cd51fed0ff459708096b2ecae3b0ec8cec266b70d441bfff634c4ab141087ff37ade1cae552386f583
7
+ data.tar.gz: 05ee95200e54ff7a6a027a58d160b2fecc15b341e80f0480a9cae6856f1278b9b1383ea25ed922df99ce6c708a74e3cf31f1ffc8ea134e1925888d80685534e0
@@ -10,7 +10,11 @@ module Thrift
10
10
  type, name = field.fetch(:type), field.fetch(:name)
11
11
  case type
12
12
  when Types::STRUCT
13
- validate source.send(name)
13
+ next if source.kind_of?(Union) && source.get_set_field.to_s != name
14
+
15
+ if source.send(name)
16
+ validate source.send(name)
17
+ end
14
18
  when Types::LIST, Types::SET
15
19
  if recurse? field.fetch(:element)
16
20
  Array(source.send(name)).each do |item|
@@ -1,5 +1,5 @@
1
1
  module Thrift
2
2
  class Validator
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
data/test.thrift CHANGED
@@ -35,3 +35,8 @@ struct MapValueExample {
35
35
  1: required map<string, SimpleStruct> required_map
36
36
  2: optional map<string, SimpleStruct> optional_map
37
37
  }
38
+
39
+ union UnionExample {
40
+ 1: SimpleStruct primary
41
+ 2: string alternate
42
+ }
@@ -26,6 +26,12 @@ class AcceptanceTest < MiniTest::Unit::TestCase
26
26
  assert_valid struct
27
27
  end
28
28
 
29
+ def test_passes_if_nested_optional_struct_is_omitted
30
+ struct = NestedExample.new
31
+ struct.required_struct = SimpleStruct.new required_string: 'foo'
32
+ assert_valid struct
33
+ end
34
+
29
35
  def test_fails_if_a_nested_list_item_is_invalid
30
36
  struct = ListExample.new
31
37
  struct.required_list = [ SimpleStruct.new ]
@@ -118,6 +124,31 @@ class AcceptanceTest < MiniTest::Unit::TestCase
118
124
  assert_valid struct
119
125
  end
120
126
 
127
+ def test_fails_if_no_union_fields_set
128
+ union = UnionExample.new
129
+ assert_raises(StandardError) do
130
+ Thrift::Validator.new.validate(union)
131
+ end
132
+ end
133
+
134
+ def test_fails_if_union_set_field_is_invalid
135
+ union = UnionExample.new
136
+ union.primary = SimpleStruct.new
137
+ refute_valid union
138
+ end
139
+
140
+ def test_passes_if_union_set_field_is_valid
141
+ union = UnionExample.new
142
+ union.primary = SimpleStruct.new required_string: 'foo'
143
+ assert_valid union
144
+ end
145
+
146
+ def test_passes_if_unions_alternate_value_is_provided
147
+ union = UnionExample.new
148
+ union.alternate = 'foo'
149
+ assert_valid union
150
+ end
151
+
121
152
  private
122
153
 
123
154
  def refute_valid(struct)
@@ -154,3 +154,32 @@ class MapValueExample
154
154
  ::Thrift::Struct.generate_accessors self
155
155
  end
156
156
 
157
+ class UnionExample < ::Thrift::Union
158
+ include ::Thrift::Struct_Union
159
+ class << self
160
+ def primary(val)
161
+ UnionExample.new(:primary, val)
162
+ end
163
+
164
+ def alternate(val)
165
+ UnionExample.new(:alternate, val)
166
+ end
167
+ end
168
+
169
+ PRIMARY = 1
170
+ ALTERNATE = 2
171
+
172
+ FIELDS = {
173
+ PRIMARY => {:type => ::Thrift::Types::STRUCT, :name => 'primary', :class => ::SimpleStruct},
174
+ ALTERNATE => {:type => ::Thrift::Types::STRING, :name => 'alternate'}
175
+ }
176
+
177
+ def struct_fields; FIELDS; end
178
+
179
+ def validate
180
+ raise(StandardError, 'Union fields are not set.') if get_set_field.nil? || get_value.nil?
181
+ end
182
+
183
+ ::Thrift::Union.generate_accessors self
184
+ end
185
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thrift-validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ahawkins