Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions acceptance/invariant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var LackingInvariantTest = map[string]bool{
func TestInvariantConfigsCoverage(t *testing.T) {
present, withPermissions, withGrants := scanInvariantConfigs(t)

keys := make([]string, 0, len(config.ResourcesTypes))
for key := range config.ResourcesTypes {
keys := make([]string, 0, len(config.ResourcesTypes()))
for key := range config.ResourcesTypes() {
keys = append(keys, key)
}
slices.Sort(keys)
Expand Down
2 changes: 1 addition & 1 deletion bundle/config/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (r *Resources) ResolveKey(ref string) (ConfigResource, string, error) {
func (r *Resources) ResolveDependsOn(ref string) (string, error) {
resourceRef, sub := ref, ""
if parts := strings.Split(ref, "."); len(parts) == 3 {
if _, ok := ResourcesTypes[parts[0]+"."+parts[2]]; !ok {
if _, ok := ResourcesTypes()[parts[0]+"."+parts[2]]; !ok {
return "", fmt.Errorf("no such resource: %s", ref)
}
resourceRef, sub = parts[0]+"."+parts[1], "."+parts[2]
Expand Down
11 changes: 6 additions & 5 deletions bundle/config/resources_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package config

import (
"reflect"
"sync"

"github.com/databricks/cli/libs/structs/structtag"
)

// ResourcesTypes maps the configuration key of each Databricks resource group (for example
// "jobs" or "pipelines") to the Go type that represents a single resource instance inside
// that group (for example `resources.Job`).
var ResourcesTypes = func() map[string]reflect.Type {
// ResourcesTypes returns a map from the configuration key of each Databricks resource
// group (for example "jobs" or "pipelines") to the Go type that represents a single
// resource instance inside that group (for example `resources.Job`).
var ResourcesTypes = sync.OnceValue(func() map[string]reflect.Type {
rt := reflect.TypeFor[Resources]()
res := make(map[string]reflect.Type, rt.NumField())

Expand Down Expand Up @@ -48,4 +49,4 @@ var ResourcesTypes = func() map[string]reflect.Type {
}

return res
}()
})
16 changes: 8 additions & 8 deletions bundle/config/resources_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
)

func TestResourcesTypesMap(t *testing.T) {
assert.Greater(t, len(ResourcesTypes), 10, "expected ResourcesTypes to have more than 10 entries")
assert.Greater(t, len(ResourcesTypes()), 10, "expected ResourcesTypes() to have more than 10 entries")

typ, ok := ResourcesTypes["jobs"]
assert.True(t, ok, "resources type for 'jobs' not found in ResourcesTypes map")
typ, ok := ResourcesTypes()["jobs"]
assert.True(t, ok, "resources type for 'jobs' not found in ResourcesTypes() map")
assert.Equal(t, reflect.TypeFor[resources.Job](), typ, "resources type for 'jobs' mismatch")

typ, ok = ResourcesTypes["jobs.permissions"]
assert.True(t, ok, "resources type for 'jobs.permissions' not found in ResourcesTypes map")
typ, ok = ResourcesTypes()["jobs.permissions"]
assert.True(t, ok, "resources type for 'jobs.permissions' not found in ResourcesTypes() map")
assert.Equal(t, reflect.TypeFor[[]resources.JobPermission](), typ, "resources type for 'jobs.permissions' mismatch")
}

Expand All @@ -43,15 +43,15 @@ func TestResourcesTypesMap(t *testing.T) {
// trip the direct engine performs. Any newly added resource whose wrapper embeds
// an SDK spec is covered automatically.
func TestResourceTypesZeroValueFieldsSerialize(t *testing.T) {
names := make([]string, 0, len(ResourcesTypes))
for name := range ResourcesTypes {
names := make([]string, 0, len(ResourcesTypes()))
for name := range ResourcesTypes() {
names = append(names, name)
}
slices.Sort(names)

for _, name := range names {
t.Run(name, func(t *testing.T) {
typ := ResourcesTypes[name]
typ := ResourcesTypes()[name]
zeros := zeroValueScalars(typ, 0, map[reflect.Type]bool{})
if zeros.Kind() != dyn.KindMap {
return
Expand Down
2 changes: 1 addition & 1 deletion bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func (r *Root) GetResourceConfig(path string) (any, error) {
}

// Resolve the Go type that represents a single resource in this group.
typ, ok := ResourcesTypes[resourceType]
typ, ok := ResourcesTypes()[resourceType]
if !ok {
return nil, fmt.Errorf("no such resource type in the config: %q", resourceType)
}
Expand Down
Loading