Skip to content

Commit

Permalink
feat: parse non-standard metadata filenames
Browse files Browse the repository at this point in the history
tile-generator creates metadata filenames that are not "metadata/metadata.yml"
with this change you can parse those tiles with the tile.ReadMetadata* functions

fixes: #403
  • Loading branch information
crhntr authored and staylor14 committed Jun 27, 2023
1 parent c956ecc commit 57a6481
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/tile/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ func ReadMetadataFromZip(ra io.ReaderAt, zipFileSize int64) ([]byte, error) {
}

func ReadMetadataFromFS(dir fs.FS) ([]byte, error) {
metadataFile, err := dir.Open("metadata/metadata.yml")
const pattern = "metadata/*.yml"
matches, err := fs.Glob(dir, pattern)
if err != nil {
return nil, err
}
if len(matches) == 0 {
return nil, fmt.Errorf("metadata file not found in the tile: expected a file matching glob %q", pattern)
}
metadataFile, err := dir.Open(matches[0])
if err != nil {
return nil, fmt.Errorf("failed to do open metadata zip file: %w", err)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/tile/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tile_test

import (
"testing"
"testing/fstest"

. "github.com/onsi/gomega"
"gopkg.in/yaml.v2"
Expand All @@ -23,3 +24,13 @@ func TestReadMetadataFromFile(t *testing.T) {

please.Expect(metadata.Name).To(Equal("hello"), string(metadataBytes))
}

func TestNonStandardMetadataFilename(t *testing.T) {
fileFS := fstest.MapFS{
"metadata/banana.yml": &fstest.MapFile{Data: []byte(`{name: "banana"}`)},
}
buf, err := tile.ReadMetadataFromFS(fileFS)
please := NewWithT(t)
please.Expect(err).NotTo(HaveOccurred())
please.Expect(string(buf)).To(Equal(`{name: "banana"}`))
}

0 comments on commit 57a6481

Please sign in to comment.