1
0
mirror of https://github.com/libp2p/go-libp2p-core.git synced 2025-04-13 15:20:49 +08:00
go-libp2p-core/routing/options.go
Raúl Kripalani 47396eb0b7 reorg code; see below.
* flatten out slim packages in routing.
* simplify naming in discovery.
* break up big monolithic files into constituents (network, pnet).
* renames for consistency.
2019-04-17 17:21:49 +01:00

51 lines
1.2 KiB
Go

package routing
// Option is a single routing option.
type Option func(opts *Options) error
// Options is a set of routing options
type Options struct {
// Allow expired values.
Expired bool
Offline bool
// Other (ValueStore implementation specific) options.
Other map[interface{}]interface{}
}
// Apply applies the given options to this Options
func (opts *Options) Apply(options ...Option) error {
for _, o := range options {
if err := o(opts); err != nil {
return err
}
}
return nil
}
// ToOption converts this Options to a single Option.
func (opts *Options) ToOption() Option {
return func(nopts *Options) error {
*nopts = *opts
if opts.Other != nil {
nopts.Other = make(map[interface{}]interface{}, len(opts.Other))
for k, v := range opts.Other {
nopts.Other[k] = v
}
}
return nil
}
}
// Expired is an option that tells the routing system to return expired records
// when no newer records are known.
var Expired Option = func(opts *Options) error {
opts.Expired = true
return nil
}
// Offline is an option that tells the routing system to operate offline (i.e., rely on cached/local data only).
var Offline Option = func(opts *Options) error {
opts.Offline = true
return nil
}