-
Notifications
You must be signed in to change notification settings - Fork 23
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 slice pointer dive tag #43
Conversation
Is this also true if the slice is not a pointer to a slice? I ask because you should never, except in some wild edge cases, ever hav a pointer to a slice. |
No. I generate some codes using oapi-codegen. components:
schemas:
FooResponse:
type: object
required:
- id
properties:
id:
type: string
attributes:
type: array
items:
$ref: "#/components/schemas/Attribute"
x-oapi-codegen-extra-tags:
mold: dive type FooResponse struct {
ID string `json:"id"`
Attributes *[]Attribute `json:"attributes,omitempty"` `mold:"dive"`
}
OK, I will try to address this with some sort of work around. Thank you. |
OK do what you must @zaneli , but seems like a bit of an overreaction. I didn’t say I wasn’t interested in fixing or handling this unusual case but was rather gathering more information to understand the problem in more detail. As it turns out you’re not in control of the type being generated by oapi-codegen is the reason why it’s a pointer to a slice(which the codegen is incorrectly generating because slice is already a smart pointer and so already nil by default). Now that I have this additional information I can evaluate your proposed fix and think about/ensure it’s not a breaking change or if theres even a better/different way to handle also. Additionally there are existing workarounds in the meantime, such as using the 'default' modifier before the dive tag to create the empty array when nothing is there, which should work.. but haven’t tested yet. |
Thanks for your kind response, it is much appreciated.
Unfortunately it doesn't seem to work fine now. After creating an empty array, the |
Fwiw I will be trying to look at it this weekend barring any RL(real life) interrupts :) |
@zaneli would you be able to share how you initialize the Asking because after doing some preliminary testing to reproduce I was unable to using |
This comment was marked as resolved.
This comment was marked as resolved.
I also realized that creating a default empty array does not fit my needs. {"id":"id1"} {"id":"id1","attributes":[]} |
- Added new default & set types for `chan`, `map`, `slice`, `time.Time` and `Pointer` types. - Updated to handle pointer to a `Slice` or `Map` for situations where code is not under your control. Fixes #43
@zaneli PR #44 should address both. You should now be able to set a default, if that's what is desired and I've also added handling of nil pointer to The change differed to keep usage safety, the original PR would have allowed this to pass without error where the new changes won't let the invalid usage pass: type Test struct {
S *string `mod:"dive"`
} Let me know if this fully resolves your use case :) |
Thank you for the correction. |
When
orig
is pointer, not only!current.CanAddr()
but alsoelse
modifyorig
, so callt.extractType(orig)
both cases.If a value is set from nil,
kind
is reassigned from ptr to slice, map.And I would prefer to allow the following cases.