Repository: cloudevents/sdk-go
Pull request #390 (2020-03-16 22:42:38)

Add missing alias imports; Moving to var-arg transformers

- Add missing alias imports
- Moving to var-arg transformers

Signed-off-by: Scott Nichols <snichols@vmware.com>

n3wscott: binding.ToEvent:
```
func ToEvent(ctx context.Context, message Message, transformers ...TransformerFactory) (e ce.Event, encoding Encoding, err error) 
```

Patch:
diff --git a/alias.go b/alias.go
index 333b010..c3497fb 100644
--- a/alias.go
+++ b/alias.go
@@ -44,6 +44,10 @@ type HTTPProtocol = http.Protocol
 
 type Encoding = binding.Encoding
 
+// Message
+
+type Message = binding.Message
+
 const (
 	// ReadEncoding
 
@@ -95,12 +99,16 @@ var (
 	NewEvent  = event.New
 	NewResult = protocol.NewResult
 
-	NewHTTPResponse = http.NewResult
+	NewHTTPResult = http.NewResult
 
 	// Message Creation
 
 	ToMessage = binding.ToMessage
 
+	// HTTP Messages
+
+	WriteHTTPRequest = http.WriteRequest
+
 	// Tracing
 
 	EnableTracing = observability.EnableTracing
diff --git a/cmd/samples/httpb/responder/main.go b/cmd/samples/httpb/responder/main.go
index c5e3282..e8d823d 100644
--- a/cmd/samples/httpb/responder/main.go
+++ b/cmd/samples/httpb/responder/main.go
@@ -50,5 +50,5 @@ func main() {
 func gotEvent(ctx context.Context, event cloudevents.Event) (*event.Event, protocol.Result) {
 	fmt.Printf("Got Event: %+v\n", event)
 
-	return &event, cloudevents.NewHTTPResponse(http.StatusAccepted, "accept")
+	return &event, cloudevents.NewHTTPResult(http.StatusAccepted, "accept")
 }
diff --git a/legacy/pkg/binding/transcoder.go b/legacy/pkg/binding/transcoder.go
index f2f47f5..dce23d3 100644
--- a/legacy/pkg/binding/transcoder.go
+++ b/legacy/pkg/binding/transcoder.go
@@ -27,8 +27,11 @@ func (t TransformerFactories) StructuredTransformer(encoder StructuredEncoder) S
 	}
 	res := encoder
 	for _, b := range t {
-		if new := b.StructuredTransformer(res); new != nil {
-			res = new
+		if b == nil {
+			continue
+		}
+		if r := b.StructuredTransformer(res); r != nil {
+			res = r
 		} else {
 			return nil // Structured not supported!
 		}
@@ -42,8 +45,11 @@ func (t TransformerFactories) BinaryTransformer(encoder BinaryEncoder) BinaryEnc
 	}
 	res := encoder
 	for _, b := range t {
-		if new := b.BinaryTransformer(res); new != nil {
-			res = new
+		if b == nil {
+			continue
+		}
+		if r := b.BinaryTransformer(res); r != nil {
+			res = r
 		} else {
 			return nil // Binary not supported!
 		}
@@ -54,8 +60,10 @@ func (t TransformerFactories) BinaryTransformer(encoder BinaryEncoder) BinaryEnc
 func (t TransformerFactories) EventTransformer() EventTransformer {
 	return func(e *ce.Event) error {
 		for _, factory := range t {
+			if factory == nil {
+				continue
+			}
 			f := factory.EventTransformer()
-
 			if f != nil {
 				err := f(e)
 				if err != nil {
diff --git a/legacy/pkg/bindings/amqp/encoder.go b/legacy/pkg/bindings/amqp/encoder.go
index c5f1879..5a60914 100644
--- a/legacy/pkg/bindings/amqp/encoder.go
+++ b/legacy/pkg/bindings/amqp/encoder.go
@@ -15,7 +15,7 @@ import (
 
 // Fill the provided amqpMessage with the message m.
 // Using context you can tweak the encoding processing (more details on binding.Translate documentation).
-func EncodeAMQPMessage(ctx context.Context, m binding.Message, amqpMessage *amqp.Message, transformerFactories binding.TransformerFactories) error {
+func EncodeAMQPMessage(ctx context.Context, m binding.Message, amqpMessage *amqp.Message, transformerFactories ...binding.TransformerFactory) error {
 	structuredEncoder := (*amqpMessageEncoder)(amqpMessage)
 	binaryEncoder := (*amqpMessageEncoder)(amqpMessage)
 
diff --git a/pkg/binding/buffering/copy_message.go b/pkg/binding/buffering/copy_message.go
index dc86bb5..62d7305 100644
--- a/pkg/binding/buffering/copy_message.go
+++ b/pkg/binding/buffering/copy_message.go
@@ -28,7 +28,7 @@ func CopyMessage(ctx context.Context, m binding.Message, transformers binding.Tr
 		return nil, binding.ErrUnknownEncoding
 	}
 	if originalMessageEncoding == binding.EncodingEvent {
-		e, err := binding.ToEvent(ctx, m, transformers)
+		e, err := binding.ToEvent(ctx, m, transformers...)
 		if err != nil {
 			return nil, err
 		}
@@ -44,7 +44,7 @@ func CopyMessage(ctx context.Context, m binding.Message, transformers binding.Tr
 	} else if encoding == binding.EncodingBinary {
 		return &bm, err
 	} else {
-		e, err := binding.ToEvent(ctx, m, transformers)
+		e, err := binding.ToEvent(ctx, m, transformers...)
 		if err != nil {
 			return nil, err
 		}
diff --git a/pkg/binding/to_event.go b/pkg/binding/to_event.go
index b62262b..171dd7c 100644
--- a/pkg/binding/to_event.go
+++ b/pkg/binding/to_event.go
@@ -19,14 +19,18 @@ var ErrCannotConvertToEvent = errors.New("cannot convert message to event")
 // This function returns the Event generated from the Message and the original encoding of the message or
 // an error that points the conversion error.
 // transformers can be nil and this function guarantees that they are invoked only once during the encoding process.
-func ToEvent(ctx context.Context, message MessageReader, transformers TransformerFactories) (*event.Event, error) {
+func ToEvent(ctx context.Context, message MessageReader, transformers ...TransformerFactory) (*event.Event, error) {
 	messageEncoding := message.ReadEncoding()
 	if messageEncoding == EncodingEvent {
 		for m := message; m != nil; {
 			if em, ok := m.(*EventMessage); ok {
 				e := (*event.Event)(em)
-				if err := transformers.EventTransformer()(e); err != nil {
-					return nil, err
+				if transformers != nil {
+					var tf TransformerFactories
+					tf = transformers
+					if err := tf.EventTransformer()(e); err != nil {
+						return nil, err
+					}
 				}
 				return e, nil
 			}
@@ -50,8 +54,12 @@ func ToEvent(ctx context.Context, message MessageReader, transformers Transforme
 	); err != nil {
 		return nil, err
 	}
-	if err := transformers.EventTransformer()(&e); err != nil {
-		return nil, err
+	if transformers != nil {
+		var tf TransformerFactories
+		tf = transformers
+		if err := tf.EventTransformer()(&e); err != nil {
+			return nil, err
+		}
 	}
 	return &e, nil
 }
diff --git a/pkg/binding/transformer.go b/pkg/binding/transformer.go
index 3a01225..2ad667c 100644
--- a/pkg/binding/transformer.go
+++ b/pkg/binding/transformer.go
@@ -28,13 +28,14 @@ func (t TransformerFactories) StructuredTransformer(encoder StructuredWriter) St
 		return nil
 	}
 	res := encoder
-	if t != nil {
-		for _, b := range t {
-			if new := b.StructuredTransformer(res); new != nil {
-				res = new
-			} else {
-				return nil // Structured not supported!
-			}
+	for _, b := range t {
+		if b == nil {
+			continue
+		}
+		if r := b.StructuredTransformer(res); r != nil {
+			res = r
+		} else {
+			return nil // Structured not supported!
 		}
 	}
 	return res
@@ -45,13 +46,15 @@ func (t TransformerFactories) BinaryTransformer(encoder BinaryWriter) BinaryWrit
 		return nil
 	}
 	res := encoder
-	if t != nil {
-		for i, _ := range t {
-			if new := t[len(t)-i-1].BinaryTransformer(res); new != nil {
-				res = new
-			} else {
-				return nil // Binary not supported!
-			}
+	for i := range t {
+		b := t[len(t)-i-1]
+		if b == nil {
+			continue
+		}
+		if r := b.BinaryTransformer(res); r != nil {
+			res = r
+		} else {
+			return nil // Binary not supported!
 		}
 	}
 	return res
@@ -59,15 +62,15 @@ func (t TransformerFactories) BinaryTransformer(encoder BinaryWriter) BinaryWrit
 
 func (t TransformerFactories) EventTransformer() EventTransformer {
 	return func(e *event.Event) error {
-		if t != nil {
-			for _, factory := range t {
-				f := factory.EventTransformer()
-
-				if f != nil {
-					err := f(e)
-					if err != nil {
-						return err
-					}
+		for _, b := range t {
+			if b == nil {
+				continue
+			}
+			f := b.EventTransformer()
+			if f != nil {
+				err := f(e)
+				if err != nil {
+					return err
 				}
 			}
 		}
diff --git a/pkg/binding/write.go b/pkg/binding/write.go
index e12df23..e6dbdaa 100644
--- a/pkg/binding/write.go
+++ b/pkg/binding/write.go
@@ -73,7 +73,7 @@ func Write(
 	message MessageReader,
 	structuredWriter StructuredWriter,
 	binaryWriter BinaryWriter,
-	transformers TransformerFactories,
+	transformers ...TransformerFactory,
 ) (Encoding, error) {
 	enc := message.ReadEncoding()
 	var err error
@@ -87,7 +87,7 @@ func Write(
 	}
 
 	var e *event.Event
-	e, err = ToEvent(ctx, message, transformers)
+	e, err = ToEvent(ctx, message, transformers...)
 	if err != nil {
 		return enc, err
 	}
diff --git a/pkg/client/client.go b/pkg/client/client.go
index 035af63..bf42889 100644
--- a/pkg/client/client.go
+++ b/pkg/client/client.go
@@ -144,7 +144,7 @@ func (c *ceClient) Request(ctx context.Context, e event.Event) (*event.Event, er
 	}()
 	if err == nil {
 		fmt.Printf("%#v", msg)
-		if rs, err := binding.ToEvent(ctx, msg, nil); err != nil {
+		if rs, err := binding.ToEvent(ctx, msg); err != nil {
 			cecontext.LoggerFrom(ctx).Infow("failed calling ToEvent", zap.Error(err), zap.Any("resp", msg))
 		} else {
 			resp = rs
diff --git a/pkg/client/invoker.go b/pkg/client/invoker.go
index 034c3df..99dc193 100644
--- a/pkg/client/invoker.go
+++ b/pkg/client/invoker.go
@@ -42,7 +42,7 @@ func (r *receiveInvoker) Invoke(ctx context.Context, m binding.Message, respFn p
 		}
 	}()
 
-	e, err := binding.ToEvent(ctx, m, nil)
+	e, err := binding.ToEvent(ctx, m)
 	if err != nil {
 		return err
 	}
diff --git a/pkg/protocol/amqp/write_message.go b/pkg/protocol/amqp/write_message.go
index eb55a4d..fb0c862 100644
--- a/pkg/protocol/amqp/write_message.go
+++ b/pkg/protocol/amqp/write_message.go
@@ -15,7 +15,7 @@ import (
 
 // Fill the provided amqpMessage with the message m.
 // Using context you can tweak the encoding processing (more details on binding.Write documentation).
-func WriteMessage(ctx context.Context, m binding.Message, amqpMessage *amqp.Message, transformers binding.TransformerFactories) error {
+func WriteMessage(ctx context.Context, m binding.Message, amqpMessage *amqp.Message, transformers ...binding.TransformerFactory) error {
 	structuredWriter := (*amqpMessageWriter)(amqpMessage)
 	binaryWriter := (*amqpMessageWriter)(amqpMessage)
 
@@ -24,7 +24,7 @@ func WriteMessage(ctx context.Context, m binding.Message, amqpMessage *amqp.Mess
 		m,
 		structuredWriter,
 		binaryWriter,
-		transformers,
+		transformers...,
 	)
 	return err
 }
diff --git a/pkg/protocol/http/write_request.go b/pkg/protocol/http/write_request.go
index 7773a08..32007b2 100644
--- a/pkg/protocol/http/write_request.go
+++ b/pkg/protocol/http/write_request.go
@@ -14,7 +14,7 @@ import (
 
 // Fill the provided httpRequest with the message m.
 // Using context you can tweak the encoding processing (more details on binding.Write documentation).
-func WriteRequest(ctx context.Context, m binding.Message, httpRequest *http.Request, transformers binding.TransformerFactories) error {
+func WriteRequest(ctx context.Context, m binding.Message, httpRequest *http.Request, transformers ...binding.TransformerFactory) error {
 	structuredWriter := (*httpRequestWriter)(httpRequest)
 	binaryWriter := (*httpRequestWriter)(httpRequest)
 
@@ -23,7 +23,7 @@ func WriteRequest(ctx context.Context, m binding.Message, httpRequest *http.Requ
 		m,
 		structuredWriter,
 		binaryWriter,
-		transformers,
+		transformers...,
 	)
 	return err
 }
diff --git a/pkg/protocol/http/write_responsewriter.go b/pkg/protocol/http/write_responsewriter.go
index a075a83..776b2dc 100644
--- a/pkg/protocol/http/write_responsewriter.go
+++ b/pkg/protocol/http/write_responsewriter.go
@@ -14,7 +14,7 @@ import (
 
 // Write out to the the provided httpResponseWriter with the message m.
 // Using context you can tweak the encoding processing (more details on binding.Write documentation).
-func WriteResponseWriter(ctx context.Context, m binding.Message, status int, rw http.ResponseWriter, transformers binding.TransformerFactories) error {
+func WriteResponseWriter(ctx context.Context, m binding.Message, status int, rw http.ResponseWriter, transformers ...binding.TransformerFactory) error {
 	if status < 200 || status >= 600 {
 		status = http.StatusOK
 	}
@@ -25,7 +25,7 @@ func WriteResponseWriter(ctx context.Context, m binding.Message, status int, rw
 		m,
 		writer,
 		writer,
-		transformers,
+		transformers...,
 	)
 	return err
 }
diff --git a/pkg/protocol/kafka_sarama/write_producer_message.go b/pkg/protocol/kafka_sarama/write_producer_message.go
index 6f763d0..c690cdc 100644
--- a/pkg/protocol/kafka_sarama/write_producer_message.go
+++ b/pkg/protocol/kafka_sarama/write_producer_message.go
@@ -15,7 +15,7 @@ import (
 
 // Fill the provided producerMessage with the message m.
 // Using context you can tweak the encoding processing (more details on binding.Write documentation).
-func WriteProducerMessage(ctx context.Context, m binding.Message, producerMessage *sarama.ProducerMessage, transformerFactories binding.TransformerFactories) error {
+func WriteProducerMessage(ctx context.Context, m binding.Message, producerMessage *sarama.ProducerMessage, transformerFactories ...binding.TransformerFactory) error {
 	enc := (*kafkaProducerMessageWriter)(producerMessage)
 
 	_, err := binding.Write(
@@ -23,7 +23,7 @@ func WriteProducerMessage(ctx context.Context, m binding.Message, producerMessag
 		m,
 		enc,
 		enc,
-		transformerFactories,
+		transformerFactories...,
 	)
 	return err
 }
diff --git a/pkg/protocol/nats/write_message.go b/pkg/protocol/nats/write_message.go
index 4596e69..ef7d5b4 100644
--- a/pkg/protocol/nats/write_message.go
+++ b/pkg/protocol/nats/write_message.go
@@ -13,7 +13,7 @@ import (
 
 // Fill the provided natsMessage with the bindings.Message m.
 // Using context you can tweak the encoding processing (more details on binding.Write documentation).
-func WriteMsg(ctx context.Context, m binding.Message, natsMessage *nats.Msg, transformers binding.TransformerFactories) error {
+func WriteMsg(ctx context.Context, m binding.Message, natsMessage *nats.Msg, transformers ...binding.TransformerFactory) error {
 	structuredWriter := (*natsMessageWriter)(natsMessage)
 
 	_, err := binding.Write(
@@ -21,7 +21,7 @@ func WriteMsg(ctx context.Context, m binding.Message, natsMessage *nats.Msg, tra
 		m,
 		structuredWriter,
 		nil,
-		transformers,
+		transformers...,
 	)
 	return err
 }


Test patch:
diff --git a/pkg/binding/buffering/copy_message_test.go b/pkg/binding/buffering/copy_message_test.go
index 05cacdc..785649d 100644
--- a/pkg/binding/buffering/copy_message_test.go
+++ b/pkg/binding/buffering/copy_message_test.go
@@ -84,7 +84,7 @@ func TestCopyMessage(t *testing.T) {
 			require.NoError(t, err)
 			// The copy can be read any number of times
 			for i := 0; i < 3; i++ {
-				got, err := binding.ToEvent(context.Background(), cpy, nil)
+				got, err := binding.ToEvent(context.Background(), cpy)
 				assert.NoError(t, err)
 				require.Equal(t, tt.encoding, cpy.ReadEncoding())
 				AssertEventEquals(t, ExToStr(t, tt.want), ExToStr(t, *got))
@@ -101,7 +101,7 @@ func TestCopyMessage(t *testing.T) {
 			require.NoError(t, err)
 			// The copy can be read any number of times
 			for i := 0; i < 3; i++ {
-				got, err := binding.ToEvent(context.Background(), cpy, nil)
+				got, err := binding.ToEvent(context.Background(), cpy)
 				assert.NoError(t, err)
 				require.Equal(t, tt.encoding, cpy.ReadEncoding())
 				AssertEventEquals(t, ExToStr(t, tt.want), ExToStr(t, *got))
diff --git a/pkg/binding/test/test.go b/pkg/binding/test/test.go
index db26fe4..349703c 100644
--- a/pkg/binding/test/test.go
+++ b/pkg/binding/test/test.go
@@ -102,7 +102,7 @@ func MustJSON(e event.Event) []byte {
 
 // Must convert the Message to event.Event
 func MustToEvent(t *testing.T, ctx context.Context, m binding.Message) event.Event {
-	e, err := binding.ToEvent(ctx, m, nil)
+	e, err := binding.ToEvent(ctx, m)
 	require.NoError(t, err)
 	return *e
 }
diff --git a/pkg/binding/test/transformer.go b/pkg/binding/test/transformer.go
index ec5c1f7..a0f5930 100644
--- a/pkg/binding/test/transformer.go
+++ b/pkg/binding/test/transformer.go
@@ -35,15 +35,15 @@ func RunTransformerTests(t *testing.T, ctx context.Context, tests []TransformerT
 			mockStructured := MockStructuredMessage{}
 			mockBinary := MockBinaryMessage{}
 
-			enc, err := binding.Write(ctx, inputMessage, &mockStructured, &mockBinary, tt.Transformers)
+			enc, err := binding.Write(ctx, inputMessage, &mockStructured, &mockBinary, tt.Transformers...)
 			require.NoError(t, err)
 
 			var e *event.Event
 			if enc == binding.EncodingStructured {
-				e, err = binding.ToEvent(ctx, &mockStructured, nil)
+				e, err = binding.ToEvent(ctx, &mockStructured)
 				require.NoError(t, err)
 			} else if enc == binding.EncodingBinary {
-				e, err = binding.ToEvent(ctx, &mockBinary, nil)
+				e, err = binding.ToEvent(ctx, &mockBinary)
 				require.NoError(t, err)
 			} else {
 				t.Fatalf("Unexpected encoding %v", enc)
diff --git a/pkg/binding/to_event_test.go b/pkg/binding/to_event_test.go
index b47becd..22ae9cd 100644
--- a/pkg/binding/to_event_test.go
+++ b/pkg/binding/to_event_test.go
@@ -65,7 +65,7 @@ func TestToEvent(t *testing.T) {
 				e := tt.event.Clone()
 				inputMessage = binding.ToMessage(&e)
 			}
-			got, err := binding.ToEvent(context.Background(), inputMessage, nil)
+			got, err := binding.ToEvent(context.Background(), inputMessage)
 			require.NoError(t, err)
 			test.AssertEventEquals(t, test.ExToStr(t, tt.want), test.ExToStr(t, *got))
 		})
diff --git a/pkg/protocol/http/write_responsewriter_test.go b/pkg/protocol/http/write_responsewriter_test.go
index 43d455b..1019b2b 100644
--- a/pkg/protocol/http/write_responsewriter_test.go
+++ b/pkg/protocol/http/write_responsewriter_test.go
@@ -68,7 +68,7 @@ func TestWriteHttpResponseWriter(t *testing.T) {
 				messageOut := NewMessage(res.Header(), ioutil.NopCloser(bytes.NewReader(res.Body.Bytes())))
 				require.Equal(t, tt.expectedEncoding, messageOut.ReadEncoding())
 
-				eventOut, err := binding.ToEvent(context.TODO(), messageOut, nil)
+				eventOut, err := binding.ToEvent(context.TODO(), messageOut)
 				require.NoError(t, err)
 				test.AssertEventEquals(t, eventIn, *eventOut)
 			})
diff --git a/pkg/protocol/kafka_sarama/message_benchmark_test.go b/pkg/protocol/kafka_sarama/message_benchmark_test.go
index dc75922..00567a0 100644
--- a/pkg/protocol/kafka_sarama/message_benchmark_test.go
+++ b/pkg/protocol/kafka_sarama/message_benchmark_test.go
@@ -58,14 +58,14 @@ func BenchmarkNewBinaryMessage(b *testing.B) {
 func BenchmarkNewStructuredMessageToEvent(b *testing.B) {
 	for i := 0; i < b.N; i++ {
 		M = kafka_sarama.NewMessageFromConsumerMessage(structuredConsumerMessage)
-		Event, Err = binding.ToEvent(context.TODO(), M, nil)
+		Event, Err = binding.ToEvent(context.TODO(), M)
 	}
 }
 
 func BenchmarkNewBinaryMessageToEvent(b *testing.B) {
 	for i := 0; i < b.N; i++ {
 		M = kafka_sarama.NewMessageFromConsumerMessage(binaryConsumerMessage)
-		Event, Err = binding.ToEvent(context.TODO(), M, nil)
+		Event, Err = binding.ToEvent(context.TODO(), M)
 	}
 }
 
diff --git a/pkg/protocol/kafka_sarama/write_producer_message_test.go b/pkg/protocol/kafka_sarama/write_producer_message_test.go
index f99ca46..0601520 100644
--- a/pkg/protocol/kafka_sarama/write_producer_message_test.go
+++ b/pkg/protocol/kafka_sarama/write_producer_message_test.go
@@ -81,7 +81,7 @@ func TestEncodeKafkaProducerMessage(t *testing.T) {
 				messageOut := NewMessage(key, value, string(headers[contentTypeHeader]), headers)
 				require.Equal(t, tt.expectedEncoding, messageOut.ReadEncoding())
 
-				eventOut, err := binding.ToEvent(context.TODO(), messageOut, nil)
+				eventOut, err := binding.ToEvent(context.TODO(), messageOut)
 				require.NoError(t, err)
 				test.AssertEventEquals(t, eventIn, *eventOut)
 			})