1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

api/message/json/codec.go: alias json_codec.Marshal/Unmarshal as encoder/decoder. so that implementation can change and tests uses the correct functions automatically.

This commit is contained in:
Henrik Hautakoski 2023-05-09 15:04:21 +02:00
parent 12d2837218
commit df840da128
2 changed files with 9 additions and 5 deletions

View file

@ -7,13 +7,17 @@ import (
"github.com/eosswedenorg/thalos/api/message"
)
var json_codec = jsontime.ConfigWithCustomTimeFormat
var (
json_codec = jsontime.ConfigWithCustomTimeFormat
encoder = json_codec.Marshal
decoder = json_codec.Unmarshal
)
func init() {
jsontime.SetDefaultTimeFormat("2006-01-02T15:04:05.000", time.UTC)
message.RegisterCodec("json", message.Codec{
Encoder: json_codec.Marshal,
Decoder: json_codec.Unmarshal,
Encoder: encoder,
Decoder: decoder,
})
}

View file

@ -37,7 +37,7 @@ func TestJson_EncodeActionTrace(t *testing.T) {
expected := `{"tx_id":"ed3b8e853647971cf8296f004c3a1aeac255f082b2cb3c12cc3222e2d7c174ab","blocknum":267372365,"blocktimestamp":"2003-03-21T17:23:09.500","name":"transfer","contract":"eosio","receiver":"account2","data":"eyJmcm9tIjoiYWNjb3VudDEiLCJxdWFudGl0eSI6IjEwMDAuMDAwMCBXQVgiLCJ0byI6ImFjY291bnQyIn0=","authorization":[{"actor":"account1","permission":"active"}],"except":"errstr","error":2,"return":"3q2+7w=="}`
data, err := json_codec.Marshal(msg)
data, err := encoder(msg)
assert.NoError(t, err)
assert.Equal(t, expected, string(data))
}
@ -70,7 +70,7 @@ func TestJson_DecodeActionTrace(t *testing.T) {
input := `{"tx_id":"952989f7464237b6cf9926e533ecd331df6794ed07564bd052bc368cbd65b4bc","blocknum":8723971,"blocktimestamp":"2024-06-21T08:08:26.500","name":"transfer","contract":"eosio","receiver":"account2","data":"eyJmcm9tIjoiYWNjb3VudDEiLCJxdWFudGl0eSI6IjEwMDAuMDAwMCBXQVgiLCJ0byI6ImFjY291bnQyIn0=","authorization":[{"actor":"account1","permission":"active"}],"except":"errstr","error":2,"return":"3q2+7w=="}`
msg := message.ActionTrace{}
err = json_codec.Unmarshal([]byte(input), &msg)
err = decoder([]byte(input), &msg)
assert.NoError(t, err)
assert.Equal(t, expected, msg)
}