-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitting.go
62 lines (59 loc) · 1.6 KB
/
fitting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package snug
import (
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
)
// Decode response body to given struct pointer.
//
// Error returned is either for invalid json or missing a value for field marked as required.
// Using snug-tag requires to use also json-tag.
//
// Due to restrictions in encoding/json, use a pointer type for fields that can hold
// a falsy value. For example empty string or number of value 0 is not interpreted as missing.
// Using pointers will provide a nil value to return an error if field is tagged as required.
//
// func postgreet(w http.ResponseWriter, r *http.Request) {
// var body struct {
// Name *string `json:"name" snug:"required"`
// Age *int `json:"age" snug:"required"`
// }
// err := snug.Fit(r.Body, &body)
// if err != nil {
// // use error for details
// return
// }
// w.Write(200)
// }
func Fit(data io.ReadCloser, o any) error {
err := json.NewDecoder(data).Decode(o)
if err != nil {
return fmt.Errorf("invalid json: %w", err)
}
t := reflect.TypeOf(o).Elem()
v := reflect.ValueOf(o).Elem()
errMsgs := []string{}
for i := 0; i < t.NumField(); i++ {
tags, ok := t.Field(i).Tag.Lookup("snug")
if !ok {
continue
}
jtags, ok := t.Field(i).Tag.Lookup("json")
if !ok {
panic("missing tag: json")
}
fieldName := strings.Split(jtags, ",")[0]
tagList := strings.Split(tags, ",")
for _, tag := range tagList {
if tag == "required" && v.Field(i).IsZero() {
errMsgs = append(errMsgs, fmt.Sprintf("'%s' is a required field", fieldName))
}
}
}
if len(errMsgs) != 0 {
return fmt.Errorf(strings.Join(errMsgs, ", "))
}
return nil
}