Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Commit

Permalink
revert change, test proper behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
plukevdh committed Apr 25, 2017
1 parent 146d561 commit bdb9243
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 16 deletions.
6 changes: 3 additions & 3 deletions spec/configs/plugin_config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions spec/fixtures/datadog.json
Original file line number Diff line number Diff line change
@@ -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"
}
14 changes: 14 additions & 0 deletions spec/fixtures/datadog.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 0 additions & 5 deletions spec/mixins/normalize_attributes_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions spec/models/plugin_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions src/biplane/child_collection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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
T.child_key
end

def each
@collection.each {|item| yield item }
@collection.each { |item| yield item }
end

def lookup(id)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/biplane/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/biplane/mixins/normalize_attributes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit bdb9243

Please sign in to comment.