Skip to content

Commit

Permalink
add generic ValidateConfig function
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Oct 4, 2024
1 parent cfa71ea commit 00356b5
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 89 deletions.
8 changes: 8 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math"
"strings"
"time"

"github.com/go-playground/validator/v10"
)

// BytesScanner allows to scan for subslices separated by separator.
Expand Down Expand Up @@ -250,3 +252,9 @@ func MergeToSorted[T Comparable](arr1, arr2 []T) ([]T, error) {

return merged, nil
}

// ValidateConfig is a generic function needed to validate the config structure using validator.
func ValidateConfig(cfg any) error {
validator := validator.New()
return validator.Struct(cfg)
}
56 changes: 51 additions & 5 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,25 +290,25 @@ func TestMergeToSorted(t *testing.T) {
})

Convey("Test with one nil array", func() {
merged, err := MergeToSorted[myInt](nil, []myInt{1, 2, 3})
merged, err := MergeToSorted(nil, []myInt{1, 2, 3})
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{1, 2, 3})
})

Convey("Test with two arrays", func() {
merged, err := MergeToSorted[myInt]([]myInt{4, 5}, []myInt{1, 2, 3})
merged, err := MergeToSorted([]myInt{4, 5}, []myInt{1, 2, 3})
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{1, 2, 3, 4, 5})
})

Convey("Test with empty array", func() {
merged, err := MergeToSorted[myInt]([]myInt{-4, 5}, []myInt{})
merged, err := MergeToSorted([]myInt{-4, 5}, []myInt{})
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{-4, 5})
})

Convey("Test with sorted values but mixed up", func() {
merged, err := MergeToSorted[myInt]([]myInt{1, 9, 10}, []myInt{4, 8, 12})
merged, err := MergeToSorted([]myInt{1, 9, 10}, []myInt{4, 8, 12})
So(err, ShouldBeNil)
So(merged, ShouldResemble, []myInt{1, 4, 8, 9, 10, 12})
})
Expand All @@ -333,9 +333,55 @@ func TestMergeToSorted(t *testing.T) {
}

expected := append(arr2, arr1...)
merged, err := MergeToSorted[myTest](arr1, arr2)
merged, err := MergeToSorted(arr1, arr2)
So(err, ShouldBeNil)
So(merged, ShouldResemble, expected)
})
})
}

