Skip to content

Commit

Permalink
better decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Feb 25, 2021
1 parent 3e15487 commit 8bae922
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
2 changes: 1 addition & 1 deletion aconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (l *Loader) loadFromFile() error {
continue
}

if err := l.setFieldData(field, fmt.Sprint(value)); err != nil {
if err := l.setFieldData2(field, value); err != nil {
return err
}
delete(actualFields, name)
Expand Down
27 changes: 26 additions & 1 deletion aconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ func TestJSON(t *testing.T) {
StructM: StructM{
M: "n",
},
MM: map[string][]string{
"first": {"val"},
"second": {"ue"},
},
MS: []map[string]string{
{"first": "val"},
{"second": "ue"},
},
}
if got := cfg; !reflect.DeepEqual(want, got) {
t.Fatalf("want %v, got %v", want, got)
Expand Down Expand Up @@ -973,6 +981,9 @@ type structConfig struct {

AA structA `json:"A"`
StructM

MM map[string][]string
MS []map[string]string
}

type structY struct {
Expand Down Expand Up @@ -1031,6 +1042,20 @@ const testfileContent = `{
}
},
"m": "n"
"m": "n",
"mm": {
"first": ["val"],
"second": ["ue"]
},
"ms": [
{
"first": "val"
},
{
"second": "ue"
}
]
}
`
60 changes: 60 additions & 0 deletions reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,66 @@ func (l *Loader) getFieldsHelper(valueObject reflect.Value, parent *fieldData) [
return fields
}

func (l *Loader) setFieldData2(field *fieldData, value interface{}) error {
// unwrap pointers
for field.value.Type().Kind() == reflect.Ptr {
if field.value.IsNil() {
field.value.Set(reflect.New(field.value.Type().Elem()))
}
field.value = field.value.Elem()
}

if value == nil {
return nil
}

switch kind := field.value.Type().Kind(); kind {
case reflect.Bool:
val, ok := value.(bool)
if !ok {
return fmt.Errorf("cannot cast %T to type bool", value)
}
if val {
field.value.SetInt(1)
}

case reflect.String:
val, ok := value.(string)
if !ok {
return fmt.Errorf("cannot cast %T to type string", value)
}
field.value.SetString(val)

case reflect.Int:
val, ok := value.(int64) // TODO: other ints
if !ok {
return fmt.Errorf("cannot cast %T to type int", value)
}
field.value.SetInt(val)

case reflect.Map:
val, ok := value.(map[string]interface{}) // TODO: other map?
if !ok {
return fmt.Errorf("cannot cast %T to type map[string]interface{}", value)
}

fmt.Printf("%v\n\n", field.field.Type.Elem())
mapField := reflect.MakeMap(field.field.Type)
for k, v := range val {
vv := reflect.ValueOf(v)
if field.field.Type.Elem().Kind() == reflect.Struct {
fdd := l.newSimpleFieldData(reflect.New(field.field.Type.Elem()).Elem())
l.setFieldData2(fdd, v)
vv = fdd.value
}
_ = vv
mapField.SetMapIndex(reflect.ValueOf(k), vv)
}
field.value.Set(mapField)
}
return nil
}

func (l *Loader) setFieldData(field *fieldData, value string) error {
// unwrap pointers
for field.value.Type().Kind() == reflect.Ptr {
Expand Down
8 changes: 4 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ func (d *jsonDecoder) DecodeFile(filename string) (map[string]interface{}, error
return nil, err
}

res := map[string]interface{}{}
for key, value := range raw {
flatten("", key, value, res)
}
res := raw //map[string]interface{}{}
// for key, value := range raw {
// flatten("", key, value, res)
// }
return res, nil
}

Expand Down

0 comments on commit 8bae922

Please sign in to comment.