Skip to content

Commit

Permalink
Support for v04 Context
Browse files Browse the repository at this point in the history
Add a new context type for new fields in the context introduced
in v0.4. Create new interfaces to match the new fields as well
and update the templates to use the new context and implement
the new interfaces when the spec version is not 0.3.x.

Add a new version of the test type that is used to test the
generator code so that we can test generating for the new version.

Updated the logic to generate the schema in a golang file to
use it as a local schema database for the jsonschema library
to validate documents. The schemas are precompiled at init
time now, which simplifies the logic downstream in the code.

Signed-off-by: Andrea Frittoli <andrea.frittoli@gmail.com>
  • Loading branch information
afrittoli committed Jun 21, 2024
1 parent 6deb676 commit 53dc41a
Show file tree
Hide file tree
Showing 149 changed files with 5,361 additions and 1,090 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: 1.21
- name: Format check
run: make fmt
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.52
version: v1.59.1
args: --build-tags testonly
- name: Check generated code
run: make generate
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: 1.21
- name: Generate coverage report
run: make test
- name: Upload coverage reports to Codecov
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ importfmt: get-fmt-deps
goimports -w $(GO_DEPENDENCIES)

get-fmt-deps: ## Install test dependencies
$(GO_NOMOD) get golang.org/x/tools/cmd/goimports
$(GO) install golang.org/x/tools/cmd/goimports

.PHONY: lint
lint: ## Lint the code
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ go get github.com/cdevents/sdk-go
```

And import the module in your code corresponding to the desired version of the specification.
For CDEvents v0.3.x, use:
For CDEvents v0.4.x, use:

```golang
import cdevents "github.com/cdevents/sdk-go/pkg/api/v03"
import cdeventsv04 "github.com/cdevents/sdk-go/pkg/api/v04"
```

## Create your first CDEvent
Expand All @@ -27,7 +27,7 @@ To create a CDEvent, for instance a [*pipelineRun queued*](https://cdevents.dev/
func main() {

// Create the base event
event, err := cdevents.NewPipelineRunQueuedEvent()
event, err := cdeventsv04.NewPipelineRunQueuedEvent()
if err != nil {
log.Fatalf("could not create a cdevent, %v", err)
}
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/cdevents/sdk-go

go 1.18
go 1.21

require (
github.com/cloudevents/sdk-go/v2 v2.15.2
github.com/google/go-cmp v0.5.8
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.1.2
github.com/package-url/packageurl-go v0.1.1
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
golang.org/x/text v0.14.0
)

Expand All @@ -16,7 +16,8 @@ require (
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/tools v0.22.0 // indirect
)

require (
Expand All @@ -27,6 +28,6 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/mod v0.9.0
golang.org/x/mod v0.18.0
gopkg.in/yaml.v2 v2.4.0 // indirect
)
25 changes: 17 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
Expand All @@ -12,8 +14,8 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ=
github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand All @@ -37,18 +39,21 @@ github.com/package-url/packageurl-go v0.1.1/go.mod h1:uQd4a7Rh3ZsVg5j0lNyAfyxIeG
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 h1:TToq11gyfNlrMFZiYujSekIsPd9AmsA2Bj/iv+s4JHE=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
Expand All @@ -58,22 +63,25 @@ go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand All @@ -84,3 +92,4 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
52 changes: 25 additions & 27 deletions pkg/api/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,27 @@ import (
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/go-playground/validator/v10"
purl "github.com/package-url/packageurl-go"
jsonschema "github.com/santhosh-tekuri/jsonschema/v5"
"golang.org/x/mod/semver"
)

const SCHEMA_ID_REGEX = `^https://cdevents.dev/([0-9]\.[0-9])\.[0-9]/schema/([^ ]*)$`

var (
// Validation helper as singleton
validate *validator.Validate
validate *validator.Validate
CDEventsSchemaIdRegex = regexp.MustCompile(SCHEMA_ID_REGEX)
)

// parts := CDEventsSchemaIdRegex.FindStringSubmatch(url)
// if len(parts) != 3 {
// return nil, fmt.Errorf("cannot parse schema Id %s", url)
// }
// schemaPath := filepath.Join("spec-v" + parts[1], "schemas", parts[2] + ".json")
// schemaFile, err := os.Open(schemaPath)
// if err != nil {
// return nil, fmt.Errorf("failed to load schema %s from %s: %s", url, schemaPath, err)
// }

