forked from decred/dcrwallet
/
amount.go
44 lines (37 loc) · 1.11 KB
/
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
41
42
43
44
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package cfgutil
import (
"strconv"
"strings"
"github.com/decred/dcrd/dcrutil"
)
// AmountFlag embeds a dcrutil.Amount and implements the flags.Marshaler and
// Unmarshaler interfaces so it can be used as a config struct field.
type AmountFlag struct {
dcrutil.Amount
}
// NewAmountFlag creates an AmountFlag with a default dcrutil.Amount.
func NewAmountFlag(defaultValue dcrutil.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) error {
value = strings.TrimSuffix(value, " DCR")
valueF64, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
amount, err := dcrutil.NewAmount(valueF64)
if err != nil {
return err
}
a.Amount = amount
return nil
}