blob: f7e10d27ab7a0568574f91740a9fab2c4bf5aa63 [file] [log] [blame]
package internal
import (
"reflect"
)
// RemainingKeys will inspect a struct and compare it to a map. Any key that
// is not defined in a JSON tag of the struct will be added to the extras map
// and returned.
//
// This is useful for determining the extra fields returned in response bodies
// for resources that can contain an arbitrary or dynamic number of fields.
func RemainingKeys(s interface{}, m map[string]interface{}) (extras map[string]interface{}) {
extras = make(map[string]interface{})
valueOf := reflect.ValueOf(s)
typeOf := reflect.TypeOf(s)
for i := 0; i < valueOf.NumField(); i++ {
field := typeOf.Field(i)
tagValue := field.Tag.Get("json")
if _, ok := m[tagValue]; !ok {
extras[tagValue] = m[tagValue]
}
}
return
}