From b16db6327b8a23d68c8a61317bf95467a8a4f405 Mon Sep 17 00:00:00 2001 From: Jason Jones Date: Tue, 1 Jul 2014 09:29:26 -0800 Subject: [PATCH 1/5] General test cleanup * Cleanup case json in tests * Fix python key existence check test * Add ruby json object enumeration test --- python/tests.py | 26 +++++++++++++------------- ruby/test/centroid_test.rb | 21 +++++++++++++++------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/python/tests.py b/python/tests.py index 56c0ed2..d67e03c 100644 --- a/python/tests.py +++ b/python/tests.py @@ -6,11 +6,11 @@ class ConfigTest(unittest.TestCase): @property def _json_config(self): - return '{"Environment": {"TheKey": "TheValue"}}' + return '{"theEnvironment": {"theKey": "TheValue"}}' @property def _json_config_with_array(self): - return '{"Array": [{"Key": "Value1"}, {"Key": "Value2"}]}' + return '{"theArray": [{"theKey": "Value1"}, {"theKey": "Value2"}]}' @property def _shared_file_path(self): @@ -18,7 +18,7 @@ def _shared_file_path(self): def test_create_from_string(self): config = Config(self._json_config) - self.assertEqual(config.Environment.TheKey, "TheValue") + self.assertEqual(config.the_environment.the_key, "TheValue") def test_create_from_file(self): config = Config.from_file(self._shared_file_path) @@ -36,11 +36,11 @@ def test_raises_if_duplicate_normalized_keys_exist(self): def test_readable_using_snake_case_property(self): config = Config(self._json_config) - self.assertEqual(config.environment.the_key, "TheValue") + self.assertEqual(config.the_environment.the_key, "TheValue") def test_environment_specific_config_is_included(self): config = Config(self._json_config) - environment_config = config.for_environment("Environment") + environment_config = config.for_environment("theEnvironment") self.assertEqual(environment_config.the_key, "TheValue") def test_shared_config_is_included(self): @@ -62,8 +62,8 @@ def test_iterating_raw_config(self): def test_modifying_raw_config(self): config = Config(self._json_config) - config.raw_config["Environment"]["TheKey"] = "NotTheValue" - self.assertEqual(config.environment.the_key, "NotTheValue") + config.raw_config["theEnvironment"]["theKey"] = "NotTheValue" + self.assertEqual(config.the_environment.the_key, "NotTheValue") def test_environment_specific_config_overrides_all(self): config = Config('{"Prod": {"Shared": "production!"}, "All": {"Shared": "none"}}') @@ -72,19 +72,19 @@ def test_environment_specific_config_overrides_all(self): def test_indexing_json_array(self): config = Config(self._json_config_with_array) - self.assertEqual(config.array[0].key, "Value1") - self.assertEqual(config.array[1].key, "Value2") + self.assertEqual(config.the_array[0].the_key, "Value1") + self.assertEqual(config.the_array[1].the_key, "Value2") def test_enumerating_json_array(self): config = Config(self._json_config_with_array) - itemCount = 0; - for item in config.array: + itemCount = 0 + for item in config.the_array: itemCount += 1 self.assertEqual(itemCount, 2) def test_enumerating_json_object(self): config = Config(self._json_config) - itemCount = 0; + itemCount = 0 for item in config: itemCount += 1 self.assertEqual(itemCount, 1) @@ -106,5 +106,5 @@ def test_supports_deep_merge(self): def test_has_key(self): config = Config(self._json_config) - self.assertTrue("environment" in config) + self.assertTrue("the_environment" in config) self.assertTrue("does_not_exist" not in config) diff --git a/ruby/test/centroid_test.rb b/ruby/test/centroid_test.rb index b8dd9b0..a7b7f36 100644 --- a/ruby/test/centroid_test.rb +++ b/ruby/test/centroid_test.rb @@ -4,7 +4,7 @@ class ConfigTests < Test::Unit::TestCase def json_config - '{"Environment":{"TheKey":"TheValue"}}' + '{"theEnvironment":{"theKey":"TheValue"}}' end def shared_file_path @@ -13,7 +13,7 @@ def shared_file_path def test_create_from_string config = Centroid::Config.new(json_config) - assert_equal(config.environment.the_key, "TheValue") + assert_equal(config.the_environment.the_key, "TheValue") end def test_create_from_file @@ -46,12 +46,12 @@ def test_raises_if_duplicate_normalized_keys_exist def test_readable_using_snake_case_property config = Centroid::Config.new(json_config) - assert_equal(config.environment.the_key, "TheValue") + assert_equal(config.the_environment.the_key, "TheValue") end def test_environment_specific_config_is_included config = Centroid::Config.new(json_config) - environment_config = config.for_environment("Environment") + environment_config = config.for_environment("theEnvironment") assert_equal(environment_config.the_key, "TheValue") end @@ -75,8 +75,8 @@ def test_iterating_raw_config def test_modifying_raw_config config = Centroid::Config.new(json_config) - config.raw_config["Environment"]["TheKey"] = "NotTheValue" - assert_equal(config.environment.the_key, "NotTheValue") + config.raw_config["theEnvironment"]["theKey"] = "NotTheValue" + assert_equal(config.the_environment.the_key, "NotTheValue") end def test_environment_specific_config_overrides_all @@ -113,4 +113,13 @@ def test_respond_to assert(config.respond_to?(:environment)) assert(!config.respond_to?(:does_not_exist)) end + + def test_enumerating_json_object + config = Centroid::Config.new(json_config) + itemCount = 0 + config.each do |item| + itemCount += 1 + end + assert_equal(itemCount, 1) + end end From 5d5f175b3728c840665eb181c895c390b2687797 Mon Sep 17 00:00:00 2001 From: Jason Jones Date: Mon, 30 Jun 2014 13:07:52 -0800 Subject: [PATCH 2/5] Ruby - fix enumeration for json object --- ruby/lib/centroid.rb | 22 ++++++++++++++++++++++ ruby/test/centroid_test.rb | 27 ++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ruby/lib/centroid.rb b/ruby/lib/centroid.rb index 11251b3..be38334 100644 --- a/ruby/lib/centroid.rb +++ b/ruby/lib/centroid.rb @@ -21,6 +21,28 @@ def respond_to_missing?(method_name, include_all) has_key?(method_name) end + def each + return enum_for :each unless block_given? + + if raw_config.is_a?(Hash) + raw_config.each do |key, value| + if value.is_a?(Hash) + yield key, Config.new(value) + else + yield key, value + end + end + else + raw_config.each do |value| + if value.is_a?(Hash) + yield Config.new(value) + else + yield value + end + end + end + end + def [](key) raise KeyError.new("Centroid::Config instance does not contain key: #{key}") unless has_key?(key) diff --git a/ruby/test/centroid_test.rb b/ruby/test/centroid_test.rb index a7b7f36..c6091b1 100644 --- a/ruby/test/centroid_test.rb +++ b/ruby/test/centroid_test.rb @@ -104,13 +104,13 @@ def test_supports_deep_merge def test_has_key config = Centroid::Config.new(json_config) - assert(config.has_key?("environment")) + assert(config.has_key?("the_environment")) assert(!config.has_key?("does_not_exist")) end def test_respond_to config = Centroid::Config.new(json_config) - assert(config.respond_to?(:environment)) + assert(config.respond_to?(:the_environment)) assert(!config.respond_to?(:does_not_exist)) end @@ -118,8 +118,29 @@ def test_enumerating_json_object config = Centroid::Config.new(json_config) itemCount = 0 config.each do |item| - itemCount += 1 + itemCount += 1 end assert_equal(itemCount, 1) end + + def test_enumerated_json_object_values_are_still_shiny + json = ' + { + "connections": { + "firstConnection": { + "user": "firstUser", + "password":"secret" + }, + "secondConnection": { + "user": "secondUser", + "password":"secret" + } + } + }' + + config = Centroid::Config.new(json) + config.connections.each do |k, v| + assert_equal(v.password, "secret") + end + end end From 1b2cafdb00c2fd56e53fc107ef74a6442d4a11ff Mon Sep 17 00:00:00 2001 From: Jason Jones Date: Tue, 1 Jul 2014 12:00:35 -0800 Subject: [PATCH 3/5] Python - fix enumeration for json objects --- python/centroid.py | 14 +++++++++++++- python/tests.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/python/centroid.py b/python/centroid.py index 3df80cc..ee696df 100644 --- a/python/centroid.py +++ b/python/centroid.py @@ -20,7 +20,19 @@ def __getitem__(self, key): return value def __iter__(self): - return self.raw_config.__iter__() + if type(self.raw_config) is dict: + for key, value in self.raw_config.iteritems(): + if type(value) is dict: + yield key, Config(value) + else: + yield key, value + + else: + for value in self.raw_config: + if type(value) is dict: + yield Config(value) + else: + yield value # to string def __str__(self): diff --git a/python/tests.py b/python/tests.py index d67e03c..6cf387c 100644 --- a/python/tests.py +++ b/python/tests.py @@ -89,6 +89,25 @@ def test_enumerating_json_object(self): itemCount += 1 self.assertEqual(itemCount, 1) + def test_enumerated_json_object_values_are_still_shiny(self): + json = """ + { + "connections": { + "firstConnection": { + "user": "firstUser", + "password":"secret" + }, + "secondConnection": { + "user": "secondUser", + "password":"secret" + } + } + }""" + + config = Config(json) + for k, v in config.connections: + self.assertEqual(v.password, "secret") + def test_all_environment_is_not_case_sensitive(self): config = Config('{"Prod": {"Shared": "production!"}, "All": {"Shared": "none", "AllOnly": "works"}}') config = config.for_environment("Prod") From 478ce24b759ee562b11e3e6e06537e5ccbbac99b Mon Sep 17 00:00:00 2001 From: Jason Jones Date: Tue, 1 Jul 2014 12:42:08 -0800 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f88cae..01b4c0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ * Deep merge support - [#56](https://github.com/ResourceDataInc/Centroid/pull/56) * Add `__contains__` support - [#50](https://github.com/ResourceDataInc/Centroid/pull/50) +### Fixes + +* Fix issue with enumerating over config values causing values to become non-Centroidy - [#61](https://github.com/ResourceDataInc/Centroid/pull/61) + ## Ruby ### Features @@ -27,6 +31,10 @@ * Deep merge support - [#56](https://github.com/ResourceDataInc/Centroid/pull/56) * Add `has_key?` method - [#50](https://github.com/ResourceDataInc/Centroid/pull/50) +### Fixes + +* Fix issue with enumerating over config values causing values to become non-Centroidy - [#61](https://github.com/ResourceDataInc/Centroid/pull/61) + # 1.0.0 - 2014/02/20 Initial public release. From 5ed3900dfcf9c45028d387b16394fc23531324fb Mon Sep 17 00:00:00 2001 From: Jason Jones Date: Tue, 1 Jul 2014 13:21:21 -0800 Subject: [PATCH 5/5] Ruby - Remove array enumeration handling --- ruby/lib/centroid.rb | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/ruby/lib/centroid.rb b/ruby/lib/centroid.rb index be38334..3ffdfde 100644 --- a/ruby/lib/centroid.rb +++ b/ruby/lib/centroid.rb @@ -24,21 +24,11 @@ def respond_to_missing?(method_name, include_all) def each return enum_for :each unless block_given? - if raw_config.is_a?(Hash) - raw_config.each do |key, value| - if value.is_a?(Hash) - yield key, Config.new(value) - else - yield key, value - end - end - else - raw_config.each do |value| - if value.is_a?(Hash) - yield Config.new(value) - else - yield value - end + raw_config.each do |key, value| + if value.is_a?(Hash) + yield key, Config.new(value) + else + yield key, value end end end