forked from topfreegames/podium
/
payload.go
126 lines (102 loc) · 2.72 KB
/
payload.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
121
122
123
124
125
126
// podium
// https://github.com/topfreegames/podium
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2016 Top Free Games <backend@tfgco.com>
// Forked from
// https://github.com/dayvson/go-leaderboard
// Copyright © 2013 Maxwell Dayvson da Silva
//go:generate easyjson -all -no_std_marshalers $GOFILE
package api
import "fmt"
//Validatable indicates that a struct can be validated
type Validatable interface {
Validate() []string
}
//ValidatePayload for any validatable payload
func ValidatePayload(payload Validatable) []string {
return payload.Validate()
}
//NewValidation for validating structs
func NewValidation() *Validation {
return &Validation{
errors: []string{},
}
}
//Validation struct
type Validation struct {
errors []string
}
func (v *Validation) validateRequired(name string, value interface{}) {
if value == nil {
v.errors = append(v.errors, fmt.Sprintf("%s is required", name))
}
}
func (v *Validation) validateRequiredInt(name string, value int) {
if value == 0 {
v.errors = append(v.errors, fmt.Sprintf("%s is required", name))
}
}
func (v *Validation) validateCustom(name string, valFunc func() []string) {
errors := valFunc()
if len(errors) > 0 {
v.errors = append(v.errors, errors...)
}
}
//Errors in validation
func (v *Validation) Errors() []string {
return v.errors
}
type incrementScorePayload struct {
Increment int `json:"increment"`
}
func (s *incrementScorePayload) Validate() []string {
v := NewValidation()
v.validateRequiredInt("increment", s.Increment)
return v.Errors()
}
type setMembersScorePayload struct {
MembersScore []*memberScorePayload `json:"members"`
}
type memberScorePayload struct {
Score int64 `json:"score"`
PublicID string `json:"publicID"`
}
func (s *setMembersScorePayload) Validate() []string {
v := NewValidation()
v.validateCustom("members", func() []string {
if len(s.MembersScore) == 0 {
return []string{"members is required"}
}
for _, memberScore := range s.MembersScore {
if memberScore.PublicID == "" {
return []string{"publicID is required"}
}
}
return []string{}
})
return v.Errors()
}
type setScorePayload struct {
Score int64 `json:"score"`
}
func (s *setScorePayload) Validate() []string {
v := NewValidation()
//v.validateRequiredInt("score", s.Score)
return v.Errors()
}
type setScoresPayload struct {
Score int64 `json:"score"`
Leaderboards []string `json:"leaderboards"`
}
func (s *setScoresPayload) Validate() []string {
v := NewValidation()
//v.validateRequiredInt("score", s.Score)
v.validateCustom("leaderboards", func() []string {
if len(s.Leaderboards) == 0 {
return []string{"leaderboards is required"}
}
return []string{}
})
return v.Errors()
}