Alanxtl commented on code in PR #3696:
URL: https://github.com/apache/dubbo-go/pull/3696#discussion_r3840233854
##########
go.mod:
##########
@@ -68,6 +67,7 @@ require (
google.golang.org/grpc v1.64.1
google.golang.org/protobuf v1.34.2
gopkg.in/natefinch/lumberjack.v2 v2.2.1
+ gopkg.in/yaml.v3 v3.0.1
Review Comment:
不要引入gopkg.in/yaml.v3
统一使用go.yaml.in/yaml/v4
##########
registry/etcdv3/service_discovery.go:
##########
@@ -325,3 +325,29 @@ func newEtcdV3ServiceDiscovery(url *common.URL)
(registry.ServiceDiscovery, erro
childListenerMap: make(map[string]*etcdv3.EventListener),
instanceListenerMap: make(map[string]*gxset.HashSet)}, nil
}
+
+func encodeJSON(in any) ([]byte, error) {
+ if in == nil {
+ return nil, fmt.Errorf("input for encoding is nil")
+ }
+
+ var buf bytes.Buffer
+ enc := json.NewEncoder(&buf)
+ if err := enc.Encode(in); err != nil {
+ return nil, err
+ }
+ return buf.Bytes(), nil
+}
+
+func decodeJSON(data []byte, out any) error {
+ if len(data) == 0 {
+ return fmt.Errorf("'data' being decoded is nil")
+ }
+ if out == nil {
+ return fmt.Errorf("output parameter 'out' is nil")
+ }
+
+ dec := json.NewDecoder(bytes.NewReader(data))
+ dec.UseNumber()
+ return dec.Decode(out)
+}
Review Comment:
这两个函数抽到common\dubboutil里面新建一个文件夹吧
##########
registry/etcdv3/service_discovery.go:
##########
@@ -325,3 +325,29 @@ func newEtcdV3ServiceDiscovery(url *common.URL)
(registry.ServiceDiscovery, erro
childListenerMap: make(map[string]*etcdv3.EventListener),
instanceListenerMap: make(map[string]*gxset.HashSet)}, nil
}
+
+func encodeJSON(in any) ([]byte, error) {
+ if in == nil {
+ return nil, fmt.Errorf("input for encoding is nil")
+ }
+
+ var buf bytes.Buffer
+ enc := json.NewEncoder(&buf)
+ if err := enc.Encode(in); err != nil {
+ return nil, err
+ }
+ return buf.Bytes(), nil
+}
+
+func decodeJSON(data []byte, out any) error {
+ if len(data) == 0 {
+ return fmt.Errorf("'data' being decoded is nil")
+ }
+ if out == nil {
+ return fmt.Errorf("output parameter 'out' is nil")
+ }
+
+ dec := json.NewDecoder(bytes.NewReader(data))
+ dec.UseNumber()
Review Comment:
```suggestion
// While decoding JSON values, interpret the integer values as
`json.Number`s instead of `float64`.
dec.UseNumber()
```
--
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]