Skip to content

Commit 598d4f9

Browse files
committed
feat: add gql marshal/unmarshal
1 parent 82ab79e commit 598d4f9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

account.go

+16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
910
"regexp"
11+
"strconv"
1012
"strings"
1113

1214
"github.com/ethereum/go-ethereum/common"
@@ -122,6 +124,20 @@ func (c *AccountID) Scan(src interface{}) error {
122124
return nil
123125
}
124126

127+
func (c AccountID) MarshalGQL(w io.Writer) {
128+
fmt.Fprint(w, strconv.Quote(strings.ToUpper(c.String())))
129+
}
130+
131+
func (c *AccountID) UnmarshalGQL(v interface{}) error {
132+
if id, ok := v.(string); ok {
133+
if err := c.Parse(id); err != nil {
134+
return fmt.Errorf("unmarshalling account id: %w", err)
135+
}
136+
}
137+
138+
return nil
139+
}
140+
125141
type EVMAccountID struct {
126142
EVMAddressable
127143
AccountID

asset.go

+16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
910
"regexp"
11+
"strconv"
1012
"strings"
1113

1214
"github.com/ethereum/go-ethereum/common"
@@ -130,6 +132,20 @@ func (a *AssetID) Scan(src interface{}) error {
130132
return nil
131133
}
132134

135+
func (a AssetID) MarshalGQL(w io.Writer) {
136+
fmt.Fprint(w, strconv.Quote(strings.ToUpper(a.String())))
137+
}
138+
139+
func (a *AssetID) UnmarshalGQL(v interface{}) error {
140+
if id, ok := v.(string); ok {
141+
if err := a.Parse(id); err != nil {
142+
return fmt.Errorf("unmarshalling asset id: %w", err)
143+
}
144+
}
145+
146+
return nil
147+
}
148+
133149
type EVMAssetID struct {
134150
EVMAddressable
135151
AssetID

chain.go

+16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
910
"regexp"
11+
"strconv"
1012
"strings"
1113
)
1214

@@ -116,3 +118,17 @@ func (c *ChainID) Scan(src interface{}) error {
116118

117119
return nil
118120
}
121+
122+
func (c ChainID) MarshalGQL(w io.Writer) {
123+
fmt.Fprint(w, strconv.Quote(strings.ToUpper(c.String())))
124+
}
125+
126+
func (c *ChainID) UnmarshalGQL(v interface{}) error {
127+
if id, ok := v.(string); ok {
128+
if err := c.Parse(id); err != nil {
129+
return fmt.Errorf("unmarshalling account id: %w", err)
130+
}
131+
}
132+
133+
return nil
134+
}

0 commit comments

Comments
 (0)