-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor reporters configuration (#47)
* feat: refactor reporters configuration * feat: validate reporters and chains names uniqueness * chore: pass reporter name to Telegram * chore: rename TelegramReporter -> Reporter * chore: pass type to reporter
- Loading branch information
1 parent
1629d04
commit 901a1fa
Showing
17 changed files
with
275 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,4 @@ linters: | |
- deadcode | ||
- depguard | ||
- interfacebloat | ||
- perfsprint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package toml_config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"main/pkg/config/types" | ||
"main/pkg/constants" | ||
"main/pkg/utils" | ||
"strings" | ||
) | ||
|
||
type TelegramConfig struct { | ||
Chat int64 `toml:"chat"` | ||
Token string `toml:"token"` | ||
Admins []int64 `toml:"admins"` | ||
} | ||
|
||
type Reporter struct { | ||
Name string `toml:"name"` | ||
Type string `default:"telegram" toml:"type"` | ||
|
||
TelegramConfig *TelegramConfig `toml:"telegram-config"` | ||
} | ||
|
||
func (reporter *Reporter) Validate() error { | ||
if reporter.Name == "" { | ||
return errors.New("reporter name not provided") | ||
} | ||
|
||
reporterTypes := constants.GetReporterTypes() | ||
if !utils.Contains(reporterTypes, reporter.Type) { | ||
return fmt.Errorf( | ||
"expected type to be one of %s, but got %s", | ||
strings.Join(reporterTypes, ", "), | ||
reporter.Type, | ||
) | ||
} | ||
|
||
if reporter.Type == constants.ReporterTypeTelegram && reporter.TelegramConfig == nil { | ||
return errors.New("missing telegram-config for Telegram reporter") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type Reporters []*Reporter | ||
|
||
func (reporters Reporters) Validate() error { | ||
for index, reporter := range reporters { | ||
if err := reporter.Validate(); err != nil { | ||
return fmt.Errorf("error in reporter %d: %s", index, err) | ||
} | ||
} | ||
|
||
// checking names uniqueness | ||
names := map[string]bool{} | ||
|
||
for _, reporter := range reporters { | ||
if _, ok := names[reporter.Name]; ok { | ||
return fmt.Errorf("duplicate reporter name: %s", reporter.Name) | ||
} | ||
|
||
names[reporter.Name] = true | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func FromAppConfigReporter(reporter *types.Reporter) *Reporter { | ||
var telegramConfig *TelegramConfig | ||
|
||
if reporter.TelegramConfig != nil { | ||
telegramConfig = &TelegramConfig{ | ||
Chat: reporter.TelegramConfig.Chat, | ||
Token: reporter.TelegramConfig.Token, | ||
Admins: reporter.TelegramConfig.Admins, | ||
} | ||
} | ||
|
||
return &Reporter{ | ||
Name: reporter.Name, | ||
Type: reporter.Type, | ||
TelegramConfig: telegramConfig, | ||
} | ||
} | ||
|
||
func (reporter *Reporter) ToAppConfigReporter() *types.Reporter { | ||
var telegramConfig *types.TelegramConfig | ||
|
||
if reporter.TelegramConfig != nil { | ||
telegramConfig = &types.TelegramConfig{ | ||
Chat: reporter.TelegramConfig.Chat, | ||
Token: reporter.TelegramConfig.Token, | ||
Admins: reporter.TelegramConfig.Admins, | ||
} | ||
} | ||
|
||
return &types.Reporter{ | ||
Name: reporter.Name, | ||
Type: reporter.Type, | ||
TelegramConfig: telegramConfig, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package types | ||
|
||
type TelegramConfig struct { | ||
Chat int64 | ||
Token string | ||
Admins []int64 | ||
} | ||
|
||
type Reporter struct { | ||
Name string | ||
Type string | ||
|
||
TelegramConfig *TelegramConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.