xiaobaicai66695 commented on code in PR #3696: URL: https://github.com/apache/dubbo-go/pull/3696#discussion_r3840565182
########## common/dubboutil/json.go: ########## @@ -0,0 +1,54 @@ +/* + * 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 dubboutil + +import ( + "bytes" + "encoding/json" + "fmt" +) + +// EncodeJSON encodes in as JSON. +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 Review Comment: 已按建议改为 `json.Marshal`,并通过 `%w` 包装编码错误,便于调用方保留错误链。 ########## common/dubboutil/json_test.go: ########## @@ -0,0 +1,45 @@ +/* + * 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 dubboutil + +import ( + "encoding/json" + "testing" +) + +import ( + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestJSONHelpersUseNumber(t *testing.T) { Review Comment: 已补充正常编码/解码、nil/空参数、不可编码类型、非法 JSON、类型不匹配,以及大整数 `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]
