Skip to content

Commit

Permalink
Add parsers for types.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Nov 20, 2024
1 parent 37d8c2a commit 709c644
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 12 deletions.
18 changes: 18 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,21 @@ func (a *Address) UnmarshalJSON(input []byte) error {
func (a Address) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", a.String())), nil
}

// Parse converts a string to an address.
func (a *Address) Parse(input string) (*Address, error) {
if err := a.UnmarshalJSON([]byte(fmt.Sprintf("%q", input))); err != nil {
return a, err
}

return a, nil
}

// MustParse converts a string to an address , panicking on error.
func (a *Address) MustParse(input string) *Address {
if _, err := a.Parse(input); err != nil {
panic(err)
}

return a
}
18 changes: 18 additions & 0 deletions types/amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,21 @@ func (a *Amount) UnmarshalJSON(input []byte) error {
func (a Amount) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", a.String())), nil
}

// Parse converts a string to an amount.
func (a *Amount) Parse(input string) (*Amount, error) {
if err := a.UnmarshalJSON([]byte(fmt.Sprintf("%q", input))); err != nil {
return a, err
}

return a, nil
}

// MustParse converts a string to an amount , panicking on error.
func (a *Amount) MustParse(input string) *Amount {
if _, err := a.Parse(input); err != nil {
panic(err)
}

return a
}
18 changes: 18 additions & 0 deletions types/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,21 @@ func (d *Data) UnmarshalJSON(input []byte) error {
func (d Data) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", d.String())), nil
}

// Parse converts a string to data.
func (d *Data) Parse(input string) (*Data, error) {
if err := d.UnmarshalJSON([]byte(fmt.Sprintf("%q", input))); err != nil {
return d, err
}

return d, nil
}

// MustParse converts a string to data, panicking on error.
func (d *Data) MustParse(input string) *Data {
if _, err := d.Parse(input); err != nil {
panic(err)
}

return d
}
18 changes: 18 additions & 0 deletions types/fieldelement.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,21 @@ func (f *FieldElement) UnmarshalJSON(input []byte) error {
func (f FieldElement) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, f.String())), nil
}

// Parse converts a string to a field element.
func (f *FieldElement) Parse(input string) (*FieldElement, error) {
if err := f.UnmarshalJSON([]byte(fmt.Sprintf("%q", input))); err != nil {
return f, err
}

return f, nil
}

// MustParse converts a string to a field element, panicking on error.
func (f *FieldElement) MustParse(input string) *FieldElement {
if _, err := f.Parse(input); err != nil {
panic(err)
}

return f
}
18 changes: 18 additions & 0 deletions types/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,21 @@ func (h *Hash) UnmarshalJSON(input []byte) error {
func (h Hash) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, h.String())), nil
}

// Parse converts a string to a hash.
func (h *Hash) Parse(input string) (*Hash, error) {
if err := h.UnmarshalJSON([]byte(fmt.Sprintf("%q", input))); err != nil {
return h, err
}

return h, nil
}

// MustParse converts a string to a hash, panicking on error.
func (h *Hash) MustParse(input string) *Hash {
if _, err := h.Parse(input); err != nil {
panic(err)
}

return h
}
18 changes: 18 additions & 0 deletions types/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,21 @@ func (n *Number) UnmarshalJSON(input []byte) error {
func (n Number) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", n.String())), nil
}

// Parse converts a string to a number.
func (n *Number) Parse(input string) (*Number, error) {
if err := n.UnmarshalJSON([]byte(fmt.Sprintf("%q", input))); err != nil {
return n, err
}

return n, nil
}

// MustParse converts a string to a number, panicking on error.
func (n *Number) MustParse(input string) *Number {
if _, err := n.Parse(input); err != nil {
panic(err)
}

return n
}
42 changes: 30 additions & 12 deletions types/publickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ type PublicKey [PublicKeyLength]byte
var zeroPublicKey = PublicKey{}

// IsZero returns true if the public key is zero.
func (a *PublicKey) IsZero() bool {
return bytes.Equal(a[:], zeroPublicKey[:])
func (p *PublicKey) IsZero() bool {
return bytes.Equal(p[:], zeroPublicKey[:])
}

// String returns the string representation of the public key.
func (a *PublicKey) String() string {
res := hex.EncodeToString(a[:])
func (p *PublicKey) String() string {
res := hex.EncodeToString(p[:])
// Leading 0s not allowed...
res = strings.TrimLeft(res, "0")
// ...unless that's all there was.
Expand All @@ -50,23 +50,23 @@ func (a *PublicKey) String() string {
}

// Format formats the address.
func (a *PublicKey) Format(state fmt.State, v rune) {
func (p *PublicKey) Format(state fmt.State, v rune) {
format := string(v)
switch v {
case 's':
fmt.Fprint(state, a.String())
fmt.Fprint(state, p.String())
case 'x', 'X':
if state.Flag('#') {
format = "#" + format
}
fmt.Fprintf(state, "%"+format, a[:])
fmt.Fprintf(state, "%"+format, p[:])
default:
fmt.Fprintf(state, "%"+format, a[:])
fmt.Fprintf(state, "%"+format, p[:])
}
}

// UnmarshalJSON implements json.Unmarshaler.
func (a *PublicKey) UnmarshalJSON(input []byte) error {
func (p *PublicKey) UnmarshalJSON(input []byte) error {
if len(input) == 0 {
return errors.New("address missing")
}
Expand All @@ -88,12 +88,30 @@ func (a *PublicKey) UnmarshalJSON(input []byte) error {
if err != nil {
return errors.New("invalid address")
}
copy(a[len(a)-len(val):], val)
copy(p[len(p)-len(val):], val)

return nil
}

// MarshalJSON implements json.Marshaler.
func (a PublicKey) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", a.String())), nil
func (p PublicKey) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", p.String())), nil
}

// Parse converts a string to a public key.
func (p *PublicKey) Parse(input string) (*PublicKey, error) {
if err := p.UnmarshalJSON([]byte(fmt.Sprintf("%q", input))); err != nil {
return p, err
}

return p, nil
}

// MustParse converts a string to a public key, panicking on error.
func (p *PublicKey) MustParse(input string) *PublicKey {
if _, err := p.Parse(input); err != nil {
panic(err)
}

return p
}
18 changes: 18 additions & 0 deletions types/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,21 @@ func (r *Root) UnmarshalJSON(input []byte) error {
func (r Root) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, r.String())), nil
}

// Parse converts a string to a root.
func (r *Root) Parse(input string) (*Root, error) {
if err := r.UnmarshalJSON([]byte(fmt.Sprintf("%q", input))); err != nil {
return r, err
}

return r, nil
}

// MustParse converts a string to a root, panicking on error.
func (r *Root) MustParse(input string) *Root {
if _, err := r.Parse(input); err != nil {
panic(err)
}

return r
}

0 comments on commit 709c644

Please sign in to comment.