Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supoort omitnil tag in json package #231

Merged
merged 4 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,12 @@ func main() {
> 注入的 `ClientToken` 在 `100000/s` 并发量以下提供全局唯一性。

## 空数组和omitempty
SDK 使用 `omitempty` 标签来序列化你的 request 对象, 因为这样可以避免上报空数组/对象.
在 v1.0.732 之前的版本, SDK使用`omitempty`标签来序列化请求, 这会导致 nil 数组和 长度为0的空数组 都无法被序列化.
这在你希望发送一个空数组的时候会造成不便, 在之前你需要使用 CommonClient 来解决这个问题.

但对有的接口而言, 长度为0的数组 和 nil数组 是有区别的, 如果你希望在请求中携带空数组, 需要使用Common Client 来发送请求.
在 >= v1.0.733 的版本, SDK使用`omitnil`标签来序列化请求, 此时nil数组会被忽略掉, 但是空数组可以被正常发送.

需要注意的是这个改动在大部分情况下对于用户是无感知的, 但是在特殊情况下依然可能会造成行为不一致.

参考[示例](https://github.com/TencentCloud/tencentcloud-sdk-go/blob/master/examples/common/omitempty.go)

Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
Expand All @@ -16,6 +15,7 @@ import (

tcerr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
tchttp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/json"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
)

Expand Down
5 changes: 3 additions & 2 deletions tencentcloud/common/cvm_role_provider.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package common

import (
"encoding/json"
"errors"
tcerr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"io/ioutil"
"net/http"
"time"

tcerr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/json"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/common/http/common_request.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package common

import (
"encoding/json"
"fmt"
tcerr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/json"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/common/http/common_request_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package common

import (
"encoding/json"
tcerr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/json"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/common/http/common_response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package common

import "encoding/json"
import "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/json"

type actionResult map[string]interface{}
type CommonResponse struct {
Expand Down
2 changes: 1 addition & 1 deletion tencentcloud/common/http/response.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package common

import (
"encoding/json"
"fmt"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/json"
"io/ioutil"
//"log"
"net/http"
Expand Down
Loading