Skip to content

Commit 8490c24

Browse files
authored
Handle event versions in the SDK (#37)
* Handle event versions in the SDK Change CDEventType to be a struct to allow for one-time parsing of the type string. The event type is still stored as string in the context so that it may unmarshal without the need for a custom unmarshaller. When producing events, the SDK produces the latest version of an event, the only one know to the SDK. When consuming events, the SDK allows for versions different from the SDK ones, as long as they are compatible with the SDK one, according to the versioning rules from the spec. The spec submodule has been moved into pkg/api so that we may use go embed to embed the schemas from the spec into the event modules which allows to embed the schema definitions in the SDK module. Signed-off-by: Andrea Frittoli <[email protected]>
1 parent 44b9ed1 commit 8490c24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1110
-753
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ jobs:
1717
steps:
1818
- name: Checkout
1919
uses: actions/checkout@v3
20+
with:
21+
submodules: recursive
2022
- name: Setup Go
2123
uses: actions/setup-go@v3
2224
with:
2325
go-version: 1.19
2426
- name: Format check
2527
run: make fmt
26-
- name: Linting check
27-
run: make lint
28+
- name: golangci-lint
29+
uses: golangci/golangci-lint-action@v3
30+
with:
31+
version: v1.52
2832

2933
test:
3034
runs-on: ubuntu-latest

.gitmodules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[submodule "spec"]
2-
path = spec
1+
[submodule "pkg/api/spec"]
2+
path = pkg/api/spec
33
url = https://github.com/cdevents/spec

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
go.uber.org/atomic v1.7.0 // indirect
3131
go.uber.org/multierr v1.6.0 // indirect
3232
go.uber.org/zap v1.17.0 // indirect
33+
golang.org/x/mod v0.9.0
3334
gopkg.in/yaml.v2 v2.4.0 // indirect
3435
gopkg.in/yaml.v3 v3.0.0 // indirect
3536
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
7474
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
7575
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
7676
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
77+
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
78+
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
7779
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
7880
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
7981
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

hack/add-event.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ import (
4646
"time"
4747
)
4848
49-
const (
50-
// ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} event
51-
${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1 CDEventType = "dev.cdevents.${SUBJECT_LOWER}.${PREDICATE_LOWER}.0.1.0"
52-
${SUBJECT_LOWER_CAMEL}${PREDICATE_UPPER_CAMEL}SchemaFile string = "${SUBJECT_LOWER}${PREDICATE_LOWER}"
49+
//go:embed spec/schemas/${SUBJECT_LOWER}${PREDICATE_LOWER}.json
50+
var ${SUBJECT_LOWER}${PREDICATE_LOWER}schema string
51+
52+
var (
53+
// ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} event v0.1.0
54+
${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}EventV1 CDEventType CDEventType = CDEventType{
55+
Subject: "${SUBJECT_LOWER}",
56+
Predicate: "${PREDICATE_LOWER}",
57+
Version: "0.1.0",
58+
}
5359
)
5460
5561
type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}SubjectContent struct{}
@@ -159,8 +165,9 @@ func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) SetCustomData(cont
159165
return nil
160166
}
161167
162-
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSchema() string {
163-
return ${SUBJECT_LOWER_CAMEL}${PREDICATE_UPPER_CAMEL}SchemaFile
168+
func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event) GetSchema() (string, string) {
169+
eType := e.GetType()
170+
return fmt.Sprintf(CDEventsSchemaURLTemplate, CDEventsSpecVersion, eType.Subject, eType.Predicate), schema${SUBJECT_LOWER}${PREDICATE_LOWER}schema
164171
}
165172
166173
func New${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event() (*${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL}Event, error) {

0 commit comments

Comments
 (0)