Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Stringify symbols before membrane validation

Change-Id: I13720ec1cbc5548e0b5f645c79ab443dff0ce8e7
  • Loading branch information...
commit 344c5e43eab435cef46038726c07a67b9cd3e4f0 1 parent 1821440
David Sabeti dsabeti authored
78 Gemfile.dea.lock
... ... @@ -0,0 +1,78 @@
  1 +GIT
  2 + remote: https://github.com/cloudfoundry/vcap-common.git
  3 + revision: 45cd194dce44d590512d50d344f66cbe84b46732
  4 + specs:
  5 + vcap_common (2.0.10)
  6 + em-http-request (~> 1.0.0.beta3, < 1.0.0.beta4)
  7 + eventmachine
  8 + httpclient
  9 + membrane (~> 0.0.2)
  10 + mime-types
  11 + multipart-post
  12 + nats (~> 0.4.24)
  13 + posix-spawn (~> 0.3.6)
  14 + thin
  15 + yajl-ruby
  16 +
  17 +PATH
  18 + remote: .
  19 + specs:
  20 + schemata-dea (0.0.1)
  21 + membrane
  22 + vcap_common
  23 + yajl-ruby
  24 +
  25 +GEM
  26 + remote: https://rubygems.org/
  27 + specs:
  28 + addressable (2.3.2)
  29 + builder (3.1.4)
  30 + ci_reporter (1.8.3)
  31 + builder (>= 2.1.2)
  32 + daemons (1.1.9)
  33 + diff-lcs (1.1.3)
  34 + em-http-request (1.0.0.beta.3)
  35 + addressable (>= 2.2.3)
  36 + em-socksify
  37 + eventmachine
  38 + http_parser.rb (>= 0.5.1)
  39 + em-socksify (0.1.0)
  40 + eventmachine
  41 + eventmachine (0.12.10)
  42 + http_parser.rb (0.5.3)
  43 + httpclient (2.3.0.1)
  44 + json_pure (1.7.5)
  45 + membrane (0.0.2)
  46 + mime-types (1.19)
  47 + multipart-post (1.1.5)
  48 + nats (0.4.28)
  49 + daemons (>= 1.1.5)
  50 + eventmachine (= 0.12.10)
  51 + json_pure (>= 1.7.3)
  52 + thin (>= 1.4.1)
  53 + posix-spawn (0.3.6)
  54 + rack (1.4.1)
  55 + rake (10.0.3)
  56 + rspec (2.12.0)
  57 + rspec-core (~> 2.12.0)
  58 + rspec-expectations (~> 2.12.0)
  59 + rspec-mocks (~> 2.12.0)
  60 + rspec-core (2.12.2)
  61 + rspec-expectations (2.12.0)
  62 + diff-lcs (~> 1.1.3)
  63 + rspec-mocks (2.12.0)
  64 + thin (1.5.0)
  65 + daemons (>= 1.0.9)
  66 + eventmachine (>= 0.12.6)
  67 + rack (>= 1.0.0)
  68 + yajl-ruby (1.1.0)
  69 +
  70 +PLATFORMS
  71 + ruby
  72 +
  73 +DEPENDENCIES
  74 + ci_reporter
  75 + rake
  76 + rspec
  77 + schemata-dea!
  78 + vcap_common!
