Skip to content

Commit

Permalink
flatten nested plugin config items (#27)
Browse files Browse the repository at this point in the history
this builds upon PR #12 in that it extends the support to also cover
plugin configuration that is deeply nested:

    config = {
      a = {
        b = 1,
        c = 2
      }
    }

    # => {
    #      "config.a.b" => 1,
    #      "config.a.c" => 2
    #    }
  • Loading branch information
amireh authored and nevalla committed Sep 7, 2018
1 parent 741a5f0 commit 7ae8f3a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/kong.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
require_relative 'kong/target'
require_relative 'kong/upstream'
require_relative 'kong/server'
require_relative 'kong/util'
22 changes: 11 additions & 11 deletions lib/kong/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative './util'

module Kong
class Plugin
include Base
Expand All @@ -8,24 +10,22 @@ class Plugin

# Create resource
def create
if attributes['config']
attributes['config'].each do |key, value|
attributes["config.#{key}"] = value
end
attributes.delete('config')
end
flatten_config
super
end

# update resource
def update
flatten_config
super
end

private

def flatten_config
if attributes['config']
attributes['config'].each do |key, value|
attributes["config.#{key}"] = value
end
attributes.delete('config')
attributes.merge!(Util.flatten(attributes.delete('config'), 'config'))
end
super
end
end
end
16 changes: 16 additions & 0 deletions lib/kong/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Kong
module Util
def self.flatten(cursor, parent_key = nil, memo = {})
memo.tap do
case cursor
when Hash
cursor.keys.each do |key|
flatten(cursor[key], [parent_key, key].compact.join('.'), memo)
end
else
memo["#{parent_key}"] = cursor
end
end
end
end
end
16 changes: 16 additions & 0 deletions spec/kong/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
subject.create
end

it 'transforms nested config keys to config.key format' do
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' }
expect(Kong::Client.instance).to receive(:post).with('/apis/:api_id/plugins/', nil, attributes, headers).and_return(attributes)
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } })
subject.create
end
end

describe '#update' do
Expand All @@ -42,5 +50,13 @@
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
subject.update
end

it 'transforms nested config keys to config.key format' do
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' }
expect(Kong::Client.instance).to receive(:patch).with('/apis/:api_id/plugins/', nil, attributes, headers).and_return(attributes)
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } })
subject.update
end
end
end
29 changes: 29 additions & 0 deletions spec/kong/util_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative "../spec_helper"

describe Kong::Util do
describe '.flatten' do
subject { described_class.method(:flatten) }

it 'works' do
expect(subject[{
a: {
b: 1,
c: {
d: 3
}
},
b: 2
}]).to include({
"a.b" => 1,
"a.c.d" => 3,
"b" => 2,
})
end

it 'accepts a scope' do
expect(subject[{ a: "1" }, 'config']).to eq({
"config.a" => "1"
})
end
end
end

0 comments on commit 7ae8f3a

Please sign in to comment.