Rename the config structs setDefaults to validate
This allows us to check for some required values and give an easy to respond to error at startup rather than a lot of validation during run time.
This commit is contained in:
@@ -13,12 +13,12 @@ type appservice struct {
|
||||
HSToken string `yaml:"hs_token"`
|
||||
}
|
||||
|
||||
func (a *appservice) setDefaults() error {
|
||||
func (a *appservice) validate() error {
|
||||
if a.ID == "" {
|
||||
a.ID = "discord"
|
||||
}
|
||||
|
||||
if err := a.Bot.setDefaults(); err != nil {
|
||||
if err := a.Bot.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -35,5 +35,5 @@ func (a *appservice) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
|
||||
*a = appservice(raw)
|
||||
|
||||
return a.setDefaults()
|
||||
return a.validate()
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ type bot struct {
|
||||
Avatar string `yaml:"avatar"`
|
||||
}
|
||||
|
||||
func (b *bot) setDefaults() error {
|
||||
func (b *bot) validate() error {
|
||||
if b.Username == "" {
|
||||
b.Username = "discordbot"
|
||||
}
|
||||
@@ -29,5 +29,5 @@ func (b *bot) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
|
||||
*b = bot(raw)
|
||||
|
||||
return b.setDefaults()
|
||||
return b.validate()
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"bytes"
|
||||
"text/template"
|
||||
)
|
||||
@@ -13,7 +11,7 @@ type bridge struct {
|
||||
usernameTemplate *template.Template `yaml:"-"`
|
||||
}
|
||||
|
||||
func (b *bridge) setDefaults() error {
|
||||
func (b *bridge) validate() error {
|
||||
var err error
|
||||
|
||||
if b.UsernameTemplate == "" {
|
||||
@@ -40,14 +38,12 @@ func (b *bridge) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
|
||||
*b = bridge(raw)
|
||||
|
||||
return b.setDefaults()
|
||||
return b.validate()
|
||||
}
|
||||
|
||||
func (b bridge) FormatUsername(userid string) string {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
fmt.Printf("bridge: %#v\n", b)
|
||||
|
||||
b.usernameTemplate.Execute(&buffer, userid)
|
||||
|
||||
return buffer.String()
|
||||
|
||||
@@ -12,12 +12,16 @@ type Config struct {
|
||||
Bridge bridge `yaml:"bridge"`
|
||||
}
|
||||
|
||||
func (cfg *Config) setDefaults() error {
|
||||
if err := cfg.Appservice.setDefaults(); err != nil {
|
||||
func (cfg *Config) validate() error {
|
||||
if err := cfg.Homeserver.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cfg.Bridge.setDefaults(); err != nil {
|
||||
if err := cfg.Appservice.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cfg.Bridge.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -32,7 +36,7 @@ func (cfg *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return cfg.setDefaults()
|
||||
return cfg.validate()
|
||||
}
|
||||
|
||||
func FromBytes(data []byte) (*Config, error) {
|
||||
@@ -42,7 +46,9 @@ func FromBytes(data []byte) (*Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg.setDefaults()
|
||||
if err := cfg.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHomeserverNoAddress = errors.New("no homeserver address specified")
|
||||
ErrHomeserverNoDomain = errors.New("no homeserver domain specified")
|
||||
)
|
||||
|
||||
type homeserver struct {
|
||||
Address string `yaml:"address"`
|
||||
Domain string `yaml:"domain"`
|
||||
StatusEndpoint string `yaml:"status_endpoint"`
|
||||
}
|
||||
|
||||
func (h *homeserver) validate() error {
|
||||
if h.Address == "" {
|
||||
return ErrHomeserverNoAddress
|
||||
}
|
||||
|
||||
if h.Domain == "" {
|
||||
return ErrHomeserverNoDomain
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *homeserver) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
type rawHomeserver homeserver
|
||||
|
||||
raw := rawHomeserver{}
|
||||
if err := unmarshal(&raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*h = homeserver(raw)
|
||||
|
||||
return h.validate()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user