diff --git a/acceptance/invariant_test.go b/acceptance/invariant_test.go index ad8e1d14022..7fee84c7e5f 100644 --- a/acceptance/invariant_test.go +++ b/acceptance/invariant_test.go @@ -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) diff --git a/bundle/config/resources.go b/bundle/config/resources.go index 51461fe90db..cb3718ddd8f 100644 --- a/bundle/config/resources.go +++ b/bundle/config/resources.go @@ -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] diff --git a/bundle/config/resources_types.go b/bundle/config/resources_types.go index dcc91545f19..291ed6f8ae9 100644 --- a/bundle/config/resources_types.go +++ b/bundle/config/resources_types.go @@ -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()) @@ -48,4 +49,4 @@ var ResourcesTypes = func() map[string]reflect.Type { } return res -}() +}) diff --git a/bundle/config/resources_types_test.go b/bundle/config/resources_types_test.go index 7a698604612..cf77ad2f9df 100644 --- a/bundle/config/resources_types_test.go +++ b/bundle/config/resources_types_test.go @@ -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") } @@ -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 diff --git a/bundle/config/root.go b/bundle/config/root.go index a64b742630e..637d38d3b9f 100644 --- a/bundle/config/root.go +++ b/bundle/config/root.go @@ -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) }