Skip to content

Commit

Permalink
implement json marshalling on asset id and logic id
Browse files Browse the repository at this point in the history
  • Loading branch information
sarvalabs-manish committed Dec 21, 2023
1 parent 0576895 commit 66ce4d1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
35 changes: 34 additions & 1 deletion assetid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package identifiers

import (
"encoding/json"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -108,7 +110,7 @@ func (asset AssetID) Identifier() (AssetIdentifier, error) {

// MarshalText implements the encoding.TextMarshaler interface for AssetID
func (asset AssetID) MarshalText() ([]byte, error) {
return []byte("0x" + string(asset)), nil
return []byte(asset.String()), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface for AssetID
Expand All @@ -129,3 +131,34 @@ func (asset *AssetID) UnmarshalText(text []byte) error {

return nil
}

// MarshalJSON implements the json.Marshaler interface for AssetID
func (asset AssetID) MarshalJSON() ([]byte, error) {
return json.Marshal(asset.String())
}

// UnmarshalJSON implements the json.Unmarshaler interface for AssetID
func (asset *AssetID) UnmarshalJSON(data []byte) error {
var decoded string

// Decode the JSON data into a string
if err := json.Unmarshal(data, &decoded); err != nil {
return err
}

// Assert that the 0x prefix exists
if !has0xPrefixString(decoded) {
return ErrMissing0xPrefix
}

// Trim the 0x prefix
decoded = trim0xPrefixString(decoded)
// Generate an identifier for the AssetID
if _, err := AssetID(decoded).Identifier(); err != nil {
return err
}

*asset = AssetID(decoded)

return nil
}
4 changes: 4 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func trim0xPrefixString(value string) string {
return strings.TrimPrefix(value, prefix0xString)
}

func has0xPrefixString(value string) bool {
return strings.HasPrefix(value, prefix0xString)
}

func trim0xPrefixBytes(value []byte) []byte {
return bytes.TrimPrefix(value, prefix0xBytes)
}
Expand Down
35 changes: 34 additions & 1 deletion logicid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package identifiers

import (
"encoding/json"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -110,7 +112,7 @@ func (logic LogicID) Identifier() (LogicIdentifier, error) {

// MarshalText implements the encoding.TextMarshaler interface for LogicID
func (logic LogicID) MarshalText() ([]byte, error) {
return []byte("0x" + string(logic)), nil
return []byte(logic.String()), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface for LogicID
Expand All @@ -131,3 +133,34 @@ func (logic *LogicID) UnmarshalText(text []byte) error {

return nil
}

// MarshalJSON implements the json.Marshaler interface for LogicID
func (logic LogicID) MarshalJSON() ([]byte, error) {
return json.Marshal(logic.String())
}

// UnmarshalJSON implements the json.Unmarshaler interface for LogicID
func (logic *LogicID) UnmarshalJSON(data []byte) error {
var decoded string

// Decode the JSON data into a string
if err := json.Unmarshal(data, &decoded); err != nil {
return err
}

// Assert that the 0x prefix exists
if !has0xPrefixString(decoded) {
return ErrMissing0xPrefix
}

// Trim the 0x prefix
decoded = trim0xPrefixString(decoded)
// Generate an identifier for the AssetID
if _, err := LogicID(decoded).Identifier(); err != nil {
return err
}

*logic = LogicID(decoded)

return nil
}

0 comments on commit 66ce4d1

Please sign in to comment.