Skip to content

Commit

Permalink
converge all json marshal and unmarshal to less wordy
Browse files Browse the repository at this point in the history
also changes the interface of unmarshal to not return an error, which is something that should really belong elsewhere, both a thing to forget and a thing to have divergences in, not the right way to do it
  • Loading branch information
mleku committed Dec 7, 2024
1 parent 85eb4cf commit 5699562
Show file tree
Hide file tree
Showing 73 changed files with 357 additions and 616 deletions.
2 changes: 1 addition & 1 deletion atomic/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestBool_InitializeDefaults(t *testing.T) {

for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
t.Run("MarshalJSON", func(t *testing.T) {
t.Run("Marshal", func(t *testing.T) {
b := tt.newBool()
marshalled, err := b.MarshalJSON()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion atomic/internal/gen-atomicwrapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func run(args []st) er {
flag.BoolVar(&opts.Swap, "swap", false,
"generate a `Swap(new) old` method; requires -pack and -unpack")
flag.BoolVar(&opts.JSON, "json", false,
"generate `MarshalJSON/UnmarshJSON` methods")
"generate `Marshal/UnmarshJSON` methods")

if err := flag.Parse(args); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion auth/nip42_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCreateUnsigned(t *testing.T) {
t.Fatal(err)
}
if !ok {
bb, _ := ev.MarshalJSON(nil)
bb := ev.Marshal(nil)
t.Fatalf("failed to validate auth event\n%s", bb)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/realy/app/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
continue
}
kin := ints.New(uint16(0))
if _, err := kin.UnmarshalJSON(split[0]); chk.E(err) {
if _, err := kin.Unmarshal(split[0]); chk.E(err) {
return
}
kk := kind.New(kin.Uint16())
Expand Down
22 changes: 11 additions & 11 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ type Envelope interface {
}

type JSON interface {
// MarshalJSON converts the data of the type into JSON, appending it to the
// provided slice and returning the extended slice.
MarshalJSON(dst by) (b by, err er)
// UnmarshalJSON decodes a JSON form of a type back into the runtime form,
// and returns whatever remains after the type has been decoded out.
UnmarshalJSON(b by) (r by, err er)
// Marshal converts the data of the type into JSON, appending it to the provided
// slice and returning the extended slice.
Marshal(dst by) (b by)
// Unmarshal decodes a JSON form of a type back into the runtime form, and
// returns whatever remains after the type has been decoded out.
Unmarshal(b by) (r by, err er)
}

type Binary interface {
// MarshalBinary converts the data of the type into binary form, appending
// it to the provided slice.
MarshalBinary(dst by) (b by, err er)
// UnmarshalBinary decodes a binary form of a type back into the runtime
// form, and returns whatever remains after the type has been decoded out.
// MarshalBinary converts the data of the type into binary form, appending it to
// the provided slice.
MarshalBinary(dst by) (b by)
// UnmarshalBinary decodes a binary form of a type back into the runtime form,
// and returns whatever remains after the type has been decoded out.
UnmarshalBinary(b by) (r by, err er)
}
2 changes: 1 addition & 1 deletion ec/chainhash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestHashString(t *testing.T) {
// t.Errorf("String: wrong hash string - got %v, want %v",
// newHash.String(), hashStr)
// }
// err = newHash.UnmarshalJSON(legacyHashStr)
// err = newHash.Unmarshal(legacyHashStr)
// if err != nil {
// t.Errorf("Unmarshal legacy json error:%v, hash:%v", err, legacyHashStr)
// }
Expand Down
32 changes: 16 additions & 16 deletions envelopes/authenvelope/authenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ func (en *Challenge) Label() string { return L }

func (en *Challenge) Write(w io.Writer) (err er) {
var b by
if b, err = en.MarshalJSON(b); chk.E(err) {
return
}
b = en.Marshal(b)
log.T.F("writing out challenge envelope: '%s'", b)
_, err = w.Write(b)
return
}

func (en *Challenge) MarshalJSON(dst by) (b by, err er) {
func (en *Challenge) Marshal(dst by) (b by) {
b = dst
b, err = envs.Marshal(b, L,
func(bst by) (o by, err er) {
var err er
b = envs.Marshal(b, L,
func(bst by) (o by) {
o = bst
o = append(o, '"')
o = text.NostrEscape(o, en.Challenge)
o = append(o, '"')
return
})
_ = err
return
}

func (en *Challenge) UnmarshalJSON(b by) (r by, err er) {
func (en *Challenge) Unmarshal(b by) (r by, err er) {
r = b
if en.Challenge, r, err = text.UnmarshalQuoted(r); chk.E(err) {
return
Expand All @@ -58,7 +58,7 @@ func (en *Challenge) UnmarshalJSON(b by) (r by, err er) {

func ParseChallenge(b by) (t *Challenge, rem by, err er) {
t = NewChallenge()
if rem, err = t.UnmarshalJSON(b); chk.E(err) {
if rem, err = t.Unmarshal(b); chk.E(err) {
return
}
return
Expand All @@ -76,14 +76,13 @@ func (en *Response) Label() string { return L }

func (en *Response) Write(w io.Writer) (err er) {
var b by
if b, err = en.MarshalJSON(b); chk.E(err) {
return
}
b = en.Marshal(b)
_, err = w.Write(b)
return
}

func (en *Response) MarshalJSON(dst by) (b by, err er) {
func (en *Response) Marshal(dst by) (b by) {
var err er
if en == nil {
err = errorf.E("nil response")
return
Expand All @@ -93,15 +92,16 @@ func (en *Response) MarshalJSON(dst by) (b by, err er) {
return
}
b = dst
b, err = envs.Marshal(b, L, en.Event.MarshalJSON)
b = envs.Marshal(b, L, en.Event.Marshal)
_ = err
return
}

func (en *Response) UnmarshalJSON(b by) (r by, err er) {
func (en *Response) Unmarshal(b by) (r by, err er) {
r = b
// literally just unmarshal the event
en.Event = event.New()
if r, err = en.Event.UnmarshalJSON(r); chk.E(err) {
if r, err = en.Event.Unmarshal(r); chk.E(err) {
return
}
if r, err = envs.SkipToTheEnd(r); chk.E(err) {
Expand All @@ -112,7 +112,7 @@ func (en *Response) UnmarshalJSON(b by) (r by, err er) {

func ParseResponse(b by) (t *Response, rem by, err er) {
t = NewResponse()
if rem, err = t.UnmarshalJSON(b); chk.E(err) {
if rem, err = t.Unmarshal(b); chk.E(err) {
return
}
return
Expand Down
20 changes: 6 additions & 14 deletions envelopes/authenvelope/authenvelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ func TestAuth(t *testing.T) {
for _ = range 1000 {
ch := auth.GenerateChallenge()
chal := Challenge{Challenge: ch}
if b1, err = chal.MarshalJSON(b1); chk.E(err) {
t.Fatal(err)
}
b1 = chal.Marshal(b1)
oChal := make(by, len(b1))
copy(oChal, b1)
var rem by
Expand All @@ -34,7 +32,7 @@ func TestAuth(t *testing.T) {
t.Fatalf("invalid sentinel %s, expect %s", l, L)
}
c2 := NewChallenge()
if rem, err = c2.UnmarshalJSON(b1); chk.E(err) {
if rem, err = c2.Unmarshal(b1); chk.E(err) {
t.Fatal(err)
}
if len(rem) != 0 {
Expand All @@ -44,9 +42,7 @@ func TestAuth(t *testing.T) {
t.Fatalf("challenge mismatch\n%s\n%s",
chal.Challenge, c2.Challenge)
}
if b2, err = c2.MarshalJSON(b2); chk.E(err) {
t.Fatal(err)
}
b2 = c2.Marshal(b2)
if !equals(oChal, b2) {
t.Fatalf("challenge mismatch\n%s\n%s", oChal, b2)
}
Expand All @@ -55,9 +51,7 @@ func TestAuth(t *testing.T) {
if err = resp.Event.Sign(signer); chk.E(err) {
t.Fatal(err)
}
if b3, err = resp.MarshalJSON(b3); chk.E(err) {
t.Fatal(err)
}
b3 = resp.Marshal(b3)
oResp := make(by, len(b3))
copy(oResp, b3)
if l, b3, err = envelopes.Identify(b3); chk.E(err) {
Expand All @@ -67,12 +61,10 @@ func TestAuth(t *testing.T) {
t.Fatalf("invalid sentinel %s, expect %s", l, L)
}
r2 := NewResponse()
if _, err = r2.UnmarshalJSON(b3); chk.E(err) {
t.Fatal(err)
}
if b4, err = r2.MarshalJSON(b4); chk.E(err) {
if _, err = r2.Unmarshal(b3); chk.E(err) {
t.Fatal(err)
}
b4 = r2.Marshal(b4)
if !equals(oResp, b4) {
t.Fatalf("challenge mismatch\n%s\n%s", oResp, b4)
}
Expand Down
20 changes: 8 additions & 12 deletions envelopes/closedenvelope/closedenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,17 @@ func (en *T) ReasonString() string { return st(en.Reason) }

func (en *T) Write(w io.Writer) (err er) {
var b by
if b, err = en.MarshalJSON(b); chk.E(err) {
return
}
b = en.Marshal(b)
_, err = w.Write(b)
return
}

func (en *T) MarshalJSON(dst by) (b by, err er) {
func (en *T) Marshal(dst by) (b by) {
b = dst
b, err = envelopes.Marshal(b, L,
func(bst by) (o by, err er) {
b = envelopes.Marshal(b, L,
func(bst by) (o by) {
o = bst
if o, err = en.Subscription.MarshalJSON(o); chk.E(err) {
return
}
o = en.Subscription.Marshal(o)
o = append(o, ',')
o = append(o, '"')
o = text.NostrEscape(o, en.Reason)
Expand All @@ -49,12 +45,12 @@ func (en *T) MarshalJSON(dst by) (b by, err er) {
return
}

func (en *T) UnmarshalJSON(b by) (r by, err er) {
func (en *T) Unmarshal(b by) (r by, err er) {
r = b
if en.Subscription, err = subscription.NewId(by{0}); chk.E(err) {
return
}
if r, err = en.Subscription.UnmarshalJSON(r); chk.E(err) {
if r, err = en.Subscription.Unmarshal(r); chk.E(err) {
return
}
if en.Reason, r, err = text.UnmarshalQuoted(r); chk.E(err) {
Expand All @@ -68,7 +64,7 @@ func (en *T) UnmarshalJSON(b by) (r by, err er) {

func Parse(b by) (t *T, rem by, err er) {
t = New()
if rem, err = t.UnmarshalJSON(b); chk.E(err) {
if rem, err = t.Unmarshal(b); chk.E(err) {
return
}
return
Expand Down
13 changes: 5 additions & 8 deletions envelopes/closedenvelope/closedenvelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"lukechampine.com/frand"

"realy.lol/envelopes"
"realy.lol/subscription"
)
Expand All @@ -24,7 +25,7 @@ func RandomMessage() by {
return messages[frand.Intn(len(messages)-1)]
}

func TestMarshalJSONUnmarshalJSON(t *testing.T) {
func TestMarshalUnmarshal(t *testing.T) {
var err er
rb, rb1, rb2 := make(by, 0, 65535), make(by, 0, 65535), make(by, 0, 65535)
for _ = range 1000 {
Expand All @@ -33,9 +34,7 @@ func TestMarshalJSONUnmarshalJSON(t *testing.T) {
t.Fatal(err)
}
req := NewFrom(s, RandomMessage())
if rb, err = req.MarshalJSON(rb); chk.E(err) {
t.Fatal(err)
}
rb = req.Marshal(rb)
rb1 = rb1[:len(rb)]
copy(rb1, rb)
var rem by
Expand All @@ -47,17 +46,15 @@ func TestMarshalJSONUnmarshalJSON(t *testing.T) {
t.Fatalf("invalid sentinel %s, expect %s", l, L)
}
req2 := New()
if rem, err = req2.UnmarshalJSON(rb); chk.E(err) {
if rem, err = req2.Unmarshal(rb); chk.E(err) {
t.Fatal(err)
}
// log.I.Ln(req2.ID)
if len(rem) > 0 {
t.Fatalf("unmarshal failed, remainder\n%d %s",
len(rem), rem)
}
if rb2, err = req2.MarshalJSON(rb2); chk.E(err) {
t.Fatal(err)
}
rb2 = req2.Marshal(rb2)
if !equals(rb1, rb2) {
if len(rb1) != len(rb2) {
t.Fatalf("unmarshal failed, different lengths\n%d %s\n%d %s\n",
Expand Down
22 changes: 8 additions & 14 deletions envelopes/closeenvelope/closeenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,27 @@ func New() *T { return &T{ID: subscription.NewStd()} }
func NewFrom(id *subscription.Id) *T { return &T{ID: id} }
func (en *T) Label() string { return L }
func (en *T) Write(w io.Writer) (err er) {
var b by
if b, err = en.MarshalJSON(b); chk.E(err) {
return
}
_, err = w.Write(b)
_, err = w.Write(en.Marshal(nil))
return
}

func (en *T) MarshalJSON(dst by) (b by, err er) {
func (en *T) Marshal(dst by) (b by) {
b = dst
b, err = envelopes.Marshal(b, L,
func(bst by) (o by, err er) {
b = envelopes.Marshal(b, L,
func(bst by) (o by) {
o = bst
if o, err = en.ID.MarshalJSON(o); chk.E(err) {
return
}
o = en.ID.Marshal(o)
return
})
return
}

func (en *T) UnmarshalJSON(b by) (r by, err er) {
func (en *T) Unmarshal(b by) (r by, err er) {
r = b
if en.ID, err = subscription.NewId(by{0}); chk.E(err) {
return
}
if r, err = en.ID.UnmarshalJSON(r); chk.E(err) {
if r, err = en.ID.Unmarshal(r); chk.E(err) {
return
}
if r, err = envelopes.SkipToTheEnd(r); chk.E(err) {
Expand All @@ -57,7 +51,7 @@ func (en *T) UnmarshalJSON(b by) (r by, err er) {

func Parse(b by) (t *T, rem by, err er) {
t = New()
if rem, err = t.UnmarshalJSON(b); chk.E(err) {
if rem, err = t.Unmarshal(b); chk.E(err) {
return
}
return
Expand Down
Loading

0 comments on commit 5699562

Please sign in to comment.