Skip to content

Commit

Permalink
use a set instead of a list
Browse files Browse the repository at this point in the history
  • Loading branch information
h4ck3rk3y committed Nov 7, 2023
1 parent 3af63fd commit a71c5aa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
16 changes: 5 additions & 11 deletions core/launcher/args/api_container_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ type APIContainerArgs struct {
CloudInstanceID metrics_client.CloudInstanceID `json:"cloud_instance_id"`
}

var skipValidation = []string{"cloud_user_id", "cloud_instance_id"}
var skipValidation = map[string]bool{
"cloud_instance_id": true,
"cloud_user_id": true,
}

func (args *APIContainerArgs) UnmarshalJSON(data []byte) error {
// create a mirror type to avoid unmarshalling infinitely https://stackoverflow.com/questions/52433467/how-to-call-json-unmarshal-inside-unmarshaljson-without-causing-stack-overflow
Expand Down Expand Up @@ -140,7 +143,7 @@ func (args APIContainerArgs) validate() error {
field := reflectValType.Field(i)
jsonFieldName := field.Tag.Get(jsonFieldTag)

if contains(skipValidation, jsonFieldName) {
if _, found := skipValidation[jsonFieldName]; found {
continue
}

Expand All @@ -152,12 +155,3 @@ func (args APIContainerArgs) validate() error {
}
return nil
}

func contains(slice []string, value string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}
16 changes: 5 additions & 11 deletions engine/launcher/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ type EngineServerArgs struct {
CloudInstanceID metrics_client.CloudInstanceID `json:"cloud_instance_id"`
}

var skipValidation = []string{"cloud_user_id", "cloud_instance_id"}
var skipValidation = map[string]bool{
"cloud_instance_id": true,
"cloud_user_id": true,
}

func (args *EngineServerArgs) UnmarshalJSON(data []byte) error {
// create a mirror type to avoid unmarshalling infinitely https://stackoverflow.com/questions/52433467/how-to-call-json-unmarshal-inside-unmarshaljson-without-causing-stack-overflow
Expand Down Expand Up @@ -141,7 +144,7 @@ func (args EngineServerArgs) validate() error {
field := reflectValType.Field(i)
jsonFieldName := field.Tag.Get(jsonFieldTag)

if contains(skipValidation, jsonFieldName) {
if _, found := skipValidation[jsonFieldName]; found {
continue
}

Expand All @@ -153,12 +156,3 @@ func (args EngineServerArgs) validate() error {
}
return nil
}

func contains(slice []string, value string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}

0 comments on commit a71c5aa

Please sign in to comment.