-
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
Mr2011/issue 310/im ordering #438
base: main
Are you sure you want to change the base?
Conversation
type Order struct { | ||
By DbColumnName | ||
Direction OrderDirection | ||
} | ||
|
||
func CreateOrderMap(order []Order) map[DbColumnName]OrderDirection { | ||
m := map[DbColumnName]OrderDirection{} | ||
for _, o := range order { | ||
m[o.By] = o.Direction | ||
} | ||
return m | ||
} | ||
|
||
func CreateOrderString(order []Order) string { | ||
orderStr := "" | ||
for i, o := range order { | ||
if i > 0 { | ||
orderStr = fmt.Sprintf("%s, %s %s", orderStr, o.By, o.Direction) | ||
} else { | ||
orderStr = fmt.Sprintf("%s %s %s", orderStr, o.By, o.Direction) | ||
} | ||
} | ||
return orderStr | ||
} |
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.
Since "common.go" is already full with stuff I'd suggest putting anything Order related into it's own file (as a logical separation).
I still think having in general more (small) files is better than having a "common.go".
func EncodeCursor(order []Order, opts ...NewCursor) (string, error) { | ||
var cursors cursors | ||
for _, opt := range opts { | ||
err := opt(&cursors) | ||
if err != nil { | ||
fmt.Println("err") | ||
return "", err | ||
} | ||
} | ||
|
||
m := CreateOrderMap(order) | ||
for _, f := range cursors.fields { | ||
if orderDirection, ok := m[f.Name]; ok { | ||
f.Order = orderDirection | ||
} | ||
} | ||
|
||
var buf bytes.Buffer | ||
encoder := base64.NewEncoder(base64.StdEncoding, &buf) | ||
err := json.NewEncoder(encoder).Encode(cursors.fields) | ||
if err != nil { | ||
return "", err | ||
} | ||
encoder.Close() | ||
return buf.String(), nil | ||
} | ||
|
||
func DecodeCursor(cursor string) ([]Field, error) { | ||
decoded, err := base64.StdEncoding.DecodeString(cursor) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode base64 string: %w", err) | ||
} | ||
|
||
var fields []Field | ||
if err := json.Unmarshal(decoded, &fields); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err) | ||
} | ||
|
||
return fields, nil | ||
} | ||
|
||
func WithIssueMatch(im IssueMatch) NewCursor { | ||
return func(cursors *cursors) error { | ||
cursors.fields = append(cursors.fields, Field{Name: IssueMatchId, Value: im.Id, Order: OrderDirectionAsc}) | ||
// cursors.fields = append(cursors.fields, Field{Name: IssueMatchRating, Value: im.Rating, Order: OrderDirectionAsc}) | ||
return 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.
I couldn't figure out how these 2 functions are used. But I guess the implementions is not complete.
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.
Yes, the Cursor is not fully implemented yet. This code might change once I start the implementation
This PR implements the ordering for IssueMatch by:
Example query can be found in
internal/api/graphql/graph/queryCollection/issueMatch/withOrder.graphql
Cursor not yet implemented.