Skip to content

resource/cmd: support comparison operators - #418

Open
Omkar-Ugal wants to merge 1 commit into
unikraft-cloud:prod-stagingfrom
Omkar-Ugal:feat/filter-comparison-374
Open

resource/cmd: support comparison operators#418
Omkar-Ugal wants to merge 1 commit into
unikraft-cloud:prod-stagingfrom
Omkar-Ugal:feat/filter-comparison-374

Conversation

@Omkar-Ugal

@Omkar-Ugal Omkar-Ugal commented Jul 11, 2026

Copy link
Copy Markdown

adds >, <, >=, <= support in --filter (e.g --filter "resources.memory>128mib").

Tested manually against live instances, volumes, and services - memory, vcpus, size, limits all work with all four operators.

Closes #374.
Requires unikraft-cloud/x#307.

@jedevc jedevc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looooks neat! Sorry for the delay again (I was on holiday 🌴).

Comment thread internal/resource/cmd/cmd.go Outdated
Comment on lines +418 to +422
// fieldAdaptor implements filters.Adaptor over a resource's fields, using
// resource.GetFieldByPath to resolve field paths, and exposing raw typed
// values so comparison operators (>, <, >=, <=) can work against native
// types (e.g. memory>128mib, created_at>2026-01-01).
type fieldAdaptor struct {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move all this out into filter.go since it's now more complex.

Comment thread internal/resource/cmd/cmd.go Outdated
Comment on lines 415 to 416
// newFieldAdaptor creates a filters.Adaptor that can traverse resource fields.
// It handles both structured fields (with subfields) and slice values (like []string tags).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dangling comment.

Comment thread internal/resource/cmd/cmd.go
Comment thread internal/resource/cmd/cmd.go Outdated
Comment on lines +482 to +485
if _, _, _, _, ok := a.resolve(full); !ok {
return nil, false
}
return &fieldAdaptor{fields: a.fields, prefix: full}, true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of storing the prefix, couldn't we store the whole field?

Comment thread internal/resource/cmd/cmd.go Outdated
return full
}

func (a *fieldAdaptor) resolve(path []string) (field *resource.Field, entries []string, sliceVal string, sliceOK bool, ok bool) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return a whole fieldAdaptor here I think. This would simplify the code, and avoid so many return values.

Then we also don't need to keep calling resolve over and over, and means we should be able to avoid storing the prefix.

Comment thread internal/resource/cmd/cmd.go Outdated
return value.Compare(field.Value, parsed), true
}

func hasOrdering(v any) bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think this should probably be opt-in not opt-out.

E.g. currently this would include arrays/structs/etc. Which aren't ordered.

Comment thread internal/resource/cmd/cmd.go Outdated
Comment on lines +539 to +543
parsed, err := value.ParseNew([]string{other}, field.Value)
if err != nil {
return 0, false
}
return value.Compare(field.Value, parsed), true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, worth checking to see how this handles comparing a float field to an integer result - e.g. 1.2<5. Or something like that (something to check works in the tests - it should work as is).

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds typed comparison support for resource filters, addressing #374.

Changes:

  • Implements >, <, >=, and <= field comparisons.
  • Preserves nested and slice field traversal.
  • Refreshes related dependencies and removes obsolete replacements.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.

File Description
internal/resource/cmd/cmd.go Adds the typed filter adaptor and comparison logic.
go.mod Updates dependencies and removes replacements.
go.sum Records updated dependency checksums.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/resource/cmd/cmd.go Outdated
Comment on lines +428 to +429
func newFieldAdaptor(fields []resource.Field) filters.Adaptor {
return &fieldAdaptor{fields: fields}
Comment thread internal/resource/cmd/cmd.go Outdated
Comment on lines +539 to +543
parsed, err := value.ParseNew([]string{other}, field.Value)
if err != nil {
return 0, false
}
return value.Compare(field.Value, parsed), true
Comment thread internal/resource/cmd/cmd.go Outdated
// fieldAdaptor implements filters.Adaptor over a resource's fields, using
// resource.GetFieldByPath to resolve field paths, and exposing raw typed
// values so comparison operators (>, <, >=, <=) can work against native
// types (e.g. memory>128mib, created_at>2026-01-01).
@Omkar-Ugal
Omkar-Ugal force-pushed the feat/filter-comparison-374 branch from 345a785 to 5fca19b Compare July 30, 2026 21:14
@Omkar-Ugal
Omkar-Ugal requested a review from jedevc July 30, 2026 21:25
Signed-off-by: Omkar Ugalmugle <omkarugalmugle285@gmail.com>
@Omkar-Ugal
Omkar-Ugal force-pushed the feat/filter-comparison-374 branch from 5fca19b to a2acf83 Compare July 31, 2026 11:26
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.

Support --filters with < and >

3 participants