forked from calibrae-project/spawn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amount.go
40 lines (35 loc) · 930 Bytes
/
amount.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
package cfgutil
import (
"github.com/p9c/duod/pkg/amt"
"strconv"
"strings"
)
// AmountFlag embeds a util.Amount and implements the flags.Marshaler and Unmarshaler interfaces so it can be used as a
// config struct field.
type AmountFlag struct {
amt.Amount
}
// NewAmountFlag creates an AmountFlag with a default util.Amount.
func NewAmountFlag(defaultValue amt.Amount) *AmountFlag {
return &AmountFlag{defaultValue}
}
// MarshalFlag satisifes the flags.Marshaler interface.
func (a *AmountFlag) MarshalFlag() (string, error) {
return a.Amount.String(), nil
}
// UnmarshalFlag satisifes the flags.Unmarshaler interface.
func (a *AmountFlag) UnmarshalFlag(value string) (e error) {
value = strings.TrimSuffix(value, " DUO")
valueF64, e := strconv.ParseFloat(value, 64)
if e != nil {
E.Ln(e)
return e
}
amount, e := amt.NewAmount(valueF64)
if e != nil {
E.Ln(e)
return e
}
a.Amount = amount
return nil
}