package api import ( "dancefloor/patterns" "github.com/gin-gonic/gin" "net/http" "reflect" ) var all = map[string]func() patterns.Pattern{ "SolidColour": func() patterns.Pattern { return &patterns.SolidColour{} }, "SuperRgb": func() patterns.Pattern { return &patterns.SuperRgb{} }, "Character": func() patterns.Pattern { return &patterns.Character{} }, "ScrollingText": func() patterns.Pattern { return &patterns.ScrollingText{} }, } func indexPatterns(ctx *gin.Context) { var out []map[string]string for _, generator := range all { fieldsMap := make(map[string]string) pattern := generator() patternType := reflect.TypeOf(pattern) if patternType.Kind() == reflect.Ptr { patternType = reflect.Indirect(reflect.ValueOf(pattern)).Type() } for i := 0; i < patternType.NumField(); i++ { field := patternType.Field(i) if field.PkgPath == "" { // if field is exported, this is empty fieldsMap[field.Name] = field.Type.Name() } } out = append(out, fieldsMap) } ctx.JSON(200, out) } func putPattern(ctx *gin.Context) { query := ctx.Param("name") generator, ok := all[query] if !ok { ctx.AbortWithStatus(http.StatusNotFound) return } pattern := generator() err := ctx.BindJSON(pattern) if err != nil { ctx.AbortWithStatus(http.StatusBadRequest) return } setter(pattern) ctx.Status(http.StatusNoContent) }