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

Truncate too long output instead of using key #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion analyzer/testdata/src/extra/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,6 @@ func testYodaExpr() {
if nil != clusterContext.PostInstallData.CoreDNSUpdateFunction { // want `\Qsuggestion: clusterContext.PostInstallData.CoreDNSUpdateFunction != nil`
}
// This is far too long, so it's shortened in the output.
if nil != clusterContext.PostInstallData.AnotherNestedStruct.DeeplyNestedField { // want `\Qsuggestion: $s != nil`
if nil != clusterContext.PostInstallData.AnotherNestedStruct.DeeplyNestedField { // want `\Qsuggestion: clusterContext.PostInstallData.AnotherNestedStruct.DeeplyNes... != nil`
}
}
2 changes: 1 addition & 1 deletion ruleguard/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (rr *rulesRunner) renderMessage(msg string, n ast.Node, nodes map[string]as
// Don't interpolate strings that are too long.
var replacement string
if truncate && buf.Len() > 60 {
replacement = key
replacement = string([]rune(buf.String())[:60]) + "..."
Copy link
Owner

Choose a reason for hiding this comment

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

Since we're using the value of 60 here twice, maybe make it a local const? It would be easier to find the connection between these two later.

Copy link
Owner

@quasilyte quasilyte Aug 23, 2020

Choose a reason for hiding this comment

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

Another thing is that we're checking for 60 bytes length but then we truncate it to the 60 runes.
We either need to check for the "width" (or runes count) in the first condition or slice the string itself instead of doing string->[]rune->string.

This example demonstrates that the current solution might panic:
https://play.golang.org/p/8WCl054GxLP

} else {
replacement = buf.String()
}
Expand Down