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

allow filtering with array of contract states #920

Merged
merged 3 commits into from
Apr 21, 2024

Conversation

Omarabdul3ziz
Copy link
Contributor

Description

allow filtering with array of contract states

Changes

  • register a custome Encoder/Decoder for in the schema pkg for []string param
  • change the type of ContractFilter to be []string
  • modify the sql query/ mock tests to check the existing of the state in an array instead of exact equal

Related Issues

Checklist

  • Tests included
  • Build pass
  • Documentation
  • Code format and docstring

- register a custome Encoder/Decoder for in the schema pkg for []string
  param
- change the type of ContractFilter to be []string
- modify the sql query/ mock tests to check the existing of the state in
  an array instead of exact equal
Copy link
Collaborator

@AbdelrahmanElawady AbdelrahmanElawady left a comment

Choose a reason for hiding this comment

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

Looks good, just some questions

Comment on lines 27 to 30
if value.Kind() == reflect.Slice && value.Type().Elem().Kind() == reflect.String {
slice := value.Interface().([]string)
return strings.Join(slice, ",")
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am curious why we are checking value type and its elements type, doesn't the first value in RegisterEncoder guarantee only []string will be passed?
Also, wouldn't something like slice, ok := slice := value.Interface().([]string) work instead of the multiple reflect calls?

@@ -96,7 +97,7 @@ func (c *RentContract) satisfies(f types.ContractFilter) bool {
return false
}

if f.State != nil && *f.State != c.State {
if f.State != nil && len(f.State) != 0 && !containsState(f.State, c.State) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think neither f.State != nil or len(f.State) != 0 is needed, containsState should be enough

@@ -783,8 +783,13 @@ func (d *PostgresDatabase) GetContracts(ctx context.Context, filter types.Contra
if filter.Type != nil {
q = q.Where("type = ?", *filter.Type)
}
if filter.State != nil {
q = q.Where("state ILIKE ?", *filter.State)
if filter.State != nil && len(filter.State) != 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

no need to check for nil len works fine with nil

@Omarabdul3ziz Omarabdul3ziz merged commit db59524 into development Apr 21, 2024
39 checks passed
@Omarabdul3ziz Omarabdul3ziz deleted the development_proxy_array_filtering branch April 21, 2024 08:38
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