-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
schema.go
162 lines (127 loc) · 4.95 KB
/
schema.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
Copyright (C) 2020 Aaron Sky.
This file is part of asc-go, a package for working with Apple's
App Store Connect API.
asc-go is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
asc-go is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with asc-go. If not, see <http://www.gnu.org/licenses/>.
*/
package asc
import (
"encoding/json"
"fmt"
"regexp"
"time"
)
const (
dateFormat = "2006-01-02"
customISO8601Format = "2006-01-02T15:04:05.999-0700"
emailRegexString = "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22))))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
)
var (
emailRegex = regexp.MustCompile(emailRegexString)
)
// ErrInvalidEmail occurs when the value does not conform to the library author's understanding of
// what constitutes a valid email address, and cannot be marshaled or unmarshaled into JSON.
type ErrInvalidEmail struct {
Value string
}
func (e ErrInvalidEmail) Error() string {
return fmt.Sprintf("email: %s failed to pass regex validation", e.Value)
}
// Date represents a date with no time component.
type Date struct {
time.Time
}
// MarshalJSON is a custom marshaller for time-less dates.
func (d Date) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Time.Format(dateFormat))
}
// UnmarshalJSON is a custom unmarshaller for time-less dates.
func (d *Date) UnmarshalJSON(data []byte) error {
var dateStr string
err := json.Unmarshal(data, &dateStr)
if err != nil {
return err
}
parsed, err := time.Parse(dateFormat, dateStr)
if err != nil {
return err
}
d.Time = parsed
return nil
}
// DateTime represents a date with an ISO8601-like date-time.
type DateTime struct {
time.Time
}
// MarshalJSON is a custom marshaller for date-times.
func (d DateTime) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Time.Format(customISO8601Format))
}
// UnmarshalJSON is a custom unmarshaller for date-times.
func (d *DateTime) UnmarshalJSON(data []byte) error {
var dateTimeStr string
err := json.Unmarshal(data, &dateTimeStr)
if err != nil {
return err
}
parsed, err := time.Parse(time.RFC3339, dateTimeStr)
if err != nil {
parsed, err = time.Parse(customISO8601Format, dateTimeStr)
if err != nil {
return err
}
}
d.Time = parsed
return nil
}
// Email is a validated email address string.
type Email string
// MarshalJSON is a custom marshaler for email addresses.
func (e Email) MarshalJSON() ([]byte, error) {
emailString := string(e)
if !emailRegex.MatchString(emailString) {
return nil, ErrInvalidEmail{Value: emailString}
}
return json.Marshal(string(e))
}
// UnmarshalJSON is a custom unmarshaller for email addresses.
func (e *Email) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
if !emailRegex.MatchString(s) {
return ErrInvalidEmail{Value: s}
}
*e = Email(s)
return nil
}
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool {
return &v
}
// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int {
return &v
}
// Float is a helper routine that allocates a new float64 value
// to store v and returns a pointer to it.
func Float(v float64) *float64 {
return &v
}
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string {
return &v
}