-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Key List Validator for KeyValueList arguments validation
- Loading branch information
1 parent
37006ef
commit 2a2f7cf
Showing
6 changed files
with
140 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project. | ||
// The CB-Spider Mission is to connect all the clouds with a single interface. | ||
// | ||
// * Cloud-Barista: https://github.com/cloud-barista | ||
// | ||
// by CB-Spider Team, 2021.11. | ||
|
||
package cloudos | ||
|
||
import ( | ||
"fmt" | ||
icbs "github.com/cloud-barista/cb-store/interfaces" | ||
) | ||
|
||
|
||
// (1) Remove list of valid keys with list of inpput keys | ||
// (2) Check the list of remaining Keys | ||
func ValidateKeyValueList(inKeyValueList []icbs.KeyValue, validKeyList []string) error { | ||
|
||
clonedKeyList := cloneSlice(validKeyList) | ||
|
||
// (1) Remove list of valid keys with list of inpput keys | ||
inputKeyList := make([]string, len(inKeyValueList)) | ||
for idx, kv := range inKeyValueList { | ||
inputKeyList[idx] = kv.Key | ||
for _, key := range clonedKeyList { | ||
if kv.Key == key { | ||
clonedKeyList = removeSlice(clonedKeyList, key) | ||
} | ||
} | ||
} | ||
// (2) Check the list of remaining Keys | ||
if len(clonedKeyList) == 0 { | ||
return nil | ||
} else { | ||
errMSG := fmt.Sprintf("Invalid Key in input arguments.\n\t...... have %v\n\t...... want %v", inputKeyList, validKeyList) | ||
return fmt.Errorf(errMSG) | ||
} | ||
} | ||
|
||
func removeSlice(inSlice []string, deleteValue string) []string { | ||
for idx, v := range inSlice { | ||
if v == deleteValue { | ||
inSlice = append(inSlice[:idx], inSlice[idx+1:]...) | ||
return inSlice | ||
} | ||
} | ||
return inSlice | ||
} | ||
|
||
func cloneSlice(inSlice []string) []string { | ||
clonedSlice := make([]string, len(inSlice)) | ||
copy(clonedSlice, inSlice) | ||
return clonedSlice | ||
} |
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
File renamed without changes.
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,48 @@ | ||
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project. | ||
// The CB-Spider Mission is to connect all the clouds with a single interface. | ||
// | ||
// * Cloud-Barista: https://github.com/cloud-barista | ||
// | ||
// by CB-Spider Team, 2021.11. | ||
|
||
package validatetest | ||
|
||
import ( | ||
cim "github.com/cloud-barista/cb-spider/cloud-info-manager" | ||
|
||
icbs "github.com/cloud-barista/cb-store/interfaces" | ||
"testing" | ||
"log" | ||
) | ||
|
||
func TestValid(t *testing.T) { | ||
inKeyValueList := []icbs.KeyValue{ | ||
{"location", "kr"}, | ||
{"ResourceGroup", "barista"}, | ||
} | ||
wantedKeyList := []string { | ||
"location", | ||
"ResourceGroup", | ||
} | ||
err := cim.ValidateKeyValueList(inKeyValueList, wantedKeyList) | ||
if err != nil { | ||
log.Fatal("something failed!") | ||
} | ||
} | ||
|
||
|
||
func TestInvalid(t *testing.T) { | ||
inKeyValueList := []icbs.KeyValue{ | ||
{"Location", "kr"}, | ||
{"ResourceGroup", "barista"}, | ||
} | ||
wantedKeyList := []string { | ||
"location", | ||
"ResourceGroup", | ||
} | ||
err := cim.ValidateKeyValueList(inKeyValueList, wantedKeyList) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|