mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-30 10:30:44 +02:00
improve debug logging, fix stringer interface, debuglog events sent, fix debuglogging affecting the actual values.
This commit is contained in:
5
event.go
5
event.go
@ -43,10 +43,7 @@ const (
|
||||
)
|
||||
|
||||
// Event Stringer interface, just returns the raw JSON as a string
|
||||
func (evt *Event) String() string {
|
||||
if evt == nil {
|
||||
return "null"
|
||||
}
|
||||
func (evt Event) String() string {
|
||||
j, _ := json.Marshal(evt)
|
||||
return string(j)
|
||||
}
|
||||
|
@ -4,8 +4,9 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/valyala/fastjson"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fastjson"
|
||||
)
|
||||
|
||||
func (evt *Event) UnmarshalJSON(payload []byte) error {
|
||||
@ -17,7 +18,7 @@ func (evt *Event) UnmarshalJSON(payload []byte) error {
|
||||
|
||||
obj, err := parsed.Object()
|
||||
if err != nil {
|
||||
return fmt.Errorf("event is not an object")
|
||||
return fmt.Errorf("event is not an object: %w", err)
|
||||
}
|
||||
|
||||
// prepare this to receive any extra property that may serialized along with the event
|
||||
|
36
log_debug.go
36
log_debug.go
@ -8,17 +8,33 @@ import (
|
||||
)
|
||||
|
||||
func debugLog(str string, args ...any) {
|
||||
// this is such that we don't modify the actual args that may be used outside of this function
|
||||
printableArgs := make([]any, len(args))
|
||||
|
||||
for i, v := range args {
|
||||
switch v.(type) {
|
||||
case []json.RawMessage:
|
||||
j, _ := json.Marshal(v)
|
||||
args[i] = string(j)
|
||||
case []byte:
|
||||
args[i] = string(v.([]byte))
|
||||
case fmt.Stringer:
|
||||
args[i] = v.(fmt.Stringer).String()
|
||||
}
|
||||
printableArgs[i] = stringify(v)
|
||||
}
|
||||
|
||||
DebugLogger.Printf(str, args...)
|
||||
DebugLogger.Printf(str, printableArgs...)
|
||||
}
|
||||
|
||||
func stringify(anything any) any {
|
||||
switch v := anything.(type) {
|
||||
case []any:
|
||||
// this is such that we don't modify the actual values that may be used outside of this function
|
||||
printableValues := make([]any, len(v))
|
||||
for i, subv := range v {
|
||||
printableValues[i] = stringify(subv)
|
||||
}
|
||||
return printableValues
|
||||
case []json.RawMessage:
|
||||
j, _ := json.Marshal(v)
|
||||
return string(j)
|
||||
case []byte:
|
||||
return string(v)
|
||||
case fmt.Stringer:
|
||||
return v.String()
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
2
relay.go
2
relay.go
@ -300,7 +300,7 @@ func (r *Relay) Publish(ctx context.Context, event Event) (Status, error) {
|
||||
defer r.okCallbacks.Delete(event.ID)
|
||||
|
||||
// publish event
|
||||
message := []interface{}{"EVENT", event}
|
||||
message := []any{"EVENT", event}
|
||||
debugLog("{%s} sending %v\n", r.URL, message)
|
||||
if err := r.Connection.WriteJSON(message); err != nil {
|
||||
return status, err
|
||||
|
@ -197,7 +197,7 @@ func parseEventMessage(t *testing.T, raw []json.RawMessage) Event {
|
||||
}
|
||||
var event Event
|
||||
if err := json.Unmarshal(raw[1], &event); err != nil {
|
||||
t.Errorf("json.Unmarshal: %v", err)
|
||||
t.Errorf("json.Unmarshal(`%s`): %v", string(raw[1]), err)
|
||||
}
|
||||
return event
|
||||
}
|
||||
|
Reference in New Issue
Block a user