schemata-router 0.0.1.beta6 → 0.0.1.beta7

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ module Schemata
6
6
  class UpdateAttributeError < StandardError
7
7
  def initialize(key, message)
8
8
  super("#{key}: #{message}")
9
- end
9
+ end
10
10
  end
11
11
 
12
12
  class IncompatibleVersionError < DecodeError
@@ -1,4 +1,5 @@
1
1
  require 'yajl'
2
+ require 'membrane'
2
3
  require 'schemata/common/error'
3
4
  require 'schemata/helpers/stringify'
4
5
  require 'membrane'
@@ -29,7 +30,7 @@ module Schemata
29
30
 
30
31
  begin
31
32
  field_schema.validate(field_value)
32
- rescue ::Membrane::SchemaValidationError => e
33
+ rescue Membrane::SchemaValidationError => e
33
34
  raise Schemata::UpdateAttributeError.new(key, e.message)
34
35
  end
35
36
 
@@ -54,7 +55,7 @@ module Schemata
54
55
  field_value = Schemata::HashCopyHelpers.stringify(field_value)
55
56
  begin
56
57
  field_schema.validate(field_value)
57
- rescue ::Membrane::SchemaValidationError => e
58
+ rescue Membrane::SchemaValidationError => e
58
59
  raise Schemata::UpdateAttributeError.new(key, e.message)
59
60
  end
60
61
  @contents[key] = Schemata::HashCopyHelpers.deep_copy(field_value)
@@ -96,7 +97,7 @@ module Schemata
96
97
  begin
97
98
  validate_contents
98
99
  validate_aux_data
99
- rescue ::Membrane::SchemaValidationError => e
100
+ rescue Membrane::SchemaValidationError => e
100
101
  raise Schemata::EncodeError.new(e.message)
101
102
  end
102
103
 
@@ -194,16 +195,16 @@ module Schemata
194
195
 
195
196
  module Dsl
196
197
  def define_schema(&blk)
197
- schema = ::Membrane::SchemaParser.parse(&blk)
198
- unless schema.kind_of? ::Membrane::Schema::Record
198
+ schema = Membrane::SchemaParser.parse(&blk)
199
+ unless schema.kind_of? Membrane::Schema::Record
199
200
  Schemata::SchemaDefinitionError.new("Schema must be a hash")
200
201
  end
201
202
  self::const_set(:SCHEMA, schema)
202
203
  end
203
204
 
204
205
  def define_aux_schema(&blk)
205
- aux_schema = ::Membrane::SchemaParser.parse(&blk)
206
- unless aux_schema.kind_of? ::Membrane::Schema::Record
206
+ aux_schema = Membrane::SchemaParser.parse(&blk)
207
+ unless aux_schema.kind_of? Membrane::Schema::Record
207
208
  Schemata::SchemaDefinitionError.new("Schema must be a hash")
208
209
  end
209
210
 
@@ -223,7 +224,7 @@ module Schemata
223
224
  # decode, when aux_data is irrelevant
224
225
  begin
225
226
  previous_version::SCHEMA.validate(old_data)
226
- rescue ::Membrane::SchemaValidationError => e
227
+ rescue Membrane::SchemaValidationError => e
227
228
  raise Schemata::DecodeError.new(e.message)
228
229
  end
229
230
 
@@ -264,7 +265,7 @@ module Schemata
264
265
  begin
265
266
  self.schema.validate(mock)
266
267
  define_constant(:MOCK_VALUES, hash)
267
- rescue ::Membrane::SchemaValidationError => e
268
+ rescue Membrane::SchemaValidationError => e
268
269
  raise SchemaDefinitionError.new("Sample mock values do not match schema: #{e}")
269
270
  end
270
271
  end
@@ -1,4 +1,4 @@
1
- #require 'vcap/common'
1
+ require 'vcap/common'
2
2
 
3
3
  module Schemata
4
4
  module Router
@@ -34,7 +34,7 @@ module Schemata
34
34
 
35
35
  define_mock_values do
36
36
  {
37
- "dea" => "deadbeef", #proc { VCAP.secure_uuid },
37
+ "dea" => proc { VCAP.secure_uuid },
38
38
  "app" => proc { Random.rand(100).to_s },
39
39
  "uris" => ["foo.vcap.me"],
40
40
  "host" => "127.0.0.1",
@@ -44,7 +44,7 @@ module Schemata
44
44
  "runtime" => "ruby18",
45
45
  "component" => "dashboard",
46
46
  },
