pubsub.go 1.12 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package options

type PubSubPeersSettings struct {
	Topic string
}

type PubSubSubscribeSettings struct {
	Discover bool
}

type PubSubPeersOption func(*PubSubPeersSettings) error
type PubSubSubscribeOption func(*PubSubSubscribeSettings) error

func PubSubPeersOptions(opts ...PubSubPeersOption) (*PubSubPeersSettings, error) {
	options := &PubSubPeersSettings{
		Topic: "",
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}
	return options, nil
}

func PubSubSubscribeOptions(opts ...PubSubSubscribeOption) (*PubSubSubscribeSettings, error) {
	options := &PubSubSubscribeSettings{
		Discover: false,
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}
	return options, nil
}

42
type pubsubOpts struct{}
Łukasz Magiera's avatar
Łukasz Magiera committed
43

44 45 46
var PubBub nameOpts

func (pubsubOpts) Topic(topic string) PubSubPeersOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
47 48 49 50 51 52
	return func(settings *PubSubPeersSettings) error {
		settings.Topic = topic
		return nil
	}
}

53
func (pubsubOpts) Discover(discover bool) PubSubSubscribeOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
54 55 56 57 58
	return func(settings *PubSubSubscribeSettings) error {
		settings.Discover = discover
		return nil
	}
}