Skip to content

Update JSON property names to camel case #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ Highlights include:
Each HTTP endpoint requires a JSON description of the desired zip file. It includes a root object with the following structure:

- `suggestedFilename` [optional, string]: The filename to suggest in the "Save As" UI in browsers. Defaults to `archive.zip` if not provided or invalid. [Limited to US-ASCII.](https://www.rfc-editor.org/rfc/rfc2183#section-2.3)
- `entries` [Required, array]: an array descibing the files to inclue in the zip file. Each array entry required 2 properties:
- `Url` [Required, string]: the URL of the file to include in the zip. Zipstreamer will fetch this via a GET request. The file must be public; if it is private, most file hosts provide query string authentication options for private files, which work well with Zipstreamer (example [AWS S3 Docs](https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html)).
- `ZipPath` [Required, string]: the path and filename where this entry should appear in the resulting zip file. This is a relative path to the root of the zip file.
- `files` [Required, array]: an array descibing the files to inclue in the zip file. Each array entry required 2 properties:
- `url` [Required, string]: the URL of the file to include in the zip. Zipstreamer will fetch this via a GET request. The file must be public; if it is private, most file hosts provide query string authentication options for private files, which work well with Zipstreamer (example [AWS S3 Docs](https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html)).
- `zipPath` [Required, string]: the path and filename where this entry should appear in the resulting zip file. This is a relative path to the root of the zip file.

Example JSON description with 2 files:

```
{
"suggestedFilename": "tps_reports.zip",
"entries": [
"files": [
{
"Url":"https://server.com/image1.jpg",
"ZipPath":"image1.jpg"
"url":"https://server.com/image1.jpg",
"zipPath":"image1.jpg"
},
{
"Url":"https://server.com/image2.jpg",
"ZipPath":"in-a-sub-folder/image2.jpg"
"url":"https://server.com/image2.jpg",
"zipPath":"in-a-sub-folder/image2.jpg"
}
]
}
Expand Down
26 changes: 22 additions & 4 deletions test/zip_descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
zip_streamer "github.com/scosman/zipstreamer/zip_streamer"
)

var invalidPayload = []byte(`{"entries": "dfdf"}`)
var emptyPayload = []byte(`{"entries": []}`)
var validPayload = []byte(`{"entries": [{"Url":"https://a.com/1","ZipPath":"file1.jpg"},{"Url":"https://a.com/2","ZipPath":"file2.jpg"}]}`)

func TestNewZipDescriptor(t *testing.T) {
zd := zip_streamer.NewZipDescriptor()

Expand All @@ -22,6 +18,8 @@ func TestNewZipDescriptor(t *testing.T) {
}
}

var invalidPayload = []byte(`{"entries": "dfdf"}`)

func TestUnmarshalJsonInvalid(t *testing.T) {
p, err := zip_streamer.UnmarshalJsonZipDescriptor(invalidPayload)

Expand All @@ -30,6 +28,8 @@ func TestUnmarshalJsonInvalid(t *testing.T) {
}
}

var emptyPayload = []byte(`{"entries": []}`)

func TestUnmarshaJsonEmpty(t *testing.T) {
r, err := zip_streamer.UnmarshalJsonZipDescriptor(emptyPayload)

Expand All @@ -42,6 +42,24 @@ func TestUnmarshaJsonEmpty(t *testing.T) {
}
}

var validPayloadLegacy = []byte(`{"entries": [{"Url":"https://a.com/1","ZipPath":"file1.jpg"},{"Url":"https://a.com/2","ZipPath":"file2.jpg"}]}`)

func TestUnmarshalJsonValidLegacy(t *testing.T) {
r, err := zip_streamer.UnmarshalJsonZipDescriptor(validPayloadLegacy)

if err != nil {
t.Fatalf("couldn't parse empty payload: %v", err)
}
if len(r.Files()) != 2 {
t.Fatalf("incorrect entry count %v", len(r.Files()))
}
if r.Files()[0].Url().String() != "https://a.com/1" || r.Files()[1].ZipPath() != "file2.jpg" {
t.Fatal("invalid parsing")
}
}

var validPayload = []byte(`{"files": [{"url":"https://a.com/1","zipPath":"file1.jpg"},{"url":"https://a.com/2","zipPath":"file2.jpg"}]}`)

func TestUnmarshalJsonValid(t *testing.T) {
r, err := zip_streamer.UnmarshalJsonZipDescriptor(validPayload)

Expand Down
30 changes: 25 additions & 5 deletions zip_streamer/zip_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ func (zd ZipDescriptor) Files() []*FileEntry {
}

type jsonZipEntry struct {
Url string `json:"Url"`
ZipPath string `json:"ZipPath"`
DeprecatedCapitalizedUrl string `json:"Url"`
DeprecatedCapitalizedZipPath string `json:"ZipPath"`
Url string `json:"url"`
ZipPath string `json:"zipPath"`
}

type jsonZipPayload struct {
Entries []jsonZipEntry `json:"entries"`
Files []jsonZipEntry `json:"files"`
DeprecatedEntries []jsonZipEntry `json:"entries"`
SuggestedFilename string `json:"suggestedFilename"`
}

Expand All @@ -66,8 +69,25 @@ func UnmarshalJsonZipDescriptor(payload []byte) (*ZipDescriptor, error) {

zd := NewZipDescriptor()
zd.suggestedFilenameRaw = parsed.SuggestedFilename
for _, entry := range parsed.Entries {
fileEntry, err := NewFileEntry(entry.Url, entry.ZipPath)

// Maintain backwards compatibility when files were named `entries`
jsonZipFileList := parsed.Files
if len(jsonZipFileList) == 0 {
jsonZipFileList = parsed.DeprecatedEntries
}

for _, jsonZipFileItem := range jsonZipFileList {
// Maintain backwards compatibility for non camel case parameters
jsonFileItemUrl := jsonZipFileItem.Url
if jsonFileItemUrl == "" {
jsonFileItemUrl = jsonZipFileItem.DeprecatedCapitalizedUrl
}
jsonFileItemZipPath := jsonZipFileItem.ZipPath
if jsonFileItemZipPath == "" {
jsonFileItemZipPath = jsonZipFileItem.DeprecatedCapitalizedZipPath
}

fileEntry, err := NewFileEntry(jsonFileItemUrl, jsonFileItemZipPath)
if err == nil {
zd.files = append(zd.files, fileEntry)
}
Expand Down