Skip to content

Commit

Permalink
Add internal tag for bundle fields to be skipped from schema (#636)
Browse files Browse the repository at this point in the history
## Changes
This PR:
1. Introduces the "internal" tag to bundle configs that should not be
visible to customers.
2. Annotates "metadata_service_url" as an internal field.

## Tests
Unit tests.
  • Loading branch information
shreyas-goenka authored Aug 10, 2023
1 parent 2a58253 commit 6b615cc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bundle/config/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Workspace struct {
Host string `json:"host,omitempty"`
Profile string `json:"profile,omitempty"`
AuthType string `json:"auth_type,omitempty"`
MetadataServiceURL string `json:"metadata_service_url,omitempty"`
MetadataServiceURL string `json:"metadata_service_url,omitempty" bundle:"internal"`

// OAuth specific attributes.
ClientID string `json:"client_id,omitempty"`
Expand Down
10 changes: 9 additions & 1 deletion bundle/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"github.com/databricks/cli/libs/jsonschema"
)

// Fields tagged "readonly" should not be emitted in the schema as they are
// computed at runtime, and should not be assigned a value by the bundle author.
const readonlyTag = "readonly"

// Annotation for internal bundle fields that should not be exposed to customers.
// Fields can be tagged as "internal" to remove them from the generated schema.
const internalTag = "internal"

// This function translates golang types into json schema. Here is the mapping
// between json schema types and golang types
//
Expand Down Expand Up @@ -197,7 +205,7 @@ func toSchema(golangType reflect.Type, docs *Docs, tracker *tracker) (*jsonschem
required := []string{}
for _, child := range children {
bundleTag := child.Tag.Get("bundle")
if bundleTag == "readonly" {
if bundleTag == readonlyTag || bundleTag == internalTag {
continue
}

Expand Down
52 changes: 52 additions & 0 deletions bundle/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1462,3 +1462,55 @@ func TestBundleReadOnlytag(t *testing.T) {
t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema))
}

func TestBundleInternalTag(t *testing.T) {
type Pokemon struct {
Pikachu string `json:"pikachu" bundle:"internal"`
Raichu string `json:"raichu"`
}

type Foo struct {
Pokemon *Pokemon `json:"pokemon"`
Apple int `json:"apple"`
Mango string `json:"mango" bundle:"internal"`
}

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))
}

0 comments on commit 6b615cc

Please sign in to comment.