func TestValidateConfig(t *testing.T) {
type ValidationStruct struct {
TestInt int `validate:"required,gt=0"`
TestURL string `validate:"required,url"`
TestBool bool
}

const (
validURL = "https://github.com/moira-alert/moira"
validInt = 1
)

Convey("Test ValidateConfig", t, func() {
Convey("With TestInt less than zero", func() {
testStruct := ValidationStruct{
TestInt: -1,
TestURL: validURL,
}

err := ValidateConfig(testStruct)
So(err, ShouldNotBeNil)
})

Convey("With invalid TestURL format", func() {
testStruct := ValidationStruct{
TestInt: validInt,
TestURL: "test",
TestBool: true,
}

err := ValidateConfig(testStruct)
So(err, ShouldNotBeNil)
})

Convey("With valid structure", func() {
testStruct := ValidationStruct{
TestInt: validInt,
TestURL: validURL,
}

err := ValidateConfig(testStruct)
So(err, ShouldBeNil)
})
})
}
8 changes: 1 addition & 7 deletions senders/discord/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/bwmarrin/discordgo"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/worker"
Expand All @@ -25,11 +24,6 @@ type config struct {
FrontURI string `mapstructure:"front_uri"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender implements moira sender interface for discord.
type Sender struct {
DataBase moira.Database
Expand All @@ -48,7 +42,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to discord config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("discord config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"time"

"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/moira-alert/moira"
)
Expand All @@ -26,11 +25,6 @@ type config struct {
TemplateFile string `mapstructure:"template_file"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender implements moira sender interface via pushover.
type Sender struct {
From string
Expand Down Expand Up @@ -70,7 +64,7 @@ func (sender *Sender) fillSettings(senderSettings interface{}, logger moira.Logg
return fmt.Errorf("failed to decode senderSettings to mail config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("mail config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/mattermost/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"time"

"github.com/go-playground/validator/v10"
"github.com/moira-alert/moira/senders/msgformat"

"github.com/moira-alert/moira"
Expand All @@ -28,11 +27,6 @@ type config struct {
EmojiMap map[string]string `mapstructure:"emoji_map"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender posts messages to Mattermost chat.
// It implements moira.Sender.
// You must call Init method before SendEvents method.
Expand All @@ -59,7 +53,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to mattermost config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("mattermost config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/msteams/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"time"

"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/moira-alert/moira"
"github.com/russross/blackfriday/v2"
Expand Down Expand Up @@ -50,11 +49,6 @@ type config struct {
MaxEvents int `mapstructure:"max_events"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender implements moira sender interface via MS Teams.
type Sender struct {
frontURI string
Expand All @@ -72,7 +66,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to msteams config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("msteams config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/opsgenie/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"time"

"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/senders"
Expand All @@ -17,11 +16,6 @@ type config struct {
APIKey string `mapstructure:"api_key" validate:"required"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender implements the Sender interface for opsgenie.
type Sender struct {
apiKey string
Expand All @@ -43,7 +37,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to opsgenie config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("opsgenie config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/pushover/pushover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"time"

"github.com/go-playground/validator/v10"
"github.com/moira-alert/moira"

pushover_client "github.com/gregdel/pushover"
Expand All @@ -24,11 +23,6 @@ type config struct {
FrontURI string `mapstructure:"front_uri"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender implements moira sender interface via pushover.
type Sender struct {
logger moira.Logger
Expand All @@ -47,7 +41,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to pushover config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("pushover config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/moira-alert/moira"
)
Expand All @@ -19,11 +18,6 @@ type config struct {
Exec string `mapstructure:"exec" validate:"required"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender implements moira sender interface via script execution.
type Sender struct {
exec string
Expand All @@ -46,7 +40,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to script config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("script config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"time"

"github.com/go-playground/validator/v10"
"github.com/moira-alert/moira/senders/msgformat"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -39,11 +38,6 @@ type config struct {
EmojiMap map[string]string `mapstructure:"emoji_map"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender implements moira sender interface via slack.
type Sender struct {
emojiProvider emoji_provider.StateEmojiGetter
Expand All @@ -60,7 +54,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to slack config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("slack config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/telegram/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"time"

"github.com/go-playground/validator/v10"
"github.com/moira-alert/moira/senders/msgformat"

"github.com/mitchellh/mapstructure"
Expand All @@ -32,11 +31,6 @@ type config struct {
FrontURI string `mapstructure:"front_uri"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Bot is abstraction over gopkg.in/telebot.v3#Bot.
type Bot interface {
Handle(endpoint interface{}, h telebot.HandlerFunc, m ...telebot.MiddlewareFunc)
Expand Down Expand Up @@ -72,7 +66,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to telegram config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("telegram config validation error: %w", err)
}

Expand Down
8 changes: 1 addition & 7 deletions senders/twilio/twilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

twilio_client "github.com/carlosdp/twiliogo"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/moira-alert/moira"
)
Expand All @@ -21,11 +20,6 @@ type config struct {
AppendMessage bool `mapstructure:"append_message"`
}

func (cfg config) validate() error {
validator := validator.New()
return validator.Struct(cfg)
}

// Sender implements moira sender interface via twilio.
type Sender struct {
sender sendEventsTwilio
Expand All @@ -50,7 +44,7 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
return fmt.Errorf("failed to decode senderSettings to twilio config: %w", err)
}

if err = cfg.validate(); err != nil {
if err = moira.ValidateConfig(cfg); err != nil {
return fmt.Errorf("twilio config validation error: %w", err)
}

Expand Down
Loading

0 comments on commit 00356b5

Please sign in to comment.