service_contract 0.0.7 → 0.0.8
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/service_contract/abstract_type.rb +12 -0
- data/lib/service_contract/assertions.rb +13 -34
- data/lib/service_contract/avro/endpoint.rb +1 -1
- data/lib/service_contract/avro/parameter.rb +4 -11
- data/lib/service_contract/avro/type.rb +41 -6
- data/lib/service_contract/avro.rb +0 -1
- data/lib/service_contract/version.rb +1 -1
- data/test/assertions_test.rb +4 -0
- data/test/sample/1/compiled/location.avpr +6 -0
- data/test/sample/1/source/location.avdl +1 -0
- metadata +2 -3
- data/lib/service_contract/avro/array_type.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a89e3a05c63f0cd654e67c928d715402fa1c944
|
4
|
+
data.tar.gz: 6080110653f611f5cfbe03ec81612f52b60f60a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 548643b68055c8241f3c8d7746b1c08255d935c5f33584961b58e81598635449d4858aabf2fffed729da10a437e726aa34323a0098d4c957e60db1d4896bfb0c
|
7
|
+
data.tar.gz: f03f58fa109ceb830b2dd3eb8f79db36058471d07c48b27cce7b8db954348518a6fa2c9e93f3763efd682ad477bc5c11af4fb2154eb6e8041b8b118e382ce02a
|
@@ -6,52 +6,31 @@ module ServiceContract
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def assert_data_matches_type(data, type, allow_nil = true)
|
9
|
-
if type.
|
10
|
-
assert data.is_a?(Array), "expected
|
9
|
+
if type.array?
|
10
|
+
assert data.is_a?(Array), "expected #{type.name} to be an Array"
|
11
11
|
data.each do |datum|
|
12
|
-
assert_data_matches_type(datum, type.subtype)
|
12
|
+
assert_data_matches_type(datum, type.subtype, allow_nil)
|
13
13
|
end
|
14
|
-
|
14
|
+
elsif type.complex?
|
15
|
+
|
16
|
+
# type should have fields
|
15
17
|
type.fields.each do |field|
|
18
|
+
|
19
|
+
# ensure the field is present
|
16
20
|
value = data.fetch(field.name) do
|
17
21
|
data.fetch(field.name.to_sym) do
|
18
22
|
assert false, "expected #{type.name} to have attribute: #{field.name}"
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
if field.type == "array"
|
26
|
-
value.each do |val|
|
27
|
-
assert_data_matches_type(val, field.subtype)
|
28
|
-
end
|
29
|
-
end
|
26
|
+
# check the data type
|
27
|
+
assert_data_matches_type(value, field.type, allow_nil)
|
30
28
|
end
|
29
|
+
else
|
30
|
+
# type is a scalar
|
31
|
+
assert (allow_nil && data.nil?) || type.valid_ruby_types.any?{|klass| data.is_a?(klass)}, "foo"
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
|
-
protected
|
35
|
-
|
36
|
-
def classes_for_parameter(field)
|
37
|
-
type = field.type
|
38
|
-
type = type.type_sym.to_s if type.respond_to?(:type_sym)
|
39
|
-
classes = case type
|
40
|
-
when "array"
|
41
|
-
Array
|
42
|
-
when "int"
|
43
|
-
Fixnum
|
44
|
-
when "string"
|
45
|
-
String
|
46
|
-
when "float"
|
47
|
-
Float
|
48
|
-
when "boolean"
|
49
|
-
[TrueClass, FalseClass]
|
50
|
-
else # a complex type
|
51
|
-
Hash
|
52
|
-
end
|
53
|
-
Array(classes)
|
54
|
-
end
|
55
|
-
|
56
35
|
end
|
57
36
|
end
|
@@ -55,7 +55,7 @@ module ServiceContract
|
|
55
55
|
# seems kinda hacky
|
56
56
|
def member?
|
57
57
|
first_field_type = request.fields.first.type
|
58
|
-
first_field_type.
|
58
|
+
first_field_type.complex? &&
|
59
59
|
first_field_type.name == protocol.main_type
|
60
60
|
end
|
61
61
|
end
|
@@ -6,20 +6,13 @@ module ServiceContract
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def type
|
9
|
-
definition.type
|
10
|
-
definition.type.name :
|
11
|
-
definition.type.type_sym.to_s
|
9
|
+
Type.build(definition.type)
|
12
10
|
end
|
13
11
|
|
14
12
|
def subtype
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
return nil unless item
|
19
|
-
|
20
|
-
item.is_a?(::Avro::Schema::PrimitiveSchema) ?
|
21
|
-
item.type_sym.to_s :
|
22
|
-
item
|
13
|
+
definition.array? ?
|
14
|
+
definition.type.items :
|
15
|
+
nil
|
23
16
|
end
|
24
17
|
|
25
18
|
def default
|
@@ -2,7 +2,11 @@ module ServiceContract
|
|
2
2
|
module Avro
|
3
3
|
class Type < AbstractType
|
4
4
|
def name
|
5
|
-
|
5
|
+
array? ?
|
6
|
+
"Array(#{subtype.name})" :
|
7
|
+
complex? ?
|
8
|
+
definition.name :
|
9
|
+
definition.type.to_s
|
6
10
|
end
|
7
11
|
|
8
12
|
def fields
|
@@ -15,17 +19,48 @@ module ServiceContract
|
|
15
19
|
name
|
16
20
|
end
|
17
21
|
|
22
|
+
def subtype
|
23
|
+
return nil unless definition.respond_to?(:items)
|
24
|
+
Type.build(definition.items)
|
25
|
+
end
|
26
|
+
|
27
|
+
def array?
|
28
|
+
type_string == "array"
|
29
|
+
end
|
30
|
+
|
18
31
|
def self.build(definition)
|
19
|
-
|
20
|
-
|
21
|
-
|
32
|
+
Type.new(definition)
|
33
|
+
end
|
34
|
+
|
35
|
+
def complex?
|
36
|
+
type_string == "record"
|
37
|
+
end
|
38
|
+
|
39
|
+
def valid_ruby_types
|
40
|
+
case type_string
|
41
|
+
when "array"
|
42
|
+
[Array]
|
43
|
+
when "int"
|
44
|
+
[Fixnum]
|
45
|
+
when "string"
|
46
|
+
[String]
|
47
|
+
when "float"
|
48
|
+
[Float]
|
49
|
+
when "boolean"
|
50
|
+
[TrueClass, FalseClass]
|
51
|
+
else # a complex type
|
52
|
+
[Hash]
|
53
|
+
end
|
22
54
|
end
|
23
55
|
|
24
56
|
protected
|
25
57
|
|
26
|
-
def
|
27
|
-
definition.type
|
58
|
+
def type_string
|
59
|
+
type = definition.type
|
60
|
+
type = type.type_sym.to_s if type.respond_to?(:type_sym)
|
61
|
+
type
|
28
62
|
end
|
63
|
+
|
29
64
|
end
|
30
65
|
end
|
31
66
|
end
|
@@ -2,7 +2,6 @@ require 'avro'
|
|
2
2
|
|
3
3
|
module ServiceContract
|
4
4
|
module Avro
|
5
|
-
autoload :ArrayType, 'service_contract/avro/array_type'
|
6
5
|
autoload :Documentation, 'service_contract/avro/documentation'
|
7
6
|
autoload :Endpoint, 'service_contract/avro/endpoint'
|
8
7
|
autoload :Errors, 'service_contract/avro/errors'
|
data/test/assertions_test.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: service_contract
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Ching
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: avro
|
@@ -88,7 +88,6 @@ files:
|
|
88
88
|
- lib/service_contract/abstract_type.rb
|
89
89
|
- lib/service_contract/assertions.rb
|
90
90
|
- lib/service_contract/avro.rb
|
91
|
-
- lib/service_contract/avro/array_type.rb
|
92
91
|
- lib/service_contract/avro/documentation.rb
|
93
92
|
- lib/service_contract/avro/endpoint.rb
|
94
93
|
- lib/service_contract/avro/errors.rb
|