-
Notifications
You must be signed in to change notification settings - Fork 43
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
refactor: GraphQL order input #3044
Changes from 1 commit
f0c625d
550c005
1224f1e
3ae8689
00415a6
497160c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package parser | ||
|
||
import ( | ||
"github.com/sourcenetwork/defradb/client/request" | ||
) | ||
|
||
func parseOrderConditionList(args []any) ([]request.OrderCondition, error) { | ||
var conditions []request.OrderCondition | ||
for _, a := range args { | ||
v, ok := a.(map[string]any) | ||
if !ok { | ||
continue // order value is nil | ||
} | ||
condition, err := parseOrderCondition(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if condition != nil { | ||
conditions = append(conditions, *condition) | ||
} | ||
} | ||
return conditions, nil | ||
} | ||
|
||
func parseOrderCondition(arg map[string]any) (*request.OrderCondition, error) { | ||
if len(arg) == 0 { | ||
return nil, nil | ||
} | ||
if len(arg) != 1 { | ||
return nil, ErrMultipleOrderFieldsDefined | ||
} | ||
var fieldName string | ||
for name := range arg { | ||
fieldName = name | ||
} | ||
switch t := arg[fieldName].(type) { | ||
case int: | ||
dir, err := parseOrderDirection(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &request.OrderCondition{ | ||
Fields: []string{fieldName}, | ||
Direction: dir, | ||
}, nil | ||
|
||
case map[string]any: | ||
cond, err := parseOrderCondition(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if cond == nil { | ||
return nil, nil | ||
} | ||
// prepend the current field name, to the parsed condition from the slice | ||
// Eg. order: {author: {name: ASC, birthday: DESC}} | ||
// This results in an array of [name, birthday] converted to | ||
// [author.name, author.birthday]. | ||
// etc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: The order object in this comment seems invalid as it's an unordered map intending for name and birthday to be in an explicit order. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would have to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch! fixed a few others as well |
||
cond.Fields = append([]string{fieldName}, cond.Fields...) | ||
return cond, nil | ||
|
||
default: | ||
// field value is null so don't include the condition | ||
return nil, nil | ||
} | ||
} | ||
|
||
func parseOrderDirection(v int) (request.OrderDirection, error) { | ||
switch v { | ||
case 0: | ||
return request.ASC, nil | ||
|
||
case 1: | ||
return request.DESC, nil | ||
|
||
default: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: please add a test that satisfies this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case is technically unreachable because the GQL parsing will fail. I've added a test to demonstrate this. I felt it was better to keep the default case instead of having it undefined, but I can remove it if you have a different preference. |
||
return request.ASC, ErrInvalidOrderDirection | ||
} | ||
} |
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.
todo: Please add a test for the case where we have more than one argument
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.
done