simplify easyjson even more, rename functions, assume ids and pubkeys will always be safe hex strings that do not need to be escaped and eliminate unnecessary variables that would probably be eliminated by the compiler anyway.

This commit is contained in:
fiatjaf
2025-05-15 15:01:51 -03:00
parent 16aa5c95f3
commit 29cf3c7c6e
2 changed files with 55 additions and 71 deletions

View File

@@ -7,7 +7,7 @@ import (
jwriter "github.com/mailru/easyjson/jwriter"
)
func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event) {
func easyjsonDecodeEvent(in *jlexer.Lexer, out *Event) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
@@ -75,37 +75,26 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event)
}
}
func easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Event) {
func easyjsonEncodeEvent(out *jwriter.Writer, in Event) {
out.RawByte('{')
first := true
_ = first
{
const prefix string = "\"kind\":"
out.RawString(prefix)
out.RawString("\"kind\":")
out.Int(int(in.Kind))
if in.ID != ZeroID {
out.RawString(",\"id\":\"")
out.RawString(hex.EncodeToString(in.ID[:]))
}
{
if in.ID != [32]byte{} {
const prefix string = ",\"id\":"
out.RawString(prefix)
out.String(hex.EncodeToString(in.ID[:]))
if in.PubKey != ZeroPK {
out.RawString("\",\"pubkey\":\"")
out.RawString(hex.EncodeToString(in.PubKey[:]))
}
}
{
if in.PubKey != [32]byte{} {
const prefix string = ",\"pubkey\":"
out.RawString(prefix)
out.String(hex.EncodeToString(in.PubKey[:]))
}
}
{
const prefix string = ",\"created_at\":"
out.RawString(prefix)
out.RawString("\",\"created_at\":")
out.Int64(int64(in.CreatedAt))
}
{
const prefix string = ",\"tags\":"
out.RawString(prefix)
out.RawString(",\"tags\":")
out.RawByte('[')
for v3, v4 := range in.Tags {
if v3 > 0 {
@@ -121,43 +110,39 @@ func easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Event)
out.RawByte(']')
}
out.RawByte(']')
}
{
const prefix string = ",\"content\":"
out.RawString(prefix)
out.RawString(",\"content\":")
out.String(in.Content)
}
{
if in.Sig != [64]byte{} {
const prefix string = ",\"sig\":"
out.RawString(prefix)
out.String(hex.EncodeToString(in.Sig[:]))
}
out.RawString(",\"sig\":\"")
out.RawString(hex.EncodeToString(in.Sig[:]) + "\"")
}
out.RawByte('}')
}
// MarshalJSON supports json.Marshaler interface
func (v Event) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{NoEscapeHTML: true}
easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(&w, v)
easyjsonEncodeEvent(&w, v)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v Event) MarshalEasyJSON(w *jwriter.Writer) {
w.NoEscapeHTML = true
easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(w, v)
easyjsonEncodeEvent(w, v)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (v *Event) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(&r, v)
easyjsonDecodeEvent(&r, v)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *Event) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(l, v)
easyjsonDecodeEvent(l, v)
}

View File

@@ -7,7 +7,7 @@ import (
jwriter "github.com/mailru/easyjson/jwriter"
)
func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter) {
func easyjsonDecodeFilter(in *jlexer.Lexer, out *Filter) {
isTopLevel := in.IsStart()
if in.IsNull() {
if isTopLevel {
@@ -121,21 +121,20 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter)
}
}
func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter) {
func easyjsonEncodeFilter(out *jwriter.Writer, in Filter) {
out.RawByte('{')
first := true
_ = first
if len(in.IDs) != 0 {
const prefix string = ",\"ids\":"
first = false
out.RawString(prefix[1:])
out.RawString("\"ids\":")
{
out.RawByte('[')
for v4, v5 := range in.IDs {
if v4 > 0 {
for i, id := range in.IDs {
if i > 0 {
out.RawByte(',')
}
out.String(hex.EncodeToString(v5[:]))
out.RawString("\"" + hex.EncodeToString(id[:]) + "\"")
}
out.RawByte(']')
}
@@ -150,11 +149,11 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter
}
{
out.RawByte('[')
for v6, v7 := range in.Kinds {
if v6 > 0 {
for i, kind := range in.Kinds {
if i > 0 {
out.RawByte(',')
}
out.Int(int(v7))
out.Int(int(kind))
}
out.RawByte(']')
}
@@ -169,11 +168,11 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter
}
{
out.RawByte('[')
for v8, v9 := range in.Authors {
if v8 > 0 {
for i, pk := range in.Authors {
if i > 0 {
out.RawByte(',')
}
out.String(hex.EncodeToString(v9[:]))
out.RawString("\"" + hex.EncodeToString(pk[:]) + "\"")
}
out.RawByte(']')
}
@@ -243,24 +242,24 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter
// MarshalJSON supports json.Marshaler interface
func (v Filter) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{NoEscapeHTML: true}
easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(&w, v)
easyjsonEncodeFilter(&w, v)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (v Filter) MarshalEasyJSON(w *jwriter.Writer) {
w.NoEscapeHTML = true
easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(w, v)
easyjsonEncodeFilter(w, v)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (v *Filter) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(&r, v)
easyjsonDecodeFilter(&r, v)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *Filter) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(l, v)
easyjsonDecodeFilter(l, v)
}