diff --git a/codecov.yaml b/codecov.yaml new file mode 100644 index 0000000..0ab367d --- /dev/null +++ b/codecov.yaml @@ -0,0 +1,2 @@ +ignore: + - docs/examples/** diff --git a/docs/README.md b/docs/README.md index dd09ece..2052ab1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,6 +2,8 @@ This folder contains example of how to use this SDK. +> **Note** For simplicity, the code below does not include error handling. The go files in example folders includes it. + ## Create a Custom CDEvent If a tool wants to emit events that are not supported by the CDEvents specification, @@ -17,21 +19,24 @@ happens, but CDEvents does not define any quota related subject. ```golang type Quota struct { - User string `json:"user,omitempty"` // The use the quota applies ot - Limit string `json:"limit,omitempty"` // The limit enforced by the quota e.g. 100Gb - Current int `json:"current,omitempty"` // The current % of the quota used e.g. 90% - Threshold int `json:"threshold,omitempty"` // The threshold for warning event e.g. 85% - Level string `json:"level,omitempty"` // INFO: threshold, quota + User string `json:"user,omitempty"` // The use the quota applies ot + Limit string `json:"limit,omitempty"` // The limit enforced by the quota e.g. 100Gb + Current int `json:"current,omitempty"` // The current % of the quota used e.g. 90% + Threshold int `json:"threshold,omitempty"` // The threshold for warning event e.g. 85% + Level string `json:"level,omitempty"` // INFO: threshold, quota } ``` + For this scenario we will need a few imports: ```golang import ( "context" - "fmt" + "fmt" "log" + "os" + examples "github.com/cdevents/sdk-go/docs/examples" cdevents "github.com/cdevents/sdk-go/pkg/api" cdeventsv04 "github.com/cdevents/sdk-go/pkg/api/v04" cloudevents "github.com/cloudevents/sdk-go/v2" @@ -63,9 +68,7 @@ quotaRule123 := Quota{ // Create the base event event, err := cdeventsv04.NewCustomTypeEvent() -if err != nil { - log.Fatalf("could not create a cdevent, %v", err) -} +examples.PanicOnError(err, "could not create a cdevent") event.SetEventType(eventType) // Set the required context fields @@ -79,6 +82,15 @@ event.SetSubjectContent(quotaRule123) // to the event so that the receiver may validate custom fields like // the event type and subject content event.SetSchemaUri("https://myregistry.dev/schemas/cdevents/quota-exceeded/0_1_0") + +// The event schema needs to be loaded, so the SDK may validate +// In this example, the schema is located in the same folder as +// the go code +customSchema, err := os.ReadFile("myregistry-quotaexceeded_schema.json") +examples.PanicOnError(err, "cannot load schema file") + +err = cdevents.LoadJsonSchema(customSchemaUri, customSchema) +examples.PanicOnError(err, "cannot load the custom schema file") ``` To see the event, let's render it as JSON and log it: @@ -86,10 +98,7 @@ To see the event, let's render it as JSON and log it: ```golang // Render the event as JSON eventJson, err := cdevents.AsJsonString(event) -if err != nil { - log.Fatalf("failed to marshal the CDEvent, %v", err) -} -// Print the event +examples.PanicOnError(err, "failed to marshal the CDEvent") fmt.Printf("%s", eventJson) ``` @@ -123,10 +132,11 @@ if result := c.Send(ctx, *ce); cloudevents.IsUndelivered(result) { } ``` -The whole code of is available under [`examples/custom.go`](./examples/custom.go): +The whole code of is available under [`examples/custom`](./examples/custom/main.go): ```shell -➜ go run custom.go | jq . +➜ cd examples/custom +➜ go run main.go | jq . { "context": { "version": "0.4.1", @@ -149,4 +159,75 @@ The whole code of is available under [`examples/custom.go`](./examples/custom.go } } } -``` \ No newline at end of file +``` + +## Consume a CDEvent with a Custom Schema + +CDEvents producers may include a `schemaUri` in their events. The extra schema **must** comply with the CDEvents schema and may add additional rules on top. +The `schemaUri` field includes the `$id` field of the custom schema and can be used for different purposes: +* specify the format of the data included in the `customData` field +* specify the format of the subject content of custom events +* refine the format of one or more fields of a specific CDEvent + +In this examples, the custom schema is used to define the format of the `customData` for a `change.created` events, which corresponds to the following golang `struct`: + +```golang +type ChangeData struct { + User string `json:"user"` // The user that created the PR + Assignee string `json:"assignee,omitempty"` // The user assigned to the PR (optional) + Head string `json:"head"` // The head commit (sha) of the PR + Base string `json:"base"` // The base commit (sha) for the PR +} +``` + +The goal of this example is to consume (parse) an event with a custom schema and validate it. In the example we load the event from disk. In real life the event will be typically received over the network or extracted from a database. + +For this scenario we will need a few imports: + +```golang +import ( + "context" + "encoding/json" + "fmt" + "log" + "os" + + examples "github.com/cdevents/sdk-go/docs/examples" + cdevents "github.com/cdevents/sdk-go/pkg/api" + cdevents04 "github.com/cdevents/sdk-go/pkg/api/v04" + cloudevents "github.com/cloudevents/sdk-go/v2" +) +``` + +Before parsing an event with a custom schema, it's required to load the schema into the SDK. This avoids having to download and compile the schema every time a message is parsed. + +```golang +// Load and register the custom schema +customSchema, err := os.ReadFile("changecreated_schema.json") + +// Unmarshal the schema to extract the $id. The $id can also be hardcoded as a const +eventAux := &struct { + Id string `json:"$id"` +}{} +err = json.Unmarshal(customSchema, eventAux) +err = cdevents.LoadJsonSchema(eventAux.Id, customSchema) +``` + +Once the schema is loaded, it's possible to parse the event itself. +In this case we know that the event is in the v0.4 version format, so we use the corresponding API. + +```golang +// Load, unmarshal and validate the event +eventBytes, err := os.ReadFile("changecreated.json") +event, err := cdevents04.NewFromJsonBytes(eventBytes) + +err = cdevent.Validate(event) +if err != nil { + log.Fatalf("cannot validate event %v: %v", event, err) +} + +// Print the event +eventJson, err := cdevents.AsJsonString(event) +examples.PanicOnError(err, "failed to marshal the CDEvent") +fmt.Printf("%s\n\n", eventJson) +``` diff --git a/docs/examples/custom.go b/docs/examples/custom.go deleted file mode 100644 index bf13bc2..0000000 --- a/docs/examples/custom.go +++ /dev/null @@ -1,105 +0,0 @@ -package main - -import ( - "context" - "fmt" - "log" - "net/http" - - cdevents "github.com/cdevents/sdk-go/pkg/api" - cdeventsv04 "github.com/cdevents/sdk-go/pkg/api/v04" - cloudevents "github.com/cloudevents/sdk-go/v2" -) - -type Quota struct { - User string `json:"user,omitempty"` // The use the quota applies ot - Limit string `json:"limit,omitempty"` // The limit enforced by the quota e.g. 100Gb - Current int `json:"current,omitempty"` // The current % of the quota used e.g. 90% - Threshold int `json:"threshold,omitempty"` // The threshold for warning event e.g. 85% - Level string `json:"level,omitempty"` // INFO: threshold, quota -} - -// Copied from https://github.com/eswdd/go-smee/blob/33b0bac1f1ef3abef04c518ddf7552b04edbadd2/smee.go#L54C1-L67C2 -func CreateSmeeChannel() (*string, error) { - httpClient := http.Client{ - CheckRedirect: func(req *http.Request, via []*http.Request) error { - return http.ErrUseLastResponse - }, - } - resp, err := httpClient.Head("https://smee.io/new") - if err != nil { - return nil, err - } - - loc := resp.Header.Get("Location") - return &loc, nil -} - -func main() { - var ce *cloudevents.Event - var c cloudevents.Client - - // Define the event type - eventType := cdevents.CDEventType{ - Subject: "quota", - Predicate: "exceeded", - Version: "0.1.0", - Custom: "myregistry", - } - - // Define the content - quotaRule123 := Quota{ - User: "heavy_user", - Limit: "50Tb", - Current: 90, - Threshold: 85, - Level: "WARNING", - } - - // Create the base event - event, err := cdeventsv04.NewCustomTypeEvent() - if err != nil { - log.Fatalf("could not create a cdevent, %v", err) - } - event.SetEventType(eventType) - - // Set the required context fields - event.SetSubjectId("quotaRule123") - event.SetSource("my/first/cdevent/program") - - // Set the required subject fields - event.SetSubjectContent(quotaRule123) - - event.SetSchemaUri("https://myregistry.dev/schemas/cdevents/quota-exceeded/0_1_0") - - // Print the event - eventJson, err := cdevents.AsJsonString(event) - if err != nil { - log.Fatalf("failed to marshal the CDEvent, %v", err) - } - fmt.Printf("%s", eventJson) - - ce, err = cdevents.AsCloudEvent(event) - if err != nil { - log.Fatalf("failed to create cloudevent, %v", err) - } - - // Set send options - source, err := CreateSmeeChannel() - if err != nil { - log.Fatalf("failed to create a smee channel: %v", err) - } - ctx := cloudevents.ContextWithTarget(context.Background(), *source) - ctx = cloudevents.WithEncodingBinary(ctx) - - c, err = cloudevents.NewClientHTTP() - if err != nil { - log.Fatalf("failed to create client, %v", err) - } - - // Send the CloudEvent - // c is a CloudEvent client - if result := c.Send(ctx, *ce); cloudevents.IsUndelivered(result) { - log.Fatalf("failed to send, %v", result) - } -} diff --git a/docs/examples/custom/main.go b/docs/examples/custom/main.go new file mode 100644 index 0000000..94ed0c6 --- /dev/null +++ b/docs/examples/custom/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + + examples "github.com/cdevents/sdk-go/docs/examples" + cdevents "github.com/cdevents/sdk-go/pkg/api" + cdeventsv04 "github.com/cdevents/sdk-go/pkg/api/v04" + cloudevents "github.com/cloudevents/sdk-go/v2" +) + +const customSchemaUri = "https://myregistry.dev/schemas/cdevents/quota-exceeded/0_1_0" + +type Quota struct { + User string `json:"user"` // The use the quota applies ot + Limit string `json:"limit"` // The limit enforced by the quota e.g. 100Gb + Current int `json:"current"` // The current % of the quota used e.g. 90% + Threshold int `json:"threshold"` // The threshold for warning event e.g. 85% + Level string `json:"level"` // INFO: threshold, quota +} + +func main() { + var ce *cloudevents.Event + var c cloudevents.Client + + // Define the event type + eventType := cdevents.CDEventType{ + Subject: "quota", + Predicate: "exceeded", + Version: "0.1.0", + Custom: "myregistry", + } + + // Define the content + quotaRule123 := Quota{ + User: "heavy_user", + Limit: "50Tb", + Current: 90, + Threshold: 85, + Level: "WARNING", + } + + // Create the base event + event, err := cdeventsv04.NewCustomTypeEvent() + examples.PanicOnError(err, "could not create a cdevent") + event.SetEventType(eventType) + + // Set the required context fields + event.SetSubjectId("quotaRule123") + event.SetSource("my/first/cdevent/program") + + // Set the required subject fields + event.SetSubjectContent(quotaRule123) + event.SetSchemaUri(customSchemaUri) + + // Print the event + eventJson, err := cdevents.AsJsonString(event) + examples.PanicOnError(err, "failed to marshal the CDEvent") + fmt.Printf("%s", eventJson) + + // To validate the event, we need to load its custom schema + customSchema, err := os.ReadFile("myregistry-quotaexceeded_schema.json") + examples.PanicOnError(err, "cannot load schema file") + + err = cdevents.LoadJsonSchema(customSchemaUri, customSchema) + examples.PanicOnError(err, "cannot load the custom schema file") + + ce, err = cdevents.AsCloudEvent(event) + examples.PanicOnError(err, "failed to create cloudevent") + + // Set send options + source, err := examples.CreateSmeeChannel() + examples.PanicOnError(err, "failed to create a smee channel") + ctx := cloudevents.ContextWithTarget(context.Background(), *source) + ctx = cloudevents.WithEncodingBinary(ctx) + + c, err = cloudevents.NewClientHTTP() + examples.PanicOnError(err, "failed to create the CloudEvents client") + + // Send the CloudEvent + // c is a CloudEvent client + if result := c.Send(ctx, *ce); cloudevents.IsUndelivered(result) { + log.Fatalf("failed to send, %v", result) + } +} diff --git a/docs/examples/custom/myregistry-quotaexceeded_schema.json b/docs/examples/custom/myregistry-quotaexceeded_schema.json new file mode 100644 index 0000000..e94b37b --- /dev/null +++ b/docs/examples/custom/myregistry-quotaexceeded_schema.json @@ -0,0 +1,138 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://myregistry.dev/schemas/cdevents/quota-exceeded/0_1_0", + "properties": { + "context": { + "properties": { + "version": { + "type": "string", + "minLength": 1 + }, + "id": { + "type": "string", + "minLength": 1 + }, + "source": { + "type": "string", + "minLength": 1, + "format": "uri-reference" + }, + "type": { + "type": "string", + "enum": [ + "dev.cdeventsx.myregistry-quota.exceeded.0.1.0" + ], + "default": "dev.cdeventsx.myregistry-quota.exceeded.0.1.0" + }, + "timestamp": { + "type": "string", + "format": "date-time" + }, + "schemaUri": { + "type": "string", + "minLength": 1, + "format": "uri", + "enum": [ + "https://myregistry.dev/schemas/cdevents/quota-exceeded/0_1_0" + ], + "default": "https://myregistry.dev/schemas/cdevents/quota-exceeded/0_1_0" + }, + "chainId": { + "type": "string", + "minLength": 1 + }, + "links": { + "$ref": "https://cdevents.dev/0.4.1/schema/links/embeddedlinksarray" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "version", + "id", + "source", + "type", + "timestamp" + ] + }, + "subject": { + "properties": { + "id": { + "type": "string", + "minLength": 1 + }, + "source": { + "type": "string", + "minLength": 1, + "format": "uri-reference" + }, + "type": { + "type": "string", + "minLength": 1, + "enum": [ + "myregistry-quota" + ], + "default": "myregistry-quota" + }, + "content": { + "properties": { + "user": { + "type": "string", + "minLength": 1 + }, + "limit": { + "type": "string", + "minLength": 1 + }, + "current": { + "type": "integer" + }, + "threshold": { + "type": "integer" + }, + "level": { + "type": "string", + "minLength": 1 + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "user", + "limit", + "current", + "threshold", + "level" + ] + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "id", + "type", + "content" + ] + }, + "customData": { + "oneOf": [ + { + "type": "object" + }, + { + "type": "string", + "contentEncoding": "base64" + } + ] + }, + "customDataContentType": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "context", + "subject" + ] +} diff --git a/docs/examples/schemauri/changecreated.json b/docs/examples/schemauri/changecreated.json new file mode 100644 index 0000000..3e3e9e2 --- /dev/null +++ b/docs/examples/schemauri/changecreated.json @@ -0,0 +1,36 @@ +{ + "context": { + "version": "0.4.1", + "id": "271069a8-fc18-44f1-b38f-9d70a1695819", + "chainId": "4c8cb7dd-3448-41de-8768-eec704e2829b", + "source": "/event/source/123", + "type": "dev.cdevents.change.created.0.3.0", + "timestamp": "2023-03-20T14:27:05.315384Z", + "schemaUri": "https://sdk-examples/0.4.1/schema/change-created-event", + "links": [] + }, + "subject": { + "id": "pullRequest123", + "source": "/event/source/123", + "type": "change", + "content": { + "description": "This PR address a bug from a recent PR", + "repository": { + "id": "TestRepo/TestOrg", + "source": "https://example.org" + } + }, + "customData": { + "user": "userA", + "assignee": "userB", + "head": "8f88722d-c667-4afa-a39a-79085224fd7b", + "base": "4db1393d-6114-4885-8dc5-75234a9375a0", + "comments": [ + "Nice PR", + "Funny title", + "LGTM" + ] + }, + "customDataContentType": "application/json" + } +} diff --git a/docs/examples/schemauri/changecreated_schema.json b/docs/examples/schemauri/changecreated_schema.json new file mode 100644 index 0000000..09fa17b --- /dev/null +++ b/docs/examples/schemauri/changecreated_schema.json @@ -0,0 +1,151 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://sdk-examples/0.4.1/schema/change-created-event", + "properties": { + "context": { + "properties": { + "version": { + "type": "string", + "minLength": 1 + }, + "id": { + "type": "string", + "minLength": 1 + }, + "source": { + "type": "string", + "minLength": 1, + "format": "uri-reference" + }, + "type": { + "type": "string", + "enum": [ + "dev.cdevents.change.created.0.3.0" + ], + "default": "dev.cdevents.change.created.0.3.0" + }, + "timestamp": { + "type": "string", + "format": "date-time" + }, + "schemaUri": { + "type": "string", + "minLength": 1, + "format": "uri" + }, + "chainId": { + "type": "string", + "minLength": 1 + }, + "links": { + "$ref": "https://cdevents.dev/0.4.1/schema/links/embeddedlinksarray" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "version", + "id", + "source", + "type", + "timestamp" + ] + }, + "subject": { + "properties": { + "id": { + "type": "string", + "minLength": 1 + }, + "source": { + "type": "string", + "minLength": 1, + "format": "uri-reference" + }, + "type": { + "type": "string", + "minLength": 1, + "enum": [ + "change" + ], + "default": "change" + }, + "content": { + "properties": { + "description": { + "type": "string", + "minLength": 1 + }, + "repository": { + "properties": { + "id": { + "type": "string", + "minLength": 1 + }, + "source": { + "type": "string", + "minLength": 1, + "format": "uri-reference" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "id" + ] + } + }, + "additionalProperties": false, + "type": "object" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "id", + "type", + "content" + ] + }, + "customData": { + "properties": { + "user": { + "type": "string", + "minLength": 1 + }, + "assignee": { + "type": "string", + "minLength": 1 + }, + "base": { + "type": "string", + "minLength": 1 + }, + "head": { + "type": "string", + "minLength": 1 + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "user", + "base", + "head" + ] + }, + "customDataContentType": { + "type": "string", + "enum": [ + "application/json" + ], + "default": "application/json" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "context", + "subject" + ] +} diff --git a/docs/examples/schemauri/main.go b/docs/examples/schemauri/main.go new file mode 100644 index 0000000..1476b54 --- /dev/null +++ b/docs/examples/schemauri/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "log" + "os" + + examples "github.com/cdevents/sdk-go/docs/examples" + cdevents "github.com/cdevents/sdk-go/pkg/api" + cdevents04 "github.com/cdevents/sdk-go/pkg/api/v04" + cloudevents "github.com/cloudevents/sdk-go/v2" +) + +type ChangeData struct { + User string `json:"user"` // The user that created the PR + Assignee string `json:"assignee,omitempty"` // The user assigned to the PR (optional) + Head string `json:"head"` // The head commit (sha) of the PR + Base string `json:"base"` // The base commit (sha) for the PR +} + +func main() { + var ce *cloudevents.Event + var c cloudevents.Client + + // Load and register the custom schema + customSchema, err := os.ReadFile("changecreated_schema.json") + examples.PanicOnError(err, "cannot load schema file") + + // Unmarshal the schema to extract the $id. The $id can also be hardcoded as a const + eventAux := &struct { + Id string `json:"$id"` + }{} + err = json.Unmarshal(customSchema, eventAux) + examples.PanicOnError(err, "cannot get $id from schema file") + err = cdevents.LoadJsonSchema(eventAux.Id, customSchema) + examples.PanicOnError(err, "cannot load the custom schema file") + + // Load and unmarshal the event + eventBytes, err := os.ReadFile("changecreated.json") + examples.PanicOnError(err, "cannot load event file") + event, err := cdevents04.NewFromJsonBytes(eventBytes) + examples.PanicOnError(err, "failed to unmarshal the CDEvent") + + // Print the event + eventJson, err := cdevents.AsJsonString(event) + examples.PanicOnError(err, "failed to marshal the CDEvent") + fmt.Printf("%s\n\n", eventJson) + + ce, err = cdevents.AsCloudEvent(event) + examples.PanicOnError(err, "failed to create cloudevent") + + // Set send options + source, err := examples.CreateSmeeChannel() + examples.PanicOnError(err, "failed to create a smee channel") + + ctx := cloudevents.ContextWithTarget(context.Background(), *source) + ctx = cloudevents.WithEncodingBinary(ctx) + + c, err = cloudevents.NewClientHTTP() + examples.PanicOnError(err, "failed to create a CloudEvents client") + + // Send the CloudEvent + // c is a CloudEvent client + if result := c.Send(ctx, *ce); cloudevents.IsUndelivered(result) { + log.Fatalf("failed to send, %v", result) + } +} diff --git a/docs/examples/shared.go b/docs/examples/shared.go new file mode 100644 index 0000000..0a2b4ea --- /dev/null +++ b/docs/examples/shared.go @@ -0,0 +1,29 @@ +package examples + +import ( + "log" + "net/http" +) + +// Copied from https://github.com/eswdd/go-smee/blob/33b0bac1f1ef3abef04c518ddf7552b04edbadd2/smee.go#L54C1-L67C2 +func CreateSmeeChannel() (*string, error) { + httpClient := http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + resp, err := httpClient.Head("https://smee.io/new") + if err != nil { + return nil, err + } + + loc := resp.Header.Get("Location") + return &loc, nil +} + +func PanicOnError(e error, message string) { + if e != nil { + log.Fatalf(message+", %v", e) + panic(e) + } +} diff --git a/pkg/api/bindings.go b/pkg/api/bindings.go index 7b10941..95738b2 100644 --- a/pkg/api/bindings.go +++ b/pkg/api/bindings.go @@ -154,16 +154,30 @@ func Validate(event CDEventReader) error { if err := json.Unmarshal([]byte(jsonString), &v); err != nil { return fmt.Errorf("cannot unmarshal event json: %v", err) } - // Validate the "jsonschema" tags - err = sch.Validate(v) - if err != nil { + // Validate the "validate" tags + if err := validate.Struct(event); err != nil { return err } - // Validate the "validate" tags - err = validate.Struct(event) - if err != nil { + // Validate the "jsonschema" tags + if err := sch.Validate(v); err != nil { return err } + // Check if there is a custom schema + v4event, ok := event.(CDEventReaderV04) + if ok { + schema, err := v4event.GetCustomSchema() + if err != nil { + return err + } + // If there is no schema defined, we're done + if schema == nil { + return nil + } + err = schema.Validate(v) + if err != nil { + return err + } + } return nil } diff --git a/pkg/api/bindings_test.go b/pkg/api/bindings_test.go index 2cde8d2..81b1176 100644 --- a/pkg/api/bindings_test.go +++ b/pkg/api/bindings_test.go @@ -20,7 +20,9 @@ package api_test import ( "encoding/json" + "fmt" "os" + "strings" "testing" "github.com/cdevents/sdk-go/pkg/api" @@ -42,6 +44,7 @@ var ( testSubjectId = "mySubject123" testValue = "testValue" testArtifactId = "pkg:oci/myapp@sha256%3A0b31b1c02ff458ad9b7b81cbdf8f028bd54699fa151f221d1e8de6817db93427" + testInvalidArtifactId = "not-in-purl-format" testDataJson = testData{TestValues: []map[string]string{{"k1": "v1"}, {"k2": "v2"}}} testDataJsonUnmarshalled = map[string]any{ "testValues": []any{map[string]any{"k1": string("v1")}, map[string]any{"k2": string("v2")}}, @@ -50,14 +53,49 @@ var ( testChangeId = "myChange123" // V04+ Examples Data - testLinks api.EmbeddedLinksArray - testContextId = "5328c37f-bb7e-4bb7-84ea-9f5f85e4a7ce" - testChainId = "4c8cb7dd-3448-41de-8768-eec704e2829b" - testSchemaUri = "https://myorg.com/schema/custom" + testLinks api.EmbeddedLinksArray + testContextId = "5328c37f-bb7e-4bb7-84ea-9f5f85e4a7ce" + testChainId = "4c8cb7dd-3448-41de-8768-eec704e2829b" + testSchemaUri = "https://myorg.com/schema/custom" + testCustomSchemaJsonTemplate = `{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "%s", + "additionalProperties": true, + "type": "object" + }` + testCustomSchemaJson = fmt.Sprintf(testCustomSchemaJsonTemplate, testSchemaUri) + testSchemaUriStricter = "https://myorg.com/schema/stricter" + testCustomSchemaJsonStricterTemplate = `{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "%s", + "additionalProperties": true, + "type": "object", + "properties": { + "customData": { + "type": "object", + "additionalProperties": false, + "properties": { + "important": { + "type": "string" + } + }, + "required": [ + "important" + ] + } + } + }` + testCustomSchemaJsonStricterJson = fmt.Sprintf(testCustomSchemaJsonStricterTemplate, testSchemaUriStricter) + testCustomSchemas = map[string][]byte{ + testSchemaUri: []byte(testCustomSchemaJson), + testSchemaUriStricter: []byte(testCustomSchemaJsonStricterJson), + } eventJsonCustomData *testapi.FooSubjectBarPredicateEvent eventNonJsonCustomData *testapi.FooSubjectBarPredicateEvent eventJsonCustomDataUnmarshalled *testapi.FooSubjectBarPredicateEvent + eventJsonCustomDataCustomSchema *testapi.FooSubjectBarPredicateEvent + eventInvalidArtifactIdFormat *testapi.FooSubjectBarPredicateEvent eventJsonCustomDataFile = "json_custom_data" eventImplicitJsonCustomDataFile = "implicit_json_custom_data" @@ -69,6 +107,21 @@ var ( Type: api.CDEventType{ Subject: "invalid", Predicate: "invalid", + Version: "#not@semver", // Invalid version format + }, + Version: "9.9.9", + }, + api.ContextLinks{}, + api.ContextCustom{}, + }, + } + + eventUnknownType = &testapi.FooSubjectBarPredicateEvent{ + Context: api.ContextV04{ + api.Context{ + Type: api.CDEventType{ + Subject: "invalid", // Unknown subject + Predicate: "invalid", // Unknown predicate Version: "1.2.3", }, Version: "9.9.9", @@ -123,6 +176,19 @@ func init() { elr, elp, ele, } + setContext(eventInvalidType, testSubjectId) + setContextV04(eventInvalidType, true, true) + eventInvalidType.SetSubjectArtifactId(testArtifactId) + + setContext(eventUnknownType, testSubjectId) + setContextV04(eventUnknownType, true, true) + eventUnknownType.SetSubjectArtifactId(testArtifactId) + + eventInvalidArtifactIdFormat, _ = testapi.NewFooSubjectBarPredicateEvent() + setContext(eventInvalidArtifactIdFormat, testSubjectId) + setContextV04(eventInvalidArtifactIdFormat, true, true) + eventInvalidArtifactIdFormat.SetSubjectArtifactId(testInvalidArtifactId) + eventJsonCustomData, _ = testapi.NewFooSubjectBarPredicateEvent() setContext(eventJsonCustomData, testSubjectId) setContextV04(eventJsonCustomData, true, true) @@ -152,6 +218,22 @@ func init() { eventNonJsonCustomData.SetSubjectObjectField(&testapi.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource}) err = eventNonJsonCustomData.SetCustomData("application/xml", testDataXml) panicOnError(err) + + eventJsonCustomDataCustomSchema, _ = testapi.NewFooSubjectBarPredicateEvent() + setContext(eventJsonCustomDataCustomSchema, testSubjectId) + setContextV04(eventJsonCustomDataCustomSchema, true, true) + eventJsonCustomDataCustomSchema.SetSchemaUri(testSchemaUriStricter) + eventJsonCustomDataCustomSchema.SetSubjectReferenceField(&api.Reference{Id: testChangeId}) + eventJsonCustomDataCustomSchema.SetSubjectPlainField(testValue) + eventJsonCustomDataCustomSchema.SetSubjectArtifactId(testArtifactId) + eventJsonCustomDataCustomSchema.SetSubjectObjectField(&testapi.FooSubjectBarPredicateSubjectContentObjectField{Required: testChangeId, Optional: testSource}) + err = eventJsonCustomDataCustomSchema.SetCustomData("application/json", testDataJson) + panicOnError(err) + + for id, jsonBytes := range testCustomSchemas { + err = api.LoadJsonSchema(id, jsonBytes) + panicOnError(err) + } } // TestAsCloudEvent produces a CloudEvent from a CDEvent using `AsCloudEvent` @@ -231,12 +313,27 @@ func TestAsCloudEventInvalid(t *testing.T) { tests := []struct { name string event api.CDEventReader + error string }{{ name: "nil event", event: nil, + error: "nil CDEvent cannot be rendered as CloudEvent", }, { name: "event with invalid type", event: eventInvalidType, + error: "cannot validate CDEvent Key: 'FooSubjectBarPredicateEventV2_2_3.Context.Context.Type.", + }, { + name: "event with unknown type", + event: eventUnknownType, + error: "cannot validate CDEvent jsonschema validation failed with 'https://cdevents.dev/99.1.0/schema/foosubject-barpredicate-event#'", + }, { + name: "event with invalid artifact id format", + event: eventInvalidArtifactIdFormat, + error: "cannot validate CDEvent Key: 'FooSubjectBarPredicateEventV2_2_3.Subject.Content.ArtifactId'", + }, { + name: "does not match the custom schema", + event: eventJsonCustomDataCustomSchema, + error: "cannot validate CDEvent jsonschema validation failed with 'https://myorg.com/schema/stricter#", }} for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { @@ -244,6 +341,9 @@ func TestAsCloudEventInvalid(t *testing.T) { if err == nil { t.Fatalf("expected it to fail, but it didn't") } + if !strings.HasPrefix(err.Error(), tc.error) { + t.Errorf("error %s does not start with the expected prefix %s", err.Error(), tc.error) + } }) } } diff --git a/pkg/api/schemas.go b/pkg/api/schemas.go index 81e2097..1c35ce4 100644 --- a/pkg/api/schemas.go +++ b/pkg/api/schemas.go @@ -30,8 +30,13 @@ import ( type SchemaDB map[string]*jsonschema.Schema var ( + // Schema compiler, with schemas preloaded + compiler *jsonschema.Compiler + // All compiled schemas by Id CompiledSchemas SchemaDB + // All compiled custom schemas by Id + CompiledCustomSchemas SchemaDB // All schemas as string by Id SchemasById = map[string]string{ @@ -11660,14 +11665,16 @@ var ( ) func init() { - compiler, err := newJsonSchemaCompiler() + var err error + compiler, err = newJsonSchemaCompiler() panicOnError(err) CompiledSchemas = make(map[string]*jsonschema.Schema) - for url, _ := range SchemasById { + for url := range SchemasById { sch, err := compiler.Compile(url) panicOnError(err) CompiledSchemas[url] = sch } + CompiledCustomSchemas = make(map[string]*jsonschema.Schema) } func (db SchemaDB) GetBySpecSubjectPredicate(specVersion, subject, predicate, custom string) (string, *jsonschema.Schema, error) { id := "" @@ -11696,3 +11703,23 @@ func newJsonSchemaCompiler() (*jsonschema.Compiler, error) { } return c, nil } + +// LoadJsonSchema compiles and loads a JSON schema in []byte format into the sdk +// custom JSON schema databased. Returns an error if the schema cannot be compiled. +// If the schemaId already exists, the previous schema definition is overwritten. +func LoadJsonSchema(schemaId string, schema []byte) error { + var loaded map[string]interface{} + err := json.Unmarshal(schema, &loaded) + if err != nil { + return err + } + if err := compiler.AddResource(schemaId, loaded); err != nil { + return err + } + sch, err := compiler.Compile(schemaId) + if err != nil { + return err + } + CompiledCustomSchemas[schemaId] = sch + return nil +} diff --git a/pkg/api/types.go b/pkg/api/types.go index 1e5fa7c..87327db 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -582,6 +582,9 @@ type CDEventReaderV04 interface { // The custom schema URI GetSchemaUri() string + + // The compiled schemaUri for the event + GetCustomSchema() (*jsonschema.Schema, error) } type CDEventWriterV04 interface { diff --git a/pkg/api/v04/conformance_test.go b/pkg/api/v04/conformance_test.go index cac3f17..f26452f 100644 --- a/pkg/api/v04/conformance_test.go +++ b/pkg/api/v04/conformance_test.go @@ -20,8 +20,10 @@ package v04_test import ( "encoding/json" + "fmt" "os" "path/filepath" + "strings" "testing" "time" @@ -125,8 +127,21 @@ var ( "list": ["data1", "data2"] } }`) - testCustomContent interface{} - + testCustomContent interface{} + testCustomSchemaId = "https://myorg.com/schema/custom" + testCustomSchemaId2 = "https://myorg.com/schema/mytool" + testCustomSchemaJsonTemplate = `{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "%s", + "additionalProperties": true, + "type": "object" +}` + testCustomSchemaJson = fmt.Sprintf(testCustomSchemaJsonTemplate, testCustomSchemaId) + testCustomSchema2Json = fmt.Sprintf(testCustomSchemaJsonTemplate, testCustomSchemaId2) + testCustomSchemas = map[string][]byte{ + testCustomSchemaId: []byte(testCustomSchemaJson), + testCustomSchemaId2: []byte(testCustomSchema2Json), + } examplesConsumed map[string][]byte examplesProduced map[string]api.CDEventV04 err error @@ -147,6 +162,25 @@ func init() { err = json.Unmarshal(testCustomContentBytes, &testCustomContent) panicOnError(err) + + for id, jsonBytes := range testCustomSchemas { + err := api.LoadJsonSchema(id, jsonBytes) + panicOnError(err) + } + + // Load event examples from the spec + examplesConsumed = make(map[string][]byte) + + for _, event := range apiv04.CDEventsTypes { + short := event.GetType().Short() + if short != "" { + examplesConsumed[short], err = os.ReadFile(filepath.Join("..", examplesFolder, short+".json")) + panicOnError(err) + } else { + // There is no type set for custom events, and the example is in a different folder + examplesConsumed[short], err = os.ReadFile(filepath.Join("..", customExample)) + } + } } func exampleArtifactPackagedEvent(e *apiv04.ArtifactPackagedEvent) { @@ -516,28 +550,11 @@ func exampleCustomTypeEvent(e *apiv04.CustomTypeEvent) { // Set the type to dev.cdeventsx.mytool-resource.created.0.1.0 e.SetEventType(testCustomEventType) e.SetSubjectContent(testCustomContent) - e.SetSchemaUri("https://myorg.com/schema/mytool") + e.SetSchemaUri(testCustomSchemaId2) e.SetSubjectId("pkg:resource/name@234fd47e07d1004f0aed9c") e.SetChainId("6ca3f9c5-1cef-4ce0-861c-2456a69cf137") } -func init() { - - // Load event examples from the spec - examplesConsumed = make(map[string][]byte) - - for _, event := range apiv04.CDEventsTypes { - short := event.GetType().Short() - if short != "" { - examplesConsumed[short], err = os.ReadFile(filepath.Join("..", examplesFolder, short+".json")) - panicOnError(err) - } else { - // There is no type set for custom events, and the example is in a different folder - examplesConsumed[short], err = os.ReadFile(filepath.Join("..", customExample)) - } - } -} - // TestExamples verifies that the SDK can produce events like those // included in the specification examples folder. // To do so: @@ -557,6 +574,14 @@ func TestExamples(t *testing.T) { if err != nil { t.Errorf("produced event failed to validate: %v", err) } + // Check that the custom schema for the produced event what's excepted + producedSchema, err := produced.GetCustomSchema() + if err != nil { + t.Errorf("failed to obtain the produced event custom schema: %v", err) + } + if d := cmp.Diff(producedSchema.ID, produced.GetSchemaUri()); d != "" { + t.Errorf("args: diff(-want,+got):\n%s", d) + } consumed, err := apiv04.NewFromJsonBytes(exampleConsumed) if err != nil { t.Fatalf("didn't expected it to fail, but it did: %v", err) @@ -589,6 +614,31 @@ func TestExamples(t *testing.T) { if d := cmp.Diff(consumed.GetLinks(), produced.GetLinks()); d != "" { t.Errorf("args: diff(-want,+got):\n%s", d) } + // Coverage for GetCustomSchema + consumedSchema, err := consumed.GetCustomSchema() + if err != nil { + t.Errorf("failed to obtain the consumed event custom schema: %v", err) + } + if d := cmp.Diff(consumedSchema.ID, producedSchema.ID); d != "" { + t.Errorf("args: diff(-want,+got):\n%s", d) + } + // Check the case of no custom schema + produced.SetSchemaUri("") + producedSchema, err = produced.GetCustomSchema() + if producedSchema != nil || err != nil { + t.Errorf("expected nil schema and error when schema is not set, got schema %v, error %v", producedSchema, err) + } + // Check the case of custom schema missing from the DB + notFoundSchema := "https://this.is.not.found/in/the/db" + produced.SetSchemaUri(notFoundSchema) + producedSchema, err = produced.GetCustomSchema() + if err == nil { + t.Errorf("expected an error when schema is not found, got schema %v, error %v", producedSchema, err) + } + expectedError := fmt.Sprintf("schema with id %s could not be found", notFoundSchema) + if !strings.HasPrefix(err.Error(), expectedError) { + t.Errorf("error %s does not start with the expected prefix %s", err.Error(), expectedError) + } }) } } diff --git a/pkg/api/ztest_schemas.go b/pkg/api/ztest_schemas.go index 4a6fcfa..6b04dca 100644 --- a/pkg/api/ztest_schemas.go +++ b/pkg/api/ztest_schemas.go @@ -29,6 +29,9 @@ import ( ) var ( + // Schema compiler, with schemas preloaded + testCompiler *jsonschema.Compiler + // All compiled schemas by Id TestCompiledSchemas SchemaDB @@ -336,17 +339,18 @@ var ( ) func init() { - compiler, err := newTestJsonSchemaCompiler() + var err error + testCompiler, err = newTestJsonSchemaCompiler() panicOnError(err) TestCompiledSchemas = make(map[string]*jsonschema.Schema) // For tests load non-test schemas first to cover links and custom - for url, _ := range SchemasById { - sch, err := compiler.Compile(url) + for url := range SchemasById { + sch, err := testCompiler.Compile(url) panicOnError(err) TestCompiledSchemas[url] = sch } - for url, _ := range TestSchemasById { - sch, err := compiler.Compile(url) + for url := range TestSchemasById { + sch, err := testCompiler.Compile(url) panicOnError(err) TestCompiledSchemas[url] = sch } diff --git a/pkg/api/zz_artifactdeleted_0_1_0.go b/pkg/api/zz_artifactdeleted_0_1_0.go index 39073a4..750d83e 100644 --- a/pkg/api/zz_artifactdeleted_0_1_0.go +++ b/pkg/api/zz_artifactdeleted_0_1_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ArtifactDeletedEventV0_1_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ArtifactDeletedEventV0_1_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ArtifactDeletedEventV0_1_0) SetId(id string) { diff --git a/pkg/api/zz_artifactdownloaded_0_1_0.go b/pkg/api/zz_artifactdownloaded_0_1_0.go index c9a7ce3..5fe3653 100644 --- a/pkg/api/zz_artifactdownloaded_0_1_0.go +++ b/pkg/api/zz_artifactdownloaded_0_1_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ArtifactDownloadedEventV0_1_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ArtifactDownloadedEventV0_1_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ArtifactDownloadedEventV0_1_0) SetId(id string) { diff --git a/pkg/api/zz_artifactpackaged_0_2_0.go b/pkg/api/zz_artifactpackaged_0_2_0.go index da556a1..584555b 100644 --- a/pkg/api/zz_artifactpackaged_0_2_0.go +++ b/pkg/api/zz_artifactpackaged_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e ArtifactPackagedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ArtifactPackagedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ArtifactPackagedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_artifactpublished_0_2_0.go b/pkg/api/zz_artifactpublished_0_2_0.go index 63acaae..172ed8b 100644 --- a/pkg/api/zz_artifactpublished_0_2_0.go +++ b/pkg/api/zz_artifactpublished_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e ArtifactPublishedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ArtifactPublishedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ArtifactPublishedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_artifactsigned_0_2_0.go b/pkg/api/zz_artifactsigned_0_2_0.go index ca58e8a..da77fac 100644 --- a/pkg/api/zz_artifactsigned_0_2_0.go +++ b/pkg/api/zz_artifactsigned_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ArtifactSignedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ArtifactSignedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ArtifactSignedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_branchcreated_0_2_0.go b/pkg/api/zz_branchcreated_0_2_0.go index 73b95f7..f6c2c30 100644 --- a/pkg/api/zz_branchcreated_0_2_0.go +++ b/pkg/api/zz_branchcreated_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e BranchCreatedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e BranchCreatedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *BranchCreatedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_branchdeleted_0_2_0.go b/pkg/api/zz_branchdeleted_0_2_0.go index f3111d3..de9b81a 100644 --- a/pkg/api/zz_branchdeleted_0_2_0.go +++ b/pkg/api/zz_branchdeleted_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e BranchDeletedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e BranchDeletedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *BranchDeletedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_buildfinished_0_2_0.go b/pkg/api/zz_buildfinished_0_2_0.go index 4723dcb..3fa9ec6 100644 --- a/pkg/api/zz_buildfinished_0_2_0.go +++ b/pkg/api/zz_buildfinished_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e BuildFinishedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e BuildFinishedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *BuildFinishedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_buildqueued_0_2_0.go b/pkg/api/zz_buildqueued_0_2_0.go index 7df434d..bb3208a 100644 --- a/pkg/api/zz_buildqueued_0_2_0.go +++ b/pkg/api/zz_buildqueued_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -117,6 +118,20 @@ func (e BuildQueuedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e BuildQueuedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *BuildQueuedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_buildstarted_0_2_0.go b/pkg/api/zz_buildstarted_0_2_0.go index 6e2b52b..85b5bfc 100644 --- a/pkg/api/zz_buildstarted_0_2_0.go +++ b/pkg/api/zz_buildstarted_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -117,6 +118,20 @@ func (e BuildStartedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e BuildStartedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *BuildStartedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_changeabandoned_0_2_0.go b/pkg/api/zz_changeabandoned_0_2_0.go index 870ecdf..4e7e12c 100644 --- a/pkg/api/zz_changeabandoned_0_2_0.go +++ b/pkg/api/zz_changeabandoned_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ChangeAbandonedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ChangeAbandonedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ChangeAbandonedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_changecreated_0_3_0.go b/pkg/api/zz_changecreated_0_3_0.go index fd9f57b..138f36e 100644 --- a/pkg/api/zz_changecreated_0_3_0.go +++ b/pkg/api/zz_changecreated_0_3_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e ChangeCreatedEventV0_3_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ChangeCreatedEventV0_3_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ChangeCreatedEventV0_3_0) SetId(id string) { diff --git a/pkg/api/zz_changemerged_0_2_0.go b/pkg/api/zz_changemerged_0_2_0.go index 9bce380..a5fdbc0 100644 --- a/pkg/api/zz_changemerged_0_2_0.go +++ b/pkg/api/zz_changemerged_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ChangeMergedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ChangeMergedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ChangeMergedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_changereviewed_0_2_0.go b/pkg/api/zz_changereviewed_0_2_0.go index 31b1c1f..6078ad5 100644 --- a/pkg/api/zz_changereviewed_0_2_0.go +++ b/pkg/api/zz_changereviewed_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ChangeReviewedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ChangeReviewedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ChangeReviewedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_changeupdated_0_2_0.go b/pkg/api/zz_changeupdated_0_2_0.go index 7bc7466..9004ed3 100644 --- a/pkg/api/zz_changeupdated_0_2_0.go +++ b/pkg/api/zz_changeupdated_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ChangeUpdatedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ChangeUpdatedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ChangeUpdatedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_customtype_0_4_1.go b/pkg/api/zz_customtype_0_4_1.go index 7d9a224..5d4c733 100644 --- a/pkg/api/zz_customtype_0_4_1.go +++ b/pkg/api/zz_customtype_0_4_1.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -110,6 +111,20 @@ func (e CustomTypeEventV0_4_1) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e CustomTypeEventV0_4_1) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *CustomTypeEventV0_4_1) SetId(id string) { diff --git a/pkg/api/zz_environmentcreated_0_2_0.go b/pkg/api/zz_environmentcreated_0_2_0.go index b0fb0bc..377cc5e 100644 --- a/pkg/api/zz_environmentcreated_0_2_0.go +++ b/pkg/api/zz_environmentcreated_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e EnvironmentCreatedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e EnvironmentCreatedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *EnvironmentCreatedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_environmentdeleted_0_2_0.go b/pkg/api/zz_environmentdeleted_0_2_0.go index 56c1f8c..3634cab 100644 --- a/pkg/api/zz_environmentdeleted_0_2_0.go +++ b/pkg/api/zz_environmentdeleted_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e EnvironmentDeletedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e EnvironmentDeletedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *EnvironmentDeletedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_environmentmodified_0_2_0.go b/pkg/api/zz_environmentmodified_0_2_0.go index 7bc152b..8b17a79 100644 --- a/pkg/api/zz_environmentmodified_0_2_0.go +++ b/pkg/api/zz_environmentmodified_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e EnvironmentModifiedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e EnvironmentModifiedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *EnvironmentModifiedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_incidentdetected_0_2_0.go b/pkg/api/zz_incidentdetected_0_2_0.go index 6058b39..d43e0b7 100644 --- a/pkg/api/zz_incidentdetected_0_2_0.go +++ b/pkg/api/zz_incidentdetected_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e IncidentDetectedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e IncidentDetectedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *IncidentDetectedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_incidentreported_0_2_0.go b/pkg/api/zz_incidentreported_0_2_0.go index 38dd7ec..60a556a 100644 --- a/pkg/api/zz_incidentreported_0_2_0.go +++ b/pkg/api/zz_incidentreported_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -126,6 +127,20 @@ func (e IncidentReportedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e IncidentReportedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *IncidentReportedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_incidentresolved_0_2_0.go b/pkg/api/zz_incidentresolved_0_2_0.go index 272ec7f..c8d8a74 100644 --- a/pkg/api/zz_incidentresolved_0_2_0.go +++ b/pkg/api/zz_incidentresolved_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e IncidentResolvedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e IncidentResolvedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *IncidentResolvedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_pipelinerunfinished_0_2_0.go b/pkg/api/zz_pipelinerunfinished_0_2_0.go index 55dc854..2357bcd 100644 --- a/pkg/api/zz_pipelinerunfinished_0_2_0.go +++ b/pkg/api/zz_pipelinerunfinished_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e PipelineRunFinishedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e PipelineRunFinishedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *PipelineRunFinishedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_pipelinerunqueued_0_2_0.go b/pkg/api/zz_pipelinerunqueued_0_2_0.go index 8f581f1..14faa0a 100644 --- a/pkg/api/zz_pipelinerunqueued_0_2_0.go +++ b/pkg/api/zz_pipelinerunqueued_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e PipelineRunQueuedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e PipelineRunQueuedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *PipelineRunQueuedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_pipelinerunstarted_0_2_0.go b/pkg/api/zz_pipelinerunstarted_0_2_0.go index 71dd2c5..c6c8a73 100644 --- a/pkg/api/zz_pipelinerunstarted_0_2_0.go +++ b/pkg/api/zz_pipelinerunstarted_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e PipelineRunStartedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e PipelineRunStartedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *PipelineRunStartedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_repositorycreated_0_2_0.go b/pkg/api/zz_repositorycreated_0_2_0.go index 86f8e71..89b8932 100644 --- a/pkg/api/zz_repositorycreated_0_2_0.go +++ b/pkg/api/zz_repositorycreated_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e RepositoryCreatedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e RepositoryCreatedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *RepositoryCreatedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_repositorydeleted_0_2_0.go b/pkg/api/zz_repositorydeleted_0_2_0.go index 3b544db..fd1c478 100644 --- a/pkg/api/zz_repositorydeleted_0_2_0.go +++ b/pkg/api/zz_repositorydeleted_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e RepositoryDeletedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e RepositoryDeletedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *RepositoryDeletedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_repositorymodified_0_2_0.go b/pkg/api/zz_repositorymodified_0_2_0.go index 1c2cdeb..ed97684 100644 --- a/pkg/api/zz_repositorymodified_0_2_0.go +++ b/pkg/api/zz_repositorymodified_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e RepositoryModifiedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e RepositoryModifiedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *RepositoryModifiedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_servicedeployed_0_2_0.go b/pkg/api/zz_servicedeployed_0_2_0.go index 681a6fb..7123a7f 100644 --- a/pkg/api/zz_servicedeployed_0_2_0.go +++ b/pkg/api/zz_servicedeployed_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e ServiceDeployedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ServiceDeployedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ServiceDeployedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_servicepublished_0_2_0.go b/pkg/api/zz_servicepublished_0_2_0.go index 48b1770..47c3b81 100644 --- a/pkg/api/zz_servicepublished_0_2_0.go +++ b/pkg/api/zz_servicepublished_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ServicePublishedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ServicePublishedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ServicePublishedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_serviceremoved_0_2_0.go b/pkg/api/zz_serviceremoved_0_2_0.go index e4e4d66..29c74bf 100644 --- a/pkg/api/zz_serviceremoved_0_2_0.go +++ b/pkg/api/zz_serviceremoved_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -118,6 +119,20 @@ func (e ServiceRemovedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ServiceRemovedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ServiceRemovedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_servicerolledback_0_2_0.go b/pkg/api/zz_servicerolledback_0_2_0.go index cab8087..c07c0f2 100644 --- a/pkg/api/zz_servicerolledback_0_2_0.go +++ b/pkg/api/zz_servicerolledback_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e ServiceRolledbackEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ServiceRolledbackEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ServiceRolledbackEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_serviceupgraded_0_2_0.go b/pkg/api/zz_serviceupgraded_0_2_0.go index e191886..049efe4 100644 --- a/pkg/api/zz_serviceupgraded_0_2_0.go +++ b/pkg/api/zz_serviceupgraded_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -120,6 +121,20 @@ func (e ServiceUpgradedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e ServiceUpgradedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *ServiceUpgradedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_taskrunfinished_0_2_0.go b/pkg/api/zz_taskrunfinished_0_2_0.go index 368c684..6407656 100644 --- a/pkg/api/zz_taskrunfinished_0_2_0.go +++ b/pkg/api/zz_taskrunfinished_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -126,6 +127,20 @@ func (e TaskRunFinishedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TaskRunFinishedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TaskRunFinishedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_taskrunstarted_0_2_0.go b/pkg/api/zz_taskrunstarted_0_2_0.go index 58f6752..817e093 100644 --- a/pkg/api/zz_taskrunstarted_0_2_0.go +++ b/pkg/api/zz_taskrunstarted_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -122,6 +123,20 @@ func (e TaskRunStartedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TaskRunStartedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TaskRunStartedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_testcaserunfinished_0_2_0.go b/pkg/api/zz_testcaserunfinished_0_2_0.go index 2f0fdcf..9dffa85 100644 --- a/pkg/api/zz_testcaserunfinished_0_2_0.go +++ b/pkg/api/zz_testcaserunfinished_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -128,6 +129,20 @@ func (e TestCaseRunFinishedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TestCaseRunFinishedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TestCaseRunFinishedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_testcaserunqueued_0_2_0.go b/pkg/api/zz_testcaserunqueued_0_2_0.go index bffaf82..865b8d2 100644 --- a/pkg/api/zz_testcaserunqueued_0_2_0.go +++ b/pkg/api/zz_testcaserunqueued_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e TestCaseRunQueuedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TestCaseRunQueuedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TestCaseRunQueuedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_testcaserunskipped_0_1_0.go b/pkg/api/zz_testcaserunskipped_0_1_0.go index 3173897..cba113c 100644 --- a/pkg/api/zz_testcaserunskipped_0_1_0.go +++ b/pkg/api/zz_testcaserunskipped_0_1_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e TestCaseRunSkippedEventV0_1_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TestCaseRunSkippedEventV0_1_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TestCaseRunSkippedEventV0_1_0) SetId(id string) { diff --git a/pkg/api/zz_testcaserunstarted_0_2_0.go b/pkg/api/zz_testcaserunstarted_0_2_0.go index dd9e91c..b6e93f5 100644 --- a/pkg/api/zz_testcaserunstarted_0_2_0.go +++ b/pkg/api/zz_testcaserunstarted_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e TestCaseRunStartedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TestCaseRunStartedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TestCaseRunStartedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_testoutputpublished_0_2_0.go b/pkg/api/zz_testoutputpublished_0_2_0.go index e34aad1..9ffce69 100644 --- a/pkg/api/zz_testoutputpublished_0_2_0.go +++ b/pkg/api/zz_testoutputpublished_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -124,6 +125,20 @@ func (e TestOutputPublishedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TestOutputPublishedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TestOutputPublishedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_testsuiterunfinished_0_2_0.go b/pkg/api/zz_testsuiterunfinished_0_2_0.go index 48c42d7..1d83dfc 100644 --- a/pkg/api/zz_testsuiterunfinished_0_2_0.go +++ b/pkg/api/zz_testsuiterunfinished_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -126,6 +127,20 @@ func (e TestSuiteRunFinishedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TestSuiteRunFinishedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TestSuiteRunFinishedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_testsuiterunqueued_0_2_0.go b/pkg/api/zz_testsuiterunqueued_0_2_0.go index ee1de07..1d1b9f2 100644 --- a/pkg/api/zz_testsuiterunqueued_0_2_0.go +++ b/pkg/api/zz_testsuiterunqueued_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -122,6 +123,20 @@ func (e TestSuiteRunQueuedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TestSuiteRunQueuedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TestSuiteRunQueuedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_testsuiterunstarted_0_2_0.go b/pkg/api/zz_testsuiterunstarted_0_2_0.go index 7082ad6..19efbad 100644 --- a/pkg/api/zz_testsuiterunstarted_0_2_0.go +++ b/pkg/api/zz_testsuiterunstarted_0_2_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -122,6 +123,20 @@ func (e TestSuiteRunStartedEventV0_2_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TestSuiteRunStartedEventV0_2_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TestSuiteRunStartedEventV0_2_0) SetId(id string) { diff --git a/pkg/api/zz_ticketclosed_0_1_0.go b/pkg/api/zz_ticketclosed_0_1_0.go index 99a3e1a..d2e7ec6 100644 --- a/pkg/api/zz_ticketclosed_0_1_0.go +++ b/pkg/api/zz_ticketclosed_0_1_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -138,6 +139,20 @@ func (e TicketClosedEventV0_1_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TicketClosedEventV0_1_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TicketClosedEventV0_1_0) SetId(id string) { diff --git a/pkg/api/zz_ticketcreated_0_1_0.go b/pkg/api/zz_ticketcreated_0_1_0.go index 113ef82..6b1f9de 100644 --- a/pkg/api/zz_ticketcreated_0_1_0.go +++ b/pkg/api/zz_ticketcreated_0_1_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -134,6 +135,20 @@ func (e TicketCreatedEventV0_1_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TicketCreatedEventV0_1_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TicketCreatedEventV0_1_0) SetId(id string) { diff --git a/pkg/api/zz_ticketupdated_0_1_0.go b/pkg/api/zz_ticketupdated_0_1_0.go index 212826b..f8aeac1 100644 --- a/pkg/api/zz_ticketupdated_0_1_0.go +++ b/pkg/api/zz_ticketupdated_0_1_0.go @@ -21,6 +21,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -136,6 +137,20 @@ func (e TicketUpdatedEventV0_1_0) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e TicketUpdatedEventV0_1_0) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *TicketUpdatedEventV0_1_0) SetId(id string) { diff --git a/pkg/api/zz_ztest_foosubjectbarpredicate_1_2_3.go b/pkg/api/zz_ztest_foosubjectbarpredicate_1_2_3.go index ee48c2c..25d9568 100644 --- a/pkg/api/zz_ztest_foosubjectbarpredicate_1_2_3.go +++ b/pkg/api/zz_ztest_foosubjectbarpredicate_1_2_3.go @@ -23,6 +23,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -126,6 +127,20 @@ func (e FooSubjectBarPredicateEventV1_2_3) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e FooSubjectBarPredicateEventV1_2_3) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *FooSubjectBarPredicateEventV1_2_3) SetId(id string) { diff --git a/pkg/api/zz_ztest_foosubjectbarpredicate_2_2_3.go b/pkg/api/zz_ztest_foosubjectbarpredicate_2_2_3.go index fad2727..1470008 100644 --- a/pkg/api/zz_ztest_foosubjectbarpredicate_2_2_3.go +++ b/pkg/api/zz_ztest_foosubjectbarpredicate_2_2_3.go @@ -23,6 +23,7 @@ SPDX-License-Identifier: Apache-2.0 package api import ( + "fmt" "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -126,6 +127,20 @@ func (e FooSubjectBarPredicateEventV2_2_3) GetSchemaUri() string { return e.Context.SchemaUri } +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e FooSubjectBarPredicateEventV2_2_3) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if !found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} + // CDEventsWriter implementation func (e *FooSubjectBarPredicateEventV2_2_3) SetId(id string) { diff --git a/tools/templates/event.go.tmpl b/tools/templates/event.go.tmpl index df5398e..7a87462 100644 --- a/tools/templates/event.go.tmpl +++ b/tools/templates/event.go.tmpl @@ -24,6 +24,9 @@ SPDX-License-Identifier: Apache-2.0 package api import ( +{{- if ne .SpecVersion "0.3.0" }} + "fmt" +{{- end }} "time" jsonschema "github.com/santhosh-tekuri/jsonschema/v6" @@ -136,6 +139,20 @@ func (e {{.Subject}}{{.Predicate}}EventV{{.VersionName}}) GetLinks() EmbeddedLin func (e {{.Subject}}{{.Predicate}}EventV{{.VersionName}}) GetSchemaUri() string { return e.Context.SchemaUri } + +// GetCustomSchema looks up the SchemaUri, if any is defined. If none is defined, it returns nil. +// If it's defined and cannot be found, it returns an error. +func (e {{.Subject}}{{.Predicate}}EventV{{.VersionName}}) GetCustomSchema() (*jsonschema.Schema, error) { + schemaUri := e.GetSchemaUri() + if schemaUri == "" { + return nil, nil + } + schema, found := CompiledCustomSchemas[schemaUri] + if ! found { + return nil, fmt.Errorf("schema with id %s could not be found in the local registry", schemaUri) + } + return schema, nil +} {{- end}} // CDEventsWriter implementation diff --git a/tools/templates/schemas.go.tmpl b/tools/templates/schemas.go.tmpl index a53b265..8d2a09a 100644 --- a/tools/templates/schemas.go.tmpl +++ b/tools/templates/schemas.go.tmpl @@ -37,9 +37,17 @@ type SchemaDB map[string]*jsonschema.Schema {{- end}} var ( + // Schema compiler, with schemas preloaded + {{if .IsTestData}}testC{{else}}c{{end}}ompiler *jsonschema.Compiler + // All compiled schemas by Id {{if .IsTestData}}Test{{end}}CompiledSchemas SchemaDB + {{- if not .IsTestData}} + // All compiled custom schemas by Id + CompiledCustomSchemas SchemaDB + {{- end }} + // All schemas as string by Id {{if .IsTestData}}Test{{end}}SchemasById = map[string]string{ {{- range $id, $data := .Data }} @@ -49,22 +57,26 @@ var ( ) func init() { - compiler, err := new{{if .IsTestData}}Test{{end}}JsonSchemaCompiler() + var err error + {{if .IsTestData}}testC{{else}}c{{end}}ompiler, err = new{{if .IsTestData}}Test{{end}}JsonSchemaCompiler() panicOnError(err) {{if .IsTestData}}Test{{end}}CompiledSchemas = make(map[string]*jsonschema.Schema) {{- if .IsTestData}} // For tests load non-test schemas first to cover links and custom - for url, _ := range SchemasById { - sch, err := compiler.Compile(url) + for url := range SchemasById { + sch, err := {{if .IsTestData}}testC{{else}}c{{end}}ompiler.Compile(url) panicOnError(err) TestCompiledSchemas[url] = sch } {{- end}} - for url, _ := range {{if .IsTestData}}Test{{end}}SchemasById { - sch, err := compiler.Compile(url) + for url := range {{if .IsTestData}}Test{{end}}SchemasById { + sch, err := {{if .IsTestData}}testC{{else}}c{{end}}ompiler.Compile(url) panicOnError(err) {{if .IsTestData}}Test{{end}}CompiledSchemas[url] = sch } + {{- if not .IsTestData}} + CompiledCustomSchemas = make(map[string]*jsonschema.Schema) + {{- end }} } {{- if not .IsTestData}} @@ -103,3 +115,25 @@ func new{{if .IsTestData}}Test{{end}}JsonSchemaCompiler() (*jsonschema.Compiler, } return c, nil } + +{{- if not .IsTestData}} +// LoadJsonSchema compiles and loads a JSON schema in []byte format into the sdk +// custom JSON schema databased. Returns an error if the schema cannot be compiled. +// If the schemaId already exists, the previous schema definition is overwritten. +func LoadJsonSchema(schemaId string, schema []byte) error { + var loaded map[string]interface{} + err := json.Unmarshal(schema, &loaded) + if err != nil { + return err + } + if err := {{if .IsTestData}}testC{{else}}c{{end}}ompiler.AddResource(schemaId, loaded); err != nil { + return err + } + sch, err := {{if .IsTestData}}testC{{else}}c{{end}}ompiler.Compile(schemaId) + if err != nil { + return err + } + CompiledCustomSchemas[schemaId] = sch + return nil +} +{{- end }} \ No newline at end of file