2023-09-22 20:46:14 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
2024-02-09 21:42:10 +00:00
|
|
|
package resource
|
2023-09-22 20:46:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sectionRefKey struct {
|
2024-02-09 21:42:10 +00:00
|
|
|
ReferenceKey
|
2023-09-22 20:46:14 +00:00
|
|
|
Section string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BoundReferenceCollector struct {
|
|
|
|
refs map[sectionRefKey]*pbresource.Reference
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBoundReferenceCollector() *BoundReferenceCollector {
|
|
|
|
return &BoundReferenceCollector{
|
|
|
|
refs: make(map[sectionRefKey]*pbresource.Reference),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BoundReferenceCollector) List() []*pbresource.Reference {
|
|
|
|
if len(c.refs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*pbresource.Reference, 0, len(c.refs))
|
|
|
|
for _, ref := range c.refs {
|
|
|
|
out = append(out, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
2024-02-09 21:42:10 +00:00
|
|
|
return LessReference(out[i], out[j])
|
2023-09-22 20:46:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2024-02-09 21:42:10 +00:00
|
|
|
func (c *BoundReferenceCollector) AddRefOrID(ref ReferenceOrID) {
|
2023-09-22 20:46:14 +00:00
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-02-09 21:42:10 +00:00
|
|
|
c.AddRef(ReferenceFromReferenceOrID(ref))
|
2023-09-22 20:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BoundReferenceCollector) AddRef(ref *pbresource.Reference) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
srk := sectionRefKey{
|
2024-02-09 21:42:10 +00:00
|
|
|
ReferenceKey: NewReferenceKey(ref),
|
2023-09-22 20:46:14 +00:00
|
|
|
Section: ref.Section,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := c.refs[srk]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.refs[srk] = ref
|
|
|
|
}
|