add constructor functions with options. update tests.
This commit is contained in:
+63
-1
@@ -3,6 +3,10 @@
|
||||
// serialization, cryptographic signatures, and subscription filters.
|
||||
package events
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Tag represents a single tag within an event as an array of strings.
|
||||
// The first element identifies the tag name, the second contains the value,
|
||||
// and subsequent elements are optional.
|
||||
@@ -13,9 +17,67 @@ type Tag []string
|
||||
type Event struct {
|
||||
ID string `json:"id"`
|
||||
PubKey string `json:"pubkey"`
|
||||
CreatedAt int `json:"created_at"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
Kind int `json:"kind"`
|
||||
Tags []Tag `json:"tags"`
|
||||
Content string `json:"content"`
|
||||
Sig string `json:"sig"`
|
||||
}
|
||||
|
||||
func NewEvent(opts ...EventOption) Event {
|
||||
e := Event{Tags: make([]Tag, 0)}
|
||||
for _, opt := range opts {
|
||||
opt(&e)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
type EventOption func(*Event)
|
||||
|
||||
func WithID(id string) EventOption {
|
||||
return func(e *Event) {
|
||||
e.ID = id
|
||||
}
|
||||
}
|
||||
|
||||
func WithPubKey(pk string) EventOption {
|
||||
return func(e *Event) {
|
||||
e.PubKey = pk
|
||||
}
|
||||
}
|
||||
|
||||
func WithCreatedAt(t int64) EventOption {
|
||||
return func(e *Event) {
|
||||
e.CreatedAt = t
|
||||
}
|
||||
}
|
||||
|
||||
func WithCreatedAtTime(t time.Time) EventOption {
|
||||
return func(e *Event) {
|
||||
e.CreatedAt = t.Unix()
|
||||
}
|
||||
}
|
||||
|
||||
func WithKind(k int) EventOption {
|
||||
return func(e *Event) {
|
||||
e.Kind = k
|
||||
}
|
||||
}
|
||||
|
||||
func WithTag(t Tag) EventOption {
|
||||
return func(e *Event) {
|
||||
e.Tags = append(e.Tags, t)
|
||||
}
|
||||
}
|
||||
|
||||
func WithContent(c string) EventOption {
|
||||
return func(e *Event) {
|
||||
e.Content = c
|
||||
}
|
||||
}
|
||||
|
||||
func WithSig(s string) EventOption {
|
||||
return func(e *Event) {
|
||||
e.Sig = s
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user