Skip to content

Commit

Permalink
Fill missing godocs (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 authored Nov 24, 2021
1 parent b9928e7 commit 048c91a
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions components/cqrs/command_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type CommandProcessor struct {
logger watermill.LoggerAdapter
}

// NewCommandProcessor creates a new CommandProcessor.
func NewCommandProcessor(
handlers []CommandHandler,
generateTopic func(commandName string) string,
Expand Down Expand Up @@ -80,6 +81,7 @@ func NewCommandProcessor(
}, nil
}

// DuplicateCommandHandlerError occurs when a handler with the same name already exists.
type DuplicateCommandHandlerError struct {
CommandName string
}
Expand All @@ -88,6 +90,7 @@ func (d DuplicateCommandHandlerError) Error() string {
return fmt.Sprintf("command handler for command %s already exists", d.CommandName)
}

// AddHandlersToRouter adds the CommandProcessor's handlers to the given router.
func (p CommandProcessor) AddHandlersToRouter(r *message.Router) error {
handledCommands := map[string]struct{}{}

Expand Down Expand Up @@ -130,6 +133,7 @@ func (p CommandProcessor) AddHandlersToRouter(r *message.Router) error {
return nil
}

// Handlers returns the CommandProcessor's handlers.
func (p CommandProcessor) Handlers() []CommandHandler {
return p.handlers
}
Expand Down
6 changes: 6 additions & 0 deletions components/cqrs/marshaler_protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"github.com/pkg/errors"
)

// ProtobufMarshaler is the default Protocol Buffers marshaler.
type ProtobufMarshaler struct {
NewUUID func() string
GenerateName func(v interface{}) string
}

// NoProtoMessageError is returned when the given value does not implement proto.Message.
type NoProtoMessageError struct {
v interface{}
}
Expand All @@ -28,6 +30,7 @@ func (e NoProtoMessageError) Error() string {
return "v is not proto.Message"
}

// Marshal marshals the given protobuf's message into watermill's Message.
func (m ProtobufMarshaler) Marshal(v interface{}) (*message.Message, error) {
protoMsg, ok := v.(proto.Message)
if !ok {
Expand Down Expand Up @@ -57,10 +60,12 @@ func (m ProtobufMarshaler) newUUID() string {
return watermill.NewUUID()
}

// Unmarshal unmarshals given watermill's Message into protobuf's message.
func (ProtobufMarshaler) Unmarshal(msg *message.Message, v interface{}) (err error) {
return proto.Unmarshal(msg.Payload, v.(proto.Message))
}

// Name returns the command or event's name.
func (m ProtobufMarshaler) Name(cmdOrEvent interface{}) string {
if m.GenerateName != nil {
return m.GenerateName(cmdOrEvent)
Expand All @@ -69,6 +74,7 @@ func (m ProtobufMarshaler) Name(cmdOrEvent interface{}) string {
return FullyQualifiedStructName(cmdOrEvent)
}

// NameFromMessage returns the metadata name value for a given Message.
func (m ProtobufMarshaler) NameFromMessage(msg *message.Message) string {
return msg.Metadata.Get("name")
}
2 changes: 2 additions & 0 deletions components/metrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type HandlerPrometheusMetricsMiddleware struct {
handlerExecutionTimeSeconds *prometheus.HistogramVec
}

// Middleware returns the middleware ready to be used with watermill's Router.
func (m HandlerPrometheusMetricsMiddleware) Middleware(h message.HandlerFunc) message.HandlerFunc {
return func(msg *message.Message) (msgs []*message.Message, err error) {
now := time.Now()
Expand All @@ -58,6 +59,7 @@ func (m HandlerPrometheusMetricsMiddleware) Middleware(h message.HandlerFunc) me
}
}

// NewRouterMiddleware returns new middleware.
func (b PrometheusMetricsBuilder) NewRouterMiddleware() HandlerPrometheusMetricsMiddleware {
var err error
m := HandlerPrometheusMetricsMiddleware{}
Expand Down
3 changes: 3 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"sync"
)

// LogFields is the logger's key-value list of fields.
type LogFields map[string]interface{}

// Add adds new fields to the list of LogFields.
func (l LogFields) Add(newFields LogFields) LogFields {
resultFields := make(LogFields, len(l)+len(newFields))

Expand All @@ -26,6 +28,7 @@ func (l LogFields) Add(newFields LogFields) LogFields {
return resultFields
}

// Copy copies the LogFields.
func (l LogFields) Copy() LogFields {
cpy := make(LogFields, len(l))
for k, v := range l {
Expand Down
10 changes: 7 additions & 3 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ func init() {
close(closedchan)
}

// Payload is the Message's payload.
type Payload []byte

// Message is the basic transfer unit.
// Messages are emitted by Publishers and received by Subscribers.
type Message struct {
// UUID is an unique identifier of message.
//
Expand All @@ -23,13 +26,13 @@ type Message struct {

// Metadata contains the message metadata.
//
// Can be used to store data which doesn't require unmarshaling entire payload.
// Can be used to store data which doesn't require unmarshaling the entire payload.
// It is something similar to HTTP request's headers.
//
// Metadata is marshaled and will be saved to PubSub.
// Metadata is marshaled and will be saved to the PubSub.
Metadata Metadata

// Payload is message's payload.
// Payload is the message's payload.
Payload Payload

// ack is closed, when acknowledge is received.
Expand All @@ -43,6 +46,7 @@ type Message struct {
ctx context.Context
}

// NewMessage creates a new Message with given uuid and payload.
func NewMessage(uuid string, payload Payload) *Message {
return &Message{
UUID: uuid,
Expand Down
2 changes: 2 additions & 0 deletions message/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package message
// Metadata is sent with every message to provide extra context without unmarshaling the message payload.
type Metadata map[string]string

// Get returns the metadata value for the given key. If the key is not found, an empty string is returned.
func (m Metadata) Get(key string) string {
if v, ok := m[key]; ok {
return v
Expand All @@ -11,6 +12,7 @@ func (m Metadata) Get(key string) string {
return ""
}

// Set sets the metadata key to value.
func (m Metadata) Set(key, value string) {
m[key] = value
}
3 changes: 3 additions & 0 deletions message/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
)

// Publisher is the emitting part of a Pub/Sub.
type Publisher interface {
// Publish publishes provided messages to given topic.
//
Expand All @@ -18,6 +19,7 @@ type Publisher interface {
Close() error
}

// Subscriber is the consuming part of the Pub/Sub.
type Subscriber interface {
// Subscribe returns output channel with messages from provided topic.
// Channel is closed, when Close() was called on the subscriber.
Expand All @@ -33,6 +35,7 @@ type Subscriber interface {
Close() error
}

// SubscribeInitializer is used to initialize subscribers.
type SubscribeInitializer interface {
// SubscribeInitialize can be called to initialize subscribe before consume.
// When calling Subscribe before Publish, SubscribeInitialize should be not required.
Expand Down
1 change: 1 addition & 0 deletions message/router/middleware/recoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
)

// RecoveredPanicError holds the recovered panic's error along with the stacktrace.
type RecoveredPanicError struct {
V interface{}
Stacktrace string
Expand Down
3 changes: 3 additions & 0 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"github.com/oklog/ulid"
)

// NewUUID returns a new UUID Version 4.
func NewUUID() string {
return uuid.New().String()
}

// NewShortUUID returns a new short UUID.
func NewShortUUID() string {
return shortuuid.New()
}

// NewULID returns a new ULID.
func NewULID() string {
return ulid.MustNew(ulid.Now(), rand.Reader).String()
}

0 comments on commit 048c91a

Please sign in to comment.