func ExampleItems() { s := MyStruct{ FirstField: "first value", SecondField: 2, ThirdField: "third value", } var structItems map[string]interface{} // Items will return a field name to // field value map structItems, _ = reflections.Items(s) fmt.Println(structItems) }
func convertInterfaceMap(p interface{}, except []string) map[string]string { nint := map[string]string{} var structItems map[string]interface{} structItems, _ = reflections.Items(p) for v, v2 := range structItems { if isZeroOfUnderlyingType(v2) || isInList(v, except) { continue } v = strings.ToLower(strings.Join(camelcase.Split(v), "_")) switch val := v2.(type) { case interface{}: sv, _ := json.Marshal(val) nint[v] = string(sv) default: nint[v] = fmt.Sprintf("%+v", v2) } } return nint }
func FullApiWithHandlers(apiSpec interface{}) map[ServerResource]Handler { handlers := make(map[ServerResource]Handler) items, err := reflections.Items(apiSpec) if err != nil { panic(err) } for _, v := range items { if target, ok := v.(*ResourceSpec); ok { verbs, handler := target.ServeAll() if handler == nil { panic(fmt.Sprintf("no handler for endpoint %v ", target)) } for _, verb := range verbs { handlers[verb] = handler } } } return handlers }