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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee4858e3b13b2a6f81dcec74ed7fe5bf0844bd1e
4
- data.tar.gz: f96f3c732a2fb5ecab8268cfda2a2e86048d7431
3
+ metadata.gz: 0a89e3a05c63f0cd654e67c928d715402fa1c944
4
+ data.tar.gz: 6080110653f611f5cfbe03ec81612f52b60f60a9
5
5
  SHA512:
6
- metadata.gz: 823d6d06c37ad5dbb1c876e537f874910ffa0a8f98bfa5eb8a9090d9bcb4e94b9618158b51a21dfb2205e61317bd9caced1a7203ee74ee864443d504f72dedc7
7
- data.tar.gz: 3f5e6cca2da6c3fefa541290d17a7877468b52fb9b28bcb51ce6300c9c4ed23aa3a2d745b0b0c7856c34ac097fa2aaa5bce58e7329ce24b89310eb18709e05d3
6
+ metadata.gz: 548643b68055c8241f3c8d7746b1c08255d935c5f33584961b58e81598635449d4858aabf2fffed729da10a437e726aa34323a0098d4c957e60db1d4896bfb0c
7
+ data.tar.gz: f03f58fa109ceb830b2dd3eb8f79db36058471d07c48b27cce7b8db954348518a6fa2c9e93f3763efd682ad477bc5c11af4fb2154eb6e8041b8b118e382ce02a
@@ -11,5 +11,17 @@ module ServiceContract
11
11
  def fields
12
12
  []
13
13
  end
14
+
15
+ def array?
16
+ false
17
+ end
18
+
19
+ def complex?
20
+ false
21
+ end
22
+
23
+ def valid_ruby_types
24
+ [Object]
25
+ end
14
26
  end
15
27
  end
@@ -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.is_a?(ServiceContract::Avro::ArrayType)
10
- assert data.is_a?(Array), "expected response to be an Array"
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
- else
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
- expected_classes = classes_for_parameter(field)
23
- assert (allow_nil && value.nil?) || expected_classes.any?{|klass| value.is_a?(klass)}, "expected #{type.name}.#{field.name} to be a #{expected_classes.join(", ")}"
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.is_a?(::Avro::Schema::RecordSchema) &&
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.type_sym == :record ?
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
- item = definition.type.is_a?(::Avro::Schema::ArraySchema) ?
16
- definition.type.items :
17
- nil
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
- definition.name
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
- definition.is_a?(::Avro::Schema::ArraySchema) ?
20
- ArrayType.new(definition) :
21
- Type.new(definition)
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 record?
27
- definition.type.type_sym == :record
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'
@@ -1,3 +1,3 @@
1
1
  module ServiceContract
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -29,6 +29,10 @@ class AssertionsTest < Minitest::Test
29
29
  timestamp: 1410278741
30
30
  }
31
31
  ],
32
+ numbers: [
33
+ 1,
34
+ 2
35
+ ],
32
36
  updated_at: {
33
37
  timestamp: 1410278741
34
38
  }
@@ -52,6 +52,12 @@
52
52
  "type" : "array",
53
53
  "items" : "Timestamp"
54
54
  }
55
+ }, {
56
+ "name" : "numbers",
57
+ "type" : {
58
+ "type" : "array",
59
+ "items" : "int"
60
+ }
55
61
  }, {
56
62
  "name" : "updated_at",
57
63
  "type" : "Timestamp"
@@ -21,6 +21,7 @@ protocol Location {
21
21
  float longitude;
22
22
  int population;
23
23
  array<Timestamp> foo;
24
+ array<int> numbers;
24
25
  Timestamp updated_at;
25
26
  }
26
27
 
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.7
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-09 00:00:00.000000000 Z
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
@@ -1,17 +0,0 @@
1
- module ServiceContract
2
- module Avro
3
- class ArrayType < Type
4
- def name
5
- "Array"
6
- end
7
-
8
- def subtype
9
- Type.build(definition.items)
10
- end
11
-
12
- def to_s
13
- "Array(#{subtype.to_s})"
14
- end
15
- end
16
- end
17
- end