Skip to content

Commit

Permalink
added Added DecoderFactory method to url.Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Dec 27, 2018
1 parent ef78595 commit 4344772
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions url/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions url/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 4344772

Please sign in to comment.