Skip to content
This repository has been archived by the owner on Mar 13, 2019. It is now read-only.

Commit

Permalink
Recursively get underlying value for Interfaces and Pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Dec 15, 2016
1 parent c9c836c commit f9a743d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 7 additions & 3 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ func (ctx *Context) DecodeWithOptions(data []byte, obj interface{}, options stri
}

value := reflect.ValueOf(obj)
switch value.Kind() {
case reflect.Ptr, reflect.Interface:
value = value.Elem()
valueFor: for {
switch value.Kind() {
case reflect.Ptr, reflect.Interface:
value = value.Elem()
default:
break valueFor
}
}

if !value.CanSet() {
Expand Down
14 changes: 11 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ func (ctx *Context) EncodeWithOptions(obj interface{}, options string) (data []b
func (ctx *Context) encode(value reflect.Value, opts *fieldOptions) (*rawValue, error) {

// Skip the interface type
switch value.Kind() {
case reflect.Interface:
value = value.Elem()
valueFor: for {
switch value.Kind() {
case reflect.Ptr, reflect.Interface:
value = value.Elem()
default:
break valueFor
}
}

// If a value is missing the default value is used
Expand Down Expand Up @@ -95,6 +99,10 @@ func (ctx *Context) encodeValue(value reflect.Value, opts *fieldOptions) (raw *r
}

if encoder == nil {
// Use the actual type instead of pointer:
switch value.Kind() {

}
// Generic types:
switch value.Kind() {
case reflect.Bool:
Expand Down

0 comments on commit f9a743d

Please sign in to comment.