lnd/kvdb/etcd/driver.go
Oliver Gugger 0b4e03f5fc
multi: add golang 1.17 compatible build tags
With go 1.17 a change to the build flags was implemented:
https://go.googlesource.com/proposal/+/master/design/draft-gobuild.md

The formatter now automatically adds the forward-compatible build tag
format and the linter checks for them, so we need to include them in our
code.
2021-09-29 17:31:37 -07:00

81 lines
1.7 KiB
Go

//go:build kvdb_etcd
// +build kvdb_etcd
package etcd
import (
"context"
"fmt"
"github.com/btcsuite/btcwallet/walletdb"
)
const (
dbType = "etcd"
)
// parseArgs parses the arguments from the walletdb Open/Create methods.
func parseArgs(funcName string, args ...interface{}) (context.Context,
*Config, error) {
if len(args) != 2 {
return nil, nil, fmt.Errorf("invalid number of arguments to "+
"%s.%s -- expected: context.Context, etcd.Config",
dbType, funcName,
)
}
ctx, ok := args[0].(context.Context)
if !ok {
return nil, nil, fmt.Errorf("argument 0 to %s.%s is invalid "+
"-- expected: context.Context",
dbType, funcName,
)
}
config, ok := args[1].(*Config)
if !ok {
return nil, nil, fmt.Errorf("argument 1 to %s.%s is invalid -- "+
"expected: etcd.Config",
dbType, funcName,
)
}
return ctx, config, nil
}
// createDBDriver is the callback provided during driver registration that
// creates, initializes, and opens a database for use.
func createDBDriver(args ...interface{}) (walletdb.DB, error) {
ctx, config, err := parseArgs("Create", args...)
if err != nil {
return nil, err
}
return newEtcdBackend(ctx, *config)
}
// openDBDriver is the callback provided during driver registration that opens
// an existing database for use.
func openDBDriver(args ...interface{}) (walletdb.DB, error) {
ctx, config, err := parseArgs("Open", args...)
if err != nil {
return nil, err
}
return newEtcdBackend(ctx, *config)
}
func init() {
// Register the driver.
driver := walletdb.Driver{
DbType: dbType,
Create: createDBDriver,
Open: openDBDriver,
}
if err := walletdb.RegisterDriver(driver); err != nil {
panic(fmt.Sprintf("Failed to regiser database driver '%s': %v",
dbType, err))
}
}