fix: handle MAL API returning numbers instead of strings for zero-value statistics
The MyAnimeList API inconsistently returns statistics status fields (watching, completed, on_hold, dropped, plan_to_watch) as quoted strings for non-zero values (e.g. "8217") but as bare numbers for zero values (e.g. 0). This caused JSON unmarshal errors for anime with zero counts in any status field. Introduce a FlexString custom type that implements json.Unmarshaler to accept both JSON strings and JSON numbers, always storing the result as a string. The type definition lives in MALTypes.go and the unmarshal logic in MALFunctions.go to keep static types and behavior separate.
This commit is contained in:
@@ -11,6 +11,21 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Unmarshalling accidental numbers received from MAL to strings
|
||||
func (f *FlexString) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err == nil {
|
||||
*f = FlexString(s)
|
||||
return nil
|
||||
}
|
||||
var n json.Number
|
||||
if err := json.Unmarshal(data, &n); err == nil {
|
||||
*f = FlexString(string(n))
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("FlexString: invalid value")
|
||||
}
|
||||
|
||||
func MALHelper(method string, malUrl string, body url.Values) (json.RawMessage, string, error) {
|
||||
client := &http.Client{}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user