Closed justinpolygon closed 9 months ago
Good catch, and thanks for the thorough explanation 👍
I was surprised and looked into it some more and found a more formalized explanation for this behavior in the Go spec if you're curious.
Great, thanks. Yeah, I was really surprised too.
This PR addresses two bugs https://github.com/polygon-io/client-go/issues/362 & https://github.com/polygon-io/client-go/issues/360 related to the JSON marshaling of
Millis
andNanos
types. Both users experienced issues where these types were not correctly marshaled into JSON, resulting in empty field in the JSON output.Here's the incorrectly marshaled JSON response with the missing
t
value.The core issue was rooted in the use of pointer receivers (
*Millis
and*Nanos
) for the MarshalJSON methods in these types. In our response structs, forAgg
,Trades
,Quotes
,Snapshot
, etc,Millis
andNanos
were utilized as non-pointer fields (e.g.Timestamp Millis
in the response struct). Consequently, whenjson.Marshal
was called, the custom MarshalJSON methods were not being triggered because of the pointer receiver vs. value receiver miss-match, leading to the observed marshaling problems. I was able to debug this by addingfmt.Println
statements toMarshalJSON
for bothMillis
andNanos
and discovered these were never being triggered.To resolve this, the MarshalJSON methods for both
Millis
andNanos
have been modified to use value receivers instead of pointer receivers. This change ensures that these methods are appropriately invoked during JSON marshaling, even whenMillis
andNanos
are used as non-pointer fields. This update aligns the method definitions with the actual usage of these types in our response structs and ensures correct JSON serialization behavior.Here's the correctly marshaled JSON response with
t
having the correct value after re-running the script above.This update specifically targets the JSON marshaling process and should not affect other areas of the application where
Millis
andNanos
are used. I ran though all the examples for stocks, options, indices, forex, and crypto without issue.