Similarityoung commented on code in PR #688:
URL: https://github.com/apache/dubbo-go-pixiu/pull/688#discussion_r2249639967


##########
pkg/common/codec/grpc/passthrough/codec.go:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+package passthrough
+
+import (
+       "fmt"
+)
+
+import (
+       "google.golang.org/grpc/encoding"
+       "google.golang.org/protobuf/proto"
+)
+
+// Name is the name of this codec.
+const Name = "pass_through"
+
+// Codec is a gRPC codec that passes through bytes as is.
+// This is used for transparent proxying where the message types are unknown 
at compile time.
+type Codec struct{}
+
+func init() {
+       encoding.RegisterCodec(Codec{})
+}
+
+// Marshal checks if the value is already bytes or a proto.Message and 
marshals accordingly.
+func (c Codec) Marshal(v any) ([]byte, error) {
+       if p, ok := v.(proto.Message); ok {
+               return proto.Marshal(p)
+       }
+       if b, ok := v.([]byte); ok {
+               return b, nil
+       }
+       return nil, fmt.Errorf("passthrough codec: cannot marshal type %T, want 
proto.Message or []byte", v)
+}
+
+// Unmarshal stores the raw data into the target, which must be a *[]byte or 
proto.Message.
+func (c Codec) Unmarshal(data []byte, v any) error {
+       if vb, ok := v.(*[]byte); ok {
+               *vb = data

Review Comment:
   这个 vb 就是 v 啊,不过类型是*[]byte,而 v 的类型是 any;*vb = data 这就是把 data 的值放进 v 里面。



-- 
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: notifications-unsubscr...@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to