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 14, 2024
1 parent 6deb676 commit 9cf5c58
Show file tree
Hide file tree
Showing 140 changed files with 5,010 additions and 1,049 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 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
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=
30 changes: 13 additions & 17 deletions pkg/api/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,17 @@ 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()
Expand All @@ -58,6 +47,8 @@ func init() {
panicOnError(err)
err = validate.RegisterValidation("purl", ValidatePurl)
panicOnError(err)
err = validate.RegisterValidation("validate-link-type", ValidateLinkType)
panicOnError(err)
}

func panicOnError(err error) {
Expand Down Expand Up @@ -85,8 +76,9 @@ func ParseType(eventType string) (*CDEventType, error) {
}

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

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

func ValidateLinkType(fl validator.FieldLevel) bool {
_, err := ParseType(fl.Field().String())
return err == nil
}

// AsCloudEvent renders a CDEvent as a CloudEvent
func AsCloudEvent(event CDEventReader) (*cloudevents.Event, error) {
if event == nil {
Expand Down Expand Up @@ -141,10 +138,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 Down
64 changes: 45 additions & 19 deletions pkg/api/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import (
"testing"

"github.com/cdevents/sdk-go/pkg/api"
testapi "github.com/cdevents/sdk-go/pkg/api/v990"
testapi "github.com/cdevents/sdk-go/pkg/api/v991"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
jsonschema "github.com/santhosh-tekuri/jsonschema/v5"
)

const testsFolder = "tests/examples"
const testsFolder = "tests-v99.1/examples"

type testData struct {
TestValues []map[string]string `json:"testValues"`
Expand All @@ -50,6 +49,10 @@ var (
testDataXml = []byte("<xml>testData</xml>")
testChangeId = "myChange123"

// V04+ Examples Data
testLinks api.EmbeddedLinksArray
testContextId = "5328c37f-bb7e-4bb7-84ea-9f5f85e4a7ce"

eventJsonCustomData *testapi.FooSubjectBarPredicateEvent
eventNonJsonCustomData *testapi.FooSubjectBarPredicateEvent
eventJsonCustomDataUnmarshalled *testapi.FooSubjectBarPredicateEvent
Expand All @@ -76,7 +79,7 @@ func init() {
eventJsonCustomData.SetSubjectReferenceField(&api.Reference{Id: testChangeId})
eventJsonCustomData.SetSubjectPlainField(testValue)
eventJsonCustomData.SetSubjectArtifactId(testArtifactId)
eventJsonCustomData.SetSubjectObjectField(&api.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
eventJsonCustomData.SetSubjectObjectField(&testapi.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
err := eventJsonCustomData.SetCustomData("application/json", testDataJson)
panicOnError(err)

Expand All @@ -85,7 +88,7 @@ func init() {
eventJsonCustomDataUnmarshalled.SetSubjectReferenceField(&api.Reference{Id: testChangeId})
eventJsonCustomDataUnmarshalled.SetSubjectPlainField(testValue)
eventJsonCustomDataUnmarshalled.SetSubjectArtifactId(testArtifactId)
eventJsonCustomDataUnmarshalled.SetSubjectObjectField(&api.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
eventJsonCustomDataUnmarshalled.SetSubjectObjectField(&testapi.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
err = eventJsonCustomDataUnmarshalled.SetCustomData("application/json", testDataJsonUnmarshalled)
panicOnError(err)

Expand All @@ -94,9 +97,31 @@ func init() {
eventNonJsonCustomData.SetSubjectReferenceField(&api.Reference{Id: testChangeId})
eventNonJsonCustomData.SetSubjectPlainField(testValue)
eventNonJsonCustomData.SetSubjectArtifactId(testArtifactId)
eventNonJsonCustomData.SetSubjectObjectField(&api.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
eventNonJsonCustomData.SetSubjectObjectField(&testapi.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
err = eventNonJsonCustomData.SetCustomData("application/xml", testDataXml)
panicOnError(err)

// Set up test links
tags := api.Tags{
"foo1": "bar",
"foo2": "bar",
}
reference := api.EventReference{
ContextId: testContextId,
}
elr := api.NewEmbeddedLinkRelation()
elr.SetTags(tags)
elr.SetLinkKind("TRIGGER")
elr.SetTarget(reference)
elp := api.NewEmbeddedLinkPath()
elp.SetTags(tags)
elp.SetFrom(reference)
ele := api.NewEmbeddedLinkEnd()
ele.SetTags(tags)
ele.SetFrom(reference)
testLinks = api.EmbeddedLinksArray{
elr, elp, ele,
}
}

// TestAsCloudEvent produces a CloudEvent from a CDEvent using `AsCloudEvent`
Expand Down Expand Up @@ -206,10 +231,9 @@ func TestAsJsonBytes(t *testing.T) {
t.Fatalf("didn't expected it to fail, but it did: %v", err)
}
// First validate that the test JSON compiles against the schema
schema, url := tc.event.GetSchema()
sch, err := jsonschema.CompileString(schema, url)
url, sch, err := tc.event.GetSchema()
if err != nil {
t.Fatalf("Cannot compile jsonschema %s: %v", url, err)
t.Fatalf("Cannot find jsonschema %s: %v", url, err)
}
var v interface{}
if err := json.Unmarshal(eventBytes, &v); err != nil {
Expand Down Expand Up @@ -274,11 +298,13 @@ func TestInvalidEvent(t *testing.T) {

// invalid event type
eventInvalidType := &testapi.FooSubjectBarPredicateEvent{
Context: api.Context{
Type: api.CDEventType{Subject: "not-a-valid-type"},
Version: api.CDEventsSpecVersion,
Context: api.ContextV04{
Context: api.Context{
Type: api.CDEventType{Subject: "not-a-valid-type"},
Version: api.CDEventsSpecVersion,
},
},
Subject: api.FooSubjectBarPredicateSubject{
Subject: testapi.FooSubjectBarPredicateSubject{
SubjectBase: api.SubjectBase{
Type: "notAValidSubjectType",
},
Expand Down Expand Up @@ -477,7 +503,7 @@ func testEventWithVersion(eventVersion string, specVersion string) *testapi.FooS
event.SetSubjectReferenceField(&api.Reference{Id: testChangeId})
event.SetSubjectPlainField(testValue)
event.SetSubjectArtifactId(testArtifactId)
event.SetSubjectObjectField(&api.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
event.SetSubjectObjectField(&testapi.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource})
err := event.SetCustomData("application/json", testDataJsonUnmarshalled)
panicOnError(err)
event.Context.Type.Version = eventVersion
Expand All @@ -487,10 +513,10 @@ func testEventWithVersion(eventVersion string, specVersion string) *testapi.FooS

func TestNewFromJsonBytes(t *testing.T) {

minorVersion := testEventWithVersion("1.999.0", testapi.SpecVersion)
patchVersion := testEventWithVersion("1.2.999", testapi.SpecVersion)
pastPatchVersion := testEventWithVersion("1.2.0", testapi.SpecVersion)
pastSpecVersion := testEventWithVersion("1.2.3", "0.1.0")
minorVersion := testEventWithVersion("2.999.1", testapi.SpecVersion)
patchVersion := testEventWithVersion("2.2.999", testapi.SpecVersion)
pastPatchVersion := testEventWithVersion("2.2.0", testapi.SpecVersion)
pastSpecVersion := testEventWithVersion("2.2.3", "0.1.0")

tests := []struct {
testFile string
Expand All @@ -500,7 +526,7 @@ func TestNewFromJsonBytes(t *testing.T) {
}{{
testFile: "future_event_major_version",
description: "A newer major version in the event is backward incompatible and cannot be parsed",
wantError: "sdk event version 1.2.3 not compatible with 999.0.0",
wantError: "sdk event version 2.2.3 not compatible with 999.1.0",
}, {
testFile: "future_event_minor_version",
description: "A newer minor version in the event is compatible and can be parsed, data is lost",
Expand Down
Loading

0 comments on commit 9cf5c58

Please sign in to comment.