To interact with swagger, I needed to make a custom BigInt
struct which does nothing more than wrap around go's big.Int
.
type BigInt struct {
big.Int
}
...
type SpendTx struct {
SenderID string `json:"sender_id,omitempty"`
RecipientID string `json:"recipient_id,omitempty"`
Amount utils.BigInt `json:"amount,omitempty"`
Fee utils.BigInt `json:"fee,omitempty"`
Payload string `json:"payload,omitempty"`
TTL uint64 `json:"ttl,omitempty"`
Nonce uint64 `json:"nonce,omitempty"`
}
func (t SpendTx) JSON() (output []byte, err error) {
return json.Marshal(t)
}
I'd expect SpendTx.JSON()
to eventually call big.Int.MarshalJSON()
, which would return 0
. Instead, I got this output:
{"sender_id":"alice","recipient_id":"bob","amount":{},"fee":{},"payload":"Hello World","ttl":10,"nonce":1}
But what I really want is this:
{"sender_id":"alice","recipient_id":"bob","amount":10,"fee":10,"payload":"Hello World","ttl":10,"nonce":1}
And I had to add this bit of code to BigInt
to do it:
func (b BigInt) MarshalJSON() ([]byte, error) {
return b.Int.MarshalJSON()
}
But according to Effective Go's section on embedding structs, this shouldn't be necessary at all. Why is big.Int
appearing as as {}
?