-
Notifications
You must be signed in to change notification settings - Fork 179
Correctly validate the nil check for interfaces. #14
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
Conversation
|
We totally missed that Go have typed nils.. Great catch on this @vijtrip2 ! but i still think that we should avoid using IMO it would be better to solve this issue at the "delta code generation" level, by generating nil checking functions for each type, and implementating nil checking functions for built-in types in the Also there is a change that Go 1.17 will bring generics :) which can be a very good solution for this issue. |
|
A small experiment to compare the performance of package main_test
import (
"reflect"
"testing"
)
func isNilReflect(i interface{}) bool { return reflect.ValueOf(i).IsNil() }
func isNilEqualOp(s *string) bool { return s == nil }
func Benchmark_reflection(b *testing.B) {
var s *string
v := interface{}(s)
for i := 0; i < b.N; i++ {
isNilReflect(v)
}
}
func Benchmark_equalOp(b *testing.B) {
var s *string
for i := 0; i < b.N; i++ {
isNilEqualOp(s)
}
} |
a-hilaly
left a comment
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.
The benchmark results are not very concerning i believe, also i just found out that reflect.ValueOf doesn't do any memory allocations. I'm Ok with merging this 👍 and seeing the mem/cpu profiling results in the soaking tests
| func isNotNil(i interface{}) bool { | ||
| return !isNil(i) | ||
| } |
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.
is this function really necessary? :)
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 thought it read well this way. I personally do not like logical operators being consecutive to each other
like && !isNil()
Saying that I also do not have strong preference to keep it this way.
jaypipes
left a comment
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.
Funnily enough, I'd originally written the pkg/compare/nil.go code to have a set of HasNilStringDifference, HasNilIntDifference, etc functions one for each type and then combined them all into this HasNilDifference method when I realized that other than the function parameter types, the function implementation was identical :)
Seems I should have stuck to that original idea!
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: A-Hilaly, jaypipes, RedbackThomson, vijtrip2 The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
I found this bug while testing newly generated apigatewayv2 controller where the update code path was not triggered because no delta was being reported for an updated api resource.
The root cause was that string
pointerswere not being checked correctly for nil value.To perform nil check on interfaces, only doing
i == nilis not sufficient as pointed out in