forked from topfreegames/khan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
80 lines (68 loc) · 1.96 KB
/
helpers.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
// khan
// https://github.com/topfreegames/khan
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2016 Top Free Games <backend@tfgco.com>
package api
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"unicode"
"unicode/utf8"
"github.com/kataras/iris"
"github.com/uber-go/zap"
)
// FailWith fails with the specified message
func FailWith(status int, message string, c *iris.Context) {
result, _ := json.Marshal(map[string]interface{}{
"success": false,
"reason": message,
})
c.SetStatusCode(status)
c.Write(string(result))
}
// SucceedWith sends payload to user with status 200
func SucceedWith(payload map[string]interface{}, c *iris.Context) {
payload["success"] = true
result, _ := json.Marshal(payload)
c.SetStatusCode(200)
c.Write(string(result))
}
// LoadJSONPayload loads the JSON payload to the given struct validating all fields are not null
func LoadJSONPayload(payloadStruct interface{}, c *iris.Context, l zap.Logger) error {
l.Debug("Loading payload...")
if err := c.ReadJSON(payloadStruct); err != nil {
if err != nil {
l.Error("Loading payload failed.", zap.Error(err))
return err
}
}
data := c.RequestCtx.Request.Body()
var jsonPayload map[string]interface{}
err := json.Unmarshal(data, &jsonPayload)
if err != nil {
l.Error("Loading payload failed.", zap.Error(err))
return err
}
var missingFieldErrors []string
v := reflect.ValueOf(payloadStruct).Elem()
t := v.Type()
for i := 0; i < v.NumField(); i++ {
r, n := utf8.DecodeRuneInString(t.Field(i).Name)
field := string(unicode.ToLower(r)) + t.Field(i).Name[n:]
if jsonPayload[field] == nil {
missingFieldErrors = append(missingFieldErrors, fmt.Sprintf("%s is required", field))
}
}
if len(missingFieldErrors) != 0 {
error := errors.New(strings.Join(missingFieldErrors[:], ", "))
l.Error("Loading payload failed.", zap.Error(err))
return error
}
l.Debug("Payload loaded successfully.")
return nil
}