Skip to content

Commit b9ccae1

Browse files
authored
Update JSON property names to camel case (#23)
1 parent c3c15bf commit b9ccae1

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ Highlights include:
3434
Each HTTP endpoint requires a JSON description of the desired zip file. It includes a root object with the following structure:
3535

3636
- `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)
37-
- `entries` [Required, array]: an array descibing the files to inclue in the zip file. Each array entry required 2 properties:
38-
- `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)).
39-
- `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.
37+
- `files` [Required, array]: an array descibing the files to inclue in the zip file. Each array entry required 2 properties:
38+
- `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)).
39+
- `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.
4040

4141
Example JSON description with 2 files:
4242

4343
```
4444
{
4545
"suggestedFilename": "tps_reports.zip",
46-
"entries": [
46+
"files": [
4747
{
48-
"Url":"https://server.com/image1.jpg",
49-
"ZipPath":"image1.jpg"
48+
"url":"https://server.com/image1.jpg",
49+
"zipPath":"image1.jpg"
5050
},
5151
{
52-
"Url":"https://server.com/image2.jpg",
53-
"ZipPath":"in-a-sub-folder/image2.jpg"
52+
"url":"https://server.com/image2.jpg",
53+
"zipPath":"in-a-sub-folder/image2.jpg"
5454
}
5555
]
5656
}

test/zip_descriptor_test.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import (
66
zip_streamer "github.com/scosman/zipstreamer/zip_streamer"
77
)
88

9-
var invalidPayload = []byte(`{"entries": "dfdf"}`)
10-
var emptyPayload = []byte(`{"entries": []}`)
11-
var validPayload = []byte(`{"entries": [{"Url":"https://a.com/1","ZipPath":"file1.jpg"},{"Url":"https://a.com/2","ZipPath":"file2.jpg"}]}`)
12-
139
func TestNewZipDescriptor(t *testing.T) {
1410
zd := zip_streamer.NewZipDescriptor()
1511

@@ -22,6 +18,8 @@ func TestNewZipDescriptor(t *testing.T) {
2218
}
2319
}
2420

21+
var invalidPayload = []byte(`{"entries": "dfdf"}`)
22+
2523
func TestUnmarshalJsonInvalid(t *testing.T) {
2624
p, err := zip_streamer.UnmarshalJsonZipDescriptor(invalidPayload)
2725

@@ -30,6 +28,8 @@ func TestUnmarshalJsonInvalid(t *testing.T) {
3028
}
3129
}
3230

31+
var emptyPayload = []byte(`{"entries": []}`)
32+
3333
func TestUnmarshaJsonEmpty(t *testing.T) {
3434
r, err := zip_streamer.UnmarshalJsonZipDescriptor(emptyPayload)
3535

@@ -42,6 +42,24 @@ func TestUnmarshaJsonEmpty(t *testing.T) {
4242
}
4343
}
4444

45+
var validPayloadLegacy = []byte(`{"entries": [{"Url":"https://a.com/1","ZipPath":"file1.jpg"},{"Url":"https://a.com/2","ZipPath":"file2.jpg"}]}`)
46+
47+
func TestUnmarshalJsonValidLegacy(t *testing.T) {
48+
r, err := zip_streamer.UnmarshalJsonZipDescriptor(validPayloadLegacy)
49+
50+
if err != nil {
51+
t.Fatalf("couldn't parse empty payload: %v", err)
52+
}
53+
if len(r.Files()) != 2 {
54+
t.Fatalf("incorrect entry count %v", len(r.Files()))
55+
}
56+
if r.Files()[0].Url().String() != "https://a.com/1" || r.Files()[1].ZipPath() != "file2.jpg" {
57+
t.Fatal("invalid parsing")
58+
}
59+
}
60+
61+
var validPayload = []byte(`{"files": [{"url":"https://a.com/1","zipPath":"file1.jpg"},{"url":"https://a.com/2","zipPath":"file2.jpg"}]}`)
62+
4563
func TestUnmarshalJsonValid(t *testing.T) {
4664
r, err := zip_streamer.UnmarshalJsonZipDescriptor(validPayload)
4765

zip_streamer/zip_descriptor.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ func (zd ZipDescriptor) Files() []*FileEntry {
4848
}
4949

5050
type jsonZipEntry struct {
51-
Url string `json:"Url"`
52-
ZipPath string `json:"ZipPath"`
51+
DeprecatedCapitalizedUrl string `json:"Url"`
52+
DeprecatedCapitalizedZipPath string `json:"ZipPath"`
53+
Url string `json:"url"`
54+
ZipPath string `json:"zipPath"`
5355
}
5456

5557
type jsonZipPayload struct {
56-
Entries []jsonZipEntry `json:"entries"`
58+
Files []jsonZipEntry `json:"files"`
59+
DeprecatedEntries []jsonZipEntry `json:"entries"`
5760
SuggestedFilename string `json:"suggestedFilename"`
5861
}
5962

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

6770
zd := NewZipDescriptor()
6871
zd.suggestedFilenameRaw = parsed.SuggestedFilename
69-
for _, entry := range parsed.Entries {
70-
fileEntry, err := NewFileEntry(entry.Url, entry.ZipPath)
72+
73+
// Maintain backwards compatibility when files were named `entries`
74+
jsonZipFileList := parsed.Files
75+
if len(jsonZipFileList) == 0 {
76+
jsonZipFileList = parsed.DeprecatedEntries
77+
}
78+
79+
for _, jsonZipFileItem := range jsonZipFileList {
80+
// Maintain backwards compatibility for non camel case parameters
81+
jsonFileItemUrl := jsonZipFileItem.Url
82+
if jsonFileItemUrl == "" {
83+
jsonFileItemUrl = jsonZipFileItem.DeprecatedCapitalizedUrl
84+
}
85+
jsonFileItemZipPath := jsonZipFileItem.ZipPath
86+
if jsonFileItemZipPath == "" {
87+
jsonFileItemZipPath = jsonZipFileItem.DeprecatedCapitalizedZipPath
88+
}
89+
90+
fileEntry, err := NewFileEntry(jsonFileItemUrl, jsonFileItemZipPath)
7191
if err == nil {
7292
zd.files = append(zd.files, fileEntry)
7393
}

0 commit comments

Comments
 (0)