github.com/Jeffail/benthos/v3@v3.65.0/internal/mqttconf/will.go (about)

     1  package mqttconf
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/Jeffail/benthos/v3/internal/docs"
     7  )
     8  
     9  // Will holds configuration for the last will message that the broker emits,
    10  // should benthos exit abnormally.
    11  type Will struct {
    12  	Enabled  bool   `json:"enabled" yaml:"enabled"`
    13  	QoS      uint8  `json:"qos" yaml:"qos"`
    14  	Retained bool   `json:"retained" yaml:"retained"`
    15  	Topic    string `json:"topic" yaml:"topic"`
    16  	Payload  string `json:"payload" yaml:"payload"`
    17  }
    18  
    19  // EmptyWill returns an empty will, meaning last will message should not be registered.
    20  func EmptyWill() Will {
    21  	return Will{}
    22  }
    23  
    24  // Validate the Will configuration and return nil or error accordingly.
    25  func (w *Will) Validate() error {
    26  	if !w.Enabled {
    27  		return nil
    28  	}
    29  	if w.Topic == "" {
    30  		return errors.New("include topic to register a last will")
    31  	}
    32  	return nil
    33  }
    34  
    35  // WillFieldSpec defines a last will message registration.
    36  func WillFieldSpec() docs.FieldSpec {
    37  	return docs.FieldAdvanced(
    38  		"will", "Set last will message in case of Benthos failure",
    39  	).WithChildren(
    40  		docs.FieldCommon("enabled", "Whether to enable last will messages."),
    41  		docs.FieldCommon("qos", "Set QoS for last will message.").HasOptions("0", "1", "2"),
    42  		docs.FieldCommon("retained", "Set retained for last will message."),
    43  		docs.FieldCommon("topic", "Set topic for last will message."),
    44  		docs.FieldCommon("payload", "Set payload for last will message."),
    45  	)
    46  }