-
Notifications
You must be signed in to change notification settings - Fork 40
feat: write failed tuples to stderr #511
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d77218
refactor: print failed tuples in input format
aaguiarz 3a9c857
Merge branch 'main' into codex/implement-fix-for-issue-449
aaguiarz 968579a
chore: added tuples in other formats to help on testing
aaguiarz 9dfe245
Delete a.fga.yml
aaguiarz d73108b
Merge branch 'main' into codex/implement-fix-for-issue-449
aaguiarz 86a01cc
chore: move changelog entry to the proper place
aaguiarz 76c7eda
Merge branch 'main' into codex/implement-fix-for-issue-449
rhamzeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package tuple | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "path" | ||
| "strings" | ||
|
|
||
| "github.com/gocarina/gocsv" | ||
| "github.com/openfga/go-sdk/client" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| const ( | ||
| csvFormat = "csv" | ||
| yamlFormat = "yaml" | ||
| jsonFormat = "json" | ||
| ) | ||
|
|
||
| // tupleCSVDTO represents a tuple in CSV format. | ||
| type tupleCSVDTO struct { | ||
| UserType string `csv:"user_type"` | ||
| UserID string `csv:"user_id"` | ||
| UserRelation string `csv:"user_relation,omitempty"` | ||
| Relation string `csv:"relation"` | ||
| ObjectType string `csv:"object_type"` | ||
| ObjectID string `csv:"object_id"` | ||
| ConditionName string `csv:"condition_name,omitempty"` | ||
| ConditionContext string `csv:"condition_context,omitempty"` | ||
| } | ||
|
|
||
| func tuplesToCSVDTO(tuples []client.ClientTupleKey) ([]tupleCSVDTO, error) { | ||
| result := make([]tupleCSVDTO, 0, len(tuples)) | ||
|
|
||
| for _, tupleKey := range tuples { | ||
| userParts := strings.SplitN(tupleKey.User, ":", 2) | ||
| if len(userParts) != 2 { | ||
| continue | ||
| } | ||
|
|
||
| userType := userParts[0] | ||
| userIDRel := userParts[1] | ||
| userID := userIDRel | ||
| userRel := "" | ||
|
|
||
| if strings.Contains(userIDRel, "#") { | ||
| parts := strings.SplitN(userIDRel, "#", 2) | ||
| userID = parts[0] | ||
| userRel = parts[1] | ||
| } | ||
|
|
||
| objParts := strings.SplitN(tupleKey.Object, ":", 2) | ||
| if len(objParts) != 2 { | ||
| continue | ||
| } | ||
|
|
||
| condName := "" | ||
| condCtx := "" | ||
|
|
||
| if tupleKey.Condition != nil { | ||
| condName = tupleKey.Condition.Name | ||
|
|
||
| if tupleKey.Condition.Context != nil { | ||
| b, err := json.Marshal(tupleKey.Condition.Context) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to convert condition context to CSV: %w", err) | ||
| } | ||
|
|
||
| condCtx = string(b) | ||
| } | ||
| } | ||
|
|
||
| result = append(result, tupleCSVDTO{ | ||
| UserType: userType, | ||
| UserID: userID, | ||
| UserRelation: userRel, | ||
| Relation: tupleKey.Relation, | ||
| ObjectType: objParts[0], | ||
| ObjectID: objParts[1], | ||
| ConditionName: condName, | ||
| ConditionContext: condCtx, | ||
| }) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| func formatFromExtension(fileName string) string { | ||
| switch strings.ToLower(path.Ext(fileName)) { | ||
| case ".csv": | ||
| return csvFormat | ||
| case ".yaml", ".yml": | ||
| return yamlFormat | ||
| default: | ||
| return jsonFormat | ||
| } | ||
| } | ||
|
|
||
| func formatTuples(tuples []client.ClientTupleKey, format string) (string, error) { | ||
| switch format { | ||
| case csvFormat: | ||
| dto, err := tuplesToCSVDTO(tuples) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| b, err := gocsv.MarshalBytes(dto) | ||
|
|
||
| return string(b), err | ||
| case yamlFormat: | ||
| b, err := yaml.Marshal(tuples) | ||
|
|
||
| return string(b), err | ||
| default: | ||
| b, err := json.MarshalIndent(tuples, "", " ") | ||
|
|
||
| return string(b), err | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| user_type,user_id,user_relation,relation,object_type,object_id | ||
| user,anne,,owner,group,foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| - "user": "user:anne" | ||
| "relation": "owner" | ||
| "object": "group:foo" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion
Dropping malformed tuples silently can hide the very tuples a user needs to retry
If
userorobjectdon’t followtype:id[#relation]the write failed, yet we skip the tuple completely when preparing the CSV.A user then loses visibility of why the write failed.
Instead, include the raw tuple in the CSV with blank split fields or fall back to JSON formatting and return an explicit error to the caller.
🤖 Prompt for AI Agents