simplify easyjson parser logic by assuming there will be no nulls.

This commit is contained in:
fiatjaf
2025-05-15 14:00:03 -03:00
parent a37b513477
commit 16aa5c95f3
3 changed files with 81 additions and 118 deletions

View File

@@ -35,10 +35,6 @@ 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(']') {
@@ -51,10 +47,6 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event)
}
for !in.IsDelim(']') {
var v Tag
if in.IsNull() {
in.Skip()
v = nil
} else {
in.Delim('[')
if !in.IsDelim(']') {
v = make(Tag, 0, 5)
@@ -66,12 +58,10 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event)
in.WantComma()
}
in.Delim(']')
}
out.Tags = append(out.Tags, v)
in.WantComma()
}
in.Delim(']')
}
case "content":
out.Content = in.String()
case "sig":

View File

@@ -28,10 +28,6 @@ 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(']') {
@@ -49,12 +45,7 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
in.WantComma()
}
in.Delim(']')
}
case "kinds":
if in.IsNull() {
in.Skip()
out.Kinds = nil
} else {
in.Delim('[')
if out.Kinds == nil {
if !in.IsDelim(']') {
@@ -70,12 +61,7 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
in.WantComma()
}
in.Delim(']')
}
case "authors":
if in.IsNull() {
in.Skip()
out.Authors = nil
} else {
in.Delim('[')
if out.Authors == nil {
if !in.IsDelim(']') {
@@ -93,21 +79,10 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
in.WantComma()
}
in.Delim(']')
}
case "since":
if in.IsNull() {
in.Skip()
out.Since = 0
} else {
out.Since = Timestamp(in.Int64())
}
case "until":
if in.IsNull() {
in.Skip()
out.Until = 0
} else {
out.Until = Timestamp(in.Int64())
}
case "limit":
out.Limit = int(in.Int())
if out.Limit == 0 {
@@ -118,7 +93,6 @@ 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(']') {
@@ -134,7 +108,6 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
in.WantComma()
}
in.Delim(']')
}
out.Tags[key[1:]] = tagValues
} else {
in.SkipRecursive()

View File

@@ -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)
}