Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
Merge a6f3a5b into af517d5
Browse files Browse the repository at this point in the history
  • Loading branch information
iahmedov committed Nov 27, 2017
2 parents af517d5 + a6f3a5b commit a84b8a9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
15 changes: 9 additions & 6 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package staert
import (
"encoding"
"encoding/base64"
"encoding/csv"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -106,8 +107,6 @@ func processKV(key string, v []byte, raw map[string]interface{}) (map[string]int
}

func decodeHook(fromType reflect.Type, toType reflect.Type, data interface{}) (interface{}, error) {
// TODO : Array support

// custom unmarshaler
textUnmarshalerType := reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
if toType.Implements(textUnmarshalerType) {
Expand Down Expand Up @@ -155,11 +154,15 @@ func decodeHook(fromType reflect.Type, toType reflect.Type, data interface{}) (i

return dataOutput, nil
} else if fromType.Kind() == reflect.String {
b, err := base64.StdEncoding.DecodeString(data.(string))
if err != nil {
return nil, err
str := data.(string)
b, err := base64.StdEncoding.DecodeString(str)
if err == nil {
return b, nil
}
return b, nil

reader := csv.NewReader(strings.NewReader(str))
reader.TrimLeadingSpace = true
return reader.Read()
}
}
return data, nil
Expand Down
58 changes: 58 additions & 0 deletions kv_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package staert

import (
"encoding/base64"
"encoding/json"
"reflect"
"strings"
Expand Down Expand Up @@ -1094,6 +1095,63 @@ func TestConvertPairs5Levels(t *testing.T) {
}
}

type StrSlices struct {
Bytes []byte
Ints []int
Bools []bool
Strings []string
StringsWithCommaAndQuotation []string
}

func TestSliceTypes(t *testing.T) {
config := StrSlices{}

kv := &KvSource{
&Mock{
KVPairs: []*store.KVPair{
{Key: "test/bytes", Value: []byte(base64.StdEncoding.EncodeToString([]byte("hello")))},
{Key: "test/ints", Value: []byte("1,2,3")},
{Key: "test/bools", Value: []byte("true,false,true")},
{Key: "test/strings", Value: []byte("hi,1,world")},
{Key: "test/stringswithcommaandquotation", Value: []byte("first, \"second\", \"with, comma\"")},
},
},
"test",
}

cmd := &flaeg.Command{
Name: "test",
Description: "description test",
Config: &config,
DefaultPointersConfig: &config,
Run: func() error { return nil },
}

if _, err := kv.Parse(cmd); err != nil {
t.Fatalf("Error %s", err)
}

expected := &StrSlices{
Bytes: []byte("hello"),
Ints: []int{1, 2, 3},
Bools: []bool{true, false, true},
Strings: []string{"hi", "1", "world"},
StringsWithCommaAndQuotation: []string{"first", "second", "with, comma"},
}

if !reflect.DeepEqual(expected, cmd.Config) {
actualJSON, err := json.Marshal(cmd.Config)
if err != nil {
t.Fatalf("Error: %v", err)
}
expectedJSON, err := json.Marshal(expected)
if err != nil {
t.Fatalf("Error: %v", err)
}
t.Fatalf("\nexpected\t: %s\ngot\t\t\t: %s\n", expectedJSON, actualJSON)
}
}

func TestCollateKvPairsBase64(t *testing.T) {
config := &struct {
Base64Bytes []byte
Expand Down

0 comments on commit a84b8a9

Please sign in to comment.