diff --git a/types/address.go b/types/address.go index b7c9f5a..b1e4b05 100644 --- a/types/address.go +++ b/types/address.go @@ -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 +} diff --git a/types/amount.go b/types/amount.go index 3bb8366..2a8227f 100644 --- a/types/amount.go +++ b/types/amount.go @@ -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 +} diff --git a/types/data.go b/types/data.go index 86fcd3f..a3b915a 100644 --- a/types/data.go +++ b/types/data.go @@ -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 +} diff --git a/types/fieldelement.go b/types/fieldelement.go index 83a2c4a..22935c1 100644 --- a/types/fieldelement.go +++ b/types/fieldelement.go @@ -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 +} diff --git a/types/hash.go b/types/hash.go index 31efd0d..90f6b04 100644 --- a/types/hash.go +++ b/types/hash.go @@ -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 +} diff --git a/types/number.go b/types/number.go index 96d202f..b4fb2f1 100644 --- a/types/number.go +++ b/types/number.go @@ -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 +} diff --git a/types/publickey.go b/types/publickey.go index 9fb2472..1e91c61 100644 --- a/types/publickey.go +++ b/types/publickey.go @@ -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. @@ -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") } @@ -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 } diff --git a/types/root.go b/types/root.go index 714acc8..9be9959 100644 --- a/types/root.go +++ b/types/root.go @@ -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 +}