github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/providers/schema_cache.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package providers
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/terramate-io/tf/addrs"
    10  )
    11  
    12  // SchemaCache is a global cache of Schemas.
    13  // This will be accessed by both core and the provider clients to ensure that
    14  // large schemas are stored in a single location.
    15  var SchemaCache = &schemaCache{
    16  	m: make(map[addrs.Provider]ProviderSchema),
    17  }
    18  
    19  // Global cache for provider schemas
    20  // Cache the entire response to ensure we capture any new fields, like
    21  // ServerCapabilities. This also serves to capture errors so that multiple
    22  // concurrent calls resulting in an error can be handled in the same manner.
    23  type schemaCache struct {
    24  	mu sync.Mutex
    25  	m  map[addrs.Provider]ProviderSchema
    26  }
    27  
    28  func (c *schemaCache) Set(p addrs.Provider, s ProviderSchema) {
    29  	c.mu.Lock()
    30  	defer c.mu.Unlock()
    31  
    32  	c.m[p] = s
    33  }
    34  
    35  func (c *schemaCache) Get(p addrs.Provider) (ProviderSchema, bool) {
    36  	c.mu.Lock()
    37  	defer c.mu.Unlock()
    38  
    39  	s, ok := c.m[p]
    40  	return s, ok
    41  }