Procházet zdrojové kódy

Define the `EventType` enum. Also, improve JSON encoding.

Arto Bendiken před 3 roky
rodič
revize
a17d266a7a
6 změnil soubory, kde provedl 117 přidání a 27 odebrání
  1. 14 5
      event.go
  2. 20 0
      event_test.go
  3. 42 0
      event_type.go
  4. 12 1
      events.go
  5. 9 1
      raw_event.go
  6. 20 20
      raw_event_test.go

+ 14 - 5
event.go

@@ -1,6 +1,7 @@
 package borealis
 
 import (
+	"encoding/json"
 	"reflect"
 	"time"
 
@@ -8,13 +9,21 @@ import (
 )
 
 type Event struct {
-	Type         uint16
-	SequentialID uint64
-	Timestamp    time.Time
-	UniqueID     ksuid.KSUID
-	Payload      interface{}
+	Type         EventType   `json:"type,string"`
+	SequentialID uint64      `json:"sequential_id"`
+	Timestamp    time.Time   `json:"timestamp"`
+	UniqueID     ksuid.KSUID `json:"unique_id"`
+	Payload      interface{} `json:"payload"`
 }
 
 func (event Event) Equal(other Event) bool {
 	return reflect.DeepEqual(event, other)
 }
+
+func (event Event) JSON() string {
+	output, err := json.Marshal(event)
+	if err != nil {
+		panic(err)
+	}
+	return string(output)
+}

+ 20 - 0
event_test.go

@@ -1 +1,21 @@
 package borealis
+
+import (
+	"encoding/json"
+	"testing"
+)
+
+const (
+	expectedEventJSON = `{"type":"1","sequential_id":2,"timestamp":"2009-01-03T20:15:08.004+02:00","unique_id":"Z5ZWs1U9RoGQVugoz5SIrkmkj9T","payload":{}}`
+)
+
+func TestEncodeEventJSON(t *testing.T) {
+	output, err := json.Marshal(expectedEvent)
+	if err != nil {
+		t.Error(err)
+	}
+	actual := string(output)
+	if actual != expectedEventJSON {
+		t.Errorf("expected %v, but got %v", expectedRawEventJSON, actual)
+	}
+}

+ 42 - 0
event_type.go

@@ -0,0 +1,42 @@
+package borealis
+
+import (
+	"fmt"
+)
+
+type EventType uint16
+
+func GetEventType(payload interface{}) EventType {
+	switch payload.(type) {
+	case GrantAccess:
+		return EventTypeGrantAccess
+	case RegisterID:
+		return EventTypeRegisterID
+	case RevokeAccess:
+		return EventTypeRevokeAccess
+	case SendEmail:
+		return EventTypeSendEmail
+	case VerifyEmail:
+		return EventTypeVerifyEmail
+	default:
+		return EventTypeUnknown
+	}
+}
+
+func (type_ EventType) String() string {
+	switch type_ {
+	case EventTypeGrantAccess:
+		return "GrantAccess"
+	case EventTypeRegisterID:
+		return "RegisterID"
+	case EventTypeRevokeAccess:
+		return "RevokeAccess"
+	case EventTypeSendEmail:
+		return "SendEmail"
+	case EventTypeVerifyEmail:
+		return "VerifyEmail"
+	case EventTypeUnknown:
+		return "Unknown"
+	}
+	return fmt.Sprintf("Unknown(%d)", int(type_))
+}

+ 12 - 1
events.go

@@ -1,9 +1,20 @@
 package borealis
 
-import events "github.com/aurora-is-near/aurora-events/go"
+import (
+	events "github.com/aurora-is-near/aurora-events/go"
+)
 
 type GrantAccess events.GrantAccess
 type RegisterID events.RegisterID
 type RevokeAccess events.RevokeAccess
 type SendEmail events.SendEmail
 type VerifyEmail events.VerifyEmail
+
+const (
+	EventTypeUnknown EventType = iota
+	EventTypeGrantAccess
+	EventTypeRegisterID
+	EventTypeRevokeAccess
+	EventTypeSendEmail
+	EventTypeVerifyEmail
+)

+ 9 - 1
raw_event.go

@@ -55,7 +55,7 @@ func (rawEvent RawEvent) Check() (*Event, error) {
 	}
 
 	event := Event{
-		Type:         rawEvent.Type,
+		Type:         EventType(rawEvent.Type),
 		SequentialID: rawEvent.SequentialID,
 		Timestamp:    timestamp,
 		UniqueID:     ksuid,
@@ -75,6 +75,14 @@ func (event RawEvent) Equal(other RawEvent) bool {
 	return reflect.DeepEqual(event.Payload, other.Payload)
 }
 
+func (event RawEvent) JSON() string {
+	output, err := json.Marshal(event)
+	if err != nil {
+		panic(err)
+	}
+	return string(output)
+}
+
 func (event RawEvent) MarshalJSON() ([]byte, error) {
 	return json.Marshal([]interface{}{
 		event.Type,

+ 20 - 20
raw_event_test.go

@@ -18,7 +18,7 @@ var expectedEvent = Event{
 	Payload:      map[string]interface{}{},
 }
 
-var expectedEventRaw = RawEvent{
+var expectedRawEvent = RawEvent{
 	Type:         1,
 	SequentialID: 2,
 	TimestampS:   3,
@@ -27,8 +27,8 @@ var expectedEventRaw = RawEvent{
 	Payload:      map[string]interface{}{},
 }
 
-var expectedEventJSON = "[1,2,3,4,\"AAECAwQFBgcICQoLDA0ODw==\",{}]"
-var expectedEventCBOR = []byte{0x86, 1, 2, 3, 4, 0x50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xa0}
+var expectedRawEventJSON = "[1,2,3,4,\"AAECAwQFBgcICQoLDA0ODw==\",{}]"
+var expectedRawEventCBOR = []byte{0x86, 1, 2, 3, 4, 0x50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0xa0}
 
 func TestNewRawEvent(t *testing.T) {
 	_, err := NewRawEvent(0x1234, make(map[string]interface{}))
@@ -38,7 +38,7 @@ func TestNewRawEvent(t *testing.T) {
 }
 
 func TestCheckEvent(t *testing.T) {
-	actualEvent, err := expectedEventRaw.Check()
+	actualEvent, err := expectedRawEvent.Check()
 	if err != nil {
 		t.Error(err)
 	}
@@ -47,42 +47,42 @@ func TestCheckEvent(t *testing.T) {
 	}
 }
 
-func TestEncodeEventJSON(t *testing.T) {
-	output, err := json.Marshal(expectedEventRaw)
+func TestEncodeRawEventJSON(t *testing.T) {
+	output, err := json.Marshal(expectedRawEvent)
 	if err != nil {
 		t.Error(err)
 	}
 	actual := string(output)
-	if actual != expectedEventJSON {
-		t.Errorf("expected %v, but got %v", expectedEventJSON, actual)
+	if actual != expectedRawEventJSON {
+		t.Errorf("expected %v, but got %v", expectedRawEventJSON, actual)
 	}
 }
 
-func TestEncodeEventCBOR(t *testing.T) {
-	actual, err := cbor.Marshal(expectedEventRaw)
+func TestEncodeRawEventCBOR(t *testing.T) {
+	actual, err := cbor.Marshal(expectedRawEvent)
 	if err != nil {
 		t.Error(err)
 	}
-	if !bytes.Equal(actual, expectedEventCBOR) {
-		t.Errorf("expected %v, but got %v", expectedEventCBOR, actual)
+	if !bytes.Equal(actual, expectedRawEventCBOR) {
+		t.Errorf("expected %v, but got %v", expectedRawEventCBOR, actual)
 	}
 }
 
-func TestDecodeEventJSON(t *testing.T) {
+func TestDecodeRawEventJSON(t *testing.T) {
 	var actual RawEvent
-	if err := json.Unmarshal([]byte(expectedEventJSON), &actual); err != nil {
+	if err := json.Unmarshal([]byte(expectedRawEventJSON), &actual); err != nil {
 		t.Error(err)
 	}
-	if !expectedEventRaw.Equal(actual) {
-		t.Errorf("expected %v, but got %v", expectedEventRaw, actual)
+	if !expectedRawEvent.Equal(actual) {
+		t.Errorf("expected %v, but got %v", expectedRawEvent, actual)
 	}
 }
-func TestDecodeEventCBOR(t *testing.T) {
+func TestDecodeRawEventCBOR(t *testing.T) {
 	var actual RawEvent
-	if err := cbor.Unmarshal([]byte(expectedEventCBOR), &actual); err != nil {
+	if err := cbor.Unmarshal([]byte(expectedRawEventCBOR), &actual); err != nil {
 		t.Error(err)
 	}
-	if !expectedEventRaw.Equal(actual) {
-		t.Errorf("expected %v, but got %v", expectedEventRaw, actual)
+	if !expectedRawEvent.Equal(actual) {
+		t.Errorf("expected %v, but got %v", expectedRawEvent, actual)
 	}
 }