func init() {
// Register custom validators
validate = validator.New()
err := validate.RegisterValidation("event-type", ValidateEventType)
panicOnError(err)
err = validate.RegisterValidation("uri-reference", ValidateUriReference)
validate.RegisterStructValidation(ValidateEventType, CDEventType{})
err := validate.RegisterValidation("uri-reference", ValidateUriReference)
panicOnError(err)
err = validate.RegisterValidation("purl", ValidatePurl)
panicOnError(err)
err = validate.RegisterValidation("event-link-type", ValidateLinkType)
panicOnError(err)
}

func panicOnError(err error) {
Expand All @@ -84,9 +74,11 @@ func ParseType(eventType string) (*CDEventType, error) {
return t, nil
}

func ValidateEventType(fl validator.FieldLevel) bool {
_, err := ParseType(fl.Field().String())
return err == nil
func ValidateEventType(sl validator.StructLevel) {
_, err := ParseType(sl.Current().Interface().(CDEventType).String())
if err != nil {
sl.ReportError(sl.Current().Interface(), "Type", "", "", "")
}
}

func ValidateUriReference(fl validator.FieldLevel) bool {
Expand All @@ -99,6 +91,12 @@ func ValidatePurl(fl validator.FieldLevel) bool {
return err == nil
}

func ValidateLinkType(fl validator.FieldLevel) bool {
lt := LinkType(fl.Field().String())
_, ok := LinkTypes[lt]
return ok
}

// AsCloudEvent renders a CDEvent as a CloudEvent
func AsCloudEvent(event CDEventReader) (*cloudevents.Event, error) {
if event == nil {
Expand Down Expand Up @@ -141,10 +139,9 @@ func AsJsonString(event CDEventReader) (string, error) {

// Validate checks the CDEvent against the JSON schema and validate constraints
func Validate(event CDEventReader) error {
url, schema := event.GetSchema()
sch, err := jsonschema.CompileString(url, schema)
_, sch, err := event.GetSchema()
if err != nil {
return fmt.Errorf("cannot compile jsonschema %s, %s", url, err)
return err
}
var v interface{}
jsonString, err := AsJsonString(event)
Expand All @@ -171,31 +168,32 @@ func Validate(event CDEventReader) error {
// This works by unmarshalling the context first, extracting the event type and using
// that to unmarshal the rest of the event into the correct object.
// `ContextType` defines the type of Context that can be used to unmarshal the event.
func NewFromJsonBytesContext[ContextType BaseContextReader](event []byte, cdeventsMap map[string]CDEvent) (CDEvent, error) {
func NewFromJsonBytesContext[CDEventType CDEvent](event []byte, cdeventsMap map[string]CDEventType) (CDEventType, error) {
eventAux := &struct {
Context ContextType `json:"context"`
Context Context `json:"context"`
}{}
var nilReturn CDEventType
err := json.Unmarshal(event, eventAux)
if err != nil {
return nil, err
return nilReturn, err
}
eventType := eventAux.Context.GetType()
receiver, ok := cdeventsMap[eventType.UnversionedString()]
if !ok {
// This should not happen as unmarshalling and validate checks if the type is known to the SDK
return nil, fmt.Errorf("unknown event type %s", eventAux.Context.GetType())
return nilReturn, fmt.Errorf("unknown event type %s", eventAux.Context.GetType())
}
// Check if the receiver is compatible. It must have the same subject and predicate
// and share the same major version.
// If the minor version is different and the message received as a version that is
// greater than the SDK one, some fields may be lost, as newer versions may add new
// fields to the event specification.
if !eventType.IsCompatible(receiver.GetType()) {
return nil, fmt.Errorf("sdk event version %s not compatible with %s", receiver.GetType().Version, eventType.Version)
return nilReturn, fmt.Errorf("sdk event version %s not compatible with %s", receiver.GetType().Version, eventType.Version)
}
err = json.Unmarshal(event, receiver)
if err != nil {
return nil, err
return nilReturn, err
}
return receiver, nil
}
Loading

0 comments on commit 53dc41a

Please sign in to comment.