Skip to content

Commit

Permalink
Merge 6d111ad into 4719832
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Jun 2, 2018
2 parents 4719832 + 6d111ad commit 5681adc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
24 changes: 14 additions & 10 deletions 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 {
Expand All @@ -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,
}
Expand Down
12 changes: 7 additions & 5 deletions proxy/formatter_benchmark_test.go
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"testing"

"github.com/devopsfaith/krakend/config"
)

func BenchmarkEntityFormatter_whitelistingFilter(b *testing.B) {
Expand All @@ -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++ {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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++ {
Expand All @@ -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++ {
Expand Down Expand Up @@ -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++ {
Expand Down
29 changes: 19 additions & 10 deletions 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}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion proxy/http.go
Expand Up @@ -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)
}
Expand Down

0 comments on commit 5681adc

Please sign in to comment.