proto

package
v0.0.0-...-bb73ec8 Latest Latest
Warning

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

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

README

About Proto

What are we doing with protobuf?

here we are using proto to define out data schema for communication

  • we will define out entites // and the contract for the api //
  • we will use protoc // protocompiler to generate GO's representation for the proto contract/entities
  • then we will test the go files generated // that its actually working in a go program

What do we write in a proto file?

  • write syntax to mention what proto version we are using

  • write the packageing scheme for our proto contracts (refer the practice folder for the scheme that you can follow to define multiple message contracts)

  • mention the go package you want the generated go classes to have (so that they can be imported in go program with ease)

    • use the options flag go_package variable to let the protoc compiler know that /// thats the go pacakge value to be embedded in generate go files
  • define the messages (entities/classes types for go program)

  • define the service (apis for grpc)

How to make protoc compile proto contracts and create files/models/services that we can use in our app

command goes like this

protoc -Igreet/proto --go_opt=module=github.com/amogh2019/dummy_go_service --go_out=. greet/proto/*.proto
  • we run this at directory root
  • -I tells compiler where to find the proto files to generate
  • go_opt tells that we have some custom flags to honor while generating go code
  • go_opt_module tells that which go module to place generated files in
  • go_out tells the relative path to the go module to place the generated files in
  • {greet/proto/*.proto} would be the specific files to compile

Some more protoc compiler feature

  • Since serialized proto is binary and not human read friendly, we can ask the compiler to decode it into tag:value wala human friendly readable print

    • cat ./greet/server/practice/messageInSerializedForm.bin | protoc --decode_raw
    1: 20
    2: 1
    3: "Dummy21"
    4: "hoyee"
    4: "dumm"
    4: "sdfsadf"
    
  • we can also ask the compiler to read the binary and parse into a specific proto

    • uss proto file ke uss message mein parse karo
    • standard input read karna hota hai usse, issliye example ke liye pipe karke input diya
    • cat ./greet/server/practice/messageInSerializedForm.bin | protoc --decode=greet.practice.level3.SampleMessage greet/proto/practice/level3/sample1.proto
    id: 20
    is_active: true
    name: "Dummy21"
    alt_names: "hoyee"
    alt_names: "dumm"
    alt_names: "sdfsadf"
    
  • we can also encode the proto human friendly readable format into proto binary format

    • cat ./greet/server/practice/messageInProtoHumanFriendly.txt | protoc --encode=greet.practice.level3.SampleMessage greet/proto/practice/level3/sample1.proto > temp.bin

About updating proto messages

  • thoda dekh ke karna // sirf name changes mein no worries
  • type change for a field mein issues ho skte // backward compatablity mein // pura data might break while deserializtion
  • field ka tag change can also mess things up // since tag is the identifier (and tag+type is encoded in binary) in the actual binary form // fieldname is just a label
  • tags update kar rahe toh future incremental tag dedo // aur purane ko block kardo
    • use reserved keyword // block tag and fieldname both

Documentation

Index

Constants

This section is empty.

Variables

View Source
var File_greet_proto protoreflect.FileDescriptor
View Source
var GreetService_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "GreetService",
	HandlerType: (*GreetServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "Greet",
			Handler:    _GreetService_Greet_Handler,
		},
		{
			MethodName: "GreetWithDeadline",
			Handler:    _GreetService_GreetWithDeadline_Handler,
		},
	},
	Streams: []grpc.StreamDesc{
		{
			StreamName:    "GreetManyTimes",
			Handler:       _GreetService_GreetManyTimes_Handler,
			ServerStreams: true,
		},
		{
			StreamName:    "LongGreet",
			Handler:       _GreetService_LongGreet_Handler,
			ClientStreams: true,
		},
		{
			StreamName:    "GreetEveryone",
			Handler:       _GreetService_GreetEveryone_Handler,
			ServerStreams: true,
			ClientStreams: true,
		},
	},
	Metadata: "greet.proto",
}

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

Functions

func RegisterGreetServiceServer

func RegisterGreetServiceServer(s grpc.ServiceRegistrar, srv GreetServiceServer)

Types

type GreetRequest

type GreetRequest struct {
	FirstName string `protobuf:"bytes,1,opt,name=first_name,json=firstName,proto3" json:"first_name,omitempty"`
	// contains filtered or unexported fields
}

func (*GreetRequest) Descriptor deprecated

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

Deprecated: Use GreetRequest.ProtoReflect.Descriptor instead.

func (*GreetRequest) GetFirstName

func (x *GreetRequest) GetFirstName() string

func (*GreetRequest) ProtoMessage

func (*GreetRequest) ProtoMessage()

func (*GreetRequest) ProtoReflect

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

func (*GreetRequest) Reset

func (x *GreetRequest) Reset()

func (*GreetRequest) String

func (x *GreetRequest) String() string

type GreetResponse

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

func (*GreetResponse) Descriptor deprecated

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

Deprecated: Use GreetResponse.ProtoReflect.Descriptor instead.

func (*GreetResponse) GetResult

func (x *GreetResponse) GetResult() string

func (*GreetResponse) ProtoMessage

func (*GreetResponse) ProtoMessage()

func (*GreetResponse) ProtoReflect

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

func (*GreetResponse) Reset

func (x *GreetResponse) Reset()

func (*GreetResponse) String

func (x *GreetResponse) String() string

type GreetServiceClient

type GreetServiceClient interface {
	Greet(ctx context.Context, in *GreetRequest, opts ...grpc.CallOption) (*GreetResponse, error)
	GreetManyTimes(ctx context.Context, in *GreetRequest, opts ...grpc.CallOption) (GreetService_GreetManyTimesClient, error)
	LongGreet(ctx context.Context, opts ...grpc.CallOption) (GreetService_LongGreetClient, error)
	GreetEveryone(ctx context.Context, opts ...grpc.CallOption) (GreetService_GreetEveryoneClient, error)
	GreetWithDeadline(ctx context.Context, in *GreetRequest, opts ...grpc.CallOption) (*GreetResponse, error)
}

GreetServiceClient is the client API for GreetService 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 GreetServiceServer

type GreetServiceServer interface {
	Greet(context.Context, *GreetRequest) (*GreetResponse, error)
	GreetManyTimes(*GreetRequest, GreetService_GreetManyTimesServer) error
	LongGreet(GreetService_LongGreetServer) error
	GreetEveryone(GreetService_GreetEveryoneServer) error
	GreetWithDeadline(context.Context, *GreetRequest) (*GreetResponse, error)
	// contains filtered or unexported methods
}

GreetServiceServer is the server API for GreetService service. All implementations must embed UnimplementedGreetServiceServer for forward compatibility

type GreetService_GreetEveryoneClient

type GreetService_GreetEveryoneClient interface {
	Send(*GreetRequest) error
	Recv() (*GreetResponse, error)
	grpc.ClientStream
}

type GreetService_GreetEveryoneServer

type GreetService_GreetEveryoneServer interface {
	Send(*GreetResponse) error
	Recv() (*GreetRequest, error)
	grpc.ServerStream
}

type GreetService_GreetManyTimesClient

type GreetService_GreetManyTimesClient interface {
	Recv() (*GreetResponse, error)
	grpc.ClientStream
}

type GreetService_GreetManyTimesServer

type GreetService_GreetManyTimesServer interface {
	Send(*GreetResponse) error
	grpc.ServerStream
}

type GreetService_LongGreetClient

type GreetService_LongGreetClient interface {
	Send(*GreetRequest) error
	CloseAndRecv() (*GreetResponse, error)
	grpc.ClientStream
}

type GreetService_LongGreetServer

type GreetService_LongGreetServer interface {
	SendAndClose(*GreetResponse) error
	Recv() (*GreetRequest, error)
	grpc.ServerStream
}

type UnimplementedGreetServiceServer

type UnimplementedGreetServiceServer struct {
}

UnimplementedGreetServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedGreetServiceServer) Greet

func (UnimplementedGreetServiceServer) GreetEveryone

func (UnimplementedGreetServiceServer) GreetManyTimes

func (UnimplementedGreetServiceServer) GreetWithDeadline

func (UnimplementedGreetServiceServer) LongGreet

type UnsafeGreetServiceServer

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

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

Directories

Path Synopsis
practice

Jump to

Keyboard shortcuts

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