78 Gemfile.router.lock
... ... @@ -0,0 +1,78 @@
  1 +GIT
  2 + remote: https://github.com/cloudfoundry/vcap-common.git
  3 + revision: 45cd194dce44d590512d50d344f66cbe84b46732
  4 + specs:
  5 + vcap_common (2.0.10)
  6 + em-http-request (~> 1.0.0.beta3, < 1.0.0.beta4)
  7 + eventmachine
  8 + httpclient
  9 + membrane (~> 0.0.2)
  10 + mime-types
  11 + multipart-post
  12 + nats (~> 0.4.24)
  13 + posix-spawn (~> 0.3.6)
  14 + thin
  15 + yajl-ruby
  16 +
  17 +PATH
  18 + remote: .
  19 + specs:
  20 + schemata-router (0.0.1)
  21 + membrane
  22 + vcap_common
  23 + yajl-ruby
  24 +
  25 +GEM
  26 + remote: https://rubygems.org/
  27 + specs:
  28 + addressable (2.3.2)
  29 + builder (3.1.4)
  30 + ci_reporter (1.8.3)
  31 + builder (>= 2.1.2)
  32 + daemons (1.1.9)
  33 + diff-lcs (1.1.3)
  34 + em-http-request (1.0.0.beta.3)
  35 + addressable (>= 2.2.3)
  36 + em-socksify
  37 + eventmachine
  38 + http_parser.rb (>= 0.5.1)
  39 + em-socksify (0.1.0)
  40 + eventmachine
  41 + eventmachine (0.12.10)
  42 + http_parser.rb (0.5.3)
  43 + httpclient (2.3.0.1)
  44 + json_pure (1.7.5)
  45 + membrane (0.0.2)
  46 + mime-types (1.19)
  47 + multipart-post (1.1.5)
  48 + nats (0.4.28)
  49 + daemons (>= 1.1.5)
  50 + eventmachine (= 0.12.10)
  51 + json_pure (>= 1.7.3)
  52 + thin (>= 1.4.1)
  53 + posix-spawn (0.3.6)
  54 + rack (1.4.1)
  55 + rake (10.0.3)
  56 + rspec (2.12.0)
  57 + rspec-core (~> 2.12.0)
  58 + rspec-expectations (~> 2.12.0)
  59 + rspec-mocks (~> 2.12.0)
  60 + rspec-core (2.12.2)
  61 + rspec-expectations (2.12.1)
  62 + diff-lcs (~> 1.1.3)
  63 + rspec-mocks (2.12.0)
  64 + thin (1.5.0)
  65 + daemons (>= 1.0.9)
  66 + eventmachine (>= 0.12.6)
  67 + rack (>= 1.0.0)
  68 + yajl-ruby (1.1.0)
  69 +
  70 +PLATFORMS
  71 + ruby
  72 +
  73 +DEPENDENCIES
  74 + ci_reporter
  75 + rake
  76 + rspec
  77 + schemata-router!
  78 + vcap_common!
77 Gemfile.staging.lock
... ... @@ -0,0 +1,77 @@
  1 +GIT
  2 + remote: https://github.com/cloudfoundry/vcap-common.git
  3 + revision: 45cd194dce44d590512d50d344f66cbe84b46732
  4 + specs:
  5 + vcap_common (2.0.10)
  6 + em-http-request (~> 1.0.0.beta3, < 1.0.0.beta4)
  7 + eventmachine
  8 + httpclient
  9 + membrane (~> 0.0.2)
  10 + mime-types
  11 + multipart-post
  12 + nats (~> 0.4.24)
  13 + posix-spawn (~> 0.3.6)
  14 + thin
  15 + yajl-ruby
  16 +
  17 +PATH
  18 + remote: .
  19 + specs:
  20 + schemata-staging (0.0.2)
  21 + membrane
  22 + yajl-ruby
  23 +
  24 +GEM
  25 + remote: https://rubygems.org/
  26 + specs:
  27 + addressable (2.3.2)
  28 + builder (3.1.4)
  29 + ci_reporter (1.8.3)
  30 + builder (>= 2.1.2)
  31 + daemons (1.1.9)
  32 + diff-lcs (1.1.3)
  33 + em-http-request (1.0.0.beta.3)
  34 + addressable (>= 2.2.3)
  35 + em-socksify
  36 + eventmachine
  37 + http_parser.rb (>= 0.5.1)
  38 + em-socksify (0.1.0)
  39 + eventmachine
  40 + eventmachine (0.12.10)
  41 + http_parser.rb (0.5.3)
  42 + httpclient (2.3.0.1)
  43 + json_pure (1.7.5)
  44 + membrane (0.0.2)
  45 + mime-types (1.19)
  46 + multipart-post (1.1.5)
  47 + nats (0.4.28)
  48 + daemons (>= 1.1.5)
  49 + eventmachine (= 0.12.10)
  50 + json_pure (>= 1.7.3)
  51 + thin (>= 1.4.1)
  52 + posix-spawn (0.3.6)
  53 + rack (1.4.1)
  54 + rake (10.0.3)
  55 + rspec (2.12.0)
  56 + rspec-core (~> 2.12.0)
  57 + rspec-expectations (~> 2.12.0)
  58 + rspec-mocks (~> 2.12.0)
  59 + rspec-core (2.12.2)
  60 + rspec-expectations (2.12.0)
  61 + diff-lcs (~> 1.1.3)
  62 + rspec-mocks (2.12.0)
  63 + thin (1.5.0)
  64 + daemons (>= 1.0.9)
  65 + eventmachine (>= 0.12.6)
  66 + rack (>= 1.0.0)
  67 + yajl-ruby (1.1.0)
  68 +
  69 +PLATFORMS
  70 + ruby
  71 +
  72 +DEPENDENCIES
  73 + ci_reporter
  74 + rake
  75 + rspec
  76 + schemata-staging!
  77 + vcap_common!
