@@ -11,8 +11,9 @@ import (
11
11
12
12
// Injection points for backward compatibility.
13
13
// 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) { ... }
16
17
var (
17
18
// UnmarshalJSON is injection point of json.Unmarshaller for money.Money
18
19
UnmarshalJSON = defaultUnmarshalJSON
@@ -80,15 +81,15 @@ type Amount = int64
80
81
// Money represents monetary value information, stores
81
82
// currency and amount value.
82
83
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"`
85
86
}
86
87
87
88
// New creates and returns new instance of Money.
88
89
func New (amount int64 , code string ) * Money {
89
90
return & Money {
90
- amount : amount ,
91
- currency : newCurrency (code ).get (),
91
+ Amount_ : amount ,
92
+ Currency_ : newCurrency (code ).get (),
92
93
}
93
94
}
94
95
@@ -101,17 +102,17 @@ func NewFromFloat(amount float64, currency string) *Money {
101
102
102
103
// Currency returns the currency used by Money.
103
104
func (m * Money ) Currency () * Currency {
104
- return m .currency
105
+ return m .Currency_
105
106
}
106
107
107
108
// Amount returns a copy of the internal monetary value as an int64.
108
109
func (m * Money ) Amount () int64 {
109
- return m .amount
110
+ return m .Amount_
110
111
}
111
112
112
113
// SameCurrency check if given Money is equals by currency.
113
114
func (m * Money ) SameCurrency (om * Money ) bool {
114
- return m .currency .equals (om .currency )
115
+ return m .Currency_ .equals (om .Currency_ )
115
116
}
116
117
117
118
func (m * Money ) assertSameCurrency (om * Money ) error {
@@ -124,9 +125,9 @@ func (m *Money) assertSameCurrency(om *Money) error {
124
125
125
126
func (m * Money ) compare (om * Money ) int {
126
127
switch {
127
- case m .amount > om .amount :
128
+ case m .Amount_ > om .Amount_ :
128
129
return 1
129
- case m .amount < om .amount :
130
+ case m .Amount_ < om .Amount_ :
130
131
return - 1
131
132
}
132
133
@@ -180,27 +181,27 @@ func (m *Money) LessThanOrEqual(om *Money) (bool, error) {
180
181
181
182
// IsZero returns boolean of whether the value of Money is equals to zero.
182
183
func (m * Money ) IsZero () bool {
183
- return m .amount == 0
184
+ return m .Amount_ == 0
184
185
}
185
186
186
187
// IsPositive returns boolean of whether the value of Money is positive.
187
188
func (m * Money ) IsPositive () bool {
188
- return m .amount > 0
189
+ return m .Amount_ > 0
189
190
}
190
191
191
192
// IsNegative returns boolean of whether the value of Money is negative.
192
193
func (m * Money ) IsNegative () bool {
193
- return m .amount < 0
194
+ return m .Amount_ < 0
194
195
}
195
196
196
197
// Absolute returns new Money struct from given Money using absolute monetary value.
197
198
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_ }
199
200
}
200
201
201
202
// Negative returns new Money struct from given Money using negative monetary value.
202
203
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_ }
204
205
}
205
206
206
207
// 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) {
209
210
return nil , err
210
211
}
211
212
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
213
214
}
214
215
215
216
// 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) {
218
219
return nil , err
219
220
}
220
221
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
222
223
}
223
224
224
225
// Multiply returns new Money struct with value representing Self multiplied value by multiplier.
225
226
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_ }
227
228
}
228
229
229
230
// Round returns new Money struct with value rounded to nearest zero.
230
231
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_ }
232
233
}
233
234
234
235
// 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) {
239
240
return nil , errors .New ("split must be higher than zero" )
240
241
}
241
242
242
- a := mutate .calc .divide (m .amount , int64 (n ))
243
+ a := mutate .calc .divide (m .Amount_ , int64 (n ))
243
244
ms := make ([]* Money , n )
244
245
245
246
for i := 0 ; i < n ; i ++ {
246
- ms [i ] = & Money {amount : a , currency : m .currency }
247
+ ms [i ] = & Money {Amount_ : a , Currency_ : m .Currency_ }
247
248
}
248
249
249
- r := mutate .calc .modulus (m .amount , int64 (n ))
250
+ r := mutate .calc .modulus (m .Amount_ , int64 (n ))
250
251
l := mutate .calc .absolute (r )
251
252
// Add leftovers to the first parties.
252
253
253
254
v := int64 (1 )
254
- if m .amount < 0 {
255
+ if m .Amount_ < 0 {
255
256
v = - 1
256
257
}
257
258
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 )
259
260
l --
260
261
}
261
262
@@ -280,23 +281,23 @@ func (m *Money) Allocate(rs ...int) ([]*Money, error) {
280
281
ms := make ([]* Money , 0 , len (rs ))
281
282
for _ , r := range rs {
282
283
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_ ,
285
286
}
286
287
287
288
ms = append (ms , party )
288
- total += party .amount
289
+ total += party .Amount_
289
290
}
290
291
291
292
// Calculate leftover value and divide to first parties.
292
- lo := m .amount - total
293
+ lo := m .Amount_ - total
293
294
sub := int64 (1 )
294
295
if lo < 0 {
295
296
sub = - sub
296
297
}
297
298
298
299
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 )
300
301
lo -= sub
301
302
}
302
303
@@ -305,14 +306,14 @@ func (m *Money) Allocate(rs ...int) ([]*Money, error) {
305
306
306
307
// Display lets represent Money struct as string in given Currency value.
307
308
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_ )
310
311
}
311
312
312
313
// AsMajorUnits lets represent Money struct as subunits (float64) in given Currency value
313
314
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_ )
316
317
}
317
318
318
319
// UnmarshalJSON is implementation of json.Unmarshaller
0 commit comments