Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

adityathebe
Copy link
Member

No description provided.

@adityathebe adityathebe force-pushed the feat/matchQuery-cel-function branch from 133ed5b to ca3230b Compare February 4, 2025 12:43
@adityathebe adityathebe force-pushed the feat/matchQuery-cel-function branch from ca3230b to 4436753 Compare February 4, 2025 12:44
Comment on lines +51 to +96
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
}
Copy link

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.

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants