Skip to content

Commit

Permalink
#131 Support File Uploads inside of Inputs (#17)
Browse files Browse the repository at this point in the history
Signed-off-by: mfinley <[email protected]>
  • Loading branch information
mfinley3 authored Apr 5, 2021
1 parent b987a48 commit 401da1e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
50 changes: 36 additions & 14 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,52 @@ func (u *UploadMap) Add(upload Upload, varName string) {
})
}

// returns a map of file names to paths.
// Used only in testing extractFiles
func (u *UploadMap) uploads() map[string]string {
var result = make(map[string]string)

for _, attachment := range *u {
result[attachment.upload.FileName] = attachment.positions[0]
}

return result
}

// function extracts attached files and sets respective variables to null
func extractFiles(input *QueryInput) *UploadMap {
uploadMap := &UploadMap{}

for varName, value := range input.Variables {
switch valueTyped := value.(type) {
case Upload:
uploadMap.Add(valueTyped, varName)
uploadMap.extract(value, varName)
if _, ok := value.(Upload); ok { //If the value was an upload, set the respective QueryInput variable to null
input.Variables[varName] = nil
case []interface{}:
for i, uploadVal := range valueTyped {
if upload, ok := uploadVal.(Upload); ok {
uploadMap.Add(upload, fmt.Sprintf("%s.%d", varName, i))
valueTyped[i] = nil
}
}
input.Variables[varName] = valueTyped
default:
//noop
}
}
return uploadMap
}

func (u *UploadMap) extract(value interface{}, path string) {
switch val := value.(type) {
case Upload: // Upload found
u.Add(val, path)
case map[string]interface{}:
for k, v := range val {
u.extract(v, fmt.Sprintf("%s.%s", path, k))
if _, ok := v.(Upload); ok { //If the value was an upload, set the respective QueryInput variable to null
val[k] = nil
}
}
case []interface{}:
for i, v := range val {
u.extract(v, fmt.Sprintf("%s.%d", path, i))
if _, ok := v.(Upload); ok { //If the value was an upload, set the respective QueryInput variable to null
val[i] = nil
}
}
}
return
}

func prepareMultipart(payload []byte, uploadMap *UploadMap) (body []byte, contentType string, err error) {
var b = bytes.Buffer{}
var fw io.Writer
Expand Down
36 changes: 35 additions & 1 deletion file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func TestExtractFiles(t *testing.T) {
upload1 := Upload{nil, "file1"}
upload2 := Upload{nil, "file2"}
upload3 := Upload{nil, "file3"}
upload4 := Upload{nil, "file4"}
upload5 := Upload{nil, "file5"}
upload6 := Upload{nil, "file6"}
upload7 := Upload{nil, "file7"}
upload8 := Upload{nil, "file8"}

input := &QueryInput{
Variables: map[string]interface{}{
Expand All @@ -25,6 +30,30 @@ func TestExtractFiles(t *testing.T) {
upload2,
upload3,
},
"input": map[string]interface{}{
"not-an-upload": true,
"files": []interface{}{
upload4,
upload5,
},
},
"these": map[string]interface{}{
"are": []interface{}{
upload6,
map[string]interface{}{
"some": map[string]interface{}{
"deeply": map[string]interface{}{
"nested": map[string]interface{}{
"uploads": []interface{}{
upload7,
upload8,
},
},
},
},
},
},
},
"integerParam": 10,
},
}
Expand All @@ -35,8 +64,13 @@ func TestExtractFiles(t *testing.T) {
expected.Add(upload1, "someFile")
expected.Add(upload2, "allFiles.0")
expected.Add(upload3, "allFiles.1")
expected.Add(upload4, "input.files.0")
expected.Add(upload5, "input.files.1")
expected.Add(upload6, "these.are.0")
expected.Add(upload7, "these.are.1.some.deeply.nested.uploads.0")
expected.Add(upload8, "these.are.1.some.deeply.nested.uploads.1")

assert.Equal(t, expected, actual)
assert.Equal(t, expected.uploads(), actual.uploads())
assert.Equal(t, "hello world", input.Variables["stringParam"])
assert.Equal(t, []interface{}{"one", "two"}, input.Variables["listParam"])
}
Expand Down

0 comments on commit 401da1e

Please sign in to comment.