Skip to content

Commit

Permalink
♻️ refactor: updated codebase #9
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Oct 28, 2023
1 parent b83a741 commit 14d8ab2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 36 deletions.
75 changes: 44 additions & 31 deletions configx/configx.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,37 +330,6 @@ func CreateConfigWithComments[T any](path string, data CommentedConfig) error {
return nil
}

func _marshal(data interface{}, comments FieldCommentConfig) ([]byte, error) {
bytes, err := yaml.Marshal(data)
if err != nil {
return nil, err
}
lines := strings.Split(string(bytes), "\n")
for field, comment := range comments {
for i, line := range lines {
if strings.Contains(line, field+":") {
if strings.Contains(comment, "\n") {
commentLines := strings.Split(comment, "\n")
for j := len(commentLines) - 1; j >= 0; j-- {
commentLine := fmt.Sprintf("# %s", commentLines[j])
lines = insertStringAt(lines, i, commentLine)
}
} else {
commentLine := fmt.Sprintf("# %s", comment)
lines = insertStringAt(lines, i, commentLine)
}
break
}
}
}
c := strings.Join(lines, "\n")
return []byte(c), nil
}

func insertStringAt(slice []string, index int, value string) []string {
return append(slice[:index], append([]string{value}, slice[index:]...)...)
}

func (c *ClusterMultiTenancyKeysConfig) FindClusterBy(key string) (MultiTenancyKeysConfig, error) {
if len(c.Clusters) == 0 {
return *NewMultiTenantKeysConfig(), fmt.Errorf("No multi-tenant cluster")
Expand All @@ -379,6 +348,19 @@ func (c *ClusterMultiTenancyKeysConfig) FindClusterBy(key string) (MultiTenancyK
return *NewMultiTenantKeysConfig(), fmt.Errorf("The multi-tenant cluster not found")
}

func (c *ClusterMultiTenancyKeysConfig) AllowedUsableDefault() bool {
if len(c.Clusters) == 0 {
return true
}
counter := 0
for _, v := range c.Clusters {
if v.IsUsableDefault {
counter++
}
}
return counter <= 1
}

func (k *KeysConfig) SetTelegram(value telegram.TelegramConfig) *KeysConfig {
k.Telegram = value
return k
Expand Down Expand Up @@ -408,3 +390,34 @@ func (k *KeysConfig) SetCorsCursor(value *corsx.CorsConfig) *KeysConfig {
k.Cors = *value
return k
}

func _marshal(data interface{}, comments FieldCommentConfig) ([]byte, error) {
bytes, err := yaml.Marshal(data)
if err != nil {
return nil, err
}
lines := strings.Split(string(bytes), "\n")
for field, comment := range comments {
for i, line := range lines {
if strings.Contains(line, field+":") {
if strings.Contains(comment, "\n") {
commentLines := strings.Split(comment, "\n")
for j := len(commentLines) - 1; j >= 0; j-- {
commentLine := fmt.Sprintf("# %s", commentLines[j])
lines = insertStringAt(lines, i, commentLine)
}
} else {
commentLine := fmt.Sprintf("# %s", comment)
lines = insertStringAt(lines, i, commentLine)
}
break
}
}
}
c := strings.Join(lines, "\n")
return []byte(c), nil
}

func insertStringAt(slice []string, index int, value string) []string {
return append(slice[:index], append([]string{value}, slice[index:]...)...)
}
14 changes: 12 additions & 2 deletions mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ func (m *MysqlConfig) SetEnabled(value bool) *MysqlConfig {

func (m *MysqlConfig) SetDatabase(value string) *MysqlConfig {
if utils.IsEmpty(value) {
log.Panic("Invalid database")
log.Panicf("Database is required")
}
m.Database = utils.TrimSpaces(value)
return m
}

func (m *MysqlConfig) SetHost(value string) *MysqlConfig {
if utils.IsEmpty(value) {
log.Panicf("Host is required")
}
m.Host = utils.TrimSpaces(value)
return m
}
Expand Down Expand Up @@ -88,12 +91,19 @@ func (m *MysqlConfig) Json() string {
return utils.ToJson(m)
}

func (m *MysqlConfig) GetConnString() string {
MysqlConfigValidator(m)
hostname := fmt.Sprintf("%s:%d", m.Host, m.Port)
return fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", m.Username, m.Password, hostname, m.Database)
}

func MysqlConfigValidator(m *MysqlConfig) {
m.SetPort(m.Port).
SetDatabase(m.Database).
SetMaxOpenConn(m.MaxOpenConn).
SetMaxIdleConn(m.MaxIdleConn).
SetMaxLifeTimeMinutesConn(m.MaxLifeTimeMinutesConn)
SetMaxLifeTimeMinutesConn(m.MaxLifeTimeMinutesConn).
SetHost(m.Host)
}

func GetMysqlConfigSample() *MysqlConfig {
Expand Down
22 changes: 19 additions & 3 deletions postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ func (p *PostgresConfig) SetEnabled(value bool) *PostgresConfig {
}

func (p *PostgresConfig) SetDatabase(value string) *PostgresConfig {
if utils.IsEmpty(value) {
log.Panicf("Database is required")
}
p.Database = utils.TrimSpaces(value)
return p
}

func (p *PostgresConfig) SetHost(value string) *PostgresConfig {
if utils.IsEmpty(value) {
log.Panicf("Host is required")
}
p.Host = utils.TrimSpaces(value)
return p
}
Expand Down Expand Up @@ -57,15 +63,15 @@ func (p *PostgresConfig) SetSslMode(value string) *PostgresConfig {

func (p *PostgresConfig) SetMaxOpenConn(value int) *PostgresConfig {
if value <= 0 {
log.Panicf("Invalid max-open-conn")
log.Panicf("Invalid max-open-conn: %v", value)
}
p.MaxOpenConn = value
return p
}

func (p *PostgresConfig) SetMaxIdleConn(value int) *PostgresConfig {
if value <= 0 {
log.Panicf("Invalid max-idle-conn")
log.Panicf("Invalid max-idle-conn: %v", value)
}
p.MaxIdleConn = value
return p
Expand All @@ -85,10 +91,20 @@ func (p *PostgresConfig) Json() string {
return utils.ToJson(p)
}

func (p *PostgresConfig) GetConnString() string {
PostgresConfigValidator(p)
conn := fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s",
p.Host, p.Port, p.Username, p.Database, p.Password, p.SSLMode)
return conn
}

func PostgresConfigValidator(p *PostgresConfig) {
p.SetPort(p.Port).
SetMaxOpenConn(p.MaxOpenConn).
SetMaxIdleConn(p.MaxIdleConn)
SetMaxIdleConn(p.MaxIdleConn).
SetDatabase(p.Database).
SetHost(p.Host).
SetSslMode(p.SSLMode)
}

func GetPostgresConfigSample() *PostgresConfig {
Expand Down
1 change: 1 addition & 0 deletions resolvers/db_ resolver_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package resolvers
1 change: 1 addition & 0 deletions resolvers/db_ resolver_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package resolvers
1 change: 1 addition & 0 deletions resolvers/db_ resolver_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package resolvers
1 change: 1 addition & 0 deletions resolvers/db_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package resolvers

0 comments on commit 14d8ab2

Please sign in to comment.