Skip to content

Commit

Permalink
fix: Better handle inconsistency in titledb.json
Browse files Browse the repository at this point in the history
  • Loading branch information
DblK committed Dec 28, 2021
1 parent c55fcf4 commit ca212e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"tmpl",
"typecheck",
"unconvert",
"unmarshalling",
"unparam",
"varcheck",
"Warnf"
Expand Down
2 changes: 1 addition & 1 deletion gamescollection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 33 additions & 2 deletions repository/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package repository

import (
"encoding/json"
"net/http"
"strconv"
"strings"
)

// GameID interface
Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit ca212e2

Please sign in to comment.