aaf3c051f2
We have many indexer functions in Consul which take interface{} and type assert before building the index. We can use generics to get rid of the initial plumbing and pass around functions with better defined signatures. This has two benefits: 1) Less verbosity; 2) Developers can parse the argument types to memdb schemas without having to introspect the function for the type assertion.
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
//go:build !consulent
|
|
// +build !consulent
|
|
|
|
package state
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/consul/proto/pbpeering"
|
|
)
|
|
|
|
func indexPeeringFromQuery(q Query) ([]byte, error) {
|
|
var b indexBuilder
|
|
b.String(strings.ToLower(q.Value))
|
|
return b.Bytes(), nil
|
|
}
|
|
|
|
func indexFromPeering(p *pbpeering.Peering) ([]byte, error) {
|
|
if p.Name == "" {
|
|
return nil, errMissingValueForIndex
|
|
}
|
|
|
|
var b indexBuilder
|
|
b.String(strings.ToLower(p.Name))
|
|
return b.Bytes(), nil
|
|
}
|
|
|
|
func indexFromPeeringTrustBundle(ptb *pbpeering.PeeringTrustBundle) ([]byte, error) {
|
|
if ptb.PeerName == "" {
|
|
return nil, errMissingValueForIndex
|
|
}
|
|
|
|
var b indexBuilder
|
|
b.String(strings.ToLower(ptb.PeerName))
|
|
return b.Bytes(), nil
|
|
}
|
|
|
|
func updatePeeringTableIndexes(tx WriteTxn, idx uint64, _ string) error {
|
|
if err := tx.Insert(tableIndex, &IndexEntry{Key: tablePeering, Value: idx}); err != nil {
|
|
return fmt.Errorf("failed updating table index: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func updatePeeringTrustBundlesTableIndexes(tx WriteTxn, idx uint64, _ string) error {
|
|
if err := tx.Insert(tableIndex, &IndexEntry{Key: tablePeeringTrustBundles, Value: idx}); err != nil {
|
|
return fmt.Errorf("failed updating table index: %w", err)
|
|
}
|
|
return nil
|
|
}
|