From 14263113796374c2a9fb02a8048ea48a20545ced Mon Sep 17 00:00:00 2001 From: evilolipop Date: Fri, 8 Mar 2024 13:34:16 +0800 Subject: [PATCH] feat: generate errors even if no services --- protobuf/protoc-gen-go-grain/generate.go | 23 +-- .../testdata/error/hello.pb.go | 105 ++---------- .../testdata/error/hello.proto | 10 -- .../testdata/error/hello_grain.pb.go | 158 +----------------- .../testdata/hello/hello_grain.pb.go | 2 +- .../multi-services/services_grain.pb.go | 2 +- .../testdata/reenter/hello_grain.pb.go | 2 +- protobuf/protoc-gen-go-grain/version.go | 2 +- 8 files changed, 31 insertions(+), 273 deletions(-) diff --git a/protobuf/protoc-gen-go-grain/generate.go b/protobuf/protoc-gen-go-grain/generate.go index 2486bac3..b3299edb 100644 --- a/protobuf/protoc-gen-go-grain/generate.go +++ b/protobuf/protoc-gen-go-grain/generate.go @@ -30,14 +30,15 @@ var ( ) func generateFile(gen *protogen.Plugin, file *protogen.File) { - if len(file.Services) == 0 { + if len(file.Services) == 0 && len(file.Enums) == 0 { return } + filename := file.GeneratedFilenamePrefix + "_grain.pb.go" g := gen.NewGeneratedFile(filename, file.GoImportPath) generateHeader(gen, g, file) - generateContent(gen, g, file) + generateContent(g, file) } func generateHeader(gen *protogen.Plugin, g *protogen.GeneratedFile, file *protogen.File) { @@ -60,9 +61,15 @@ func generateHeader(gen *protogen.Plugin, g *protogen.GeneratedFile, file *proto g.P() } -func generateContent(gen *protogen.Plugin, g *protogen.GeneratedFile, file *protogen.File) { +func generateContent(g *protogen.GeneratedFile, file *protogen.File) { g.P("package ", file.GoPackageName) + for _, enum := range file.Enums { + if enum.Desc.Name() == "ErrorReason" { + generateErrorReasons(g, enum) + } + } + if len(file.Services) == 0 { return } @@ -74,21 +81,15 @@ func generateContent(gen *protogen.Plugin, g *protogen.GeneratedFile, file *prot g.QualifiedGoIdent(timePackage.Ident("")) g.QualifiedGoIdent(slogPackage.Ident("")) - for _, enum := range file.Enums { - if enum.Desc.Name() == "ErrorReason" { - generateErrorReasons(g, enum) - } - } - for _, service := range file.Services { - generateService(service, file, g) + generateService(service, g) g.P() } generateRespond(g) } -func generateService(service *protogen.Service, file *protogen.File, g *protogen.GeneratedFile) { +func generateService(service *protogen.Service, g *protogen.GeneratedFile) { if service.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated() { g.P("//") g.P(deprecationComment) diff --git a/protobuf/protoc-gen-go-grain/testdata/error/hello.pb.go b/protobuf/protoc-gen-go-grain/testdata/error/hello.pb.go index 94389bc0..00582f78 100644 --- a/protobuf/protoc-gen-go-grain/testdata/error/hello.pb.go +++ b/protobuf/protoc-gen-go-grain/testdata/error/hello.pb.go @@ -9,7 +9,6 @@ package hello import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - emptypb "google.golang.org/protobuf/types/known/emptypb" reflect "reflect" sync "sync" ) @@ -67,76 +66,20 @@ func (ErrorReason) EnumDescriptor() ([]byte, []int) { return file_testdata_error_hello_proto_rawDescGZIP(), []int{0} } -type SayHelloResponse struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` -} - -func (x *SayHelloResponse) Reset() { - *x = SayHelloResponse{} - if protoimpl.UnsafeEnabled { - mi := &file_testdata_error_hello_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *SayHelloResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*SayHelloResponse) ProtoMessage() {} - -func (x *SayHelloResponse) ProtoReflect() protoreflect.Message { - mi := &file_testdata_error_hello_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use SayHelloResponse.ProtoReflect.Descriptor instead. -func (*SayHelloResponse) Descriptor() ([]byte, []int) { - return file_testdata_error_hello_proto_rawDescGZIP(), []int{0} -} - -func (x *SayHelloResponse) GetMessage() string { - if x != nil { - return x.Message - } - return "" -} - var File_testdata_error_hello_proto protoreflect.FileDescriptor var file_testdata_error_hello_proto_rawDesc = []byte{ 0x0a, 0x1a, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x68, 0x65, - 0x6c, 0x6c, 0x6f, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x22, 0x2c, 0x0a, 0x10, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2a, 0x36, - 0x0a, 0x0b, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x52, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x12, 0x12, 0x0a, - 0x0e, 0x55, 0x53, 0x45, 0x52, 0x5f, 0x4e, 0x4f, 0x54, 0x5f, 0x46, 0x4f, 0x55, 0x4e, 0x44, 0x10, - 0x00, 0x12, 0x13, 0x0a, 0x0f, 0x43, 0x4f, 0x4e, 0x54, 0x45, 0x4e, 0x54, 0x5f, 0x4d, 0x49, 0x53, - 0x53, 0x49, 0x4e, 0x47, 0x10, 0x01, 0x32, 0x46, 0x0a, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x12, - 0x3d, 0x0a, 0x08, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x16, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, - 0x70, 0x74, 0x79, 0x1a, 0x17, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x53, 0x61, 0x79, 0x48, - 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x46, - 0x5a, 0x44, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x73, 0x79, - 0x6e, 0x6b, 0x72, 0x6f, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x61, 0x63, 0x74, 0x6f, 0x72, - 0x2d, 0x67, 0x6f, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x67, - 0x6f, 0x2d, 0x67, 0x72, 0x61, 0x69, 0x6e, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, - 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6c, 0x6c, 0x6f, 0x2a, 0x36, 0x0a, 0x0b, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x52, 0x65, 0x61, 0x73, + 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x0e, 0x55, 0x53, 0x45, 0x52, 0x5f, 0x4e, 0x4f, 0x54, 0x5f, 0x46, + 0x4f, 0x55, 0x4e, 0x44, 0x10, 0x00, 0x12, 0x13, 0x0a, 0x0f, 0x43, 0x4f, 0x4e, 0x54, 0x45, 0x4e, + 0x54, 0x5f, 0x4d, 0x49, 0x53, 0x53, 0x49, 0x4e, 0x47, 0x10, 0x01, 0x42, 0x46, 0x5a, 0x44, 0x67, + 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x73, 0x79, 0x6e, 0x6b, 0x72, + 0x6f, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x2d, 0x67, 0x6f, + 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x67, 0x6f, 0x2d, 0x67, + 0x72, 0x61, 0x69, 0x6e, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x68, 0x65, + 0x6c, 0x6c, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -152,17 +95,12 @@ func file_testdata_error_hello_proto_rawDescGZIP() []byte { } var file_testdata_error_hello_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_testdata_error_hello_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_testdata_error_hello_proto_goTypes = []interface{}{ - (ErrorReason)(0), // 0: hello.ErrorReason - (*SayHelloResponse)(nil), // 1: hello.SayHelloResponse - (*emptypb.Empty)(nil), // 2: google.protobuf.Empty + (ErrorReason)(0), // 0: hello.ErrorReason } var file_testdata_error_hello_proto_depIdxs = []int32{ - 2, // 0: hello.Hello.SayHello:input_type -> google.protobuf.Empty - 1, // 1: hello.Hello.SayHello:output_type -> hello.SayHelloResponse - 1, // [1:2] is the sub-list for method output_type - 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name @@ -173,34 +111,19 @@ func file_testdata_error_hello_proto_init() { if File_testdata_error_hello_proto != nil { return } - if !protoimpl.UnsafeEnabled { - file_testdata_error_hello_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*SayHelloResponse); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_testdata_error_hello_proto_rawDesc, NumEnums: 1, - NumMessages: 1, + NumMessages: 0, NumExtensions: 0, - NumServices: 1, + NumServices: 0, }, GoTypes: file_testdata_error_hello_proto_goTypes, DependencyIndexes: file_testdata_error_hello_proto_depIdxs, EnumInfos: file_testdata_error_hello_proto_enumTypes, - MessageInfos: file_testdata_error_hello_proto_msgTypes, }.Build() File_testdata_error_hello_proto = out.File file_testdata_error_hello_proto_rawDesc = nil diff --git a/protobuf/protoc-gen-go-grain/testdata/error/hello.proto b/protobuf/protoc-gen-go-grain/testdata/error/hello.proto index 3a2e0eca..5cf23283 100644 --- a/protobuf/protoc-gen-go-grain/testdata/error/hello.proto +++ b/protobuf/protoc-gen-go-grain/testdata/error/hello.proto @@ -2,19 +2,9 @@ syntax = "proto3"; package hello; -import "google/protobuf/empty.proto"; - option go_package = "github.com/asynkron/protoactor-go/protoc-gen-go-grain/testdata/hello"; enum ErrorReason { USER_NOT_FOUND = 0; CONTENT_MISSING = 1; } - -message SayHelloResponse { - string message = 1; -} - -service Hello { - rpc SayHello (google.protobuf.Empty) returns (SayHelloResponse) {} -} \ No newline at end of file diff --git a/protobuf/protoc-gen-go-grain/testdata/error/hello_grain.pb.go b/protobuf/protoc-gen-go-grain/testdata/error/hello_grain.pb.go index cd6966ea..8f3f244e 100644 --- a/protobuf/protoc-gen-go-grain/testdata/error/hello_grain.pb.go +++ b/protobuf/protoc-gen-go-grain/testdata/error/hello_grain.pb.go @@ -1,21 +1,11 @@ // Code generated by protoc-gen-grain. DO NOT EDIT. // versions: -// protoc-gen-grain v0.5.0 +// protoc-gen-grain v0.5.1 // protoc v4.25.0 // source: testdata/error/hello.proto package hello -import ( - fmt "fmt" - actor "github.com/asynkron/protoactor-go/actor" - cluster "github.com/asynkron/protoactor-go/cluster" - proto "google.golang.org/protobuf/proto" - emptypb "google.golang.org/protobuf/types/known/emptypb" - slog "log/slog" - time "time" -) - func ErrUserNotFound(format string, args ...interface{}) *cluster.GrainErrorResponse { return cluster.NewGrainErrorResponse(ErrorReason_USER_NOT_FOUND.String(), fmt.Sprintf(format, args...)) } @@ -39,149 +29,3 @@ func IsContentMissing(err error) bool { e := cluster.FromError(err) return e.Reason == ErrorReason_CONTENT_MISSING.String() } - -var xHelloFactory func() Hello - -// HelloFactory produces a Hello -func HelloFactory(factory func() Hello) { - xHelloFactory = factory -} - -// GetHelloGrainClient instantiates a new HelloGrainClient with given Identity -func GetHelloGrainClient(c *cluster.Cluster, id string) *HelloGrainClient { - if c == nil { - panic(fmt.Errorf("nil cluster instance")) - } - if id == "" { - panic(fmt.Errorf("empty id")) - } - return &HelloGrainClient{Identity: id, cluster: c} -} - -// GetHelloKind instantiates a new cluster.Kind for Hello -func GetHelloKind(opts ...actor.PropsOption) *cluster.Kind { - props := actor.PropsFromProducer(func() actor.Actor { - return &HelloActor{ - Timeout: 60 * time.Second, - } - }, opts...) - kind := cluster.NewKind("Hello", props) - return kind -} - -// GetHelloKind instantiates a new cluster.Kind for Hello -func NewHelloKind(factory func() Hello, timeout time.Duration, opts ...actor.PropsOption) *cluster.Kind { - xHelloFactory = factory - props := actor.PropsFromProducer(func() actor.Actor { - return &HelloActor{ - Timeout: timeout, - } - }, opts...) - kind := cluster.NewKind("Hello", props) - return kind -} - -// Hello interfaces the services available to the Hello -type Hello interface { - Init(ctx cluster.GrainContext) - Terminate(ctx cluster.GrainContext) - ReceiveDefault(ctx cluster.GrainContext) - SayHello(req *emptypb.Empty, ctx cluster.GrainContext) (*SayHelloResponse, error) -} - -// HelloGrainClient holds the base data for the HelloGrain -type HelloGrainClient struct { - Identity string - cluster *cluster.Cluster -} - -// SayHello requests the execution on to the cluster with CallOptions -func (g *HelloGrainClient) SayHello(r *emptypb.Empty, opts ...cluster.GrainCallOption) (*SayHelloResponse, error) { - bytes, err := proto.Marshal(r) - if err != nil { - return nil, err - } - reqMsg := &cluster.GrainRequest{MethodIndex: 0, MessageData: bytes} - resp, err := g.cluster.Request(g.Identity, "Hello", reqMsg, opts...) - if err != nil { - return nil, fmt.Errorf("error request: %w", err) - } - switch msg := resp.(type) { - case *SayHelloResponse: - return msg, nil - case *cluster.GrainErrorResponse: - if msg == nil { - return nil, nil - } - return nil, msg - default: - return nil, fmt.Errorf("unknown response type %T", resp) - } -} - -// HelloActor represents the actor structure -type HelloActor struct { - ctx cluster.GrainContext - inner Hello - Timeout time.Duration -} - -// Receive ensures the lifecycle of the actor for the received message -func (a *HelloActor) Receive(ctx actor.Context) { - switch msg := ctx.Message().(type) { - case *actor.Started: //pass - case *cluster.ClusterInit: - a.ctx = cluster.NewGrainContext(ctx, msg.Identity, msg.Cluster) - a.inner = xHelloFactory() - a.inner.Init(a.ctx) - - if a.Timeout > 0 { - ctx.SetReceiveTimeout(a.Timeout) - } - case *actor.ReceiveTimeout: - ctx.Poison(ctx.Self()) - case *actor.Stopped: - a.inner.Terminate(a.ctx) - case actor.AutoReceiveMessage: // pass - case actor.SystemMessage: // pass - - case *cluster.GrainRequest: - switch msg.MethodIndex { - case 0: - req := &emptypb.Empty{} - err := proto.Unmarshal(msg.MessageData, req) - if err != nil { - ctx.Logger().Error("[Grain] SayHello(emptypb.Empty) proto.Unmarshal failed.", slog.Any("error", err)) - resp := cluster.NewGrainErrorResponse(cluster.ErrorReason_INVALID_ARGUMENT, err.Error()). - WithMetadata(map[string]string{ - "argument": req.String(), - }) - ctx.Respond(resp) - return - } - - r0, err := a.inner.SayHello(req, a.ctx) - if err != nil { - resp := cluster.FromError(err) - ctx.Respond(resp) - return - } - ctx.Respond(r0) - } - default: - a.inner.ReceiveDefault(a.ctx) - } -} - -// onError should be used in ctx.ReenterAfter -// you can just return error in reenterable method for other errors -func (a *HelloActor) onError(err error) { - resp := cluster.FromError(err) - a.ctx.Respond(resp) -} - -func respond[T proto.Message](ctx cluster.GrainContext) func(T) { - return func(resp T) { - ctx.Respond(resp) - } -} diff --git a/protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go b/protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go index f7230ecf..8b9718dc 100644 --- a/protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go +++ b/protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-grain. DO NOT EDIT. // versions: -// protoc-gen-grain v0.5.0 +// protoc-gen-grain v0.5.1 // protoc v4.25.0 // source: testdata/hello/hello.proto diff --git a/protobuf/protoc-gen-go-grain/testdata/multi-services/services_grain.pb.go b/protobuf/protoc-gen-go-grain/testdata/multi-services/services_grain.pb.go index 61f4686b..32e0a67f 100644 --- a/protobuf/protoc-gen-go-grain/testdata/multi-services/services_grain.pb.go +++ b/protobuf/protoc-gen-go-grain/testdata/multi-services/services_grain.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-grain. DO NOT EDIT. // versions: -// protoc-gen-grain v0.5.0 +// protoc-gen-grain v0.5.1 // protoc v4.25.0 // source: testdata/multi-services/services.proto diff --git a/protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go b/protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go index 3d14e505..3dd0833e 100644 --- a/protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go +++ b/protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-grain. DO NOT EDIT. // versions: -// protoc-gen-grain v0.5.0 +// protoc-gen-grain v0.5.1 // protoc v4.25.0 // source: testdata/reenter/hello.proto diff --git a/protobuf/protoc-gen-go-grain/version.go b/protobuf/protoc-gen-go-grain/version.go index 0454b2c7..3fa6ff25 100644 --- a/protobuf/protoc-gen-go-grain/version.go +++ b/protobuf/protoc-gen-go-grain/version.go @@ -1,3 +1,3 @@ package main -const version = "v0.5.1" +const version = "v0.6.0"