Commit 783f8cd4 authored by zauberstuhl's avatar zauberstuhl

Extend structs helper with removeSkippedJsonFields

parent a4b93feb
...@@ -32,15 +32,31 @@ func SelectStructFields(n interface{}, fieldParam string) interface{} { ...@@ -32,15 +32,31 @@ func SelectStructFields(n interface{}, fieldParam string) interface{} {
m := make(map[int]interface{}, val.Len()) m := make(map[int]interface{}, val.Len())
for i := 0; i < val.Len(); i++ { for i := 0; i < val.Len(); i++ {
model := val.Index(i).Interface() model := val.Index(i).Interface()
m[i] = reduceStructField(structs.Map(model), fieldParam) mapModel := removeSkippedJsonFields(model)
m[i] = reduceStructField(mapModel, fieldParam)
} }
return m return m
} else if kind == reflect.Struct { } else if kind == reflect.Struct {
return reduceStructField(structs.Map(n), fieldParam) mapModel := removeSkippedJsonFields(n)
return reduceStructField(mapModel, fieldParam)
} }
panic("Cannot type cast to slice or struct") panic("Cannot type cast to slice or struct")
} }
// We should exclude json:"-" tags (structs library ignores them)
func removeSkippedJsonFields(n interface {}) map[string]interface{} {
typeOf := reflect.TypeOf(n)
mapModel := structs.Map(n)
for i := 0; i < typeOf.NumField(); i++ {
field := typeOf.Field(i)
tag := field.Tag.Get("json")
if tag == "-" {
delete(mapModel, field.Name)
}
}
return mapModel
}
// Iterate through the structure and remove unnecessary fields // Iterate through the structure and remove unnecessary fields
func reduceStructField(m map[string]interface{}, fieldParam string) map[string]interface{} { func reduceStructField(m map[string]interface{}, fieldParam string) map[string]interface{} {
f, s := parseFieldParam(fieldParam) f, s := parseFieldParam(fieldParam)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment