-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
162 lines (138 loc) · 2.88 KB
/
common.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
package domain
import (
"database/sql/driver"
"encoding/json"
"github.com/gofrs/uuid"
"github.com/shopspring/decimal"
"github.com/michaljemala/payments-sample/pkg/internal/errors"
)
type ID uuid.UUID
func MustIDFrom(s string) ID {
id, err := IDFrom(s)
if err != nil {
panic(err)
}
return id
}
func IDFrom(s string) (ID, error) {
id, err := uuid.FromString(s)
if err != nil {
return ID{}, errors.Generic(
errors.ErrCodeGenericInvalidArgument,
"invalid format of ID",
err.Error(),
)
}
return ID(id), nil
}
func (id ID) String() string {
return uuid.UUID(id).String()
}
func (id ID) IsNil() bool {
return uuid.UUID(id) == uuid.Nil
}
func (id ID) MarshalJSON() ([]byte, error) {
return json.Marshal(uuid.UUID(id))
}
func (id *ID) UnmarshalJSON(data []byte) error {
var uuid uuid.UUID
err := json.Unmarshal(data, &uuid)
if err != nil {
return errors.Generic(
errors.ErrCodeGenericInvalidArgument,
"invalid format of ID",
err.Error(),
)
}
*id = ID(uuid)
return nil
}
func (id ID) Value() (driver.Value, error) {
return uuid.UUID(id).Value()
}
func (id *ID) Scan(src interface{}) error {
uid := new(uuid.UUID)
err := uid.Scan(src)
if err != nil {
return err
}
*id = ID(*uid)
return nil
}
type Decimal decimal.Decimal
func MustDecimalFrom(s string) Decimal {
d, err := DecimalFrom(s)
if err != nil {
panic(err)
}
return d
}
func DecimalFrom(s string) (Decimal, error) {
d, err := decimal.NewFromString(s)
if err != nil {
return Decimal{}, errors.Generic(
errors.ErrCodeGenericInvalidArgument,
"invalid format of decimal",
err.Error(),
)
}
return Decimal(d), nil
}
func (d Decimal) String() string {
return decimal.Decimal(d).String()
}
func (d Decimal) MarshalJSON() ([]byte, error) {
return json.Marshal(decimal.Decimal(d))
}
func (d *Decimal) UnmarshalJSON(data []byte) error {
var dec decimal.Decimal
err := json.Unmarshal(data, &dec)
if err != nil {
return errors.Generic(
errors.ErrCodeGenericInvalidArgument,
"invalid format of Decimal",
err.Error(),
)
}
*d = Decimal(dec)
return nil
}
func (d Decimal) Value() (driver.Value, error) {
return decimal.Decimal(d).Value()
}
func (d *Decimal) Scan(src interface{}) error {
dec := new(decimal.Decimal)
err := dec.Scan(src)
if err != nil {
return err
}
*d = Decimal(*dec)
return nil
}
type Address struct {
Line1 string `json:"line1"`
Line2 *string `json:"line2,omitempty"`
City string `json:"city"`
Region *string `json:"region,omitempty"`
PostalCode string `json:"postal_code"`
CountryCode string `json:"country_code"`
}
type Object interface {
GetID() string
SetID(s string) error
}
type BaseObject struct {
ID ID `json:"-"`
}
func (o BaseObject) GetID() string {
return o.ID.String()
}
func (o *BaseObject) SetID(s string) error {
id, err := IDFrom(s)
if err != nil {
return err
}
o.ID = id
return nil
}
type EnumName string