func decodeParams(myroute *Route, rw http.ResponseWriter, r *http.Request) interface{} { if myroute.Params == nil { return nil } // get form parameters err := r.ParseForm() if err != nil { fmt.Println("ParseForm err: ", err) return nil } // get mux parameters muxvars := mux.Vars(r) v := reflect.New(myroute.Params) params := v.Interface() // set all defaults sanitizer.DefaultStruct(params) st := reflect.TypeOf(params).Elem() vt := reflect.ValueOf(params).Elem() for i := 0; i < st.NumField(); i++ { field := st.Field(i) value := vt.Field(i) paramval := "" if val, ok := muxvars[strings.ToLower(field.Name)]; ok { paramval = val } else { paramval = r.FormValue(strings.ToLower(field.Name)) } switch field.Type.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: def, _ := strconv.ParseInt(paramval, 10, 64) value.SetInt(def) case reflect.Float32, reflect.Float64: def, _ := strconv.ParseFloat(paramval, 64) value.SetFloat(def) case reflect.Bool: def, _ := strconv.ParseBool(paramval) value.SetBool(def) case reflect.String: value.SetString(paramval) default: log.Println("decodeParams on type", field.Type.Name(), "not implemented") } } return params }
func decodeContent(myroute *Route, rw http.ResponseWriter, r *http.Request, s *session.Session) interface{} { if myroute.Json == nil { return nil } // decode json from request //fmt.Println("Json : ", myroute.Json) v := reflect.New(myroute.Json) o := v.Interface() // set to default the new structure sanitizer.DefaultStruct(o) // Check if multipart mt, params, err := mime.ParseMediaType(r.Header.Get("Content-Type")) if err != nil { log.Println("error parsing header:", err) } if strings.HasPrefix(mt, "multipart/") { log.Println(" = MULTIPART POST = ") mr := multipart.NewReader(r.Body, params["boundary"]) // For each part for { p, err := mr.NextPart() if err == io.EOF { // EOF we can stop here return o } if err != nil { log.Println("error getting part of multipart form", err) return nil } j, err := ioutil.ReadAll(p) if err != nil { log.Println("error: unable to get content of the file") } //log.Println(" => part ... ", string(j)) // Is it a file ? if p.FileName() != "" { // Check if target interface has a FileName field ta := reflect.Indirect(reflect.ValueOf(o)).FieldByName("File") if !ta.IsValid() { log.Println("error: target interface does not have File field") return nil } // Check if target interface "File" field is type of routes.File if ta.Type() != reflect.TypeOf(&File{}) { log.Println("error: target interface field \"File\" is not of type routes.File") return nil } // Assign file to structure fs := reflect.New(reflect.TypeOf(File{})) fsi := fs.Interface() ee := reflect.Indirect(reflect.ValueOf(fsi)) ee.FieldByName("Name").SetString(p.FileName()) //ee.FieldByName("Content").SetString(string(j[:])) ee.FieldByName("Content").SetBytes(j) ta.Set(fs) } else { // Unmarshall datas into structure json.Unmarshal(j, o) } } } else { //log.Println(" = normal POST = ") decoder := json.NewDecoder(r.Body) //fmt.Printf("t : %t\n", o) //fmt.Println("o : ", o) err := decoder.Decode(o) if err != nil { log.Panicln("decode failed", err) } return o } }