-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: match Query cel function #1317
base: main
Are you sure you want to change the base?
Conversation
133ed5b
to
ca3230b
Compare
ca3230b
to
4436753
Compare
func matchQuery(resourceSelectableRaw map[string]any, peg string) (bool, error) { | ||
var resourceSelectable dutyTypes.ResourceSelectable | ||
|
||
// NOTE: We check for fields in the map to determine what resource to unmarshal to. | ||
|
||
if _, ok := resourceSelectableRaw["config_class"]; ok { | ||
var config models.ConfigItem | ||
if b, err := json.Marshal(resourceSelectableRaw); err != nil { | ||
return false, err | ||
} else if err := json.Unmarshal(b, &config); err != nil { | ||
return false, err | ||
} | ||
|
||
resourceSelectable = config | ||
} else if _, ok := resourceSelectableRaw["category"]; ok { | ||
var playbook models.Playbook | ||
if b, err := json.Marshal(resourceSelectableRaw); err != nil { | ||
return false, err | ||
} else if err := json.Unmarshal(b, &playbook); err != nil { | ||
return false, err | ||
} | ||
|
||
resourceSelectable = &playbook | ||
} else if _, ok := resourceSelectableRaw["topology_id"]; ok { | ||
var component models.Component | ||
if b, err := json.Marshal(resourceSelectableRaw); err != nil { | ||
return false, err | ||
} else if err := json.Unmarshal(b, &component); err != nil { | ||
return false, err | ||
} | ||
|
||
resourceSelectable = component | ||
} else if _, ok := resourceSelectableRaw["canary_id"]; ok { | ||
var check models.Check | ||
if b, err := json.Marshal(resourceSelectableRaw); err != nil { | ||
return false, err | ||
} else if err := json.Unmarshal(b, &check); err != nil { | ||
return false, err | ||
} | ||
|
||
resourceSelectable = check | ||
} | ||
|
||
rs := dutyTypes.ResourceSelector{Search: peg} | ||
return rs.Matches(resourceSelectable), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The matchQuery
function currently returns a nil
resourceSelectable
to rs.Matches()
when no resource type is matched. This could lead to a panic or undefined behavior. Consider adding this check after the if-else chain:
if resourceSelectable == nil {
return false, fmt.Errorf("unknown resource type")
}
Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adityathebe I think we should default back to map if type detection fails
No description provided.