github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue113_test.go (about)

     1  /*
     2   * Copyright 2021 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package issue_test
    18  
    19  import (
    20      `testing`
    21  
    22      `github.com/bytedance/sonic/encoder`
    23      `github.com/stretchr/testify/require`
    24  )
    25  
    26  type Issue113_OmitemptyOpt struct {
    27      Bool      bool                `json:"bool,omitempty"`
    28      Int       int                 `json:"int,omitempty"`
    29      Int8      int8                `json:"int8,omitempty"`
    30      Int16     int16               `json:"int16,omitempty"`
    31      Int32     int32               `json:"int32,omitempty"`
    32      Int64     int64               `json:"int64,omitempty"`
    33      Uint      uint                `json:"uint,omitempty"`
    34      Uint8     uint8               `json:"uint8,omitempty"`
    35      Uint16    uint16              `json:"uint16,omitempty"`
    36      Uint32    uint32              `json:"uint32,omitempty"`
    37      Uint64    uint64              `json:"uint64,omitempty"`
    38      Float32   float32             `json:"float32,omitempty"`
    39      Float64   float64             `json:"float64,omitempty"`
    40      Uintptr   uintptr             `json:"uintptr,omitempty"`
    41      String    string              `json:"string,omitempty"`
    42      Array0    [0]uint             `json:"array0,omitempty"`
    43      Array     [2]int              `json:"array,omitempty"`
    44      Interface interface{}         `json:"interface,omitempty"`
    45      Map0      map[int]interface{} `json:"map0,omitempty"`
    46      Map       map[string]float64  `json:"map,omitempty"`
    47      Slice0    []int               `json:"slice0,omitempty"`
    48      Slice     []byte              `json:"slice,omitempty"`
    49      Ptr       * Issue113_Inner    `json:"ptr,omitempty"`
    50      Struct1   Issue113_Inner      `json:"struct1,omitempty"`
    51      Struct2   struct{}            `json:"struct2,omitempty"`
    52  }
    53  
    54  type Issue113_Inner struct {
    55      S string `json:"s,"`
    56      So string `json:"so,omitempty"`
    57  }
    58  
    59  var issue13ExpectedEmptyOpt = `{
    60   "array": [
    61    0,
    62    0
    63   ],
    64   "struct1": {
    65    "s": ""
    66   },
    67   "struct2": {}
    68  }`
    69  
    70  var issue13ExpectedNonemptyOpt = `{
    71   "bool": true,
    72   "int": 1,
    73   "int8": -1,
    74   "int16": 1,
    75   "int32": 2,
    76   "int64": 64,
    77   "uint": 1,
    78   "uint8": 8,
    79   "uint16": 16,
    80   "uint32": 32,
    81   "uint64": 64,
    82   "float32": 1,
    83   "float64": -2.34e+64,
    84   "uintptr": 1,
    85   "string": "string",
    86   "array": [
    87    0,
    88    -1
    89   ],
    90   "interface": {
    91    "s": "not omit"
    92   },
    93   "map0": {
    94    "0": "zero"
    95   },
    96   "map": {
    97    "key": 0
    98   },
    99   "slice0": [
   100    0
   101   ],
   102   "slice": "Yg==",
   103   "ptr": {
   104    "s": "not omit"
   105   },
   106   "struct1": {
   107    "s": "not omit"
   108   },
   109   "struct2": {}
   110  }`
   111  
   112  func TestIssue113_MarshalEmptyFieldsWithOmitemptyOpt(t *testing.T) {
   113      var obj Issue113_OmitemptyOpt
   114      obj.Slice0 = make([]int, 0, 100)     // empty slice
   115      obj.Map0 = make(map[int]interface{}) // empty map
   116  
   117      got, err := encoder.EncodeIndented(&obj, "", " ", 0)
   118  
   119      require.NoError(t, err)
   120      require.Equal(t, issue13ExpectedEmptyOpt, string(got))
   121  }
   122  
   123  func TestIssue113_MarshalNonemptyFieldsWithOmitemptyOpt(t *testing.T) {
   124      var inner = & Issue113_Inner {
   125          S : "not omit",
   126      }
   127  
   128      var obj = & Issue113_OmitemptyOpt{
   129          Bool      : true,
   130          Int       : 1,
   131          Int8      : -1,
   132          Int16     : 1,
   133          Int32     : 2,
   134          Int64     : 64,
   135          Uint      : 1,
   136          Uint8     : 8,
   137          Uint16    : 16,
   138          Uint32    : 32,
   139          Uint64    : 64,
   140          Float32   : 1.0,
   141          Float64   : -2.34e+64,
   142          Uintptr   : uintptr(0x1),
   143          String    : "string", 
   144          Array0    : [0]uint{},
   145          Array     : [2]int{0, -1},
   146          Interface : *inner,
   147          Map0      : map[int]interface{}{0 : "zero"},
   148          Map       : map[string]float64{"key" : 0.0},
   149          Slice0    : make([]int, 1, 1),
   150          Slice     : []byte("b"),
   151          Ptr       : inner,
   152          Struct1   : *inner,
   153          Struct2   : struct{}{},
   154      }
   155  
   156      got, err := encoder.EncodeIndented(&obj, "", " ", 0)
   157  
   158      require.NoError(t, err)
   159      require.Equal(t, issue13ExpectedNonemptyOpt, string(got))
   160  }