-
Notifications
You must be signed in to change notification settings - Fork 25
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
handle nil slices correctly #88
Conversation
This is definitely more correct and does what Maybe make this optional? A flag when generating the code and/or a tag on the struct field? |
Yeah, thats a good idea (and what i was worried about) |
31a71ef
to
075d157
Compare
for now i added a |
return xerrors.Errorf("Slice value in field {{ .Name }} was too long") | ||
} | ||
|
||
{{ if .PreserveNil }} |
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.
Given that we now have this tag, do we need to duplicate the array code? We'll only have a compile error if the user intentionally specifies "preservenil" on an array, which, IMO, we should have anyways.
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.
hrm... my concern is that the template conditionals are getting overly complicated. We have the .IsArray check and the .PreserveNil check in the same template and i think theyre overlapping enough to make it really hard to read
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.
Ah, I see. This code is just getting a bit long. But you're right.
return xerrors.Errorf("Slice value in field {{ .Name }} was too long") | ||
} | ||
|
||
{{ if .PreserveNil }} |
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.
Ah, I see. This code is just getting a bit long. But you're right.
gen.go
Outdated
{{ end }} | ||
{{ MajorType "cw" "cbg.MajByteString" (print "len(" .Name ")" ) }} | ||
|
||
if _, err := cw.Write({{ .Name }}[:]); err != 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.
No need to re-slice.
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.
And the "this re-slices" comment above is outdated.
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.
Missed a bug.
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.
Was going to suggest what @Stebalien said. This is technically more correct behaviour but will inevitably break downstreams if applied by default. Struct tag LGTM!
Now that we handle arrays separately.
885450a
to
bc0d20e
Compare
This now distinguishes (and correctly encodes) nil slices from zero length slices.
As a result I also had to separate the handling of array types and slice types intro separate generators, since non-pointer array types are not niladic.