80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
|
package response
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
// EventID represents an event type happening on the chain.
|
||
|
EventID byte
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
// InvalidEventID is an invalid event id that is the default value of
|
||
|
// EventID. It's only used as an initial value similar to nil.
|
||
|
InvalidEventID EventID = iota
|
||
|
// BlockEventID is a `block_added` event.
|
||
|
BlockEventID
|
||
|
// TransactionEventID corresponds to `transaction_added` event.
|
||
|
TransactionEventID
|
||
|
// NotificationEventID represents `notification_from_execution` events.
|
||
|
NotificationEventID
|
||
|
// ExecutionEventID is used for `transaction_executed` events.
|
||
|
ExecutionEventID
|
||
|
)
|
||
|
|
||
|
// String is a good old Stringer implementation.
|
||
|
func (e EventID) String() string {
|
||
|
switch e {
|
||
|
case BlockEventID:
|
||
|
return "block_added"
|
||
|
case TransactionEventID:
|
||
|
return "transaction_added"
|
||
|
case NotificationEventID:
|
||
|
return "notification_from_execution"
|
||
|
case ExecutionEventID:
|
||
|
return "transaction_executed"
|
||
|
default:
|
||
|
return "unknown"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// GetEventIDFromString converts input string into an EventID if it's possible.
|
||
|
func GetEventIDFromString(s string) (EventID, error) {
|
||
|
switch s {
|
||
|
case "block_added":
|
||
|
return BlockEventID, nil
|
||
|
case "transaction_added":
|
||
|
return TransactionEventID, nil
|
||
|
case "notification_from_execution":
|
||
|
return NotificationEventID, nil
|
||
|
case "transaction_executed":
|
||
|
return ExecutionEventID, nil
|
||
|
default:
|
||
|
return 255, errors.New("invalid stream name")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// MarshalJSON implements json.Marshaler interface.
|
||
|
func (e EventID) MarshalJSON() ([]byte, error) {
|
||
|
return json.Marshal(e.String())
|
||
|
}
|
||
|
|
||
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
||
|
func (e *EventID) UnmarshalJSON(b []byte) error {
|
||
|
var s string
|
||
|
|
||
|
err := json.Unmarshal(b, &s)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
id, err := GetEventIDFromString(s)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
*e = id
|
||
|
return nil
|
||
|
}
|