-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoptions.go
45 lines (40 loc) · 1.1 KB
/
options.go
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
42
43
44
45
package mqtt
import (
"fmt"
"github.com/kubemq-io/kubemq-targets/config"
"github.com/kubemq-io/kubemq-targets/pkg/uuid"
)
type options struct {
host string
username string
password string
clientId string
defaultTopic string
defaultQos int
}
func parseOptions(cfg config.Spec) (options, error) {
o := options{}
var err error
o.host, err = cfg.Properties.MustParseString("host")
if err != nil {
return options{}, fmt.Errorf("error parsing host, %w", err)
}
o.username = cfg.Properties.ParseString("username", "")
o.password = cfg.Properties.ParseString("password", "")
o.clientId = cfg.Properties.ParseString("client_id", uuid.New().String())
o.defaultTopic = cfg.Properties.ParseString("default_topic", "")
o.defaultQos, err = cfg.Properties.ParseIntWithRange("default_qos", 0, 0, 2)
if err != nil {
return options{}, fmt.Errorf("error parsing default_qos, %w", err)
}
return o, nil
}
func (o options) defaultMetadata() (metadata, bool) {
if o.defaultTopic != "" {
return metadata{
topic: o.defaultTopic,
qos: o.defaultQos,
}, true
}
return metadata{}, false
}