-
Notifications
You must be signed in to change notification settings - Fork 50
/
naming.go
65 lines (52 loc) · 1.47 KB
/
naming.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package sns
import (
"fmt"
"strings"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/cloud/aws"
)
type TopicNameSettingsAware interface {
GetAppId() cfg.AppId
GetClientName() string
GetTopicId() string
}
type TopicNameSettings struct {
AppId cfg.AppId
ClientName string
TopicId string
}
func (s TopicNameSettings) GetAppId() cfg.AppId {
return s.AppId
}
func (s TopicNameSettings) GetClientName() string {
return s.ClientName
}
func (s TopicNameSettings) GetTopicId() string {
return s.TopicId
}
type TopicNamingSettings struct {
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-{group}-{topicId}"`
}
func GetTopicName(config cfg.Config, topicSettings TopicNameSettingsAware) (string, error) {
if len(topicSettings.GetClientName()) == 0 {
return "", fmt.Errorf("the client name shouldn't be empty")
}
namingKey := fmt.Sprintf("%s.naming", aws.GetClientConfigKey("sns", topicSettings.GetClientName()))
namingSettings := &TopicNamingSettings{}
config.UnmarshalKey(namingKey, namingSettings)
name := namingSettings.Pattern
appId := topicSettings.GetAppId()
values := map[string]string{
"project": appId.Project,
"env": appId.Environment,
"family": appId.Family,
"group": appId.Group,
"app": appId.Application,
"topicId": topicSettings.GetTopicId(),
}
for key, val := range values {
templ := fmt.Sprintf("{%s}", key)
name = strings.ReplaceAll(name, templ, val)
}
return name, nil
}