Avoid pointer redirection for Optional slices in Go #198
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.