-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal.go
120 lines (95 loc) · 2.65 KB
/
marshal.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package formdata
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"path/filepath"
"reflect"
"strconv"
"strings"
)
const formTag = "form"
// File represents a file to be marshaled into a multipart/form-data request body.
type File interface {
Name() string
io.Reader
}
// Marshal encodes the given value into a multipart/form-data request body.
// The value must be a struct or a pointer to a struct.
// If the field implements the File interface, the field is marshaled as a file.
func Marshal(value any) (data []byte, contentType string, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("formdata: %v", r)
}
}()
v := reflect.ValueOf(value)
t := reflect.TypeOf(value)
if t.Kind() == reflect.Ptr {
v = v.Elem()
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return nil, "", errors.New("formdata: value must be a struct or a pointer to a struct")
}
buf := new(bytes.Buffer)
writer := multipart.NewWriter(buf)
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
tag := t.Field(i).Tag.Get(formTag)
if strings.Contains(tag, "omitempty") && field.IsZero() {
continue
}
tag = strings.Split(tag, ",")[0]
if tag == "-" {
continue
}
if tag == "" {
tag = strings.ToLower(t.Field(i).Name)
}
if field.Type().Implements(reflect.TypeOf((*File)(nil)).Elem()) {
if field.IsNil() {
continue
}
file := field.Interface().(File)
filename := filepath.Base(file.Name())
formFile, err := writer.CreateFormFile(tag, filename)
if err != nil {
return nil, "", err
}
if _, err = io.Copy(formFile, file); err != nil {
return nil, "", err
}
continue
}
if field.Kind() == reflect.Interface || field.Kind() == reflect.Ptr {
field = field.Elem()
}
var fieldValue string
switch field.Kind() {
case reflect.String:
fieldValue = field.String()
case reflect.Bool:
fieldValue = strconv.FormatBool(field.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue = strconv.FormatInt(field.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fieldValue = strconv.FormatUint(field.Uint(), 10)
case reflect.Float32:
fieldValue = strconv.FormatFloat(field.Float(), 'f', -1, 32)
case reflect.Float64:
fieldValue = strconv.FormatFloat(field.Float(), 'f', -1, 64)
default:
return nil, "", fmt.Errorf("formdata: unsupported type: %s", field.Kind())
}
if err = writer.WriteField(tag, fieldValue); err != nil {
return nil, "", err
}
}
if err = writer.Close(); err != nil {
return nil, "", err
}
return buf.Bytes(), writer.FormDataContentType(), nil
}