-
Notifications
You must be signed in to change notification settings - Fork 14
Fix/game pgn #19
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
Fix/game pgn #19
Conversation
|
Warning Rate limit exceeded@CorentinGS has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 4 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (2)
WalkthroughThe changes refactor the game's output logic by updating the Changes
Sequence Diagram(s)sequenceDiagram
participant Game
participant Builder
participant MoveFormatter
Game->>Builder: Initialize strings.Builder
Game->>Builder: Append tag pairs
alt Moves exist?
Game->>MoveFormatter: Call writeMoves() with moves, comments, variations
MoveFormatter-->>Game: Return formatted moves string
Game->>Builder: Append moves string
end
Builder-->>Game: Return complete PGN string via String()
Assessment against linked issues
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (4)
game.go (3)
358-444: Consider refactoring for lower cognitive complexity.Static analysis flagged the
writeMovesfunction with a cognitive complexity of 24, exceeding recommended thresholds. Extracting tasks into smaller helper functions (e.g., handling comments, variations, and sub-variation logic) can improve readability and maintainability without losing clarity.🧰 Tools
🪛 golangci-lint (1.62.2)
360-360: cognitive complexity 24 of func
writeMovesis high (> 20)(gocognit)
401-401: S1009: should omit nil check; len() for map[string]string is defined as zero
(gosimple)
401-401: Remove redundant nil-check for command.In Go, calling
len(nilMap)returns 0, so explicitly checkingcurrentMove.command != nilbeforelen(currentMove.command) > 0is unnecessary. Removing the extra check simplifies the logic.-if currentMove.command != nil && len(currentMove.command) > 0 { +if len(currentMove.command) > 0 {🧰 Tools
🪛 golangci-lint (1.62.2)
401-401: S1009: should omit nil check; len() for map[string]string is defined as zero
(gosimple)
452-463: Validate partial PGN data.
UnmarshalTextcorrectly constructs a reader and callsPGN. Consider expanding negative tests (e.g., invalid PGN tokens) to ensure robust error handling. This function already returns errors fromPGN(r), so coverage looks sufficient, but you can add more edge-case tests if the parser is prone to partial read edge cases.game_test.go (1)
1015-1176: Improve subtest naming for clarity.The subtests "GameStringWithDifferentResults" appear thrice (lines 1082, 1091, and 1100). Having identical names can confuse some test runners or hamper debugging. Rename them to distinguish White/Black/Draw results.
Otherwise, these tests provide comprehensive coverage of the PGN rendering logic and are highly commendable.
- name: "GameStringWithDifferentResults", + name: "GameStringWithWhiteWinResult", - name: "GameStringWithDifferentResults", + name: "GameStringWithBlackWinResult", - name: "GameStringWithDifferentResults", + name: "GameStringWithDrawResult",
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
game.go(3 hunks)game_test.go(1 hunks)move.go(1 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
game.go
360-360: cognitive complexity 24 of func writeMoves is high (> 20)
(gocognit)
401-401: S1009: should omit nil check; len() for map[string]string is defined as zero
(gosimple)
🔇 Additional comments (3)
game.go (2)
25-29: No issues with the added imports.
337-356: Add fallback for empty move list.The method gracefully handles the scenario where
rootMovehas no children by simply appending the outcome string. However, ifrootMoveis nil (in a pathological case) or incorrectly initialized, you might risk a panic when accessinglen(g.rootMove.children). Consider adding a fallback so that an uninitializedrootMoveyields just the outcome string.move.go (1)
75-77: Properly initialize command map.This ensures you never write to a nil map, preventing runtime panics. If concurrency is a concern, consider using synchronization or concurrent-safe data structures since multiple goroutines could modify
commandsimultaneously.
Docstrings generation was requested by @CorentinGS. * #19 (comment) The following files were modified: * `game.go`
|
Note Generated docstrings for this pull request at #20 |
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
game.go (2)
358-444: Consider breaking down the complex writeMoves function.While the implementation is correct, the function has high cognitive complexity (23). Consider extracting the following into helper functions to improve readability and maintainability:
- Move encoding logic (lines 385-394)
- Comment handling (lines 397-407)
- Variation handling (lines 419-428)
Example refactor:
func writeMoves(node *Move, moveNum int, isWhite bool, sb *strings.Builder, subVariation bool) { if node == nil { return } var currentMove *Move if subVariation { currentMove = node } else { if len(node.children) == 0 { return } currentMove = node.children[0] } writeMoveNumber(moveNum, isWhite, subVariation, sb) - // Encode the move using your AlgebraicNotation. - if node.Parent() == nil { - sb.WriteString(AlgebraicNotation{}.Encode(node.Position(), currentMove)) - } else { - moveStr := AlgebraicNotation{}.Encode(node.Parent().Position(), currentMove) - sb.WriteString(moveStr) - } + writeMoveEncoding(node, currentMove, sb) - // Append a comment if present. - if currentMove.comments != "" { - sb.WriteString(" {" + currentMove.comments + "}") - } + writeComments(currentMove, sb) - if len(currentMove.command) > 0 { - sb.WriteString(" {") - for key, value := range currentMove.command { - sb.WriteString(" [%" + key + " " + value + "]") - } - sb.WriteString(" }") - } + writeCommands(currentMove, sb) if !subVariation { sb.WriteString(" ") } else if len(currentMove.children) > 0 { sb.WriteString(" ") } - // Process any variations - if len(node.children) > 1 { - for i := 1; i < len(node.children); i++ { - variation := node.children[i] - sb.WriteString("(") - writeMoves(variation, moveNum, isWhite, sb, true) - sb.WriteString(") ") - } - } + writeVariations(node, moveNum, isWhite, sb) if len(currentMove.children) > 0 { var nextMoveNum int var nextIsWhite bool if isWhite { nextMoveNum = moveNum nextIsWhite = false } else { nextMoveNum = moveNum + 1 nextIsWhite = true } writeMoves(currentMove, nextMoveNum, nextIsWhite, sb, false) } } +func writeMoveNumber(moveNum int, isWhite bool, subVariation bool, sb *strings.Builder) { + if isWhite { + sb.WriteString(fmt.Sprintf("%d. ", moveNum)) + } else if subVariation { + sb.WriteString(fmt.Sprintf("%d... ", moveNum)) + } +} + +func writeMoveEncoding(node *Move, currentMove *Move, sb *strings.Builder) { + if node.Parent() == nil { + sb.WriteString(AlgebraicNotation{}.Encode(node.Position(), currentMove)) + } else { + moveStr := AlgebraicNotation{}.Encode(node.Parent().Position(), currentMove) + sb.WriteString(moveStr) + } +} + +func writeComments(move *Move, sb *strings.Builder) { + if move.comments != "" { + sb.WriteString(" {" + move.comments + "}") + } +} + +func writeCommands(move *Move, sb *strings.Builder) { + if len(move.command) > 0 { + sb.WriteString(" {") + for key, value := range move.command { + sb.WriteString(" [%" + key + " " + value + "]") + } + sb.WriteString(" }") + } +} + +func writeVariations(node *Move, moveNum int, isWhite bool, sb *strings.Builder) { + if len(node.children) > 1 { + for i := 1; i < len(node.children); i++ { + variation := node.children[i] + sb.WriteString("(") + writeMoves(variation, moveNum, isWhite, sb, true) + sb.WriteString(") ") + } + } +}🧰 Tools
🪛 golangci-lint (1.62.2)
360-360: cognitive complexity 23 of func
writeMovesis high (> 20)(gocognit)
409-409: Track the TODO for Nags support.The comment indicates that Nags notation support is incomplete. Would you like me to create an issue to track this enhancement?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
game.go(3 hunks)game_test.go(1 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
game.go
360-360: cognitive complexity 23 of func writeMoves is high (> 20)
(gocognit)
🔇 Additional comments (3)
game.go (2)
336-356: Well-structured PGN output implementation!The String() method efficiently uses strings.Builder to generate a proper PGN representation, correctly handling tag pairs, moves, and game outcome.
454-464: Clean and correct UnmarshalText implementation!The implementation properly handles PGN parsing and error propagation.
game_test.go (1)
1015-1176: Excellent test coverage for PGN output!The test suite is comprehensive and well-structured, covering all important aspects of PGN notation:
- Basic game states
- Move notation
- Comments and variations
- Tag pairs
- Game outcomes
- Complex scenarios with nested variations
Docstrings generation was requested by @CorentinGS. * #19 (comment) The following files were modified: * `game.go` Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Corentin Giaufer Saubert <[email protected]>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #19 +/- ##
==========================================
+ Coverage 73.96% 74.35% +0.38%
==========================================
Files 27 27
Lines 5078 5186 +108
==========================================
+ Hits 3756 3856 +100
- Misses 1188 1194 +6
- Partials 134 136 +2 ☔ View full report in Codecov by Sentry. |
…larity # Conflicts: # game.go
This PR should resolve #18 but needs more work to support Nags notation
Summary by CodeRabbit
New Features
Bug Fixes
Tests