Files
threshold/internal/bsr/summary.go
T
429247f407 [COMPLIANCE] Update Copyright Headers (#6282)
* [COMPLIANCE] Update Copyright and License Headers

* chore: make gen to generate files with copyright headers

* chore: update missed copyright headers

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Justin Nguyen <justin.nguyen@hashicorp.com>
2026-01-28 11:05:07 -06:00

52 lines
1.3 KiB
Go

// Copyright IBM Corp. 2020, 2025
// SPDX-License-Identifier: BUSL-1.1
package bsr
import (
"context"
"fmt"
)
// SummaryAllocFunc is a function that returns a summary type
type SummaryAllocFunc func(ctx context.Context) Summary
// summaryAllocFuncRegistry mappings of protocols and container type
// for each SummaryAllocFunc
type summaryAllocFuncRegistry map[Protocol]map[ContainerType]SummaryAllocFunc
func (r summaryAllocFuncRegistry) get(p Protocol, c ContainerType) (SummaryAllocFunc, bool) {
protocol, ok := r[p]
if !ok {
return nil, false
}
af, ok := protocol[c]
return af, ok
}
var summaryAllocFuncs summaryAllocFuncRegistry
// RegisterSummaryAllocFunc registers a SummaryAllocFunc for the given Protocol.
// A given Protocol and Container can only have one SummaryAllocFunc function
// registered.
func RegisterSummaryAllocFunc(p Protocol, c ContainerType, af SummaryAllocFunc) error {
const op = "bsr.RegisterSummaryAllocFunc"
if summaryAllocFuncs == nil {
summaryAllocFuncs = make(map[Protocol]map[ContainerType]SummaryAllocFunc)
}
protocol, ok := summaryAllocFuncs[p]
if !ok {
protocol = make(map[ContainerType]SummaryAllocFunc)
}
_, ok = protocol[c]
if ok {
return fmt.Errorf("%s: %s protocol with %s container: %w", op, p, c, ErrAlreadyRegistered)
}
protocol[c] = af
summaryAllocFuncs[p] = protocol
return nil
}