price marchalling
This commit is contained in:
@@ -87,6 +87,34 @@ func (p Price) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(wire{ExVat: exVat, IncVat: p.IncVat, Vat: vat})
|
||||
}
|
||||
|
||||
func (p *Price) UnmarshalJSON(data []byte) error {
|
||||
type wire struct {
|
||||
ExVat int64 `json:"exVat"`
|
||||
IncVat int64 `json:"incVat"`
|
||||
Vat map[string]int64 `json:"vat,omitempty"`
|
||||
}
|
||||
var w wire
|
||||
if err := json.Unmarshal(data, &w); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.IncVat = w.IncVat
|
||||
if len(w.Vat) > 0 {
|
||||
p.VatRates = make(map[float32]int64, len(w.Vat))
|
||||
for rateStr, amount := range w.Vat {
|
||||
rate, err := strconv.ParseFloat(rateStr, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.VatRates[float32(rate)] = amount
|
||||
}
|
||||
} else {
|
||||
p.VatRates = make(map[float32]int64)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// trimFloat converts a float32 tax rate like 25 or 12.5 into a compact string without
|
||||
// unnecessary decimals ("25", "12.5").
|
||||
func trimFloat(f float32) string {
|
||||
|
||||
Reference in New Issue
Block a user