simplify easyjson parser logic by assuming there will be no nulls.
This commit is contained in:
@@ -35,43 +35,33 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event)
|
|||||||
case "kind":
|
case "kind":
|
||||||
out.Kind = Kind(in.Int())
|
out.Kind = Kind(in.Int())
|
||||||
case "tags":
|
case "tags":
|
||||||
if in.IsNull() {
|
in.Delim('[')
|
||||||
in.Skip()
|
if out.Tags == nil {
|
||||||
out.Tags = nil
|
if !in.IsDelim(']') {
|
||||||
} else {
|
out.Tags = make(Tags, 0, 7)
|
||||||
in.Delim('[')
|
|
||||||
if out.Tags == nil {
|
|
||||||
if !in.IsDelim(']') {
|
|
||||||
out.Tags = make(Tags, 0, 7)
|
|
||||||
} else {
|
|
||||||
out.Tags = Tags{}
|
|
||||||
}
|
|
||||||
} else {
|
} 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(']') {
|
for !in.IsDelim(']') {
|
||||||
var v Tag
|
v = append(v, in.String())
|
||||||
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)
|
|
||||||
in.WantComma()
|
in.WantComma()
|
||||||
}
|
}
|
||||||
in.Delim(']')
|
in.Delim(']')
|
||||||
|
out.Tags = append(out.Tags, v)
|
||||||
|
in.WantComma()
|
||||||
}
|
}
|
||||||
|
in.Delim(']')
|
||||||
case "content":
|
case "content":
|
||||||
out.Content = in.String()
|
out.Content = in.String()
|
||||||
case "sig":
|
case "sig":
|
||||||
|
|||||||
@@ -28,86 +28,61 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
|
|||||||
}
|
}
|
||||||
switch key {
|
switch key {
|
||||||
case "ids":
|
case "ids":
|
||||||
if in.IsNull() {
|
in.Delim('[')
|
||||||
in.Skip()
|
if out.IDs == nil {
|
||||||
out.IDs = nil
|
if !in.IsDelim(']') {
|
||||||
} else {
|
out.IDs = make([]ID, 0, 20)
|
||||||
in.Delim('[')
|
|
||||||
if out.IDs == nil {
|
|
||||||
if !in.IsDelim(']') {
|
|
||||||
out.IDs = make([]ID, 0, 20)
|
|
||||||
} else {
|
|
||||||
out.IDs = []ID{}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
out.IDs = (out.IDs)[:0]
|
out.IDs = []ID{}
|
||||||
}
|
}
|
||||||
for !in.IsDelim(']') {
|
} else {
|
||||||
id := [32]byte{}
|
out.IDs = (out.IDs)[:0]
|
||||||
hex.Decode(id[:], in.UnsafeBytes())
|
|
||||||
out.IDs = append(out.IDs, id)
|
|
||||||
in.WantComma()
|
|
||||||
}
|
|
||||||
in.Delim(']')
|
|
||||||
}
|
}
|
||||||
|
for !in.IsDelim(']') {
|
||||||
|
id := [32]byte{}
|
||||||
|
hex.Decode(id[:], in.UnsafeBytes())
|
||||||
|
out.IDs = append(out.IDs, id)
|
||||||
|
in.WantComma()
|
||||||
|
}
|
||||||
|
in.Delim(']')
|
||||||
case "kinds":
|
case "kinds":
|
||||||
if in.IsNull() {
|
in.Delim('[')
|
||||||
in.Skip()
|
if out.Kinds == nil {
|
||||||
out.Kinds = nil
|
if !in.IsDelim(']') {
|
||||||
} else {
|
out.Kinds = make([]Kind, 0, 8)
|
||||||
in.Delim('[')
|
|
||||||
if out.Kinds == nil {
|
|
||||||
if !in.IsDelim(']') {
|
|
||||||
out.Kinds = make([]Kind, 0, 8)
|
|
||||||
} else {
|
|
||||||
out.Kinds = []Kind{}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
out.Kinds = (out.Kinds)[:0]
|
out.Kinds = []Kind{}
|
||||||
}
|
}
|
||||||
for !in.IsDelim(']') {
|
} else {
|
||||||
out.Kinds = append(out.Kinds, Kind(in.Int()))
|
out.Kinds = (out.Kinds)[:0]
|
||||||
in.WantComma()
|
|
||||||
}
|
|
||||||
in.Delim(']')
|
|
||||||
}
|
}
|
||||||
|
for !in.IsDelim(']') {
|
||||||
|
out.Kinds = append(out.Kinds, Kind(in.Int()))
|
||||||
|
in.WantComma()
|
||||||
|
}
|
||||||
|
in.Delim(']')
|
||||||
case "authors":
|
case "authors":
|
||||||
if in.IsNull() {
|
in.Delim('[')
|
||||||
in.Skip()
|
if out.Authors == nil {
|
||||||
out.Authors = nil
|
if !in.IsDelim(']') {
|
||||||
} else {
|
out.Authors = make([]PubKey, 0, 40)
|
||||||
in.Delim('[')
|
|
||||||
if out.Authors == nil {
|
|
||||||
if !in.IsDelim(']') {
|
|
||||||
out.Authors = make([]PubKey, 0, 40)
|
|
||||||
} else {
|
|
||||||
out.Authors = []PubKey{}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
out.Authors = (out.Authors)[:0]
|
out.Authors = []PubKey{}
|
||||||
}
|
}
|
||||||
for !in.IsDelim(']') {
|
} else {
|
||||||
pk := [32]byte{}
|
out.Authors = (out.Authors)[:0]
|
||||||
hex.Decode(pk[:], in.UnsafeBytes())
|
|
||||||
out.Authors = append(out.Authors, pk)
|
|
||||||
in.WantComma()
|
|
||||||
}
|
|
||||||
in.Delim(']')
|
|
||||||
}
|
}
|
||||||
|
for !in.IsDelim(']') {
|
||||||
|
pk := [32]byte{}
|
||||||
|
hex.Decode(pk[:], in.UnsafeBytes())
|
||||||
|
out.Authors = append(out.Authors, pk)
|
||||||
|
in.WantComma()
|
||||||
|
}
|
||||||
|
in.Delim(']')
|
||||||
case "since":
|
case "since":
|
||||||
if in.IsNull() {
|
out.Since = Timestamp(in.Int64())
|
||||||
in.Skip()
|
|
||||||
out.Since = 0
|
|
||||||
} else {
|
|
||||||
out.Since = Timestamp(in.Int64())
|
|
||||||
}
|
|
||||||
case "until":
|
case "until":
|
||||||
if in.IsNull() {
|
out.Until = Timestamp(in.Int64())
|
||||||
in.Skip()
|
|
||||||
out.Until = 0
|
|
||||||
} else {
|
|
||||||
out.Until = Timestamp(in.Int64())
|
|
||||||
}
|
|
||||||
case "limit":
|
case "limit":
|
||||||
out.Limit = int(in.Int())
|
out.Limit = int(in.Int())
|
||||||
if out.Limit == 0 {
|
if out.Limit == 0 {
|
||||||
@@ -118,23 +93,21 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
|
|||||||
default:
|
default:
|
||||||
if len(key) > 1 && key[0] == '#' {
|
if len(key) > 1 && key[0] == '#' {
|
||||||
tagValues := make([]string, 0, 40)
|
tagValues := make([]string, 0, 40)
|
||||||
if !in.IsNull() {
|
in.Delim('[')
|
||||||
in.Delim('[')
|
if out.Authors == nil {
|
||||||
if out.Authors == nil {
|
if !in.IsDelim(']') {
|
||||||
if !in.IsDelim(']') {
|
tagValues = make([]string, 0, 4)
|
||||||
tagValues = make([]string, 0, 4)
|
|
||||||
} else {
|
|
||||||
tagValues = []string{}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
tagValues = (tagValues)[:0]
|
tagValues = []string{}
|
||||||
}
|
}
|
||||||
for !in.IsDelim(']') {
|
} else {
|
||||||
tagValues = append(tagValues, in.String())
|
tagValues = (tagValues)[:0]
|
||||||
in.WantComma()
|
|
||||||
}
|
|
||||||
in.Delim(']')
|
|
||||||
}
|
}
|
||||||
|
for !in.IsDelim(']') {
|
||||||
|
tagValues = append(tagValues, in.String())
|
||||||
|
in.WantComma()
|
||||||
|
}
|
||||||
|
in.Delim(']')
|
||||||
out.Tags[key[1:]] = tagValues
|
out.Tags[key[1:]] = tagValues
|
||||||
} else {
|
} else {
|
||||||
in.SkipRecursive()
|
in.SkipRecursive()
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestIDJSONEncoding(t *testing.T) {
|
func TestIDJSONEncoding(t *testing.T) {
|
||||||
id := MustIDFromHex("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789")
|
id := MustIDFromHex("6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904")
|
||||||
|
|
||||||
// test marshaling
|
// test marshaling
|
||||||
b, err := json.Marshal(id)
|
b, err := json.Marshal(id)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, `"abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"`, string(b))
|
require.Equal(t, `"6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904"`, string(b))
|
||||||
|
|
||||||
// test unmarshaling
|
// test unmarshaling
|
||||||
var id2 ID
|
var id2 ID
|
||||||
@@ -30,12 +30,12 @@ func TestIDJSONEncoding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPubKeyJSONEncoding(t *testing.T) {
|
func TestPubKeyJSONEncoding(t *testing.T) {
|
||||||
pk := MustPubKeyFromHex("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789")
|
pk := MustPubKeyFromHex("6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904")
|
||||||
|
|
||||||
// test marshaling
|
// test marshaling
|
||||||
b, err := json.Marshal(pk)
|
b, err := json.Marshal(pk)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, `"abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"`, string(b))
|
require.Equal(t, `"6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904"`, string(b))
|
||||||
|
|
||||||
// test unmarshaling
|
// test unmarshaling
|
||||||
var pk2 PubKey
|
var pk2 PubKey
|
||||||
@@ -60,7 +60,7 @@ type TestStruct struct {
|
|||||||
|
|
||||||
func TestStructWithIDAndPubKey(t *testing.T) {
|
func TestStructWithIDAndPubKey(t *testing.T) {
|
||||||
ts := TestStruct{
|
ts := TestStruct{
|
||||||
ID: MustIDFromHex("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"),
|
ID: MustIDFromHex("6348118f31cc19fe6b699fa2db5edff315429f7ebb6cc16d3627fdbc4dcae904"),
|
||||||
PubKey: MustPubKeyFromHex("123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0"),
|
PubKey: MustPubKeyFromHex("123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0"),
|
||||||
Name: "test",
|
Name: "test",
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,7 @@ func TestStructWithIDAndPubKey(t *testing.T) {
|
|||||||
// test marshaling
|
// test marshaling
|
||||||
b, err := json.Marshal(ts)
|
b, err := json.Marshal(ts)
|
||||||
require.NoError(t, err)
|
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
|
// test unmarshaling
|
||||||
var ts2 TestStruct
|
var ts2 TestStruct
|
||||||
@@ -89,6 +89,6 @@ func TestStructWithIDAndPubKey(t *testing.T) {
|
|||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
// test unmarshaling with invalid PubKey
|
// 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)
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user