Refactor call fill

This commit is contained in:
Daniel Ponte 2024-07-24 22:49:42 -04:00
parent d94ae3b701
commit b3e7d107a8

View file

@ -21,9 +21,9 @@ type callUploadRequest struct {
Frequencies []int `form:"frequencies"` Frequencies []int `form:"frequencies"`
Frequency int `form:"frequency"` Frequency int `form:"frequency"`
Key string `form:"key"` Key string `form:"key"`
Patches []string `form:"patches"` Patches []int `form:"patches"`
Source int `form:"source"` Source int `form:"source"`
Sources []string `form:"sources"` Sources []int `form:"sources"`
System int `form:"system"` System int `form:"system"`
SystemLabel string `form:"systemLabel"` SystemLabel string `form:"systemLabel"`
Talkgroup int `form:"talkgroup"` Talkgroup int `form:"talkgroup"`
@ -76,10 +76,11 @@ func (car *callUploadRequest) fill(r *http.Request) error {
for i := 0; i < rv.NumField(); i++ { for i := 0; i < rv.NumField(); i++ {
f := rv.Field(i) f := rv.Field(i)
ff := rt.Field(i).Tag.Get("form") fi := f.Interface()
switch ff { formField := rt.Field(i).Tag.Get("form")
case "audio": switch v := fi.(type) {
file, _, err := r.FormFile(ff) case []byte:
file, _, err := r.FormFile(formField)
if err != nil { if err != nil {
return fmt.Errorf("get form file: %w", err) return fmt.Errorf("get form file: %w", err)
} }
@ -90,14 +91,14 @@ func (car *callUploadRequest) fill(r *http.Request) error {
} }
f.SetBytes(audioBytes) f.SetBytes(audioBytes)
case "dateTime": case time.Time:
t, err := time.Parse(time.RFC3339, r.Form.Get(ff)) t, err := time.Parse(time.RFC3339, r.Form.Get(formField))
if err != nil { if err != nil {
return fmt.Errorf("parse time: %w", err) return fmt.Errorf("parse time: %w", err)
} }
f.Set(reflect.ValueOf(t)) f.Set(reflect.ValueOf(t))
case "frequencies", "patches", "sources": case []int:
val := strings.Trim(r.Form.Get(ff), "[]") val := strings.Trim(r.Form.Get(formField), "[]")
if val == "" { if val == "" {
continue continue
} }
@ -110,14 +111,16 @@ func (car *callUploadRequest) fill(r *http.Request) error {
} }
} }
f.Set(reflect.ValueOf(ar)) f.Set(reflect.ValueOf(ar))
case "frequency", "talkgroup", "system", "source": case int:
val, err := strconv.Atoi(r.Form.Get(ff)) val, err := strconv.Atoi(r.Form.Get(formField))
if err != nil { if err != nil {
return fmt.Errorf("atoi('%s'): %w", ff, err) return fmt.Errorf("atoi('%s'): %w", formField, err)
} }
f.SetInt(int64(val)) f.SetInt(int64(val))
case string:
f.SetString(r.Form.Get(formField))
default: default:
f.SetString(r.Form.Get(ff)) panic(fmt.Errorf("unsupported type %T", v))
} }
} }