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
This commit is contained in:
shreyas-goenka 2023-04-04 12:16:07 +02:00 committed by GitHub
parent ddbb17b0d9
commit 8de7d32ed1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 324 deletions

View File

@ -12,9 +12,9 @@ type NotebookArtifact struct {
Path string `json:"path"` Path string `json:"path"`
// Language is detected during build step. // 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. // Paths are synthesized during build step.
LocalPath string `json:"local_path,omitempty"` LocalPath string `json:"local_path,omitempty" bundle:"readonly"`
RemotePath string `json:"remote_path,omitempty"` RemotePath string `json:"remote_path,omitempty" bundle:"readonly"`
} }

View File

@ -16,12 +16,12 @@ type Bundle struct {
// DefaultWarehouse string `json:"default_warehouse,omitempty"` // DefaultWarehouse string `json:"default_warehouse,omitempty"`
// Environment is set by the mutator that selects the environment. // 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. // Terraform holds configuration related to Terraform.
// For example, where to find the binary, which version to use, etc. // 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 configures locking behavior on deployment.
Lock Lock `json:"lock"` Lock Lock `json:"lock" bundle:"readonly"`
} }

View File

@ -3,7 +3,7 @@ package resources
import "github.com/databricks/databricks-sdk-go/service/jobs" import "github.com/databricks/databricks-sdk-go/service/jobs"
type Job struct { type Job struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty" bundle:"readonly"`
Permissions []Permission `json:"permissions,omitempty"` Permissions []Permission `json:"permissions,omitempty"`
*jobs.JobSettings *jobs.JobSettings

View File

@ -3,7 +3,7 @@ package resources
import "github.com/databricks/databricks-sdk-go/service/pipelines" import "github.com/databricks/databricks-sdk-go/service/pipelines"
type Pipeline struct { type Pipeline struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty" bundle:"readonly"`
Permissions []Permission `json:"permissions,omitempty"` Permissions []Permission `json:"permissions,omitempty"`
*pipelines.PipelineSpec *pipelines.PipelineSpec

View File

@ -50,7 +50,7 @@ type Workspace struct {
// CurrentUser holds the current user. // CurrentUser holds the current user.
// This is set after configuration initialization. // 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. // Remote base path for deployment state, for artifacts, as synchronization target.
// This defaults to "~/.bundle/${bundle.name}/${bundle.environment}" where "~" expands to // This defaults to "~/.bundle/${bundle.name}/${bundle.environment}" where "~" expands to

View File

@ -9,17 +9,8 @@
"notebook": { "notebook": {
"description": "", "description": "",
"properties": { "properties": {
"language": {
"description": ""
},
"local_path": {
"description": ""
},
"path": { "path": {
"description": "" "description": ""
},
"remote_path": {
"description": ""
} }
} }
} }
@ -29,30 +20,8 @@
"bundle": { "bundle": {
"description": "Bundle contains details about this bundle, such as its name,\nversion of the spec (TODO), default cluster, default warehouse, etc.\n", "description": "Bundle contains details about this bundle, such as its name,\nversion of the spec (TODO), default cluster, default warehouse, etc.\n",
"properties": { "properties": {
"environment": {
"description": "Environment is set by the mutator that selects the environment."
},
"lock": {
"description": "",
"properties": {
"enabled": {
"description": ""
},
"force": {
"description": ""
}
}
},
"name": { "name": {
"description": "" "description": ""
},
"terraform": {
"description": "",
"properties": {
"exec_path": {
"description": ""
}
}
} }
} }
}, },
@ -69,17 +38,8 @@
"notebook": { "notebook": {
"description": "", "description": "",
"properties": { "properties": {
"language": {
"description": ""
},
"local_path": {
"description": ""
},
"path": { "path": {
"description": "" "description": ""
},
"remote_path": {
"description": ""
} }
} }
} }
@ -89,30 +49,8 @@
"bundle": { "bundle": {
"description": "Bundle contains details about this bundle, such as its name,\nversion of the spec (TODO), default cluster, default warehouse, etc.\n", "description": "Bundle contains details about this bundle, such as its name,\nversion of the spec (TODO), default cluster, default warehouse, etc.\n",
"properties": { "properties": {
"environment": {
"description": "Environment is set by the mutator that selects the environment."
},
"lock": {
"description": "",
"properties": {
"enabled": {
"description": ""
},
"force": {
"description": ""
}
}
},
"name": { "name": {
"description": "" "description": ""
},
"terraform": {
"description": "",
"properties": {
"exec_path": {
"description": ""
}
}
} }
} }
}, },
@ -252,9 +190,6 @@
} }
} }
}, },
"id": {
"description": ""
},
"job_clusters": { "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.", "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": { "items": {
@ -1581,129 +1516,6 @@
"azure_workspace_resource_id": { "azure_workspace_resource_id": {
"description": "" "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": { "file_path": {
"description": "", "description": "",
"properties": { "properties": {
@ -1882,9 +1694,6 @@
} }
} }
}, },
"id": {
"description": ""
},
"job_clusters": { "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.", "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": { "items": {
@ -3211,129 +3020,6 @@
"azure_workspace_resource_id": { "azure_workspace_resource_id": {
"description": "" "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": { "file_path": {
"description": "", "description": "",
"properties": { "properties": {
@ -3371,4 +3057,4 @@
} }
} }
} }
} }

View File

@ -238,6 +238,11 @@ func toSchema(golangType reflect.Type, docs *Docs, tracker *tracker) (*Schema, e
properties := map[string]*Schema{} properties := map[string]*Schema{}
required := []string{} required := []string{}
for _, child := range children { for _, child := range children {
bundleTag := child.Tag.Get("bundle")
if bundleTag == "readonly" {
continue
}
// get child json tags // get child json tags
childJsonTag := strings.Split(child.Tag.Get("json"), ",") childJsonTag := strings.Split(child.Tag.Get("json"), ",")
childName := childJsonTag[0] childName := childJsonTag[0]

View File

@ -1410,3 +1410,55 @@ func TestInterfaceGeneratesEmptySchema(t *testing.T) {
t.Log("[DEBUG] expected: ", expected) t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema)) 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))
}