mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-08 07:09:56 +02:00
get rid of WriteJSON() and replace calls with manually marshaled envelopes.
This commit is contained in:
28
envelopes.go
28
envelopes.go
@ -32,6 +32,9 @@ func ParseMessage(message []byte) Envelope {
|
||||
v = &OKEnvelope{}
|
||||
case bytes.Contains(label, []byte("AUTH")):
|
||||
v = &AuthEnvelope{}
|
||||
case bytes.Contains(label, []byte("CLOSE")):
|
||||
x := CloseEnvelope("")
|
||||
v = &x
|
||||
}
|
||||
|
||||
if err := v.UnmarshalJSON(message); err != nil {
|
||||
@ -56,6 +59,7 @@ var (
|
||||
_ Envelope = (*ReqEnvelope)(nil)
|
||||
_ Envelope = (*NoticeEnvelope)(nil)
|
||||
_ Envelope = (*EOSEEnvelope)(nil)
|
||||
_ Envelope = (*CloseEnvelope)(nil)
|
||||
_ Envelope = (*OKEnvelope)(nil)
|
||||
_ Envelope = (*AuthEnvelope)(nil)
|
||||
)
|
||||
@ -173,6 +177,30 @@ func (v EOSEEnvelope) MarshalJSON() ([]byte, error) {
|
||||
return w.BuildBytes()
|
||||
}
|
||||
|
||||
type CloseEnvelope string
|
||||
|
||||
func (_ CloseEnvelope) Label() string { return "CLOSE" }
|
||||
|
||||
func (v *CloseEnvelope) UnmarshalJSON(data []byte) error {
|
||||
r := gjson.ParseBytes(data)
|
||||
arr := r.Array()
|
||||
switch len(arr) {
|
||||
case 2:
|
||||
*v = CloseEnvelope(arr[1].Str)
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("failed to decode CLOSE envelope")
|
||||
}
|
||||
}
|
||||
|
||||
func (v CloseEnvelope) MarshalJSON() ([]byte, error) {
|
||||
w := jwriter.Writer{}
|
||||
w.RawString(`["CLOSE",`)
|
||||
w.Raw(json.Marshal(string(v)))
|
||||
w.RawString(`]`)
|
||||
return w.BuildBytes()
|
||||
}
|
||||
|
||||
type OKEnvelope struct {
|
||||
EventID string
|
||||
OK bool
|
||||
|
Reference in New Issue
Block a user