Skip to content

Commit

Permalink
inline pointer_to_struce mode: update comments. return error on point…
Browse files Browse the repository at this point in the history
…er not to struct
  • Loading branch information
Larry Cinnabar committed Apr 29, 2018
1 parent 3fb5f17 commit 46c4b47
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,8 @@ func getStructInfo(st reflect.Type) (*structInfo, error) {
inlineMap = info.Num
case reflect.Ptr:
// allow only pointer to struct
if field.Type.Elem().Kind() != reflect.Struct {
break
if kind := field.Type.Elem().Kind(); kind != reflect.Struct {
return nil, errors.New("Option ,inline allows a pointer only to a struct, was given pointer to " + kind.String())
}

field.Type = field.Type.Elem()
Expand All @@ -773,7 +773,7 @@ func getStructInfo(st reflect.Type) (*structInfo, error) {
fieldsList = append(fieldsList, finfo)
}
default:
panic("Option ,inline needs a struct value or map field")
panic("Option ,inline needs a struct value or a pointer to a struct or map field")
}
continue
}
Expand Down
6 changes: 6 additions & 0 deletions bson/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ func (e *encoder) addStruct(v reflect.Value) {
if info.Inline == nil {
value = v.Field(info.Num)
} else {
// as pointers to struct are allowed here,
// there is no guarantee that pointer won't be nil.
//
// It is expected allowed behaviour
// so info.Inline MAY consist index to a nil pointer
// and that is why we safely call v.FieldByIndex and just continue on panic
field, errField := safeFieldByIndex(v, info.Inline)
if errField != nil {
continue
Expand Down

0 comments on commit 46c4b47

Please sign in to comment.