From 8de7d32ed1ec6570fd4ae2bb0c088a3dd4af2582 Mon Sep 17 00:00:00 2001 From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com> Date: Tue, 4 Apr 2023 12:16:07 +0200 Subject: [PATCH] Add readonly bundle tag for internal fields (#302) This PR adds a bundle: "readonly" struct tag to the json schema generator. This allows us to skip generating json schema for internal readonly fields Tested using unit test --- bundle/config/artifact.go | 6 +- bundle/config/bundle.go | 6 +- bundle/config/resources/job.go | 2 +- bundle/config/resources/pipeline.go | 2 +- bundle/config/workspace.go | 2 +- bundle/schema/docs/bundle_descriptions.json | 316 +------------------- bundle/schema/schema.go | 5 + bundle/schema/schema_test.go | 52 ++++ 8 files changed, 67 insertions(+), 324 deletions(-) diff --git a/bundle/config/artifact.go b/bundle/config/artifact.go index 2ff6b122..f782fcfc 100644 --- a/bundle/config/artifact.go +++ b/bundle/config/artifact.go @@ -12,9 +12,9 @@ type NotebookArtifact struct { Path string `json:"path"` // Language is detected during build step. - Language workspace.Language `json:"language,omitempty"` + Language workspace.Language `json:"language,omitempty" bundle:"readonly"` // Paths are synthesized during build step. - LocalPath string `json:"local_path,omitempty"` - RemotePath string `json:"remote_path,omitempty"` + LocalPath string `json:"local_path,omitempty" bundle:"readonly"` + RemotePath string `json:"remote_path,omitempty" bundle:"readonly"` } diff --git a/bundle/config/bundle.go b/bundle/config/bundle.go index 982f9c55..c38013e2 100644 --- a/bundle/config/bundle.go +++ b/bundle/config/bundle.go @@ -16,12 +16,12 @@ type Bundle struct { // DefaultWarehouse string `json:"default_warehouse,omitempty"` // Environment is set by the mutator that selects the environment. - Environment string `json:"environment,omitempty"` + Environment string `json:"environment,omitempty" bundle:"readonly"` // Terraform holds configuration related to Terraform. // For example, where to find the binary, which version to use, etc. - Terraform *Terraform `json:"terraform,omitempty"` + Terraform *Terraform `json:"terraform,omitempty" bundle:"readonly"` // Lock configures locking behavior on deployment. - Lock Lock `json:"lock"` + Lock Lock `json:"lock" bundle:"readonly"` } diff --git a/bundle/config/resources/job.go b/bundle/config/resources/job.go index 124bcb9c..8cfc0e7b 100644 --- a/bundle/config/resources/job.go +++ b/bundle/config/resources/job.go @@ -3,7 +3,7 @@ package resources import "github.com/databricks/databricks-sdk-go/service/jobs" type Job struct { - ID string `json:"id,omitempty"` + ID string `json:"id,omitempty" bundle:"readonly"` Permissions []Permission `json:"permissions,omitempty"` *jobs.JobSettings diff --git a/bundle/config/resources/pipeline.go b/bundle/config/resources/pipeline.go index caf400b9..fd46ecf2 100644 --- a/bundle/config/resources/pipeline.go +++ b/bundle/config/resources/pipeline.go @@ -3,7 +3,7 @@ package resources import "github.com/databricks/databricks-sdk-go/service/pipelines" type Pipeline struct { - ID string `json:"id,omitempty"` + ID string `json:"id,omitempty" bundle:"readonly"` Permissions []Permission `json:"permissions,omitempty"` *pipelines.PipelineSpec diff --git a/bundle/config/workspace.go b/bundle/config/workspace.go index f284ce95..0e3ff09b 100644 --- a/bundle/config/workspace.go +++ b/bundle/config/workspace.go @@ -50,7 +50,7 @@ type Workspace struct { // CurrentUser holds the current user. // This is set after configuration initialization. - CurrentUser *scim.User `json:"current_user,omitempty"` + CurrentUser *scim.User `json:"current_user,omitempty" bundle:"readonly"` // Remote base path for deployment state, for artifacts, as synchronization target. // This defaults to "~/.bundle/${bundle.name}/${bundle.environment}" where "~" expands to diff --git a/bundle/schema/docs/bundle_descriptions.json b/bundle/schema/docs/bundle_descriptions.json index c2065f39..cbcef5a6 100644 --- a/bundle/schema/docs/bundle_descriptions.json +++ b/bundle/schema/docs/bundle_descriptions.json @@ -9,17 +9,8 @@ "notebook": { "description": "", "properties": { - "language": { - "description": "" - }, - "local_path": { - "description": "" - }, "path": { "description": "" - }, - "remote_path": { - "description": "" } } } @@ -29,30 +20,8 @@ "bundle": { "description": "Bundle contains details about this bundle, such as its name,\nversion of the spec (TODO), default cluster, default warehouse, etc.\n", "properties": { - "environment": { - "description": "Environment is set by the mutator that selects the environment." - }, - "lock": { - "description": "", - "properties": { - "enabled": { - "description": "" - }, - "force": { - "description": "" - } - } - }, "name": { "description": "" - }, - "terraform": { - "description": "", - "properties": { - "exec_path": { - "description": "" - } - } } } }, @@ -69,17 +38,8 @@ "notebook": { "description": "", "properties": { - "language": { - "description": "" - }, - "local_path": { - "description": "" - }, "path": { "description": "" - }, - "remote_path": { - "description": "" } } } @@ -89,30 +49,8 @@ "bundle": { "description": "Bundle contains details about this bundle, such as its name,\nversion of the spec (TODO), default cluster, default warehouse, etc.\n", "properties": { - "environment": { - "description": "Environment is set by the mutator that selects the environment." - }, - "lock": { - "description": "", - "properties": { - "enabled": { - "description": "" - }, - "force": { - "description": "" - } - } - }, "name": { "description": "" - }, - "terraform": { - "description": "", - "properties": { - "exec_path": { - "description": "" - } - } } } }, @@ -252,9 +190,6 @@ } } }, - "id": { - "description": "" - }, "job_clusters": { "description": "A list of job cluster specifications that can be shared and reused by tasks of this job. Libraries cannot be declared in a shared job cluster. You must declare dependent libraries in task settings.", "items": { @@ -1581,129 +1516,6 @@ "azure_workspace_resource_id": { "description": "" }, - "current_user": { - "description": "", - "properties": { - "active": { - "description": "" - }, - "displayName": { - "description": "" - }, - "emails": { - "description": "", - "items": { - "description": "", - "properties": { - "$ref": { - "description": "" - }, - "display": { - "description": "" - }, - "primary": { - "description": "" - }, - "type": { - "description": "" - }, - "value": { - "description": "" - } - } - } - }, - "entitlements": { - "description": "", - "items": { - "description": "", - "properties": { - "$ref": { - "description": "" - }, - "display": { - "description": "" - }, - "primary": { - "description": "" - }, - "type": { - "description": "" - }, - "value": { - "description": "" - } - } - } - }, - "externalId": { - "description": "" - }, - "groups": { - "description": "", - "items": { - "description": "", - "properties": { - "$ref": { - "description": "" - }, - "display": { - "description": "" - }, - "primary": { - "description": "" - }, - "type": { - "description": "" - }, - "value": { - "description": "" - } - } - } - }, - "id": { - "description": "" - }, - "name": { - "description": "", - "properties": { - "familyName": { - "description": "" - }, - "givenName": { - "description": "" - } - } - }, - "roles": { - "description": "", - "items": { - "description": "", - "properties": { - "$ref": { - "description": "" - }, - "display": { - "description": "" - }, - "primary": { - "description": "" - }, - "type": { - "description": "" - }, - "value": { - "description": "" - } - } - } - }, - "userName": { - "description": "" - } - } - }, "file_path": { "description": "", "properties": { @@ -1882,9 +1694,6 @@ } } }, - "id": { - "description": "" - }, "job_clusters": { "description": "A list of job cluster specifications that can be shared and reused by tasks of this job. Libraries cannot be declared in a shared job cluster. You must declare dependent libraries in task settings.", "items": { @@ -3211,129 +3020,6 @@ "azure_workspace_resource_id": { "description": "" }, - "current_user": { - "description": "", - "properties": { - "active": { - "description": "" - }, - "displayName": { - "description": "" - }, - "emails": { - "description": "", - "items": { - "description": "", - "properties": { - "$ref": { - "description": "" - }, - "display": { - "description": "" - }, - "primary": { - "description": "" - }, - "type": { - "description": "" - }, - "value": { - "description": "" - } - } - } - }, - "entitlements": { - "description": "", - "items": { - "description": "", - "properties": { - "$ref": { - "description": "" - }, - "display": { - "description": "" - }, - "primary": { - "description": "" - }, - "type": { - "description": "" - }, - "value": { - "description": "" - } - } - } - }, - "externalId": { - "description": "" - }, - "groups": { - "description": "", - "items": { - "description": "", - "properties": { - "$ref": { - "description": "" - }, - "display": { - "description": "" - }, - "primary": { - "description": "" - }, - "type": { - "description": "" - }, - "value": { - "description": "" - } - } - } - }, - "id": { - "description": "" - }, - "name": { - "description": "", - "properties": { - "familyName": { - "description": "" - }, - "givenName": { - "description": "" - } - } - }, - "roles": { - "description": "", - "items": { - "description": "", - "properties": { - "$ref": { - "description": "" - }, - "display": { - "description": "" - }, - "primary": { - "description": "" - }, - "type": { - "description": "" - }, - "value": { - "description": "" - } - } - } - }, - "userName": { - "description": "" - } - } - }, "file_path": { "description": "", "properties": { @@ -3371,4 +3057,4 @@ } } } -} \ No newline at end of file +} diff --git a/bundle/schema/schema.go b/bundle/schema/schema.go index af3dfd76..7a55cbd2 100644 --- a/bundle/schema/schema.go +++ b/bundle/schema/schema.go @@ -238,6 +238,11 @@ func toSchema(golangType reflect.Type, docs *Docs, tracker *tracker) (*Schema, e properties := map[string]*Schema{} required := []string{} for _, child := range children { + bundleTag := child.Tag.Get("bundle") + if bundleTag == "readonly" { + continue + } + // get child json tags childJsonTag := strings.Split(child.Tag.Get("json"), ",") childName := childJsonTag[0] diff --git a/bundle/schema/schema_test.go b/bundle/schema/schema_test.go index edb56371..66baf873 100644 --- a/bundle/schema/schema_test.go +++ b/bundle/schema/schema_test.go @@ -1410,3 +1410,55 @@ func TestInterfaceGeneratesEmptySchema(t *testing.T) { t.Log("[DEBUG] expected: ", expected) assert.Equal(t, expected, string(jsonSchema)) } + +func TestBundleReadOnlytag(t *testing.T) { + type Pokemon struct { + Pikachu string `json:"pikachu" bundle:"readonly"` + Raichu string `json:"raichu"` + } + + type Foo struct { + Pokemon *Pokemon `json:"pokemon"` + Apple int `json:"apple"` + Mango string `json:"mango" bundle:"readonly"` + } + + elem := Foo{} + + schema, err := New(reflect.TypeOf(elem), nil) + assert.NoError(t, err) + + jsonSchema, err := json.MarshalIndent(schema, " ", " ") + assert.NoError(t, err) + + expected := + `{ + "type": "object", + "properties": { + "apple": { + "type": "number" + }, + "pokemon": { + "type": "object", + "properties": { + "raichu": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "raichu" + ] + } + }, + "additionalProperties": false, + "required": [ + "pokemon", + "apple" + ] + }` + + t.Log("[DEBUG] actual: ", string(jsonSchema)) + t.Log("[DEBUG] expected: ", expected) + assert.Equal(t, expected, string(jsonSchema)) +}