diff --git a/spec/configs/plugin_config_spec.cr b/spec/configs/plugin_config_spec.cr index d9edf26..b3bc280 100644 --- a/spec/configs/plugin_config_spec.cr +++ b/spec/configs/plugin_config_spec.cr @@ -22,7 +22,7 @@ module Biplane plugin.member_route.to_s.should eq "/apis/#{parent.name}/plugins/:id" end - it "ignores empty values" do + it "passes through empty values" do yaml = <<-YAML name: 'datadog' attributes: @@ -44,8 +44,8 @@ module Biplane empty_plugin = PluginConfig.from_yaml(yaml) config = empty_plugin.attributes["config"] as Hash - config.keys.should_not contain("tags") - # config["tags"].should eq Hash(String, PluginConfig::Type).new + config.keys.should contain("tags") + config["tags"].should eq Hash(String, PluginConfig::Type).new end it "can present config as params" do diff --git a/spec/fixtures/datadog.json b/spec/fixtures/datadog.json new file mode 100644 index 0000000..c6a355b --- /dev/null +++ b/spec/fixtures/datadog.json @@ -0,0 +1,31 @@ +{ + "api_id": "abc-123-583-123", + "config": { + "host": "datadog", + "timeout": 10000, + "metrics": [ + "request_count", + "latency", + "request_size", + "status_count", + "response_size", + "unique_users", + "request_per_user" + ], + "port": 1234, + "tags": { + "latency": {}, + "request_count": {}, + "request_per_user": {}, + "request_size": {}, + "response_size": {}, + "status_count": {}, + "unique_users": {}, + "upstream_latency": {} + } + }, + "created_at": 1493065738000, + "enabled": true, + "id": "abc-123-456-123as", + "name": "datadog" +} diff --git a/spec/fixtures/datadog.yaml b/spec/fixtures/datadog.yaml new file mode 100644 index 0000000..de0b6d1 --- /dev/null +++ b/spec/fixtures/datadog.yaml @@ -0,0 +1,14 @@ +name: 'datadog' +attributes: + config: + host: datadog + timeout: 10000 + metrics: + - request_count + - latency + - request_size + - status_count + - response_size + - unique_users + - request_per_user + port: 1234 diff --git a/spec/mixins/normalize_attributes_spec.cr b/spec/mixins/normalize_attributes_spec.cr index 688fc5e..08ed88b 100644 --- a/spec/mixins/normalize_attributes_spec.cr +++ b/spec/mixins/normalize_attributes_spec.cr @@ -35,10 +35,5 @@ module Biplane result = Dummy.new({"one": {"two": "three"}}).norm({"four": "five"}) result.should eq({"one": {"two": "three"}, "four": "five"}) end - - it "ignores empty nests" do - result = Dummy.new({"one": {"two": "three"}}).norm({"four": Hash(String, String).new}) - result.should eq({"one": {"two": "three"}}) - end end end diff --git a/spec/models/plugin_spec.cr b/spec/models/plugin_spec.cr index 7c24647..d97ff37 100644 --- a/spec/models/plugin_spec.cr +++ b/spec/models/plugin_spec.cr @@ -94,6 +94,13 @@ module Biplane plugin.diff(new_cfg).should eq({"attributes": {"config": Diff.new(nil, plugin.attributes["config"])}}) end + + it "will ignore nested empties" do + plugin = build_json_fixture(Plugin, "datadog") + cfg = build_yaml_fixture(PluginConfig, "datadog") + + plugin.should eq(cfg) + end end end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index f20a061..f3c467a 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -3,14 +3,22 @@ require "mock" require "../src/biplane" +def build_json_fixture(type, name) + type.from_json File.read("./spec/fixtures/#{name}.json") +end + +def build_yaml_fixture(type, name) + type.from_yaml File.read("./spec/fixtures/#{name}.yaml") +end + def json_fixture(type) base = type.name.split("::").last.downcase - type.from_json File.read("./spec/fixtures/#{base}.json") + build_json_fixture(type, base) end def yaml_fixture(type) base = type.name.split("::").last.gsub("Config", "").downcase - type.from_yaml File.read("./spec/fixtures/#{base}.yaml") + build_yaml_fixture(type, base) end def build_response(body : Hash | Nil, status = 200) diff --git a/src/biplane/child_collection.cr b/src/biplane/child_collection.cr index 1b7be87..6bee930 100644 --- a/src/biplane/child_collection.cr +++ b/src/biplane/child_collection.cr @@ -8,7 +8,7 @@ module Biplane delegate empty?, last, @collection def initialize(@collection : Array(T), @parent = nil) - @collection.map! {|item| item.parent = @parent; item } unless @parent.nil? + @collection.map! { |item| item.parent = @parent; item } unless @parent.nil? end def id_key @@ -16,7 +16,7 @@ module Biplane end def each - @collection.each {|item| yield item } + @collection.each { |item| yield item } end def lookup(id) @@ -42,19 +42,21 @@ module Biplane end def diff(other : ChildCollection) + # if union of keys is empty, we have no comparable items all_keys = keys | other.keys return if all_keys.empty? + # compare on a key-by-key basis diffs = all_keys.map do |k| this_inst = lookup(k) other_inst = other.lookup(k) result = compare(this_inst, other_inst) - { k => result } if result && !result.empty? + {k => result} if result && !result.empty? end.compact - diffs.empty? ? nil : diffs.reduce {|memo, item| memo.merge(item) } + diffs.empty? ? nil : diffs.reduce { |memo, item| memo.merge(item) } end # Removal diff --git a/src/biplane/config.cr b/src/biplane/config.cr index 88c6a98..919199d 100644 --- a/src/biplane/config.cr +++ b/src/biplane/config.cr @@ -36,6 +36,7 @@ module Biplane io << serialize.to_s end + # overrides to allow for empty values private def valid_value?(value) !value.is_a?(Nil) end diff --git a/src/biplane/mixins/normalize_attributes.cr b/src/biplane/mixins/normalize_attributes.cr index eae3d92..b28b13a 100644 --- a/src/biplane/mixins/normalize_attributes.cr +++ b/src/biplane/mixins/normalize_attributes.cr @@ -11,8 +11,6 @@ module Biplane::Mixins end private def normalize(memo, key, value) - return memo if value.is_a?(Hash) && value.empty? - stretch_key(memo, key.split('.'), value) end