Skip to content

Commit

Permalink
add omitempty in new added fields (bnb-chain#792)
Browse files Browse the repository at this point in the history
* add omitempty in new added fields

* fix imports
  • Loading branch information
yutianwu authored Aug 21, 2020
1 parent 0ebef43 commit 9cdbda8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
4 changes: 2 additions & 2 deletions common/types/mini_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ type MiniToken struct {
Mintable bool `json:"mintable"`
TokenType SupplyRangeType `json:"token_type"`
TokenURI string `json:"token_uri"` //TODO set max length
ContractAddress string `json:"contract_address"`
ContractDecimals int8 `json:"contract_decimals"`
ContractAddress string `json:"contract_address,omitempty"`
ContractDecimals int8 `json:"contract_decimals,omitempty"`
}

var _ IToken = &MiniToken{}
Expand Down
4 changes: 2 additions & 2 deletions common/types/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type Token struct {
TotalSupply utils.Fixed8 `json:"total_supply"`
Owner sdk.AccAddress `json:"owner"`
Mintable bool `json:"mintable"`
ContractAddress string `json:"contract_address"`
ContractDecimals int8 `json:"contract_decimals"`
ContractAddress string `json:"contract_address,omitempty"`
ContractDecimals int8 `json:"contract_decimals,omitempty"`
}

func (token Token) GetName() string {
Expand Down
40 changes: 40 additions & 0 deletions common/types/token_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package types_test

import (
"encoding/json"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/binance-chain/node/common/types"
"github.com/binance-chain/node/common/utils"
)

var issueMsgSymbolTestCases = []struct {
Expand Down Expand Up @@ -106,3 +109,40 @@ func TestValidateTokenSymbol(t *testing.T) {
t.Errorf("ValidateIssueSymbol() error = %v, expected XYZ to be invalid", err)
}
}

func TestMarshalToken(t *testing.T) {
type beforeToken struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
OrigSymbol string `json:"original_symbol"`
TotalSupply utils.Fixed8 `json:"total_supply"`
Owner sdk.AccAddress `json:"owner"`
Mintable bool `json:"mintable"`
}

type beforeMiniToken struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
OrigSymbol string `json:"original_symbol"`
TotalSupply utils.Fixed8 `json:"total_supply"`
Owner sdk.AccAddress `json:"owner"`
Mintable bool `json:"mintable"`
TokenType types.SupplyRangeType `json:"token_type"`
TokenURI string `json:"token_uri"` //TODO set max length
}

emptyBeforeToken, err := json.Marshal(beforeToken{})
require.Nil(t, err, "error should be nil")

emptyBeforeMiniToken, err := json.Marshal(beforeMiniToken{})
require.Nil(t, err, "error should be nil")

emptyToken, err := json.Marshal(types.Token{})
require.Nil(t, err, "error should be nil")

emptyMiniToken, err := json.Marshal(types.MiniToken{})
require.Nil(t, err, "error should be nil")

require.Equal(t, string(emptyBeforeToken), string(emptyToken))
require.Equal(t, string(emptyBeforeMiniToken), string(emptyMiniToken))
}

0 comments on commit 9cdbda8

Please sign in to comment.