From 16aa5c95f391c772bc4008f9ed47e47ddd0631e7 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 15 May 2025 14:00:03 -0300 Subject: [PATCH] simplify easyjson parser logic by assuming there will be no `null`s. --- event_easyjson.go | 50 +++++++---------- filter_easyjson.go | 135 ++++++++++++++++++--------------------------- types_test.go | 14 ++--- 3 files changed, 81 insertions(+), 118 deletions(-) diff --git a/event_easyjson.go b/event_easyjson.go index fa6f63c..190257c 100644 --- a/event_easyjson.go +++ b/event_easyjson.go @@ -35,43 +35,33 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event) case "kind": out.Kind = Kind(in.Int()) case "tags": - if in.IsNull() { - in.Skip() - out.Tags = nil - } else { - in.Delim('[') - if out.Tags == nil { - if !in.IsDelim(']') { - out.Tags = make(Tags, 0, 7) - } else { - out.Tags = Tags{} - } + in.Delim('[') + if out.Tags == nil { + if !in.IsDelim(']') { + out.Tags = make(Tags, 0, 7) } else { - out.Tags = (out.Tags)[:0] + out.Tags = Tags{} + } + } else { + out.Tags = (out.Tags)[:0] + } + for !in.IsDelim(']') { + var v Tag + in.Delim('[') + if !in.IsDelim(']') { + v = make(Tag, 0, 5) + } else { + v = Tag{} } for !in.IsDelim(']') { - var v Tag - if in.IsNull() { - in.Skip() - v = nil - } else { - in.Delim('[') - if !in.IsDelim(']') { - v = make(Tag, 0, 5) - } else { - v = Tag{} - } - for !in.IsDelim(']') { - v = append(v, in.String()) - in.WantComma() - } - in.Delim(']') - } - out.Tags = append(out.Tags, v) + v = append(v, in.String()) in.WantComma() } in.Delim(']') + out.Tags = append(out.Tags, v) + in.WantComma() } + in.Delim(']') case "content": out.Content = in.String() case "sig": diff --git a/filter_easyjson.go b/filter_easyjson.go index 55b0658..72b194a 100644 --- a/filter_easyjson.go +++ b/filter_easyjson.go @@ -28,86 +28,61 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter) } switch key { case "ids": - if in.IsNull() { - in.Skip() - out.IDs = nil - } else { - in.Delim('[') - if out.IDs == nil { - if !in.IsDelim(']') { - out.IDs = make([]ID, 0, 20) - } else { - out.IDs = []ID{} - } + in.Delim('[') + if out.IDs == nil { + if !in.IsDelim(']') { + out.IDs = make([]ID, 0, 20) } else { - out.IDs = (out.IDs)[:0] + out.IDs = []ID{} } - for !in.IsDelim(']') { - id := [32]byte{} - hex.Decode(id[:], in.UnsafeBytes()) - out.IDs = append(out.IDs, id) - in.WantComma() - } - in.Delim(']') + } else { + out.IDs = (out.IDs)[:0] } + for !in.IsDelim(']') { + id := [32]byte{} + hex.Decode(id[:], in.UnsafeBytes()) + out.IDs = append(out.IDs, id) + in.WantComma() + } + in.Delim(']') case "kinds": - if in.IsNull() { - in.Skip() - out.Kinds = nil - } else { - in.Delim('[') - if out.Kinds == nil { - if !in.IsDelim(']') { - out.Kinds = make([]Kind, 0, 8) - } else { - out.Kinds = []Kind{} - } + in.Delim('[') + if out.Kinds == nil { + if !in.IsDelim(']') { + out.Kinds = make([]Kind, 0, 8) } else { - out.Kinds = (out.Kinds)[:0] + out.Kinds = []Kind{} } - for !in.IsDelim(']') { - out.Kinds = append(out.Kinds, Kind(in.Int())) - in.WantComma() - } - in.Delim(']') + } else { + out.Kinds = (out.Kinds)[:0] } + for !in.IsDelim(']') { + out.Kinds = append(out.Kinds, Kind(in.Int())) + in.WantComma() + } + in.Delim(']') case "authors": - if in.IsNull() { - in.Skip() - out.Authors = nil - } else { - in.Delim('[') - if out.Authors == nil { - if !in.IsDelim(']') { - out.Authors = make([]PubKey, 0, 40) - } else { - out.Authors = []PubKey{} - } + in.Delim('[') + if out.Authors == nil { + if !in.IsDelim(']') { + out.Authors = make([]PubKey, 0, 40) } else { - out.Authors = (out.Authors)[:0] + out.Authors = []PubKey{} } - for !in.IsDelim(']') { - pk := [32]byte{} - hex.Decode(pk[:], in.UnsafeBytes()) - out.Authors = append(out.Authors, pk) - in.WantComma() - } - in.Delim(']') + } else { + out.Authors = (out.Authors)[:0] } + for !in.IsDelim(']') { + pk := [32]byte{} + hex.Decode(pk[:], in.UnsafeBytes()) + out.Authors = append(out.Authors, pk) + in.WantComma() + } + in.Delim(']') case "since": - if in.IsNull() { - in.Skip() - out.Since = 0 - } else { - out.Since = Timestamp(in.Int64()) - } + out.Since = Timestamp(in.Int64()) case "until": - if in.IsNull() { - in.Skip() - out.Until = 0 - } else { - out.Until = Timestamp(in.Int64()) - } + out.Until = Timestamp(in.Int64()) case "limit": out.Limit = int(in.Int()) if out.Limit == 0 { @@ -118,23 +93,21 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter) default: if len(key) > 1 && key[0] == '#' { tagValues := make([]string, 0, 40) - if !in.IsNull() { - in.Delim('[') - if out.Authors == nil { - if !in.IsDelim(']') { - tagValues = make([]string, 0, 4) - } else { - tagValues = []string{} - } + in.Delim('[') + if out.Authors == nil { + if !in.IsDelim(']') { + tagValues = make([]string, 0, 4) } else { - tagValues = (tagValues)[:0] + tagValues = []string{} } - for !in.IsDelim(']') { - tagValues = append(tagValues, in.String()) - in.WantComma() - } - in.Delim(']') + } else { + tagValues = (tagValues)[:0] } + for !in.IsDelim(']') { + tagValues = append(tagValues, in.String()) + in.WantComma() + } + in.Delim(']') out.Tags[key[1:]] = tagValues } else { in.SkipRecursive() diff --git a/types_test.go b/types_test.go index 39b3e1a..d95fa64 100644 --- a/types_test.go +++ b/types_test.go @@ -7,12 +7,12 @@ import ( ) func TestIDJSONEncoding(t *testing.T) { - id := MustIDFromHex("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789") + id := MustIDFromHex("6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904") // test marshaling b, err := json.Marshal(id) require.NoError(t, err) - require.Equal(t, `"abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"`, string(b)) + require.Equal(t, `"6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904"`, string(b)) // test unmarshaling var id2 ID @@ -30,12 +30,12 @@ func TestIDJSONEncoding(t *testing.T) { } func TestPubKeyJSONEncoding(t *testing.T) { - pk := MustPubKeyFromHex("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789") + pk := MustPubKeyFromHex("6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904") // test marshaling b, err := json.Marshal(pk) require.NoError(t, err) - require.Equal(t, `"abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"`, string(b)) + require.Equal(t, `"6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904"`, string(b)) // test unmarshaling var pk2 PubKey @@ -60,7 +60,7 @@ type TestStruct struct { func TestStructWithIDAndPubKey(t *testing.T) { ts := TestStruct{ - ID: MustIDFromHex("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"), + ID: MustIDFromHex("6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904"), PubKey: MustPubKeyFromHex("123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0"), Name: "test", } @@ -68,7 +68,7 @@ func TestStructWithIDAndPubKey(t *testing.T) { // test marshaling b, err := json.Marshal(ts) require.NoError(t, err) - require.Equal(t, `{"id":"abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789","pubkey":"123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0","name":"test"}`, string(b)) + require.Equal(t, `{"id":"6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904","pubkey":"123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0","name":"test"}`, string(b)) // test unmarshaling var ts2 TestStruct @@ -89,6 +89,6 @@ func TestStructWithIDAndPubKey(t *testing.T) { require.Error(t, err) // test unmarshaling with invalid PubKey - err = json.Unmarshal([]byte(`{"id":"abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789","pubkey":"invalid","name":"test"}`), &ts2) + err = json.Unmarshal([]byte(`{"id":"6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904","pubkey":"invalid","name":"test"}`), &ts2) require.Error(t, err) }