Skip to content

Commit

Permalink
Adding temporary legacy import to allow integrators to migrate. (#391)
Browse files Browse the repository at this point in the history
* Adding temporary legacy import to allow integrators to migrate.

Signed-off-by: Scott Nichols <snichols@vmware.com>

* lint

Signed-off-by: Scott Nichols <snichols@vmware.com>
  • Loading branch information
n3wscott authored Mar 17, 2020
1 parent 3e150c4 commit a4293fb
Show file tree
Hide file tree
Showing 224 changed files with 31,889 additions and 2 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/cloudevents/sdk-go

require (
cloud.google.com/go v0.38.0
contrib.go.opencensus.io/exporter/prometheus v0.1.0
github.com/Azure/azure-sdk-for-go v30.1.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.2.0 // indirect
Expand All @@ -26,8 +27,8 @@ require (
golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 // indirect
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58
google.golang.org/api v0.15.0 // indirect
google.golang.org/grpc v1.26.0 // indirect
google.golang.org/api v0.15.0
google.golang.org/grpc v1.26.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
pack.ag/amqp v0.11.0
Expand Down
76 changes: 76 additions & 0 deletions legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# NOTE: The Legacy directory will be removed for v2.0.0.

We will make a final migration release that will match the v2.0.0 release with
the addition of this legacy directory for migration support.

# Migration Guide

To enable an easier migration, this directory holds a copy of the code in the
branch `release-1.y.z`.

Switch your imports from `github.com/cloudevents/sdk-go` to
`github.com/cloudevents/sdk-go/legacy` and your code should compile again,
letting you get on with the task of migrating to v2.

## Background

In the migration from v1 to v2 of the SDK, there are a lot of API breaking
changes. It is shorter to define what is not a breaking change:

- The `Event` object marshaling results in the same json.
- cloudevents.NewDefault should get an http server working out of the box.
- Most of alias.go file remains with some exceptions.
- Most of the original demos remain in the cmd dir to see how the new
integrations should be.

Large breaking changes have to do with a strategy change inside the SDK to shift
the control to the integrator, allowing more direct access to the knobs required
to make choices over plumbing those knobs through the SDK down to the original
transports that implement the features integrators are really trying to control.

If you implemented a custom transport, the migration to how protocol bindings
work is covered in the document [TBD](TODO).

## Architectural Changes

New Architectural Layout:

```
Client <-- Operates on event.Event
|
v
Protocol <-- Operates on binding.Message
|
v
3rd Party <-- Operates out of our control
```

Some Architectural changes that are worth noting:

- Client still exists but it has a new API.
- client.Request allows for responses from outbound events.
- client.StartReceiver has a mode that will test for underlying support if the
receiver function is allowed to produce responses to inbound events.
- Client interface is event.Event focused.
- Protocol layer operates on `binding.Message`
- This is a change from v1, `transport.Transport` mixed up `event.Event`
objects into the interface. With the thinking that codecs were specific to a
transport. But as we implemented bindings, it became clear that there are
many cases where the cost to convert a 3rd Party message into a
`event.Event` is too high and it is better to stay in the intermediate state
of a `binding.Message` (similar to a `transport.Message` but
`transport.Message` was never exposed in the v1 architecture).
- Setting a transport to emit a specific version of cloudevents is an
anti-pattern. If a version is required, the burden should be on the integrator
to implement what they need. The edge cases the SDK had to handle made that
code unrulely. It is simpler if the SDK does simple things. So outbound event
encoding is based on the `event.Event` that is passed in.

## Moves and renames

Note these are based on internal packages unless noted as from alias.

- `cloudevents.Event` --> `event.Event`
- `transport.Codec` --> Deleted, the binding concept replaced it.
- `transport.Transport` --> Deleted, the
protocol.Sender/Receiver/Requester/Responder interfaces replaced it.
241 changes: 241 additions & 0 deletions legacy/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
package cloudevents

// Package cloudevents alias' common functions and types to improve discoverability and reduce
// the number of imports for simple HTTP clients.

import (
"github.com/cloudevents/sdk-go/legacy/pkg/cloudevents"
"github.com/cloudevents/sdk-go/legacy/pkg/cloudevents/client"
"github.com/cloudevents/sdk-go/legacy/pkg/cloudevents/context"
"github.com/cloudevents/sdk-go/legacy/pkg/cloudevents/observability"
"github.com/cloudevents/sdk-go/legacy/pkg/cloudevents/transport/http"
"github.com/cloudevents/sdk-go/legacy/pkg/cloudevents/types"
)

// Client

// Deprecated: legacy.
type ClientOption client.Option

// Deprecated: legacy.
type Client = client.Client

// Deprecated: legacy.
type ConvertFn = client.ConvertFn

// Event

// Deprecated: legacy.
type Event = cloudevents.Event

// Deprecated: legacy.
type EventResponse = cloudevents.EventResponse

// Context

// Deprecated: legacy.
type EventContext = cloudevents.EventContext

// Deprecated: legacy.
type EventContextV1 = cloudevents.EventContextV1

// Deprecated: legacy.
type EventContextV01 = cloudevents.EventContextV01

// Deprecated: legacy.
type EventContextV02 = cloudevents.EventContextV02

// Deprecated: legacy.
type EventContextV03 = cloudevents.EventContextV03

// Custom Types

// Deprecated: legacy.
type Timestamp = types.Timestamp

// Deprecated: legacy.
type URLRef = types.URLRef

// HTTP Transport

// Deprecated: legacy.
type HTTPOption http.Option

// Deprecated: legacy.
type HTTPTransport = http.Transport

// Deprecated: legacy.
type HTTPTransportContext = http.TransportContext

// Deprecated: legacy.
type HTTPTransportResponseContext = http.TransportResponseContext

// Deprecated: legacy.
type HTTPEncoding = http.Encoding

const (
// Encoding
// Deprecated: legacy.
ApplicationXML = cloudevents.ApplicationXML
// Deprecated: legacy.
ApplicationJSON = cloudevents.ApplicationJSON
// Deprecated: legacy.
ApplicationCloudEventsJSON = cloudevents.ApplicationCloudEventsJSON
// Deprecated: legacy.
ApplicationCloudEventsBatchJSON = cloudevents.ApplicationCloudEventsBatchJSON
// Deprecated: legacy.
Base64 = cloudevents.Base64

// Event Versions

// Deprecated: legacy.
VersionV1 = cloudevents.CloudEventsVersionV1
// Deprecated: legacy.
VersionV01 = cloudevents.CloudEventsVersionV01
// Deprecated: legacy.
VersionV02 = cloudevents.CloudEventsVersionV02
// Deprecated: legacy.
VersionV03 = cloudevents.CloudEventsVersionV03

// HTTP Transport Encodings

// Deprecated: legacy.
HTTPBinaryV1 = http.BinaryV1
// Deprecated: legacy.
HTTPStructuredV1 = http.StructuredV1
// Deprecated: legacy.
HTTPBatchedV1 = http.BatchedV1
// Deprecated: legacy.
HTTPBinaryV01 = http.BinaryV01
// Deprecated: legacy.
HTTPStructuredV01 = http.StructuredV01
// Deprecated: legacy.
HTTPBinaryV02 = http.BinaryV02
// Deprecated: legacy.
HTTPStructuredV02 = http.StructuredV02
// Deprecated: legacy.
HTTPBinaryV03 = http.BinaryV03
// Deprecated: legacy.
HTTPStructuredV03 = http.StructuredV03
// Deprecated: legacy.
HTTPBatchedV03 = http.BatchedV03

// Context HTTP Transport Encodings

Binary = http.Binary
Structured = http.Structured
)

var (
// ContentType Helpers

// Deprecated: legacy.
StringOfApplicationJSON = cloudevents.StringOfApplicationJSON
// Deprecated: legacy.
StringOfApplicationXML = cloudevents.StringOfApplicationXML
// Deprecated: legacy.
StringOfApplicationCloudEventsJSON = cloudevents.StringOfApplicationCloudEventsJSON
// Deprecated: legacy.
StringOfApplicationCloudEventsBatchJSON = cloudevents.StringOfApplicationCloudEventsBatchJSON
// Deprecated: legacy.
StringOfBase64 = cloudevents.StringOfBase64

// Client Creation

// Deprecated: legacy.
NewClient = client.New
// Deprecated: legacy.
NewDefaultClient = client.NewDefault

// Client Options

// Deprecated: legacy.
WithEventDefaulter = client.WithEventDefaulter
// Deprecated: legacy.
WithUUIDs = client.WithUUIDs
// Deprecated: legacy.
WithTimeNow = client.WithTimeNow
// Deprecated: legacy.
WithConverterFn = client.WithConverterFn
// Deprecated: legacy.
WithDataContentType = client.WithDataContentType
// Deprecated: legacy.
WithoutTracePropagation = client.WithoutTracePropagation

// Event Creation

// Deprecated: legacy.
NewEvent = cloudevents.New

// Tracing

// Deprecated: legacy.
EnableTracing = observability.EnableTracing

// Context

// Deprecated: legacy.
ContextWithTarget = context.WithTarget
// Deprecated: legacy.
TargetFromContext = context.TargetFrom
// Deprecated: legacy.
ContextWithEncoding = context.WithEncoding
// Deprecated: legacy.
EncodingFromContext = context.EncodingFrom

// Custom Types

// Deprecated: legacy.
ParseTimestamp = types.ParseTimestamp
// Deprecated: legacy.
ParseURLRef = types.ParseURLRef
// Deprecated: legacy.
ParseURIRef = types.ParseURIRef
// Deprecated: legacy.
ParseURI = types.ParseURI

// HTTP Transport

// Deprecated: legacy.
NewHTTPTransport = http.New

// HTTP Transport Options

// Deprecated: legacy.
WithTarget = http.WithTarget
// Deprecated: legacy.
WithMethod = http.WithMethod
// Deprecated: legacy.
WitHHeader = http.WithHeader
// Deprecated: legacy.
WithShutdownTimeout = http.WithShutdownTimeout
// Deprecated: legacy.
WithEncoding = http.WithEncoding
// Deprecated: legacy.
WithContextBasedEncoding = http.WithContextBasedEncoding
// Deprecated: legacy.
WithBinaryEncoding = http.WithBinaryEncoding
// Deprecated: legacy.
WithStructuredEncoding = http.WithStructuredEncoding
// Deprecated: legacy.
WithPort = http.WithPort
// Deprecated: legacy.
WithPath = http.WithPath
// Deprecated: legacy.
WithMiddleware = http.WithMiddleware
// Deprecated: legacy.
WithLongPollTarget = http.WithLongPollTarget
// Deprecated: legacy.
WithListener = http.WithListener
// Deprecated: legacy.
WithHTTPTransport = http.WithHTTPTransport

// HTTP Context

// Deprecated: legacy.
HTTPTransportContextFrom = http.TransportContextFrom
// Deprecated: legacy.
ContextWithHeader = http.ContextWithHeader
// Deprecated: legacy.
SetContextHeaders = http.SetContextHeaders
)
33 changes: 33 additions & 0 deletions legacy/pkg/binding/buffering/acks_before_finish_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package buffering

import (
"sync/atomic"

"github.com/cloudevents/sdk-go/legacy/pkg/binding"
)

type acksMessage struct {
binding.Message
requiredAcks int32
}

func (m *acksMessage) GetWrappedMessage() binding.Message {
return m.Message
}

func (m *acksMessage) Finish(err error) error {
remainingAcks := atomic.AddInt32(&m.requiredAcks, -1)
if remainingAcks == 0 {
return m.Message.Finish(err)
}
return nil
}

var _ binding.MessageWrapper = (*acksMessage)(nil)

// WithAcksBeforeFinish returns a wrapper for m that calls m.Finish()
// only after the specified number of acks are received.
// Use it when you need to route a Message to more Sender instances
func WithAcksBeforeFinish(m binding.Message, requiredAcks int) binding.Message {
return &acksMessage{Message: m, requiredAcks: int32(requiredAcks)}
}
Loading

0 comments on commit a4293fb

Please sign in to comment.