47
- "private_instance_id" => "deadbeef1",#proc { VCAP.secure_uuid },
47
+ "private_instance_id" => proc { VCAP.secure_uuid },
48
48
  }
49
49
  end
50
50
  end
@@ -25,7 +25,7 @@ module Schemata
25
25
 
26
26
  define_mock_values do
27
27
  {
28
- "id" => "deadbeef", #proc { VCAP.secure_uuid },
28
+ "id" => proc { VCAP.secure_uuid },
29
29
  "version" => 0.1,
30
30
  }
31
31
  end
@@ -1,5 +1,5 @@
1
1
  module Schemata
2
2
  module Router
3
- VERSION = "0.0.1.beta6"
3
+ VERSION = "0.0.1.beta7"
4
4
  end
5
5
  end
@@ -52,3 +52,46 @@ def num_mandatory_fields(msg_obj)
52
52
  diff = mandatory - optional
53
53
  return diff.size
54
54
  end
55
+
56
+ def get_allowed_classes(schema)
57
+ case schema
58
+ when Membrane::Schema::Bool
59
+ return Set.new([TrueClass, FalseClass])
60
+ when Membrane::Schema::Enum
61
+ return Set.new(schema.elem_schemas.map {|x| get_allowed_classes(x)}).flatten
62
+ when Membrane::Schema::Class
63
+ return Set.new([schema.klass])
64
+ when Membrane::Schema::Record
65
+ return Set.new([Hash])
66
+ when Membrane::Schema::List
67
+ return Set.new([Array])
68
+ end
69
+ end
70
+
71
+ def get_unallowed_classes(schema)
72
+ all_classes = Set.new([Integer, String, Float, TrueClass, FalseClass, NilClass, Hash, Array])
73
+ allowed_classes = get_allowed_classes(schema)
74
+ all_classes - allowed_classes
75
+ end
76
+
77
+ def default_value(klass)
78
+ # Yes this is silly
79
+ if klass == Integer
80
+ return 0
81
+ elsif klass == String
82
+ return "foo"
83
+ elsif klass == Float
84
+ return 3.14
85
+ elsif klass == TrueClass
86
+ return true
87
+ elsif klass == FalseClass
88
+ return false
89
+ elsif klass == NilClass
90
+ return nil
91
+ elsif klass == Hash
92
+ return {}
93
+ elsif klass == Array
94
+ return []
95
+ else
96
+ end
97
+ end
@@ -35,11 +35,10 @@ shared_examples "a message" do
35
35
  mock_hash = component.send(mock_method, 1).contents
36
36
  first_key = mock_hash.keys[0]
37
37
  first_value = mock_hash[first_key]
38
- if first_value.kind_of? Integer
39
- bad_value = "foo"
40
- else
41
- bad_value = 1
42
- end
38
+
39
+ schema = message.schema.schemas[first_key]
40
+ unallowed_classes = get_unallowed_classes(schema)
41
+ bad_value = default_value(unallowed_classes.to_a[0])
43
42
 
44
43
  expect {
45
44
  msg_obj = message.new({first_key => bad_value})
@@ -120,11 +119,9 @@ shared_examples "a message" do
120
119
  it "should raise an error if the wrong type is written" do
121
120
  mock_value = component.send(mock_method, version).contents[attr]
122
121
 
123
- if mock_value.kind_of? Integer
124
- bad_value = "foo"
125
- else
126
- bad_value = 1
127
- end
122
+ schema = message.schema.schemas[attr]
123
+ unallowed_classes = get_unallowed_classes(schema)
124
+ bad_value = default_value(unallowed_classes.to_a[0])
128
125
 
129
126
  msg_obj = message.new
130
127
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemata-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta6
4
+ version: 0.0.1.beta7
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: vcap_common
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: ci_reporter
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -99,11 +115,13 @@ executables: []
99
115
  extensions: []
100
116
  extra_rdoc_files: []
101
117
  files:
118
+ - lib/schemata/router/register_directory_server_request/register_directory_server_request_v1.rb
102
119
  - lib/schemata/router/register_request.rb
103
120
  - lib/schemata/router/register_request/register_request_v1.rb
104
121
  - lib/schemata/router/start_message.rb
105
122
  - lib/schemata/router/version.rb
106
123
  - lib/schemata/router/start_message/start_message_v1.rb
124
+ - lib/schemata/router/register_directory_server_request.rb
107
125
  - lib/schemata/router.rb
108
126
  - lib/schemata/common/msgtypebase.rb
109
127
  - lib/schemata/common/parsed_msg.rb