oneof

package
v0.0.0-...-a152ea0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

Oneof

Proto

message HelloRequest {
  oneof name {
    string first_name = 1;
    string last_name = 2;
  }

  oneof gender {
    bool male = 3;
    bool female = 4;
  }

  oneof default_values {
    int32 default_int = 5;
    string default_string = 6;
  }
}

I defined 3 oneof values, and I will use each for different purposes.

  • name: set a non-default value.
  • gender: not set.
  • default_values: set a default value.

Outcome

  • In generated Go code, each field is compiled into a struct which implements a generated interface and contains its real value.
  • We can use type assertion to determine which oneof field has been set.
  • On the other hand, if a oneof field is set to default value, the value will be serialized to the wire, and it is diffrent from not being set, which is nil struct.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var File_oneof_hello_proto protoreflect.FileDescriptor
View Source
var HelloService_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "oneof.HelloService",
	HandlerType: (*HelloServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "SayHello",
			Handler:    _HelloService_SayHello_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "oneof/hello.proto",
}

HelloService_ServiceDesc is the grpc.ServiceDesc for HelloService service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

Functions

func RegisterHelloServiceServer

func RegisterHelloServiceServer(s grpc.ServiceRegistrar, srv HelloServiceServer)

Types

type HelloRequest

type HelloRequest struct {

	// Types that are assignable to Name:
	//
	//	*HelloRequest_FirstName
	//	*HelloRequest_LastName
	Name isHelloRequest_Name `protobuf_oneof:"name"`
	// Types that are assignable to Gender:
	//
	//	*HelloRequest_Male
	//	*HelloRequest_Female
	Gender isHelloRequest_Gender `protobuf_oneof:"gender"`
	// Types that are assignable to DefaultValues:
	//
	//	*HelloRequest_DefaultInt
	//	*HelloRequest_DefaultString
	DefaultValues isHelloRequest_DefaultValues `protobuf_oneof:"default_values"`
	// contains filtered or unexported fields
}

The request message containing the user's name.

func (*HelloRequest) Descriptor deprecated

func (*HelloRequest) Descriptor() ([]byte, []int)

Deprecated: Use HelloRequest.ProtoReflect.Descriptor instead.

func (*HelloRequest) GetDefaultInt

func (x *HelloRequest) GetDefaultInt() int32

func (*HelloRequest) GetDefaultString

func (x *HelloRequest) GetDefaultString() string

func (*HelloRequest) GetDefaultValues

func (m *HelloRequest) GetDefaultValues() isHelloRequest_DefaultValues

func (*HelloRequest) GetFemale

func (x *HelloRequest) GetFemale() bool

func (*HelloRequest) GetFirstName

func (x *HelloRequest) GetFirstName() string

func (*HelloRequest) GetGender

func (m *HelloRequest) GetGender() isHelloRequest_Gender

func (*HelloRequest) GetLastName

func (x *HelloRequest) GetLastName() string

func (*HelloRequest) GetMale

func (x *HelloRequest) GetMale() bool

func (*HelloRequest) GetName

func (m *HelloRequest) GetName() isHelloRequest_Name

func (*HelloRequest) ProtoMessage

func (*HelloRequest) ProtoMessage()

func (*HelloRequest) ProtoReflect

func (x *HelloRequest) ProtoReflect() protoreflect.Message

func (*HelloRequest) Reset

func (x *HelloRequest) Reset()

func (*HelloRequest) String

func (x *HelloRequest) String() string

type HelloRequest_DefaultInt

type HelloRequest_DefaultInt struct {
	DefaultInt int32 `protobuf:"varint,5,opt,name=default_int,json=defaultInt,proto3,oneof"`
}

type HelloRequest_DefaultString

type HelloRequest_DefaultString struct {
	DefaultString string `protobuf:"bytes,6,opt,name=default_string,json=defaultString,proto3,oneof"`
}

type HelloRequest_Female

type HelloRequest_Female struct {
	Female bool `protobuf:"varint,4,opt,name=female,proto3,oneof"`
}

type HelloRequest_FirstName

type HelloRequest_FirstName struct {
	FirstName string `protobuf:"bytes,1,opt,name=first_name,json=firstName,proto3,oneof"`
}

type HelloRequest_LastName

type HelloRequest_LastName struct {
	LastName string `protobuf:"bytes,2,opt,name=last_name,json=lastName,proto3,oneof"`
}

type HelloRequest_Male

type HelloRequest_Male struct {
	Male bool `protobuf:"varint,3,opt,name=male,proto3,oneof"`
}

type HelloResponse

type HelloResponse struct {
	Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
	// contains filtered or unexported fields
}

The response message containing the greetings

func (*HelloResponse) Descriptor deprecated

func (*HelloResponse) Descriptor() ([]byte, []int)

Deprecated: Use HelloResponse.ProtoReflect.Descriptor instead.

func (*HelloResponse) GetMessage

func (x *HelloResponse) GetMessage() string

func (*HelloResponse) ProtoMessage

func (*HelloResponse) ProtoMessage()

func (*HelloResponse) ProtoReflect

func (x *HelloResponse) ProtoReflect() protoreflect.Message

func (*HelloResponse) Reset

func (x *HelloResponse) Reset()

func (*HelloResponse) String

func (x *HelloResponse) String() string

type HelloServiceClient

type HelloServiceClient interface {
	// Sends a greeting
	SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloResponse, error)
}

HelloServiceClient is the client API for HelloService service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

type HelloServiceServer

type HelloServiceServer interface {
	// Sends a greeting
	SayHello(context.Context, *HelloRequest) (*HelloResponse, error)
	// contains filtered or unexported methods
}

HelloServiceServer is the server API for HelloService service. All implementations must embed UnimplementedHelloServiceServer for forward compatibility

type UnimplementedHelloServiceServer

type UnimplementedHelloServiceServer struct {
}

UnimplementedHelloServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedHelloServiceServer) SayHello

type UnsafeHelloServiceServer

type UnsafeHelloServiceServer interface {
	// contains filtered or unexported methods
}

UnsafeHelloServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to HelloServiceServer will result in compilation errors.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL