From 1908976143427b158761a030c85c8d500ee5cd4e Mon Sep 17 00:00:00 2001 From: Linus789 Date: Thu, 12 Jan 2023 20:09:21 +0100 Subject: [PATCH] fix parsing the bromite index --- fdroid/index.go | 65 +++++++++++++++++++++++++++++++++++++------------ show.go | 6 ++--- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/fdroid/index.go b/fdroid/index.go index 07d5762..b94a229 100644 --- a/fdroid/index.go +++ b/fdroid/index.go @@ -10,6 +10,7 @@ import ( "html" "io" "sort" + "strconv" "strings" "mvdan.cc/fdroidcl/adb" @@ -193,21 +194,22 @@ func (a *App) TextDesc(w io.Writer) { // Apk is an Android package type Apk struct { - VersName string `json:"versionName"` - VersCode int `json:"versionCode"` - Size int64 `json:"size"` - MinSdk int `json:"minSdkVersion"` - MaxSdk int `json:"maxSdkVersion"` - ABIs []string `json:"nativecode"` - ApkName string `json:"apkName"` - SrcName string `json:"srcname"` - Sig HexVal `json:"sig"` - Signer HexVal `json:"signer"` - Added UnixDate `json:"added"` - Perms []Permission `json:"uses-permission"` - Feats []string `json:"features"` - Hash HexVal `json:"hash"` - HashType string `json:"hashType"` + VersName string `json:"versionName"` + VersCode int `json:"versionCode"` + Size int64 `json:"size"` + MinSdk StringInt `json:"minSdkVersion"` + MaxSdk StringInt `json:"maxSdkVersion"` + TargetSdk StringInt `json:"targetSdkVersion"` + ABIs []string `json:"nativecode"` + ApkName string `json:"apkName"` + SrcName string `json:"srcname"` + Sig HexVal `json:"sig"` + Signer HexVal `json:"signer"` + Added UnixDate `json:"added"` + Perms []Permission `json:"uses-permission"` + Feats []string `json:"features"` + Hash HexVal `json:"hash"` + HashType string `json:"hashType"` AppID string `json:"-"` RepoURL string `json:"-"` @@ -233,6 +235,37 @@ func (p *Permission) UnmarshalJSON(data []byte) error { return nil } +// https://codesahara.com/blog/golang-cannot-unmarshal-string-into-go-value-of-type-int/ +type StringInt struct { + Value int +} + +func (st *StringInt) UnmarshalJSON(b []byte) error { + // convert the bytes into an interface + // this will help us check the type of our value + // if it is a string that can be converted into an int we convert it + // otherwise we return an error + var item interface{} + if err := json.Unmarshal(b, &item); err != nil { + return err + } + switch v := item.(type) { + case int: + *st = StringInt{Value: v} + case float64: + *st = StringInt{Value: int(v)} + case string: + // here convert the string into an integer + i, err := strconv.Atoi(v) + if err != nil { + // the string might not be of integer type, so return an error + return err + } + *st = StringInt{Value: i} + } + return nil +} + func (a *Apk) URL() string { return fmt.Sprintf("%s/%s", a.RepoURL, a.ApkName) } @@ -256,7 +289,7 @@ func (a *Apk) IsCompatibleABI(ABIs []string) bool { } func (a *Apk) IsCompatibleAPILevel(sdk int) bool { - return sdk >= a.MinSdk && (a.MaxSdk == 0 || sdk <= a.MaxSdk) + return sdk >= a.MinSdk.Value && (a.MaxSdk.Value == 0 || sdk <= a.MaxSdk.Value) } func (a *Apk) IsCompatible(device *adb.Device) bool { diff --git a/show.go b/show.go index e2d659b..b31cb04 100644 --- a/show.go +++ b/show.go @@ -133,9 +133,9 @@ func printAppDetailed(app fdroid.App) { fmt.Println() fmt.Printf(" Version : %s (%d)\n", apk.VersName, apk.VersCode) fmt.Printf(" Size : %d\n", apk.Size) - fmt.Printf(" MinSdk : %d\n", apk.MinSdk) - if apk.MaxSdk > 0 { - fmt.Printf(" MaxSdk : %d\n", apk.MaxSdk) + fmt.Printf(" MinSdk : %d\n", apk.MinSdk.Value) + if apk.MaxSdk.Value > 0 { + fmt.Printf(" MaxSdk : %d\n", apk.MaxSdk.Value) } if apk.ABIs != nil { fmt.Printf(" ABIs : %s\n", strings.Join(apk.ABIs, ", "))