-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from clamp-orchestrator/show_workflow
Fetch workflows (GET api)
- Loading branch information
Showing
12 changed files
with
252 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package models | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
//SortByFields contains an array of []sortBy type | ||
//Used to store all sortBy key-value pairs | ||
type SortByFields []sortBy | ||
|
||
//SortBy contains a key and an order to be used to sort a DB query by | ||
//Order supported is asc/desc | ||
type sortBy struct { | ||
Key string | ||
Order string | ||
} | ||
|
||
//ParseFromQuery is used to parse sortBy query string to a custom SortByFields type | ||
//It returns an ordered sortBy struct containing KEY VALUE pair | ||
//If an unknown key is used, an error is raised | ||
//Fields are seperated using a comma and key values are sepearted using a colon | ||
func ParseFromQuery(sortByString string) (SortByFields, error) { | ||
var sortArr SortByFields = []sortBy{} | ||
if len(sortByString) == 0 { | ||
return sortArr, nil | ||
} | ||
sortByString = cleanUpQuery(sortByString) | ||
sortByArgs := strings.Split(sortByString, ",") | ||
for _, value := range sortByArgs { | ||
sortPair := strings.Split(value, ":") | ||
|
||
if len(sortPair) != 2 || !verifySortValues(sortPair[0], sortPair[1]) { | ||
return SortByFields{}, errors.New("Unsupported value provided for sortBy query") | ||
} | ||
key := sortPair[0] | ||
value := sortPair[1] | ||
sort := sortBy{Key: key, Order: value} | ||
sortArr = append(sortArr, sort) | ||
fmt.Println(sortArr) | ||
} | ||
return sortArr, nil | ||
} | ||
|
||
func cleanUpQuery(sortByQuery string) string { | ||
length := len(sortByQuery) | ||
sortByQuery = strings.ToLower(sortByQuery) | ||
if sortByQuery[length-1] == ',' { | ||
sortByQuery = sortByQuery[0 : length-1] | ||
} | ||
return sortByQuery | ||
} | ||
|
||
func verifySortValues(key string, value string) bool { | ||
if value != "desc" && value != "asc" { | ||
return false | ||
} | ||
supportedKeys := []string{"id", "name", "createdate"} | ||
for _, keyVal := range supportedKeys { | ||
if key == keyVal { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package models | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSortByParser(t *testing.T) { | ||
sortByQuery := `id:asc,createdate:desc,name:desc` | ||
sortUsing, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 3, len(sortUsing)) | ||
assert.Equal(t, "id", sortUsing[0].Key) | ||
assert.Equal(t, "asc", sortUsing[0].Order) | ||
assert.Equal(t, "createdate", sortUsing[1].Key) | ||
assert.Equal(t, "desc", sortUsing[1].Order) | ||
assert.Equal(t, "name", sortUsing[2].Key) | ||
assert.Equal(t, "desc", sortUsing[2].Order) | ||
} | ||
|
||
func TestSortByParserFailsOnUnknownFieldName(t *testing.T) { | ||
sortByQuery := "id:asc,createdate:desc,name:desc,invalid:desc" | ||
_, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Unsupported value provided for sortBy query", err.Error()) | ||
} | ||
|
||
func TestSortByParserThrowErrorOnIllegalValue(t *testing.T) { | ||
sortByQuery := `"id":"randomValue","createddate": "desc","name": "desc"` | ||
_, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Unsupported value provided for sortBy query", err.Error()) | ||
} | ||
|
||
func TestSortByParserAllowEmptyString(t *testing.T) { | ||
sortByQuery := "" | ||
sortUsing, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 0, len(sortUsing)) | ||
} | ||
|
||
func TestSortByParserAllowAnyCaseString(t *testing.T) { | ||
sortByQuery := `id:aSc,CReatedaTe:DeSc,NAME:desc` | ||
sortUsing, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 3, len(sortUsing)) | ||
assert.Equal(t, "id", sortUsing[0].Key) | ||
assert.Equal(t, "asc", sortUsing[0].Order) | ||
assert.Equal(t, "createdate", sortUsing[1].Key) | ||
assert.Equal(t, "desc", sortUsing[1].Order) | ||
assert.Equal(t, "name", sortUsing[2].Key) | ||
assert.Equal(t, "desc", sortUsing[2].Order) | ||
} | ||
|
||
func TestSortByParserNotAllowEmptyValueForSoryByString(t *testing.T) { | ||
sortByQuery := "id:,creaTeDate:dEsc,naMe:desc" | ||
_, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Unsupported value provided for sortBy query", err.Error()) | ||
} | ||
|
||
func TestSortByParserAllowCommaAtTheEnd(t *testing.T) { | ||
sortByQuery := `id:asc,createdate:desc,name:desc,` | ||
sortUsing, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 3, len(sortUsing)) | ||
assert.Equal(t, "id", sortUsing[0].Key) | ||
assert.Equal(t, "asc", sortUsing[0].Order) | ||
assert.Equal(t, "createdate", sortUsing[1].Key) | ||
assert.Equal(t, "desc", sortUsing[1].Order) | ||
assert.Equal(t, "name", sortUsing[2].Key) | ||
assert.Equal(t, "desc", sortUsing[2].Order) | ||
} | ||
|
||
func TestPreserveOrderOfSortKeys(t *testing.T) { | ||
sortByQuery := `createdate:desc,id:asc,name:desc,` | ||
|
||
sortUsing, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 3, len(sortUsing)) | ||
assert.Equal(t, "createdate", sortUsing[0].Key) | ||
assert.Equal(t, "desc", sortUsing[0].Order) | ||
assert.Equal(t, "id", sortUsing[1].Key) | ||
assert.Equal(t, "asc", sortUsing[1].Order) | ||
assert.Equal(t, "name", sortUsing[2].Key) | ||
assert.Equal(t, "desc", sortUsing[2].Order) | ||
} | ||
|
||
func TestSortByParserAllowEmptyKeyValue(t *testing.T) { | ||
sortByQuery := "id:desc,creaTeDate:dEsc,naMe:desc,," | ||
_, err := ParseFromQuery(sortByQuery) | ||
|
||
assert.NotNil(t, err) | ||
assert.Equal(t, "Unsupported value provided for sortBy query", err.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.