From 6d111adf380a7d581b61654850b97135533ddd4b Mon Sep 17 00:00:00 2001 From: kpacha Date: Sat, 2 Jun 2018 16:21:22 +0200 Subject: [PATCH] pass a backend definition to the formater --- proxy/formatter.go | 24 ++++++++++++++---------- proxy/formatter_benchmark_test.go | 12 +++++++----- proxy/formatter_test.go | 29 +++++++++++++++++++---------- proxy/http.go | 2 +- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/proxy/formatter.go b/proxy/formatter.go index 40dc162f2..d32b054a7 100644 --- a/proxy/formatter.go +++ b/proxy/formatter.go @@ -1,6 +1,10 @@ package proxy -import "strings" +import ( + "strings" + + "github.com/devopsfaith/krakend/config" +) // EntityFormatter formats the response data type EntityFormatter interface { @@ -22,22 +26,22 @@ type entityFormatter struct { Mapping map[string]string } -// NewEntityFormatter creates an entity formatter with the received params -func NewEntityFormatter(target string, whitelist, blacklist []string, group string, mappings map[string]string) EntityFormatter { +// NewEntityFormatter creates an entity formatter with the received backend definition +func NewEntityFormatter(remote *config.Backend) EntityFormatter { var propertyFilter propertyFilter - if len(whitelist) > 0 { - propertyFilter = newWhitelistingFilter(whitelist) + if len(remote.Whitelist) > 0 { + propertyFilter = newWhitelistingFilter(remote.Whitelist) } else { - propertyFilter = newBlacklistingFilter(blacklist) + propertyFilter = newBlacklistingFilter(remote.Blacklist) } - sanitizedMappings := make(map[string]string, len(mappings)) - for i, m := range mappings { + sanitizedMappings := make(map[string]string, len(remote.Mapping)) + for i, m := range remote.Mapping { v := strings.Split(m, ".") sanitizedMappings[i] = v[0] } return entityFormatter{ - Target: target, - Prefix: group, + Target: remote.Target, + Prefix: remote.Group, PropertyFilter: propertyFilter, Mapping: sanitizedMappings, } diff --git a/proxy/formatter_benchmark_test.go b/proxy/formatter_benchmark_test.go index c59ac91bb..341f325ba 100644 --- a/proxy/formatter_benchmark_test.go +++ b/proxy/formatter_benchmark_test.go @@ -4,6 +4,8 @@ import ( "bytes" "fmt" "testing" + + "github.com/devopsfaith/krakend/config" ) func BenchmarkEntityFormatter_whitelistingFilter(b *testing.B) { @@ -30,7 +32,7 @@ func BenchmarkEntityFormatter_whitelistingFilter(b *testing.B) { IsComplete: true, } b.Run(fmt.Sprintf("with %d elements with %d extra fields", len(testCase), extraFields), func(b *testing.B) { - f := NewEntityFormatter("", testCase, []string{}, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Whitelist: testCase}) b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { @@ -88,7 +90,7 @@ func BenchmarkEntityFormatter_deepWhitelistingFilter(b *testing.B) { Data: data, IsComplete: true, } - f := NewEntityFormatter("", whitelist, []string{}, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Whitelist: whitelist}) b.Run(fmt.Sprintf("numTargets:%d,depth:%d,extraFields:%d,extraSiblings:%d", nTargets, depth, extraFields, extraSiblings), func(b *testing.B) { b.ReportAllocs() b.ResetTimer() @@ -124,7 +126,7 @@ func BenchmarkEntityFormatter_blacklistingFilter(b *testing.B) { IsComplete: true, } b.Run(fmt.Sprintf("with %d elements with %d extra fields", len(testCase), extraFields), func(b *testing.B) { - f := NewEntityFormatter("", []string{}, testCase, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Blacklist: testCase}) b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { @@ -147,7 +149,7 @@ func BenchmarkEntityFormatter_grouping(b *testing.B) { IsComplete: true, } b.Run(fmt.Sprintf("with %d elements", extraFields), func(b *testing.B) { - f := NewEntityFormatter("", []string{}, []string{}, preffix, map[string]string{}) + f := NewEntityFormatter(&config.Backend{Group: preffix}) b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { @@ -176,7 +178,7 @@ func BenchmarkEntityFormatter_mapping(b *testing.B) { IsComplete: true, } b.Run(fmt.Sprintf("with %d elements with %d extra fields", len(testCase), extraFields), func(b *testing.B) { - f := NewEntityFormatter("", []string{}, []string{}, "", testCase) + f := NewEntityFormatter(&config.Backend{Mapping: testCase}) b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { diff --git a/proxy/formatter_test.go b/proxy/formatter_test.go index cddbcc778..8eb32bf38 100644 --- a/proxy/formatter_test.go +++ b/proxy/formatter_test.go @@ -1,6 +1,10 @@ package proxy -import "testing" +import ( + "testing" + + "github.com/devopsfaith/krakend/config" +) func TestEntityFormatterFunc(t *testing.T) { expected := Response{Data: map[string]interface{}{"one": 1}, IsComplete: true} @@ -39,7 +43,7 @@ func TestEntityFormatter_newWhitelistingFilter(t *testing.T) { }, IsComplete: true, } - f := NewEntityFormatter("", []string{"supu", "a.b", "a.c", "foo.unknown"}, []string{}, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Whitelist: []string{"supu", "a.b", "a.c", "foo.unknown"}}) result := f.Format(sample) if v, ok := result.Data["supu"]; !ok || v != expected.Data["supu"] { t.Errorf("The formatter returned an unexpected result for the field supu: %v\n", result) @@ -86,7 +90,7 @@ func TestEntityFormatter_newWhitelistingDeepFields(t *testing.T) { expectedSupuChild := 1 var ok bool - f := NewEntityFormatter("", []string{"tupu.muku.supu", "tupu.muku.gutu.kugu"}, []string{}, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Whitelist: []string{"tupu.muku.supu", "tupu.muku.gutu.kugu"}}) res := f.Format(sample) var tupu map[string]interface{} var muku map[string]interface{} @@ -140,7 +144,7 @@ func TestEntityFormatter_newblacklistingFilter(t *testing.T) { }, IsComplete: true, } - f := NewEntityFormatter("", []string{}, []string{"supu", "a.b", "a.c", "foo.unknown"}, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Blacklist: []string{"supu", "a.b", "a.c", "foo.unknown"}}) result := f.Format(sample) if v, ok := result.Data["tupu"]; !ok || v != expected.Data["tupu"] { t.Errorf("The formatter returned an unexpected result for the field tupu: %v\n", result) @@ -184,7 +188,7 @@ func TestEntityFormatter_grouping(t *testing.T) { }, IsComplete: true, } - f := NewEntityFormatter("", []string{}, []string{}, preffix, map[string]string{}) + f := NewEntityFormatter(&config.Backend{Group: preffix}) result := f.Format(sample) if len(result.Data) != 1 || result.IsComplete != expected.IsComplete { t.Fail() @@ -226,7 +230,7 @@ func TestEntityFormatter_mapping(t *testing.T) { }, IsComplete: true, } - f := NewEntityFormatter("", []string{}, []string{}, "", mapping) + f := NewEntityFormatter(&config.Backend{Mapping: mapping}) result := f.Format(sample) if len(result.Data) != 4 || result.IsComplete != expected.IsComplete { @@ -272,7 +276,7 @@ func TestEntityFormatter_targeting(t *testing.T) { Data: sub, IsComplete: true, } - f := NewEntityFormatter(target, []string{}, []string{}, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Target: target}) result := f.Format(sample) if len(result.Data) != 3 || result.IsComplete != expected.IsComplete { t.Errorf("The formatter returned an unexpected result size: %v\n", result) @@ -294,7 +298,7 @@ func TestEntityFormatter_targetingUnknownFields(t *testing.T) { }, IsComplete: true, } - f := NewEntityFormatter(target, []string{}, []string{}, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Target: target}) result := f.Format(sample) if len(result.Data) != 0 || result.IsComplete != sample.IsComplete { t.Errorf("The formatter returned an unexpected result size: %v\n", result) @@ -312,7 +316,7 @@ func TestEntityFormatter_targetingNonObjects(t *testing.T) { }, IsComplete: true, } - f := NewEntityFormatter(target, []string{}, []string{}, "", map[string]string{}) + f := NewEntityFormatter(&config.Backend{Target: target}) result := f.Format(sample) if len(result.Data) != 0 || result.IsComplete != sample.IsComplete { t.Errorf("The formatter returned an unexpected result size: %v\n", result) @@ -341,7 +345,12 @@ func TestEntityFormatter_altogether(t *testing.T) { }, IsComplete: true, } - f := NewEntityFormatter("a", []string{"d"}, []string{}, "group", map[string]string{"d": "D"}) + f := NewEntityFormatter(&config.Backend{ + Target: "a", + Whitelist: []string{"d"}, + Group: "group", + Mapping: map[string]string{"d": "D"}, + }) result := f.Format(sample) v, ok := result.Data["group"] if !ok { diff --git a/proxy/http.go b/proxy/http.go index ff514e556..9ba7d96e8 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -38,7 +38,7 @@ func NewHTTPProxyWithHTTPExecutor(remote *config.Backend, requestExecutor HTTPRe return NewHTTPProxyDetailed(remote, requestExecutor, NoOpHTTPStatusHandler, NoOpHTTPResponseParser) } - ef := NewEntityFormatter(remote.Target, remote.Whitelist, remote.Blacklist, remote.Group, remote.Mapping) + ef := NewEntityFormatter(remote) rp := DefaultHTTPResponseParserFactory(HTTPResponseParserConfig{dec, ef}) return NewHTTPProxyDetailed(remote, requestExecutor, DefaultHTTPStatusHandler, rp) }