Skip to content

Instantly share code, notes, and snippets.

@shimastripe
Created July 17, 2016 14:09
Show Gist options
  • Save shimastripe/b24b07ab0fb0da9798b23a0d29be13e7 to your computer and use it in GitHub Desktop.
Save shimastripe/b24b07ab0fb0da9798b23a0d29be13e7 to your computer and use it in GitHub Desktop.
package models
import (
"reflect"
"strings"
)
func contains(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}
func ParseFields(fields string) ([]string, map[string][]string) {
fieldsParse := strings.Split(fields, ",")
roop := make([]string, len(fieldsParse))
copy(roop, fieldsParse)
nestFields := make(map[string][]string)
offset := 0
for k, v := range roop {
l := strings.Split(v, ".")
ok := false
if len(l) > 1 {
_, ok = nestFields[l[0]]
nestFields[l[0]] = append(nestFields[l[0]], l[1])
}
if ok {
fieldsParse = append(fieldsParse[:(k-offset)], fieldsParse[(k+1-offset):]...)
offset += 1
} else {
fieldsParse[k-offset] = l[0]
}
}
return fieldsParse, nestFields
}
func FieldToMap(model User, fields []string, nestFields map[string][]string) map[string]interface{} {
u := make(map[string]interface{})
p := reflect.MakeMap(model)
return p
ts, vs := reflect.TypeOf(model), reflect.ValueOf(model)
if len(nestFields) == 0 {
for i := 0; i < ts.NumField(); i++ {
var jsonKey string
field := ts.Field(i)
jsonTag := field.Tag.Get("json")
if jsonTag == "" {
jsonKey = field.Name
} else {
jsonKey = strings.Split(jsonTag, ",")[0]
}
if fields[0] == "*" || contains(fields, jsonKey) {
u[jsonKey] = vs.Field(i).Interface()
}
}
} else {
for i := 0; i < ts.NumField(); i++ {
var jsonKey string
field := ts.Field(i)
jsonTag := field.Tag.Get("json")
if jsonTag == "" {
jsonKey = field.Name
} else {
jsonKey = strings.Split(jsonTag, ",")[0]
}
if fields[0] == "*" || contains(fields, jsonKey) {
_, ok := nestFields[jsonKey]
if ok {
f, n := ParseFields(strings.Join(nestFields[jsonKey], ","))
u[jsonKey] = FieldToMap(vs.Field(i).Interface(), f, n)
} else {
u[jsonKey] = vs.Field(i).Interface()
}
}
}
}
return u
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment