forked from benthosdev/benthos
/
will.go
46 lines (40 loc) · 1.37 KB
/
will.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
46
package mqttconf
import (
"errors"
"github.com/dafanshu/benthos/v3/internal/docs"
)
// Will holds configuration for the last will message that the broker emits,
// should benthos exit abnormally.
type Will struct {
Enabled bool `json:"enabled" yaml:"enabled"`
QoS uint8 `json:"qos" yaml:"qos"`
Retained bool `json:"retained" yaml:"retained"`
Topic string `json:"topic" yaml:"topic"`
Payload string `json:"payload" yaml:"payload"`
}
// EmptyWill returns an empty will, meaning last will message should not be registered.
func EmptyWill() Will {
return Will{}
}
// Validate the Will configuration and return nil or error accordingly.
func (w *Will) Validate() error {
if !w.Enabled {
return nil
}
if w.Topic == "" {
return errors.New("include topic to register a last will")
}
return nil
}
// WillFieldSpec defines a last will message registration.
func WillFieldSpec() docs.FieldSpec {
return docs.FieldAdvanced(
"will", "Set last will message in case of Benthos failure",
).WithChildren(
docs.FieldCommon("enabled", "Whether to enable last will messages."),
docs.FieldCommon("qos", "Set QoS for last will message.").HasOptions("0", "1", "2"),
docs.FieldCommon("retained", "Set retained for last will message."),
docs.FieldCommon("topic", "Set topic for last will message."),
docs.FieldCommon("payload", "Set payload for last will message."),
)
}