diff --git a/.vscode/settings.json b/.vscode/settings.json index 15a973b..5585a6d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -57,6 +57,7 @@ "tmpl", "typecheck", "unconvert", + "unmarshalling", "unparam", "varcheck", "Warnf" diff --git a/gamescollection/collection.go b/gamescollection/collection.go index 39092f7..c88680a 100644 --- a/gamescollection/collection.go +++ b/gamescollection/collection.go @@ -231,5 +231,5 @@ func GetKey(gameID string) (string, error) { if key == "" { return "", errors.New("TitleDBKey for game " + gameID + " is not found") } - return key, nil + return string(key), nil } diff --git a/repository/interfaces.go b/repository/interfaces.go index 9c73a9a..659ee84 100644 --- a/repository/interfaces.go +++ b/repository/interfaces.go @@ -6,7 +6,10 @@ package repository import ( + "encoding/json" "net/http" + "strconv" + "strings" ) // GameID interface @@ -88,13 +91,41 @@ type GameFileType struct { URL string `json:"url"` } +// NInt is a nullable int +type NInt int + +// NString is a nullable string +type NString string + +// UnmarshalJSON handles unmarshalling null string +func (n *NString) UnmarshalJSON(b []byte) (err error) { + if string(b) == "null" { + return nil + } + return json.Unmarshal(b, (*string)(n)) +} + +// UnmarshalJSON handles unmarshalling null int +func (n *NInt) UnmarshalJSON(b []byte) (err error) { + if string(b) == "null" { + return nil + } + // Handle bad data in json file + if strings.Contains(string(b), "\"") { + newNumber, _ := strconv.Atoi(string(b)[1 : len(string(b))-1]) + bs := []byte(strconv.Itoa(newNumber)) + return json.Unmarshal(bs, (*int)(n)) + } + return json.Unmarshal(b, (*int)(n)) +} + // TitleDBEntry describe the various fields for entries type TitleDBEntry struct { ID string `mapstructure:"id" json:"id"` RightsID string `mapstructure:"rightsId" json:"rightsId,omitempty"` Name string `mapstructure:"name" json:"name,omitempty"` - Version string `mapstructure:"version" json:"version,omitempty"` - Key string `mapstructure:"key" json:"key,omitempty"` + Version NInt `mapstructure:"version" json:"version,omitempty"` + Key NString `mapstructure:"key" json:"key,omitempty"` IsDemo bool `mapstructure:"isDemo" json:"isDemo,omitempty"` Region string `mapstructure:"region" json:"region,omitempty"` Regions []string `mapstructure:"regions" json:"regions,omitempty"`