Skip to content

Commit

Permalink
Check illegal characters
Browse files Browse the repository at this point in the history
  • Loading branch information
kavir1698 committed Mar 22, 2024
1 parent 15af396 commit 0793a80
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions datasetIngestor/checkMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func CheckMetadata(client *http.Client, APIServer string, metadatafile string, u
metaDataMap = metadataObj.(map[string]interface{})
beamlineAccount = false

// Check scientificMetadata for illegal keys
if checkIllegalKeys(metaDataMap) {
log.Fatal("Metadata contains keys with illegal characters (., [], or <>).")
}

if user["displayName"] != "ingestor" {
if ownerGroup, ok := metaDataMap["ownerGroup"]; ok {
validOwner := false
Expand Down Expand Up @@ -259,3 +264,43 @@ func CheckMetadata(client *http.Client, APIServer string, metadatafile string, u
return metaDataMap, sourceFolder, beamlineAccount

}

// checkIllegalKeys checks whether the metadata contains illegal keys
func checkIllegalKeys(metadata map[string]interface{}) bool {
for _, item := range metadata {
// Use a type switch to check if the item is a slice of interface{} type, e.g. the scientificMetadata key.
switch v := item.(type) {
case []interface{}:
// If the item is a slice of interface{}, iterate over each subItem in the slice.
for _, subItem := range v {
if subMap, ok := subItem.(map[string]interface{}); ok {
if containsIllegalKeys(subMap) {
return true
}
}
}
}
}
return false
}

// containsIllegalKeys checks whether the key contains illegal characters
func containsIllegalKeys(m map[string]interface{}) bool {
for k := range m {
if containsIllegalCharacters(k) {
return true
}
}
return false
}

func containsIllegalCharacters(s string) bool {
// Check if the string contains periods, brackets, or other illegal characters
// You can adjust this condition based on your specific requirements
for _, char := range s {
if char == '.' || char == '[' || char == ']' || char == '<' || char == '>' {
return true
}
}
return false
}

0 comments on commit 0793a80

Please sign in to comment.