/
parse_body.go
81 lines (64 loc) · 1.76 KB
/
parse_body.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
package utils
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
"github.com/asaskevich/govalidator"
)
// ParseBodyparse json-formatted request body into given struct.
func ParseBodyArray(ctx context.Context, r *http.Request, data interface{}) error {
if r.Body == nil {
return errors.New("lib-go: error parsebody, body is empty!")
}
bBody, err := ioutil.ReadAll(r.Body)
if err != nil {
return errors.Wrapf(err, "lib-go: error read body data, %v", err)
}
err = json.Unmarshal([]byte(bBody), data)
if err != nil {
return errors.Wrapf(err, "lib-go: error unmarshal data, %v", err)
}
return nil
}
// ParseBody parse json-formatted request body into given struct.
func ParseBody(ctx context.Context, r *http.Request, data interface{}) error {
if r.Body == nil {
return errors.New("lib-go: error parsebody, body is empty!")
}
bBody, err := ioutil.ReadAll(r.Body)
if err != nil {
return errors.Wrapf(err, "lib-go: error read body data, %v", err)
}
err = json.Unmarshal(bBody, data)
if err != nil {
return errors.Wrapf(err, "lib-go: error unmarshal data, %v", err)
}
valid, err := govalidator.ValidateStruct(data)
if err != nil {
return errors.Wrapf(err, "lib-go: error govalidator struct, %v", err)
}
if !valid {
return errors.Errorf("lib-go: error validate struct, %t", valid)
}
return nil
}
// IsEmptyString is check parameter is empty
func IsEmptyString(data string) bool {
if data == "" {
return true
}
return false
}
// QueryURL is wrap data for query string
type QueryURL map[string]interface{}
// URLQuery is generate data to URL query string
func URLQuery(q QueryURL) string {
tempQuery := url.Values{}
for i, j := range q {
tempQuery.Add(i, j.(string))
}
return tempQuery.Encode()
}