Skip to content

Instantly share code, notes, and snippets.

@narqo
Last active April 11, 2017 18:02
Show Gist options
  • Save narqo/7270b3783a29999774b74db82abb78d8 to your computer and use it in GitHub Desktop.
Save narqo/7270b3783a29999774b74db82abb78d8 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/mailru/easyjson"
)
type FlatJSONResponse struct {
Tmpl string
Vars easyjson.RawMessage
}
func (r *FlatJSONResponse) MarshalJSON() ([]byte, error) {
// TODO:
}
func writePrefixed(sw *sotaWriter, prefix string, v interface{}) {
original := reflect.ValueOf(v)
kind := original.Kind()
if kind == reflect.Ptr || kind == reflect.Interface {
original = reflect.Indirect(original)
kind = original.Kind()
}
t := original.Type()
switch kind {
case reflect.Struct:
for i := 0; i < original.NumField(); i += 1 {
childValue := original.Field(i)
childKey := t.Field(i).Name
writePrefixed(sw, prefix + "_" + childKey, childValue.Interface())
}
case reflect.Slice:
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
writePrefixed(sw, prefix + "_" + strconv.Itoa(i), s.Index(i).Interface())
}
default:
sw.w.RawByte(',')
sw.writeLeaf(prefix, v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment