Skip to content

Commit

Permalink
Fixing lint errors. (#88)
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Nichols <[email protected]>
  • Loading branch information
n3wscott authored and markpeek committed Mar 21, 2019
1 parent b4a29bf commit 5118a97
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/cloudevents/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
)

// Option is the function signature required to be considered an client.Option.
type Option func(*ceClient) error

// WithEventDefaulter adds an event defaulter to the end of the defaulter chain.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudevents/datacodec/json/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
Package datacodec/json holds the encoder/decoder implementation for `application/json`.
Package json holds the encoder/decoder implementation for `application/json`.
*/
package json
2 changes: 1 addition & 1 deletion pkg/cloudevents/datacodec/xml/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
Package datacodec/xml holds the encoder/decoder implementation for `application/xml`.
Package xml holds the encoder/decoder implementation for `application/xml`.
*/
package xml
1 change: 1 addition & 0 deletions pkg/cloudevents/eventcontext.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cloudevents

// EventContext is conical interface for a CloudEvents Context.
type EventContext interface {
// AsV01 provides a translation from whatever the "native" encoding of the
// CloudEvent was to the equivalent in v0.1 field names, moving fields to or
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloudevents/observability/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Package observability holds metrics and tracing recording implementations.
*/
package observability
6 changes: 5 additions & 1 deletion pkg/cloudevents/observability/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
)

var (
// KeyMethod is the tag used for marking method on a metric.
KeyMethod, _ = tag.NewKey("method")
// KeyResult is the tag used for marking result on a metric.
KeyResult, _ = tag.NewKey("result")
)

const (
// ResultError is a shared result tag value for error.
ResultError = "error"
ResultOK = "success"
// ResultOK is a shared result tag value for success.
ResultOK = "success"
)
4 changes: 4 additions & 0 deletions pkg/cloudevents/observability/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func LatencyTags() []tag.Key {
return []tag.Key{KeyMethod, KeyResult}
}

// NewReporter creates and returns a reporter wrapping the provided Observable,
// and injects a trace span into the context.
func NewReporter(ctx context.Context, on Observable) (context.Context, Reporter) {
ctx, span := trace.StartSpan(ctx, on.TraceName())
r := &reporter{
Expand All @@ -66,12 +68,14 @@ func (r *reporter) record() {
r.span.End()
}

// Error records the result as an error.
func (r *reporter) Error() {
r.once.Do(func() {
r.result(ResultError)
})
}

// OK records the result as a success.
func (r *reporter) OK() {
r.once.Do(func() {
r.result(ResultOK)
Expand Down
6 changes: 6 additions & 0 deletions pkg/cloudevents/transport/http/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ import (
"github.com/cloudevents/sdk-go/pkg/cloudevents/transport"
)

// Codec is the wrapper for all versions of codecs supported by the http
// transport.
type Codec struct {
// Encoding is the setting to inform the DefaultEncodingSelectionFn for
// selecting a codec.
Encoding Encoding

// DefaultEncodingSelectionFn allows for encoding selection strategies to be injected.
DefaultEncodingSelectionFn EncodingSelector

v01 *CodecV01
v02 *CodecV02
v03 *CodecV03
}

// Adheres to Codec
var _ transport.Codec = (*Codec)(nil)

// DefaultBinaryEncodingSelectionStrategy implements a selection process for
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloudevents/transport/http/codec_v01.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import (
"strings"
)

// CodecV01 represents a http transport codec that uses CloudEvents spec v0.3
type CodecV01 struct {
Encoding Encoding
}

// Adheres to Codec
var _ transport.Codec = (*CodecV01)(nil)

// Encode implements Codec.Encode
func (v CodecV01) Encode(e cloudevents.Event) (transport.Message, error) {
// TODO: wire context
_, r := observability.NewReporter(context.Background(), CodecObserved{o: reportEncode, c: v.Encoding.Codec()})
Expand All @@ -46,6 +49,7 @@ func (v CodecV01) obsEncode(e cloudevents.Event) (transport.Message, error) {
}
}

// Decode implements Codec.Decode
func (v CodecV01) Decode(msg transport.Message) (*cloudevents.Event, error) {
// TODO: wire context
_, r := observability.NewReporter(context.Background(), CodecObserved{o: reportDecode, c: v.inspectEncoding(msg).Codec()}) // TODO: inspectEncoding is not free.
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloudevents/transport/http/codec_v02.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import (
"strings"
)

// CodecV02 represents a http transport codec that uses CloudEvents spec v0.2
type CodecV02 struct {
Encoding Encoding
}

// Adheres to Codec
var _ transport.Codec = (*CodecV02)(nil)

// Encode implements Codec.Encode
func (v CodecV02) Encode(e cloudevents.Event) (transport.Message, error) {
// TODO: wire context
_, r := observability.NewReporter(context.Background(), CodecObserved{o: reportEncode, c: v.Encoding.Codec()})
Expand All @@ -46,6 +49,7 @@ func (v CodecV02) obsEncode(e cloudevents.Event) (transport.Message, error) {
}
}

// Decode implements Codec.Decode
func (v CodecV02) Decode(msg transport.Message) (*cloudevents.Event, error) {
// TODO: wire context
_, r := observability.NewReporter(context.Background(), CodecObserved{o: reportDecode, c: v.inspectEncoding(msg).Codec()}) // TODO: inspectEncoding is not free.
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloudevents/transport/http/codec_v03.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import (
"strings"
)

// CodecV03 represents a http transport codec that uses CloudEvents spec v0.3
type CodecV03 struct {
Encoding Encoding
}

// Adheres to Codec
var _ transport.Codec = (*CodecV03)(nil)

// Encode implements Codec.Encode
func (v CodecV03) Encode(e cloudevents.Event) (transport.Message, error) {
// TODO: wire context
_, r := observability.NewReporter(context.Background(), CodecObserved{o: reportEncode, c: v.Encoding.Codec()})
Expand Down Expand Up @@ -48,6 +51,7 @@ func (v CodecV03) obsEncode(e cloudevents.Event) (transport.Message, error) {
}
}

// Decode implements Codec.Decode
func (v CodecV03) Decode(msg transport.Message) (*cloudevents.Event, error) {
// TODO: wire context
_, r := observability.NewReporter(context.Background(), CodecObserved{o: reportDecode, c: v.inspectEncoding(msg).Codec()}) // TODO: inspectEncoding is not free.
Expand Down
3 changes: 3 additions & 0 deletions pkg/cloudevents/transport/http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type TransportContext struct {
IgnoreHeaderPrefixes []string
}

// NewTransportContext creates a new TransportContext from a http.Request.
func NewTransportContext(req *http.Request) TransportContext {
var tx *TransportContext
if req != nil {
Expand Down Expand Up @@ -116,6 +117,8 @@ func WithTransportContext(ctx context.Context, tcxt TransportContext) context.Co
return context.WithValue(ctx, transportContextKey, tcxt)
}

// TransportContextFrom pulls a TransportContext out of a context. Always
// returns a non-nil object.
func TransportContextFrom(ctx context.Context) TransportContext {
tctx := ctx.Value(transportContextKey)
if tctx != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudevents/transport/http/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
Package transport/http implements the CloudEvent transport implementation using HTTP.
Package http implements the CloudEvent transport implementation using HTTP.
*/
package http
13 changes: 13 additions & 0 deletions pkg/cloudevents/transport/http/encoding.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package http

// Encoding to use for HTTP transport.
type Encoding int32

const (
// Default
Default Encoding = iota
// BinaryV01 is Binary CloudEvents spec v0.1.
BinaryV01
// StructuredV01 is Structured CloudEvents spec v0.1.
StructuredV01
// BinaryV02 is Binary CloudEvents spec v0.2.
BinaryV02
// StructuredV02 is Structured CloudEvents spec v0.2.
StructuredV02
// BinaryV03 is Binary CloudEvents spec v0.3.
BinaryV03
// StructuredV03 is Structured CloudEvents spec v0.3.
StructuredV03
// BatchedV03 is Batched CloudEvents spec v0.3.
BatchedV03
// Unknown is unknown.
Unknown
)

// String pretty-prints the encoding as a string.
func (e Encoding) String() string {
switch e {
case Default:
Expand Down Expand Up @@ -44,6 +55,7 @@ func (e Encoding) String() string {
}
}

// Version pretty-prints the encoding version as a string.
func (e Encoding) Version() string {
switch e {
case Default:
Expand Down Expand Up @@ -75,6 +87,7 @@ func (e Encoding) Version() string {
}
}

// Codec creates a structured string to represent the the codec version.
func (e Encoding) Codec() string {
switch e {
case Default:
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudevents/transport/http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"
)

// Option is the function signature required to be considered an http.Option.
type Option func(*Transport) error

// WithTarget sets the outbound recipient of cloudevents when using an HTTP
Expand Down
6 changes: 5 additions & 1 deletion pkg/cloudevents/transport/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

type EncodingSelector func(e cloudevents.Event) Encoding

// type check that this transport message impl matches the contract
// Transport adheres to transport.Transport.
var _ transport.Transport = (*Transport)(nil)

const (
Expand Down Expand Up @@ -119,6 +119,7 @@ func copyHeaders(from, to http.Header) {
}
}

// Send implements Transport.Send
func (t *Transport) Send(ctx context.Context, event cloudevents.Event) (*cloudevents.Event, error) {
ctx, r := observability.NewReporter(ctx, reportSend)
resp, err := t.obsSend(ctx, event)
Expand Down Expand Up @@ -197,10 +198,13 @@ func (t *Transport) obsSend(ctx context.Context, event cloudevents.Event) (*clou
return nil, fmt.Errorf("failed to encode Event into a Message")
}

// SetReceiver implements Transport.SetReceiver
func (t *Transport) SetReceiver(r transport.Receiver) {
t.Receiver = r
}

// StartReceiver implements Transport.StartReceiver
// NOTE: This is a blocking call.
func (t *Transport) StartReceiver(ctx context.Context) error {
t.reMu.Lock()
defer t.reMu.Unlock()
Expand Down
6 changes: 1 addition & 5 deletions pkg/cloudevents/transport/message.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package transport

// Message is the abstract transport message wrapper.
type Message interface {
// CloudEventsVersion returns the version of the CloudEvent.
CloudEventsVersion() string

// TODO maybe get encoding
}

type Response struct {
ResponseCode int
Body []byte
}
2 changes: 1 addition & 1 deletion pkg/cloudevents/transport/nats/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
Package transport/nats implements the CloudEvent transport implementation using NATS.
Package nats implements the CloudEvent transport implementation using NATS.
*/
package nats
7 changes: 7 additions & 0 deletions pkg/cloudevents/transport/nats/encoding.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package nats

// Encoding to use for NATS transport.
type Encoding int32

const (
// Default allows NATS transport implementation to pick.
Default Encoding = iota
// StructuredV02 is Structured CloudEvents spec v0.2.
StructuredV02
// StructuredV03 is Structured CloudEvents spec v0.3.
StructuredV03
// Unknown is unknown.
Unknown
)

// String pretty-prints the encoding as a string.
func (e Encoding) String() string {
switch e {
case Default:
Expand All @@ -25,6 +31,7 @@ func (e Encoding) String() string {
}
}

// Version pretty-prints the encoding version as a string.
func (e Encoding) Version() string {
switch e {

Expand Down
1 change: 1 addition & 0 deletions pkg/cloudevents/transport/nats/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nats

// Option is the function signature required to be considered an nats.Option.
type Option func(*Transport) error

// WithEncoding sets the encoding for NATS transport.
Expand Down
8 changes: 6 additions & 2 deletions pkg/cloudevents/transport/nats/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"log"
)

// type check that this transport message impl matches the contract
// Transport adheres to transport.Transport.
var _ transport.Transport = (*Transport)(nil)

// Transport acts as both a http client and a http handler.
Expand All @@ -25,6 +25,7 @@ type Transport struct {
codec transport.Codec
}

// New creates a new NATS transport.
func New(natsServer, subject string, opts ...Option) (*Transport, error) {
conn, err := nats.Connect(natsServer)
if err != nil {
Expand Down Expand Up @@ -66,6 +67,7 @@ func (t *Transport) loadCodec() bool {
return true
}

// Send implements Transport.Send
func (t *Transport) Send(ctx context.Context, event cloudevents.Event) (*cloudevents.Event, error) {
if ok := t.loadCodec(); !ok {
return nil, fmt.Errorf("unknown encoding set on transport: %d", t.Encoding)
Expand All @@ -83,11 +85,13 @@ func (t *Transport) Send(ctx context.Context, event cloudevents.Event) (*cloudev
return nil, fmt.Errorf("failed to encode Event into a Message")
}

// SetReceiver implements Transport.SetReceiver
func (t *Transport) SetReceiver(r transport.Receiver) {
t.Receiver = r
}

// Blocking
// StartReceiver implements Transport.StartReceiver
// NOTE: This is a blocking call.
func (t *Transport) StartReceiver(ctx context.Context) error {
if t.Conn == nil {
return fmt.Errorf("no active nats connection")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudevents/types/allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package types

import "reflect"

// Allocates allocates a new instance of type t and returns:
// Allocate allocates a new instance of type t and returns:
// asPtr is of type t if t is a pointer type and of type &t otherwise
// asValue is a Value of type t pointing to the same data as asPtr
func Allocate(obj interface{}) (asPtr interface{}, asValue reflect.Value) {
Expand Down

0 comments on commit 5118a97

Please sign in to comment.