chaokunyang commented on code in PR #3698:
URL: https://github.com/apache/fory/pull/3698#discussion_r3401342591


##########
compiler/fory_compiler/generators/go.py:
##########
@@ -206,6 +207,10 @@ def generate(self) -> List[GeneratedFile]:
         # Generate a single Go file with all types
         files.append(self.generate_file())
 
+        # Generate gRPC service stubs if requested
+        if self.options.grpc:

Review Comment:
   The CLI already appends `generator.generate_services()` when `--grpc` is 
set, so adding service files again inside `GoGenerator.generate()` makes Go 
emit the same `_grpc.go` file twice. The duplicate currently overwrites 
identical content and prints duplicate `Generated:` lines, but it leaves two 
owners for the same output path and can hide future generation divergence. Keep 
service companion emission in the common CLI path only.



##########
integration_tests/grpc_tests/go/main.go:
##########
@@ -0,0 +1,299 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Binary grpc-interop is the Go peer for Java-driven gRPC integration tests.
+// It is invoked as a subprocess by GrpcInteropTest.java and supports two 
modes:
+//
+//     server --port-file <path>  start a gRPC server and write the bound port 
to the file
+//     client --target <addr>     connect to addr and exercise all four 
streaming modes
+package main
+
+import (
+       "context"
+       "flag"
+       "fmt"
+       "io"
+       "log"
+       "net"
+       "os"
+       "strings"
+
+       "github.com/apache/fory/go/fory"
+       grpc_fdl 
"github.com/apache/fory/integration_tests/grpc_tests/go/generated/grpc_fdl"
+       "google.golang.org/grpc"
+       "google.golang.org/grpc/credentials/insecure"
+       "google.golang.org/grpc/encoding"
+)
+
+// --- helpers ----------------------------------------------------------------
+
+func newFory() *fory.Fory {
+       f := fory.New(fory.WithXlang(true), fory.WithRefTracking(true), 
fory.WithCompatible(true))
+       if err := grpc_fdl.RegisterTypes(f); err != nil {
+               log.Fatalf("RegisterTypes: %v", err)
+       }
+       return f
+}
+
+func fdlResponse(req *grpc_fdl.GrpcFdlRequest, tag string, offset int) 
*grpc_fdl.GrpcFdlResponse {
+       return &grpc_fdl.GrpcFdlResponse{
+               Id:      fmt.Sprintf("%s:%s", tag, req.Id),
+               Count:   req.Count + int32(offset),
+               Payload: fmt.Sprintf("%s:%s", tag, req.Payload),
+       }
+}
+
+func fdlAggregate(requests []*grpc_fdl.GrpcFdlRequest) 
*grpc_fdl.GrpcFdlResponse {
+       ids := make([]string, len(requests))
+       payloads := make([]string, len(requests))
+       var count int32
+       for i, req := range requests {
+               ids[i] = req.Id
+               payloads[i] = req.Payload
+               count += req.Count
+       }
+       return &grpc_fdl.GrpcFdlResponse{
+               Id:      "client:" + strings.Join(ids, "+"),
+               Count:   count,
+               Payload: "client:" + strings.Join(payloads, "+"),
+       }
+}
+
+// protoFallbackCodec overrides the built-in "proto" codec (v1 registry) with
+// Fory so the server can decode requests from Java clients, which send with 
the
+// default content-type (application/grpc) rather than application/grpc+fory.
+type protoFallbackCodec struct{ grpc_fdl.CodecV2 }
+
+func (protoFallbackCodec) Name() string { return "proto" }
+
+func (c protoFallbackCodec) Marshal(v interface{}) ([]byte, error) {
+       b, err := c.Fory.Marshal(v)
+       if err != nil {
+               return nil, err
+       }
+       out := make([]byte, len(b))
+       copy(out, b)
+       return out, nil
+}
+
+func (c protoFallbackCodec) Unmarshal(data []byte, v interface{}) error {
+       return c.Fory.Unmarshal(data, v)
+}
+
+// --- server -----------------------------------------------------------------
+
+type fdlService struct {
+       grpc_fdl.UnimplementedFdlGrpcServiceServer
+}
+
+func (s *fdlService) UnaryMessage(_ context.Context, req 
*grpc_fdl.GrpcFdlRequest) (*grpc_fdl.GrpcFdlResponse, error) {
+       return fdlResponse(req, "unary", 10), nil
+}
+
+func (s *fdlService) ServerStreamMessage(req *grpc_fdl.GrpcFdlRequest, stream 
grpc_fdl.FdlGrpcService_ServerStreamMessageServer) error {
+       for i := 0; i < 3; i++ {
+               if err := stream.Send(fdlResponse(req, fmt.Sprintf("server-%d", 
i), i)); err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+func (s *fdlService) ClientStreamMessage(stream 
grpc_fdl.FdlGrpcService_ClientStreamMessageServer) error {
+       var requests []*grpc_fdl.GrpcFdlRequest
+       for {
+               req, err := stream.Recv()
+               if err == io.EOF {
+                       return stream.SendAndClose(fdlAggregate(requests))
+               }
+               if err != nil {
+                       return err
+               }
+               requests = append(requests, req)
+       }
+}
+
+func (s *fdlService) BidiStreamMessage(stream 
grpc_fdl.FdlGrpcService_BidiStreamMessageServer) error {
+       index := 0
+       for {
+               req, err := stream.Recv()
+               if err == io.EOF {
+                       return nil
+               }
+               if err != nil {
+                       return err
+               }
+               if err := stream.Send(fdlResponse(req, fmt.Sprintf("bidi-%d", 
index), index)); err != nil {
+                       return err
+               }
+               index++
+       }
+}
+
+func runServer(portFile string, f *fory.Fory) error {
+       lis, err := net.Listen("tcp", "127.0.0.1:0")
+       if err != nil {
+               return fmt.Errorf("listen: %w", err)
+       }
+       codec := grpc_fdl.CodecV2{Fory: f}
+       // "fory" (v2): used by Go clients via grpc.ForceCodecV2.
+       encoding.RegisterCodecV2(codec)
+       // "proto" (v1): overrides the built-in proto codec so Java clients are
+       // also handled by Fory. RegisterCodecV2 writes to a separate registry 
and
+       // does not replace the v1 "proto" entry; RegisterCodec must be used.
+       encoding.RegisterCodec(protoFallbackCodec{codec})

Review Comment:
   Registering a Fory codec under the global `proto` name is a test-only 
workaround for the server not forcing the generated codec. It mutates grpc-go 
process-global codec state and can affect unrelated gRPC clients or services in 
the same process. The Go server should be created with 
`grpc.ForceServerCodecV2(...)` using the generated codec, then this fallback 
codec and global registry mutation can be removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to