-
Notifications
You must be signed in to change notification settings - Fork 85
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
Enable funlen linter (part 2) #1171
Conversation
@rekby Please review. |
internal/stack/record.go
Outdated
return name, fileName | ||
} | ||
|
||
func parseFunctionName(name string) (pkgPath, pkgName, structName, funcName string, lambdas []string) { |
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.
Can you add a unit tests for newest functions?
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.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1171 +/- ##
==========================================
+ Coverage 40.80% 41.08% +0.28%
==========================================
Files 309 310 +1
Lines 32747 32959 +212
==========================================
+ Hits 13361 13540 +179
- Misses 18959 18986 +27
- Partials 427 433 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
Hello, thanks for the work!
it is very good for your work for code style.
I see new part better, for example you add linter skip for one of functions, which really doesn't need to split.
I have some for the before merge it.
internal/decimal/decimal.go
Outdated
*scale-- | ||
} else if !dot { | ||
if *integral == 0 { | ||
remaining += string(c) |
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.
Every addition to string has linear complexity and re-allocate memory.
Use strings.Builder{}
for build the string.
internal/decimal/decimal.go
Outdated
} | ||
|
||
integral := precision - scale | ||
s, err := parseNumber(s, v, &integral, &scale) |
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.
What mean integral
and scale
after return from parseNumber?
Do we need pass the values by pointers?
internal/decimal/decimal.go
Outdated
return syntaxError(s) | ||
} | ||
plus := c > '5' | ||
if !plus && c == '5' { |
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.
no need check plus
, you can check c == '5'
only
internal/decimal/decimal.go
Outdated
return v.Set(nan), nil | ||
func shouldRoundUp(v *big.Int, s string) bool { | ||
var x big.Int | ||
plus := x.And(v, big.NewInt(1)).Cmp(big.NewInt(0)) != 0 |
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.
use one
and zero
variables for prevent new allocation for every function call
internal/stack/record.go
Outdated
return name, fileName | ||
} | ||
|
||
func parseFunctionName(name string) (pkgPath, pkgName, structName, funcName string, lambdas []string) { |
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.
A lot of results, especially with same type is difficult to understand in code and use.
What about return a structure?
retry/retry.go
Outdated
|
||
if err == nil { | ||
return nil | ||
} | ||
|
||
if ctxErr := ctx.Err(); ctxErr != nil { | ||
return xerrors.WithStackTrace( |
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.
no need extract simple error formatting
Thanks, it seems good. I need some time to check PR for logic equals, because it was not only split code to smaller parts, but refactor too. |
Is there any news on this PR? |
Thanks for remind, I will plan to see the PR today. |
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.
Good job, thanks.
See for one additional method for test equals of old and new function while refactoring.
internal/decimal/decimal.go
Outdated
v.Set(inf) | ||
|
||
if c >= '5' { | ||
if c > '5' || shouldRoundUp(v, s[1:]) { |
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.
in prev version it was shouldRoundUp(v, s)
, why che call changed to shouldRoundUp(v, s[1:])
and start check from next symbol?
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.
reverted the change.
return v, nil | ||
} | ||
|
||
func SetSpecialValue(v *big.Int, s string) bool { | ||
neg := s[0] == '-' |
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.
it will be good to use function parseSign here
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.
Used existing function parseSign
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.
Now the function setSpecialValue do many things, instead only parse special values.
Now it:
- Detect negative values
- Cut sign from string
And it has interface with difficult to understand: it change input "v" and return it.
Usually function return new value without change input, rarer change input without return it.
Change and return used for chaining calls within builder - for you can call Obj.Method1().Method2().Method3().... with one line.
please return separate function parseSign
function for detect and cut off sign and reuse the function here and in parseNumber.
Or detect and cut sign before call setSpecialValue
(better parseSpecialValue
) and pass the sign to the function, then to parseNumber
if you won't check the sign twice.
P.S. i think about check the sign twice better - it is very small check, but it more understandable to read, then many parameters.
@@ -95,129 +95,177 @@ func Parse(s string, precision, scale uint32) (*big.Int, error) { | |||
return v, 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.
New version has difference with old version of Parse in some cases.
please add fuzzing test for check equals of old and new Parse function results:
func FuzzParse(f *testing.F) {
f.Fuzz(func(t *testing.T, s string, precision, scale uint32) {
expectedRes, expectedErr := oldParse(s, precision, scale)
res, err := Parse(s, precision, scale)
if expectedErr == nil {
require.Equal(t, expectedRes, res)
} else {
require.Error(t, err)
}
})
}
Where oldParse
is copy of Parse
from main branch.
Now result has differences at arguments "100", 0, 0.
You can past oldParse
near the test in decimal_test.go
, I will remove it from the branch after merge.
You can add better fuzz-test for better test long numbers instead of random strings only.
You can run fuzzing by command:
go test -fuzz=FuzzParse
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.
added the provided test function FuzzParse and oldParse function to decimal_test.go
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.
Test FuzzParse is failed.
=== RUN FuzzParse
--- FAIL: FuzzParse (27.30s)
=== RUN FuzzParse/1279e0a916766c26
decimal_test.go:366:
Error Trace: /Users/rekby/projects/yandex/ydb-go-sdk-workspace/ydb-go-sdk/internal/decimal/decimal_test.go:366
/Users/rekby/gosdk/go1.22.0/src/reflect/value.go:596
/Users/rekby/gosdk/go1.22.0/src/reflect/value.go:380
/Users/rekby/gosdk/go1.22.0/src/testing/fuzz.go:335
Error: Not equal:
expected: 100000000000000000000000000000000000
actual : 0
Diff:
--- Expected
+++ Actual
@@ -2,6 +2,3 @@
neg: (bool) false,
- abs: (big.nat) (len=2) {
- (big.Word) 3136633892082024448,
- (big.Word) 5421010862427522
- }
+ abs: (big.nat) <nil>
})
Test: FuzzParse/1279e0a916766c26
--- FAIL: FuzzParse/1279e0a916766c26 (27.30s)
Expected :100000000000000000000000000000000000
Actual :0
With same input as in my previous comment:
go test fuzz v1
string("100")
uint32(0)
uint32(0)
It mean that result of old and new functions have differences for string "100", zero scale and zero precision.
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.
Hello, good work, thanks!
I don't found errors now.
It need only small cosmetic fix and resolve conflicts before merge
return v, nil | ||
} | ||
|
||
func SetSpecialValue(v *big.Int, s string) bool { | ||
neg := s[0] == '-' |
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.
Now the function setSpecialValue do many things, instead only parse special values.
Now it:
- Detect negative values
- Cut sign from string
And it has interface with difficult to understand: it change input "v" and return it.
Usually function return new value without change input, rarer change input without return it.
Change and return used for chaining calls within builder - for you can call Obj.Method1().Method2().Method3().... with one line.
please return separate function parseSign
function for detect and cut off sign and reuse the function here and in parseNumber.
Or detect and cut sign before call setSpecialValue
(better parseSpecialValue
) and pass the sign to the function, then to parseNumber
if you won't check the sign twice.
P.S. i think about check the sign twice better - it is very small check, but it more understandable to read, then many parameters.
I'm not quite sure how to resolve the particular merge conflict. |
You can't merge it "automatically", because you have refactored the code. |
@rekby please review now. |
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.
Thanks for great work!
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.
Attention:
I have pushed commit to your branch for resolve merge conflict with master. Pull the branch before work with the code.
internal/stack/record.go
Outdated
if i := strings.LastIndex(file, "/"); i > -1 { | ||
file = file[i+1:] | ||
fileName = file[i+1:] | ||
} else { |
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.
Once more point:
record.go was 100% coveraged before refactor and 98.8% after.
One branch of the code not tested: point when file
doesn't contain /
.
Add test for this please and PR will be merge.
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 fixed the issue by replace the code by function from standard library - for extrace file name from path.
I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en
Pull request type
Please check the type of change your PR introduces:
What is the current behavior?
Issue Number: 934
What is the new behavior?
Other information