github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/pkg/cache/v3/resources.go (about)

     1  package cache
     2  
     3  import "github.com/hxx258456/ccgo/go-control-plane/pkg/cache/types"
     4  
     5  // Resources is a versioned group of resources.
     6  type Resources struct {
     7  	// Version information.
     8  	Version string
     9  
    10  	// Items in the group indexed by name.
    11  	Items map[string]types.ResourceWithTTL
    12  }
    13  
    14  // IndexResourcesByName creates a map from the resource name to the resource.
    15  func IndexResourcesByName(items []types.ResourceWithTTL) map[string]types.ResourceWithTTL {
    16  	indexed := make(map[string]types.ResourceWithTTL)
    17  	for _, item := range items {
    18  		indexed[GetResourceName(item.Resource)] = item
    19  	}
    20  	return indexed
    21  }
    22  
    23  // IndexRawResourcesByName creates a map from the resource name to the resource.
    24  func IndexRawResourcesByName(items []types.Resource) map[string]types.Resource {
    25  	indexed := make(map[string]types.Resource)
    26  	for _, item := range items {
    27  		indexed[GetResourceName(item)] = item
    28  	}
    29  	return indexed
    30  }
    31  
    32  // NewResources creates a new resource group.
    33  func NewResources(version string, items []types.Resource) Resources {
    34  	itemsWithTTL := []types.ResourceWithTTL{}
    35  	for _, item := range items {
    36  		itemsWithTTL = append(itemsWithTTL, types.ResourceWithTTL{Resource: item})
    37  	}
    38  	return NewResourcesWithTTL(version, itemsWithTTL)
    39  }
    40  
    41  // NewResourcesWithTTL creates a new resource group.
    42  func NewResourcesWithTTL(version string, items []types.ResourceWithTTL) Resources {
    43  	return Resources{
    44  		Version: version,
    45  		Items:   IndexResourcesByName(items),
    46  	}
    47  }