14 lib/schemata/common/msgbase.rb
@@ -14,6 +14,17 @@ def initialize(data = {})
14 14 field_schema = @schema.schemas[key]
15 15 next unless field_schema
16 16
  17 + # TODO This call to stringify should be removed when cc/dea stop using
  18 + # Symbols.
  19 + #
  20 + # Currently, some fields (for example, 'states' in requests sent
  21 + # on dea.find.droplet), are are symbols, During Yajl decoding, however,
  22 + # they become strings. Thus, on the encoding side, Schemata should expect
  23 + # symbols, but on the decoding side, it should expect strings. To allow
  24 + # for this in the schema definition, Schemata stringifies all symbols during
  25 + # construction of Schemata objects.
  26 + field_value = Schemata::HashCopyHelpers.stringify(field_value)
  27 +
17 28 begin
18 29 field_schema.validate(field_value)
19 30 rescue Membrane::SchemaValidationError => e
@@ -35,7 +46,10 @@ def self.define(schema)
35 46 nil
36 47 end
37 48
  49 + # TODO This call to stringify should be removed when cc/dea stops using
  50 + # symbols. See comment above for a better description.
38 51 vc_klass.send(:define_method, "#{key}=") do |field_value|
  52 + field_value = Schemata::HashCopyHelpers.stringify(field_value)
39 53 begin
40 54 field_schema.validate(field_value)
41 55 rescue Membrane::SchemaValidationError => e
26 lib/schemata/helpers/stringify.rb
... ... @@ -0,0 +1,26 @@
  1 +module Schemata
  2 + module HashCopyHelpers
  3 +
  4 + def self.stringify(node)
  5 + case node
  6 + when String
  7 + return node
  8 + when Numeric, TrueClass, FalseClass
  9 + return node
  10 + when Hash
  11 + copy = {}
  12 + node.each { |k, v| copy[k.to_s] = stringify(v) }
  13 + return copy
  14 + when Array
  15 + return node.map { |v| stringify(v) }
  16 + when NilClass
  17 + return nil
  18 + when Symbol
  19 + return node.to_s
  20 + else
  21 + raise CopyError.new("Unexpected class: #{node.class}")
  22 + end
  23 + end
  24 +
  25 + end
  26 +end
28 spec/common/helpers_spec.rb
... ... @@ -1,4 +1,5 @@
1 1 require 'schemata/helpers/hash_copy'
  2 +require 'schemata/helpers/stringify'
2 3
3 4 describe Schemata::HashCopyHelpers do
4 5 describe "#deep_copy" do
@@ -29,7 +30,7 @@
29 30 copy.should be_an_instance_of Fixnum
30 31
31 32 # set original to be max fixnum + 1
32   - original = 2**(0.size * 8 -2)
  33 + original = 2**(0.size * 8 - 2)
33 34 copy = Schemata::HashCopyHelpers.deep_copy(original)
34 35 copy.should == original
35 36 copy.should be_an_instance_of Bignum
@@ -86,4 +87,29 @@
86 87 end.to raise_error(described_class::CopyError, /Unexpected class: /)
87 88 end
88 89 end
  90 +
  91 + describe "#stringify" do
  92 + it "should stringify nil" do
  93 + str = Schemata::HashCopyHelpers.stringify(nil)
  94 + str.should == nil
  95 + end
  96 +
  97 + it "should stringify a string" do
  98 + original = "foo"
  99 + str = Schemata::HashCopyHelpers.stringify(original)
  100 + str.should == "foo"
  101 + end
  102 +
  103 + it "should stringify a symbol" do
  104 + original = :foo
  105 + str = Schemata::HashCopyHelpers.stringify(original)
  106 + str.should == "foo"
  107 + end
  108 +
  109 + it "should stringify a hash" do
  110 + original = { "foo" => :foo }
  111 + str = Schemata::HashCopyHelpers.stringify(original)
  112 + str.should == { "foo" => "foo" }
  113 + end
  114 + end
89 115 end
17 spec/dea/find_droplet_request_spec.rb
@@ -7,4 +7,21 @@
7 7
8 8 describe Schemata::DEA::FindDropletRequest::V1 do
9 9 it_behaves_like "a message"
  10 +
  11 + it "should stringify the 'states' field when it is given to the constructor as a symbol" do
  12 + hash = {
  13 + "droplet" => "deadbeef",
  14 + "states" => [:RUNNING],
  15 + "version" => "0.1.0",
  16 + }
  17 +
  18 + msg_obj = Schemata::DEA::FindDropletRequest::V1.new(hash)
  19 + msg_obj.states.should =~ ["RUNNING"]
  20 + end
  21 +
  22 + it "should stringify the 'states' field when a symbold is passed ot the attr writer" do
  23 + msg_obj = Schemata::DEA.mock_find_droplet_request
  24 + msg_obj.states = [:RUNNING]
  25 + msg_obj.states.should =~ ["RUNNING"]
  26 + end
10 27 end

0 comments on commit 344c5e4

Please sign in to comment.
Something went wrong with that request. Please try again.