Skip to content

Commit

Permalink
Replace UnmarshalJSON() with UnmarshalText() for transaction statuses (
Browse files Browse the repository at this point in the history
…#2220)

* Replace UnmarshalJSON() with UnmarshalText() for transaction statuses

UnmarshalText avoids issues with forgetting quotes in JSON, making it simpler for parsing plain text values.
  • Loading branch information
kirugan authored Oct 16, 2024
1 parent 935b903 commit ca006de
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
54 changes: 23 additions & 31 deletions starknet/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ const (
Rejected
)

func (es *ExecutionStatus) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"SUCCEEDED"`:
func (es *ExecutionStatus) UnmarshalText(data []byte) error {
switch str := string(data); str {
case "SUCCEEDED":
*es = Succeeded
case `"REVERTED"`:
case "REVERTED":
*es = Reverted
case `"REJECTED"`:
case "REJECTED":
*es = Rejected
default:
return errors.New("unknown ExecutionStatus")
return fmt.Errorf("unknown ExecutionStatus %q", str)
}
return nil
}
Expand All @@ -39,18 +39,18 @@ const (
Received
)

func (fs *FinalityStatus) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"ACCEPTED_ON_L2"`:
func (fs *FinalityStatus) UnmarshalText(data []byte) error {
switch str := string(data); str {
case "ACCEPTED_ON_L2":
*fs = AcceptedOnL2
case `"ACCEPTED_ON_L1"`:
case "ACCEPTED_ON_L1":
*fs = AcceptedOnL1
case `"NOT_RECEIVED"`:
case "NOT_RECEIVED":
*fs = NotReceived
case `"RECEIVED"`:
case "RECEIVED":
*fs = Received
default:
return errors.New("unknown FinalityStatus")
return fmt.Errorf("unknown FinalityStatus %q", str)
}
return nil
}
Expand Down Expand Up @@ -83,24 +83,24 @@ func (t TransactionType) String() string {
}
}

func (t TransactionType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", t)), nil
func (t TransactionType) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}

func (t *TransactionType) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"DECLARE"`:
func (t *TransactionType) UnmarshalText(data []byte) error {
switch str := string(data); str {
case "DECLARE":
*t = TxnDeclare
case `"DEPLOY"`:
case "DEPLOY":
*t = TxnDeploy
case `"DEPLOY_ACCOUNT"`:
case "DEPLOY_ACCOUNT":
*t = TxnDeployAccount
case `"INVOKE"`, `"INVOKE_FUNCTION"`:
case "INVOKE", "INVOKE_FUNCTION":
*t = TxnInvoke
case `"L1_HANDLER"`:
case "L1_HANDLER":
*t = TxnL1Handler
default:
return errors.New("unknown TransactionType")
return fmt.Errorf("unknown TransactionType %q", str)
}
return nil
}
Expand Down Expand Up @@ -139,14 +139,6 @@ func (r Resource) MarshalText() ([]byte, error) {
}
}

func (r Resource) MarshalJSON() ([]byte, error) {
result, err := r.MarshalText()
if err != nil {
return nil, err
}
return []byte(`"` + string(result) + `"`), nil
}

type DataAvailabilityMode uint32

const (
Expand Down
38 changes: 26 additions & 12 deletions starknet/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package starknet_test

import (
"encoding/json"
"testing"

"github.com/NethermindEth/juno/starknet"
Expand All @@ -10,22 +11,35 @@ import (

func TestUnmarshalExecutionStatus(t *testing.T) {
es := new(starknet.ExecutionStatus)
require.NoError(t, es.UnmarshalJSON([]byte(`"SUCCEEDED"`)))
assert.Equal(t, starknet.Succeeded, *es)

require.NoError(t, es.UnmarshalJSON([]byte(`"REVERTED"`)))
assert.Equal(t, starknet.Reverted, *es)

require.ErrorContains(t, es.UnmarshalJSON([]byte("ABC")), "unknown ExecutionStatus")
cases := map[string]starknet.ExecutionStatus{
"SUCCEEDED": starknet.Succeeded,
"REVERTED": starknet.Reverted,
"REJECTED": starknet.Rejected,
}
for str, expected := range cases {
quotedStr := `"` + str + `"`
require.NoError(t, json.Unmarshal([]byte(quotedStr), es))
assert.Equal(t, expected, *es)
}

require.ErrorContains(t, json.Unmarshal([]byte(`"ABC"`), es), "unknown ExecutionStatus")
}

func TestUnmarshalFinalityStatus(t *testing.T) {
fs := new(starknet.FinalityStatus)
require.NoError(t, fs.UnmarshalJSON([]byte(`"ACCEPTED_ON_L1"`)))
assert.Equal(t, starknet.AcceptedOnL1, *fs)

require.NoError(t, fs.UnmarshalJSON([]byte(`"ACCEPTED_ON_L2"`)))
assert.Equal(t, starknet.AcceptedOnL2, *fs)

require.ErrorContains(t, fs.UnmarshalJSON([]byte("ABC")), "unknown FinalityStatus")
cases := map[string]starknet.FinalityStatus{
"ACCEPTED_ON_L2": starknet.AcceptedOnL2,
"ACCEPTED_ON_L1": starknet.AcceptedOnL1,
"NOT_RECEIVED": starknet.NotReceived,
"RECEIVED": starknet.Received,
}
for str, expected := range cases {
quotedStr := `"` + str + `"`
require.NoError(t, json.Unmarshal([]byte(quotedStr), fs))
assert.Equal(t, expected, *fs)
}

require.ErrorContains(t, json.Unmarshal([]byte(`"ABC"`), fs), "unknown FinalityStatus")
}

0 comments on commit ca006de

Please sign in to comment.