Skip to content

Commit abffbe1

Browse files
committed
chore: allow fields to be exported for swagger
1 parent f2e7208 commit abffbe1

File tree

2 files changed

+78
-77
lines changed

2 files changed

+78
-77
lines changed

money.go

+35-34
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111

1212
// Injection points for backward compatibility.
1313
// If you need to keep your JSON marshal/unmarshal way, overwrite them like below.
14-
// money.UnmarshalJSON = func (m *Money, b []byte) error { ... }
15-
// money.MarshalJSON = func (m Money) ([]byte, error) { ... }
14+
//
15+
// money.UnmarshalJSON = func (m *Money, b []byte) error { ... }
16+
// money.MarshalJSON = func (m Money) ([]byte, error) { ... }
1617
var (
1718
// UnmarshalJSON is injection point of json.Unmarshaller for money.Money
1819
UnmarshalJSON = defaultUnmarshalJSON
@@ -80,15 +81,15 @@ type Amount = int64
8081
// Money represents monetary value information, stores
8182
// currency and amount value.
8283
type Money struct {
83-
amount Amount
84-
currency *Currency
84+
Amount_ Amount `json:"amount" swaggertype:"primitive,integer"`
85+
Currency_ *Currency `json:"currency" swaggertype:"primitive,string"`
8586
}
8687

8788
// New creates and returns new instance of Money.
8889
func New(amount int64, code string) *Money {
8990
return &Money{
90-
amount: amount,
91-
currency: newCurrency(code).get(),
91+
Amount_: amount,
92+
Currency_: newCurrency(code).get(),
9293
}
9394
}
9495

@@ -101,17 +102,17 @@ func NewFromFloat(amount float64, currency string) *Money {
101102

102103
// Currency returns the currency used by Money.
103104
func (m *Money) Currency() *Currency {
104-
return m.currency
105+
return m.Currency_
105106
}
106107

107108
// Amount returns a copy of the internal monetary value as an int64.
108109
func (m *Money) Amount() int64 {
109-
return m.amount
110+
return m.Amount_
110111
}
111112

112113
// SameCurrency check if given Money is equals by currency.
113114
func (m *Money) SameCurrency(om *Money) bool {
114-
return m.currency.equals(om.currency)
115+
return m.Currency_.equals(om.Currency_)
115116
}
116117

117118
func (m *Money) assertSameCurrency(om *Money) error {
@@ -124,9 +125,9 @@ func (m *Money) assertSameCurrency(om *Money) error {
124125

125126
func (m *Money) compare(om *Money) int {
126127
switch {
127-
case m.amount > om.amount:
128+
case m.Amount_ > om.Amount_:
128129
return 1
129-
case m.amount < om.amount:
130+
case m.Amount_ < om.Amount_:
130131
return -1
131132
}
132133

@@ -180,27 +181,27 @@ func (m *Money) LessThanOrEqual(om *Money) (bool, error) {
180181

181182
// IsZero returns boolean of whether the value of Money is equals to zero.
182183
func (m *Money) IsZero() bool {
183-
return m.amount == 0
184+
return m.Amount_ == 0
184185
}
185186

186187
// IsPositive returns boolean of whether the value of Money is positive.
187188
func (m *Money) IsPositive() bool {
188-
return m.amount > 0
189+
return m.Amount_ > 0
189190
}
190191

191192
// IsNegative returns boolean of whether the value of Money is negative.
192193
func (m *Money) IsNegative() bool {
193-
return m.amount < 0
194+
return m.Amount_ < 0
194195
}
195196

196197
// Absolute returns new Money struct from given Money using absolute monetary value.
197198
func (m *Money) Absolute() *Money {
198-
return &Money{amount: mutate.calc.absolute(m.amount), currency: m.currency}
199+
return &Money{Amount_: mutate.calc.absolute(m.Amount_), Currency_: m.Currency_}
199200
}
200201

201202
// Negative returns new Money struct from given Money using negative monetary value.
202203
func (m *Money) Negative() *Money {
203-
return &Money{amount: mutate.calc.negative(m.amount), currency: m.currency}
204+
return &Money{Amount_: mutate.calc.negative(m.Amount_), Currency_: m.Currency_}
204205
}
205206

206207
// Add returns new Money struct with value representing sum of Self and Other Money.
@@ -209,7 +210,7 @@ func (m *Money) Add(om *Money) (*Money, error) {
209210
return nil, err
210211
}
211212

212-
return &Money{amount: mutate.calc.add(m.amount, om.amount), currency: m.currency}, nil
213+
return &Money{Amount_: mutate.calc.add(m.Amount_, om.Amount_), Currency_: m.Currency_}, nil
213214
}
214215

215216
// Subtract returns new Money struct with value representing difference of Self and Other Money.
@@ -218,17 +219,17 @@ func (m *Money) Subtract(om *Money) (*Money, error) {
218219
return nil, err
219220
}
220221

221-
return &Money{amount: mutate.calc.subtract(m.amount, om.amount), currency: m.currency}, nil
222+
return &Money{Amount_: mutate.calc.subtract(m.Amount_, om.Amount_), Currency_: m.Currency_}, nil
222223
}
223224

224225
// Multiply returns new Money struct with value representing Self multiplied value by multiplier.
225226
func (m *Money) Multiply(mul int64) *Money {
226-
return &Money{amount: mutate.calc.multiply(m.amount, mul), currency: m.currency}
227+
return &Money{Amount_: mutate.calc.multiply(m.Amount_, mul), Currency_: m.Currency_}
227228
}
228229

229230
// Round returns new Money struct with value rounded to nearest zero.
230231
func (m *Money) Round() *Money {
231-
return &Money{amount: mutate.calc.round(m.amount, m.currency.Fraction), currency: m.currency}
232+
return &Money{Amount_: mutate.calc.round(m.Amount_, m.Currency_.Fraction), Currency_: m.Currency_}
232233
}
233234

234235
// Split returns slice of Money structs with split Self value in given number.
@@ -239,23 +240,23 @@ func (m *Money) Split(n int) ([]*Money, error) {
239240
return nil, errors.New("split must be higher than zero")
240241
}
241242

242-
a := mutate.calc.divide(m.amount, int64(n))
243+
a := mutate.calc.divide(m.Amount_, int64(n))
243244
ms := make([]*Money, n)
244245

245246
for i := 0; i < n; i++ {
246-
ms[i] = &Money{amount: a, currency: m.currency}
247+
ms[i] = &Money{Amount_: a, Currency_: m.Currency_}
247248
}
248249

249-
r := mutate.calc.modulus(m.amount, int64(n))
250+
r := mutate.calc.modulus(m.Amount_, int64(n))
250251
l := mutate.calc.absolute(r)
251252
// Add leftovers to the first parties.
252253

253254
v := int64(1)
254-
if m.amount < 0 {
255+
if m.Amount_ < 0 {
255256
v = -1
256257
}
257258
for p := 0; l != 0; p++ {
258-
ms[p].amount = mutate.calc.add(ms[p].amount, v)
259+
ms[p].Amount_ = mutate.calc.add(ms[p].Amount_, v)
259260
l--
260261
}
261262

@@ -280,23 +281,23 @@ func (m *Money) Allocate(rs ...int) ([]*Money, error) {
280281
ms := make([]*Money, 0, len(rs))
281282
for _, r := range rs {
282283
party := &Money{
283-
amount: mutate.calc.allocate(m.amount, r, sum),
284-
currency: m.currency,
284+
Amount_: mutate.calc.allocate(m.Amount_, r, sum),
285+
Currency_: m.Currency_,
285286
}
286287

287288
ms = append(ms, party)
288-
total += party.amount
289+
total += party.Amount_
289290
}
290291

291292
// Calculate leftover value and divide to first parties.
292-
lo := m.amount - total
293+
lo := m.Amount_ - total
293294
sub := int64(1)
294295
if lo < 0 {
295296
sub = -sub
296297
}
297298

298299
for p := 0; lo != 0; p++ {
299-
ms[p].amount = mutate.calc.add(ms[p].amount, sub)
300+
ms[p].Amount_ = mutate.calc.add(ms[p].Amount_, sub)
300301
lo -= sub
301302
}
302303

@@ -305,14 +306,14 @@ func (m *Money) Allocate(rs ...int) ([]*Money, error) {
305306

306307
// Display lets represent Money struct as string in given Currency value.
307308
func (m *Money) Display() string {
308-
c := m.currency.get()
309-
return c.Formatter().Format(m.amount)
309+
c := m.Currency_.get()
310+
return c.Formatter().Format(m.Amount_)
310311
}
311312

312313
// AsMajorUnits lets represent Money struct as subunits (float64) in given Currency value
313314
func (m *Money) AsMajorUnits() float64 {
314-
c := m.currency.get()
315-
return c.Formatter().ToMajorUnits(m.amount)
315+
c := m.Currency_.get()
316+
return c.Formatter().ToMajorUnits(m.Amount_)
316317
}
317318

318319
// UnmarshalJSON is implementation of json.Unmarshaller

0 commit comments

Comments
 (0)