Skip to content
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

Scalar Value Types Problems repeated string --> why not []*string pass by reference #450

Closed
shiywang opened this issue Oct 30, 2017 · 2 comments

Comments

@shiywang
Copy link

according to scalar value types in golang

.proto Type                Golang Type
string               -->      *string

But why repeated string --> []string instead of repeated string --> []*string ?

My user case is here:
I'm using reflect to set value to Message struct, roughly like this:

v := reflect.ValueOf(msg).Elem()
switch v.Field(i).Interface().(type) {
         case *bool:
                      var tmpBool bool
                      cmd.Flags().BoolVarP(&tmpBool, name, shorthand, bVal, usage) 
		      v.Field(i).Set(reflect.ValueOf(&tmpBool))
         case []string:
                      var tmpSliceString []string
		      cmd.Flags().StringSliceVarP(&tmpSliceString, name, shorthand, defaultValue, usage)
                      v.Field(i).Set(reflect.ValueOf(tmpSliceString))   //set the SliceFlag here
...


My message struct is below:
type Test struct {
	BoolFlag         *bool    `protobuf:"varint,1,opt,name=BoolFlag" json:"BoolFlag,omitempty"`
	StringFlag       *string  `protobuf:"bytes,2,opt,name=StringFlag" json:"StringFlag,omitempty"`
	Int32Flag        *int32   `protobuf:"varint,3,opt,name=Int32Flag" json:"Int32Flag,omitempty"`
	SliceFlag        []string `protobuf:"bytes,4,rep,name=SliceFlag" json:"SliceFlag,omitempty"`
	XXX_unrecognized []byte   `json:"-"`
}

the value tmpBool is currently only initial but it may be changed in other places, so pass a *bool can make my message perceive the changes, but []string without pointer only copy the value which initial just empty, but that value may changed, but message can't perceive that.

is there any way to generate []*string maybe ? if not, is there any workaround here?
Thanks

@shiywang
Copy link
Author

shiywang commented Oct 30, 2017

I've find a workaround here using an extra message which can pass by reference instead of value

message Slice {
    repeated string value = 1;
}

case *cmdproto.Slice:
	switch ext {
	case cmdproto.FlagDetail_ISSTRSLICE:
		var tmpSliceString cmdproto.Slice
		cmd.Flags().StringSliceVarP(&tmpSliceString.Value, name, shorthand, defaultValue, usage)
		v.Field(i).Set(reflect.ValueOf(&tmpSliceString))

but still wondering why not repeated string --> []*string

@shiywang shiywang changed the title Scalar Value Types Problems why not []*string Scalar Value Types Problems repeated string --> why not []*string pass by reference Oct 30, 2017
@dsnet
Copy link
Member

dsnet commented Dec 6, 2017

The proto2 format allows for optional values. Since Go lacks optional types or unions types, it resorted to using pointers to indicate the difference between a set zero value and an unset value. As for slices, there is no need to make that distinction.

Furthermore, I don't think it's feasible to add an option just to allow generating []*string instead of []string.

@dsnet dsnet closed this as completed Dec 6, 2017
@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants