-
Notifications
You must be signed in to change notification settings - Fork 94
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
Avoid pointer redirection for Optional slices in Go #198
base: main
Are you sure you want to change the base?
Avoid pointer redirection for Optional slices in Go #198
Conversation
…lea/typeshare into hculea/dont-redirect-optional-slices
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.
Could we add a unit test to assert this setting works as expected in both cases
@@ -50,6 +50,7 @@ pub struct TypeScriptParams { | |||
pub struct GoParams { | |||
pub package: String, | |||
pub uppercase_acronyms: Vec<String>, | |||
pub no_pointer_slice: bool, |
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 description of the problem in the PR description was very good. Could we copy/paste some of that prose here as a doc comment
In Go, optional values are represented by pointers. This makes sense for most types. For example, using a "*bool" pointer allows for the type to express three values:
nil
,true
orfalse
.In the 1Password SDKs, we expose the types generated by typeshare (e.g. see the types for Go) to be user facing.
Typeshare currently uses a pointer redirection to mark the Go types as optional. While most of the values are translated correctly, using a pointer to a slice (i.e. the translation of
Vec<T>
currently is[]*string
) is not usual in Go, and it leads to more complex code for the users of the SDK.This is because, in Go, slices are already reference types. That is, passing a slice around passes a reference to the underlying data.
However, an edge case can occur:
For type
A
, bothSlice: nil
andSlice: []string{}
have the same JSON serialisation (Slice
is omitted).For type
B
Slice: nil
andSlice: []string{}
both have a different JSON serialisation. In the first case,Slice
is omitted. In the second case the field has the value[]
.This, however, is rarely applicable in practice, and having this feature does not justify exposing an unintuitive user interface. Therefore, this PR is adding a configuration option disabling that for Go.