From 4344772a3c66d7b4c8855fc8cd6dc5a46f0f1ca3 Mon Sep 17 00:00:00 2001 From: adrianwit Date: Thu, 27 Dec 2018 09:32:02 -0800 Subject: [PATCH] added Added DecoderFactory method to url.Resource --- CHANGELOG.md | 6 +++++- url/resource.go | 12 ++++++++++++ url/resource_test.go | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c91a2f6..0bfae56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -## Dec 24 2018 - v0.5.2 +## Dec 27 2018 - v0.5.3 + - Added DecoderFactory method to url.Resource + - Patched secret location with URL scheme + +## Dec 26 2018 - v0.5.2 - Patched KV nested slice conversion - Patched handling unexported fields - Minor patches diff --git a/url/resource.go b/url/resource.go index f9fbd0e..fe675db 100644 --- a/url/resource.go +++ b/url/resource.go @@ -159,6 +159,17 @@ func (r *Resource) Decode(target interface{}) (err error) { return err } +//DecoderFactory returns new decoder factory for resource +func (r *Resource) DecoderFactory() toolbox.DecoderFactory { + ext := path.Ext(r.ParsedURL.Path) + switch ext { + case ".yaml", ".yml": + return toolbox.NewYamlDecoderFactory() + default: + return toolbox.NewJSONDecoderFactory() + } +} + //Decode decodes url's data into target, it takes decoderFactory which decodes data into target func (r *Resource) DecodeWith(target interface{}, decoderFactory toolbox.DecoderFactory) error { if r == nil { @@ -203,6 +214,7 @@ func (r *Resource) YAMLDecode(target interface{}) error { if err := r.DecodeWith(&mapSlice, toolbox.NewYamlDecoderFactory()); err != nil { return err } + if !toolbox.IsMap(target) { converter := toolbox.NewColumnConverter(toolbox.DefaultDateLayout) return converter.AssignConverted(target, mapSlice) diff --git a/url/resource_test.go b/url/resource_test.go index 0e05547..94833d0 100644 --- a/url/resource_test.go +++ b/url/resource_test.go @@ -126,3 +126,16 @@ func TestResource_JsonDecode(t *testing.T) { assert.EqualValues(t, resourceData["b"], "123") } + +func TestResource_DecoderFactory(t *testing.T) { + { + resource := url.NewResource("abc.yaml") + factory := resource.DecoderFactory() + assert.NotNil(t, factory) + } + { + resource := url.NewResource("abc.json") + factory := resource.DecoderFactory() + assert.NotNil(t, factory) + } +}