Skip to content

Commit

Permalink
[bugfix] Correct format of JSON
Browse files Browse the repository at this point in the history
- Corrects format of JSON by adding assets parent
- Fixes FileURL variable to map to the proper field
- Adds basic unit test for JSON loading
  • Loading branch information
tystuyfzand committed Jan 26, 2024
1 parent 5cfbfa6 commit 85fc249
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
26 changes: 20 additions & 6 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ func loadMapCSV(f io.Reader) (map[string]string, error) {
return m, nil
}

// Map represents a JSON format of an asset list
type Map struct {
Assets []ReleaseFile `json:"assets"`
}

// ReleaseFile represents a file to be mapped
type ReleaseFile struct {
BoardSlug string `json:"board_slug"`
FileURI string `json:"file_uri"`
FileURL string `json:"file_url"`
FileUpdated string `json:"file_updated"`
FileSize string `json:"file_size"`
DistroRelease string `json:"distro_release"`
Expand All @@ -84,20 +89,29 @@ var distroCaser = cases.Title(language.Und)
func loadMapJSON(f io.Reader) (map[string]string, error) {
m := make(map[string]string)

var files []ReleaseFile
var data Map

if err := json.NewDecoder(f).Decode(&files); err != nil {
if err := json.NewDecoder(f).Decode(&data); err != nil {
return nil, err
}

for _, file := range files {
builtUri := fmt.Sprintf("%s/%s-%s.%s",
for _, file := range data.Assets {
builtUri := fmt.Sprintf("%s/%s_%s.%s",
file.BoardSlug,
distroCaser.String(file.DistroRelease),
file.KernelBranch,
file.Extension)

m[builtUri] = file.FileURI
m[builtUri] = file.FileURL

if file.Extension == "img.xz" {
noExtUri := fmt.Sprintf("%s/%s_%s",
file.BoardSlug,
distroCaser.String(file.DistroRelease),
file.KernelBranch)

m[noExtUri] = file.FileURL
}
}

return m, nil
Expand Down
27 changes: 27 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redirector
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
"strings"
)

Expand All @@ -13,4 +14,30 @@ var _ = Describe("Map", func() {
Expect(err).To(BeNil())
Expect(m["bananapi/Bullseye_current"]).To(Equal("bananapi/archive/Armbian_21.08.1_Bananapi_bullseye_current_5.10.60.img.xz"))
})
It("Should successfully load the map from a JSON file", func() {
data := `{
"assets": [
{
"board_slug": "aml-s9xx-box",
"armbian_version": "23.11.1",
"file_url": "https://dl.armbian.com/aml-s9xx-box/archive/Armbian_23.11.1_Aml-s9xx-box_bookworm_current_6.1.63.img.xz",
"file_updated": "2023-11-30T01:14:49Z",
"file_size": "566235552",
"distro_release": "bookworm",
"kernel_branch": "current",
"image_variant": "server",
"preinstalled_application": "",
"promoted": "true",
"download_repository": "archive",
"file_extension": "img.xz"
}
]
}`

m, err := loadMapJSON(strings.NewReader(data))

log.Println(m)
Expect(err).To(BeNil())
Expect(m["aml-s9xx-box/Bookworm_current"]).To(Equal("https://dl.armbian.com/aml-s9xx-box/archive/Armbian_23.11.1_Aml-s9xx-box_bookworm_current_6.1.63.img.xz"))
})
})

0 comments on commit 85fc249

Please sign in to comment.