Skip to content

Commit c3641e2

Browse files
committed
refactor: Address Maintainability
Replacing the magic strings with constants instead
1 parent 46db4e6 commit c3641e2

6 files changed

+77
-50
lines changed

cmd/configuration.go

+46-31
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ import (
1212
"github.com/spf13/viper"
1313
)
1414

15+
const (
16+
env_user = "D2_TARGET_DB_USER"
17+
env_password = "D2_TARGET_DB_PASSWORD"
18+
env_host = "D2_TARGET_DB_HOST"
19+
env_port = "D2_TARGET_DB_PORT"
20+
env_name = "D2_TARGET_DB_NAME"
21+
env_type = "D2_TARGET_DB_TYPE"
22+
env_links = "VIRTUAL_LINKS"
23+
env_links_path = "VIRTUAL_LINKS_PATH"
24+
env_groups = "TABLE_GROUPS"
25+
env_groups_path = "TABLE_GROUPS_PATH"
26+
env_restriction = "RESTRICTOR_TYPE"
27+
env_perspective_user = "DESIGNATED_USER"
28+
)
29+
1530
// get_virtual_links returns the virtual links for the program.
1631
// These are currently set by a file specified by the VIRTUAL_LINKS_PATH environment variable.
1732
// The file should be a json array of virtual links. See the virtual package for more information.
1833
func get_virtual_links() []virtual.VirtualLink {
19-
links_reader, err := os.Open(viper.GetString("VIRTUAL_LINKS_PATH"))
34+
links_reader, err := os.Open(viper.GetString(env_links_path))
2035
if err != nil {
2136
//TODO: Log error, or bubble up instead of printing to console
2237
fmt.Println("Failed to open virtual links file")
@@ -43,7 +58,7 @@ func read_virtual_links(_input io.Reader) ([]virtual.VirtualLink, error) {
4358
// These are currently set by a file specified by the TABLE_GROUPS_PATH environment variable.
4459
// The file should be a json array of table groups. See the core package for more information.
4560
func get_table_groups() []core.TableGroup {
46-
table_groups_reader, err := os.Open(viper.GetString("TABLE_GROUPS_PATH"))
61+
table_groups_reader, err := os.Open(viper.GetString(env_groups_path))
4762
if err != nil {
4863
//TODO: Log error, or bubble up instead of printing to console
4964
fmt.Println("Failed to read table groups file")
@@ -70,7 +85,7 @@ func read_table_groups(_input io.Reader) ([]core.TableGroup, error) {
7085
// get_designated_user returns the designated user for the program. Set by the DESIGNATED_USER environment variable.
7186
// This is used to restrict the schema to the tables that the designated user has access to. See the mysql package for more information.
7287
func get_designated_user() string {
73-
return viper.GetString("DESIGNATED_USER")
88+
return viper.GetString(env_perspective_user)
7489
}
7590

7691
// options is a struct that contains the options for the program.
@@ -98,10 +113,10 @@ func get_options() options {
98113
register_environent_variables()
99114
//TODO: Add validation for options
100115
return options{
101-
use_virtual_links: viper.GetBool("VIRTUAL_LINKS"),
102-
use_table_groups: viper.GetBool("TABLE_GROUPS"),
103-
restrictor_type: viper.GetString("RESTRICTOR_TYPE"),
104-
db_source_type: viper.GetString("D2_TARGET_DB_TYPE"),
116+
use_virtual_links: viper.GetBool(env_links),
117+
use_table_groups: viper.GetBool(env_groups),
118+
restrictor_type: viper.GetString(env_restriction),
119+
db_source_type: viper.GetString(env_type),
105120
}
106121
}
107122

@@ -122,62 +137,62 @@ func register_commandline_flags() {
122137

123138
pflag.Parse()
124139
if *virtual_links != "" {
125-
viper.RegisterAlias("VIRTUAL_LINKS", "VirtualLinks")
140+
viper.RegisterAlias(env_links, "VirtualLinks")
126141
}
127142
if *virtual_links_path != "" {
128-
viper.RegisterAlias("VIRTUAL_LINKS_PATH", "VirtualLinksPath")
143+
viper.RegisterAlias(env_links_path, "VirtualLinksPath")
129144
}
130145
if *table_groups != "" {
131-
viper.RegisterAlias("TABLE_GROUPS", "TableGroups")
146+
viper.RegisterAlias(env_groups, "TableGroups")
132147
}
133148

134149
if *table_groups_path != "" {
135-
viper.RegisterAlias("TABLE_GROUPS_PATH", "TableGroupsPath")
150+
viper.RegisterAlias(env_groups_path, "TableGroupsPath")
136151
}
137152
if *restrictor_type != "" {
138-
viper.RegisterAlias("RESTRICTOR_TYPE", "RestrictorType")
153+
viper.RegisterAlias(env_restriction, "RestrictorType")
139154
}
140155
if *db_user != "" {
141-
viper.RegisterAlias("D2_TARGET_DB_USER", "D2TargetDbUser")
156+
viper.RegisterAlias(env_user, "D2TargetDbUser")
142157
}
143158
if *db_password != "" {
144-
viper.RegisterAlias("D2_TARGET_DB_PASSWORD", "D2TargetDbPassword")
159+
viper.RegisterAlias(env_password, "D2TargetDbPassword")
145160
}
146161
if *db_host != "" {
147-
viper.RegisterAlias("D2_TARGET_DB_HOST", "D2TargetDbHost")
162+
viper.RegisterAlias(env_host, "D2TargetDbHost")
148163
}
149164

150165
if *db_port != "" {
151-
viper.RegisterAlias("D2_TARGET_DB_PORT", "D2TargetDbPort")
166+
viper.RegisterAlias(env_port, "D2TargetDbPort")
152167
}
153168
if *db_name != "" {
154-
viper.RegisterAlias("D2_TARGET_DB_NAME", "D2TargetDbName")
169+
viper.RegisterAlias(env_name, "D2TargetDbName")
155170
}
156171
if *db_type != "" {
157-
viper.RegisterAlias("D2_TARGET_DB_TYPE", "D2TargetDbType")
172+
viper.RegisterAlias(env_type, "D2TargetDbType")
158173
}
159174
if *db_designated_user != "" {
160-
viper.RegisterAlias("DESIGNATED_USER", "DesignatedUser")
175+
viper.RegisterAlias(env_perspective_user, "DesignatedUser")
161176
}
162177
viper.BindPFlags(pflag.CommandLine)
163178
}
164179

165180
func register_environent_variables() {
166-
viper.BindEnv("D2_TARGET_DB_USER")
167-
viper.BindEnv("D2_TARGET_DB_PASSWORD")
168-
viper.BindEnv("D2_TARGET_DB_HOST")
169-
viper.BindEnv("D2_TARGET_DB_PORT")
170-
viper.BindEnv("D2_TARGET_DB_NAME")
171-
viper.BindEnv("D2_TARGET_DB_TYPE")
181+
viper.BindEnv(env_user)
182+
viper.BindEnv(env_password)
183+
viper.BindEnv(env_host)
184+
viper.BindEnv(env_port)
185+
viper.BindEnv(env_name)
186+
viper.BindEnv(env_type)
172187

173-
viper.BindEnv("VIRTUAL_LINKS")
174-
viper.BindEnv("VIRTUAL_LINKS_PATH")
188+
viper.BindEnv(env_links)
189+
viper.BindEnv(env_links_path)
175190

176-
viper.BindEnv("TABLE_GROUPS")
177-
viper.BindEnv("TABLE_GROUPS_PATH")
191+
viper.BindEnv(env_groups)
192+
viper.BindEnv(env_groups_path)
178193

179-
viper.BindEnv("RESTRICTOR_TYPE")
194+
viper.BindEnv(env_restriction)
180195

181-
viper.BindEnv("DESIGNATED_USER")
196+
viper.BindEnv(env_perspective_user)
182197

183198
}

cmd/configuration_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import (
88
)
99

1010
func TestGet_virtual_links(t *testing.T) {
11-
os.Setenv("VIRTUAL_LINKS_PATH", "test")
12-
viper.BindEnv("VIRTUAL_LINKS_PATH", "VIRTUAL_LINKS_PATH")
13-
if viper.Get("VIRTUAL_LINKS_PATH") != "test" {
11+
os.Setenv(env_links_path, "test")
12+
viper.BindEnv(env_links_path)
13+
if viper.Get(env_links_path) != "test" {
1414
t.Log("Failed to set VIRTUAL_LINKS_PATH")
1515
t.Fail()
1616
}
1717
}
1818

1919
func Test_boolean_flags(t *testing.T) {
20-
os.Setenv("VIRTUAL_LINKS", "true")
21-
viper.BindEnv("VIRTUAL_LINKS", "VIRTUAL_LINKS")
22-
if !viper.GetBool("VIRTUAL_LINKS") {
20+
os.Setenv(env_links, "true")
21+
viper.BindEnv(env_links)
22+
if !viper.GetBool(env_links) {
2323
t.Log("Failed to set VIRTUAL_LINKS")
2424
t.Fail()
2525
}

pkg/core/d2_generator.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"strings"
66
)
77

8+
const Virtual_key string = "VIRTUAL"
9+
810
// Converts a schema to a string containing corresponding d2 definitions.
911
func Schema_to_d2(schema Schema, _groups []TableGroup) string {
1012
var builder strings.Builder
@@ -55,7 +57,7 @@ func Schema_to_d2(schema Schema, _groups []TableGroup) string {
5557
// Only this arrangement allows for multiple virtual connections from a common reference
5658
// point without inverting all the original foreign keys from the information schema.
5759
// This should be rectified in a re-write of the internals of the mysql or virtual package
58-
if column.Key == "VIRTUAL" {
60+
if column.Key == Virtual_key {
5961
builder.WriteString(fmt.Sprintf(
6062
"%s.%s -> %s.%s",
6163
wrap_name_in_group(column.Reference.Table, _groups),
@@ -72,7 +74,7 @@ func Schema_to_d2(schema Schema, _groups []TableGroup) string {
7274
}
7375
builder.WriteString(" {\n")
7476
builder.WriteString(" target-arrowhead: {shape: cf-many}\n")
75-
if column.Key == "VIRTUAL" {
77+
if column.Key == Virtual_key {
7678
builder.WriteString(" style: {stroke-dash: 3}")
7779
}
7880
builder.WriteString("}")
@@ -100,7 +102,7 @@ func table_to_d2(_table Table) string {
100102
builder.WriteString(" {constraint: foreign_key}")
101103
case "UNK":
102104
builder.WriteString(" {constraint: unique}")
103-
case "VIRTUAL":
105+
case Virtual_key:
104106
builder.WriteString(" {constraint: foreign_key}")
105107
}
106108

pkg/mysql/schema_extract.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import (
99
"github.com/spf13/viper"
1010
)
1111

12+
// TODO - Merge this definition with the one in Configuration (Likely needs a config module)
13+
14+
const (
15+
env_user = "D2_TARGET_DB_USER"
16+
env_password = "D2_TARGET_DB_PASSWORD"
17+
env_host = "D2_TARGET_DB_HOST"
18+
env_port = "D2_TARGET_DB_PORT"
19+
env_name = "D2_TARGET_DB_NAME"
20+
)
21+
1222
// connect_to_db connects to the database specified by the environment variables
1323
// - D2_TARGET_DB_USER,
1424
// - D2_TARGET_DB_PASSWORD,
@@ -18,11 +28,11 @@ import (
1828
// The database must be a MySQL database.
1929
// Returns a pointer to the database and an error if the connection failed.
2030
func connect_to_db() (*sql.DB, error) {
21-
user := viper.GetString("D2_TARGET_DB_USER")
22-
password := viper.GetString("D2_TARGET_DB_PASSWORD")
23-
host := viper.GetString("D2_TARGET_DB_HOST")
24-
port := viper.GetString("D2_TARGET_DB_PORT")
25-
dbname := viper.GetString("D2_TARGET_DB_NAME")
31+
user := viper.GetString(env_user)
32+
password := viper.GetString(env_password)
33+
host := viper.GetString(env_host)
34+
port := viper.GetString(env_port)
35+
dbname := viper.GetString(env_name)
2636
essential_vars := []string{user, password, host, port, dbname}
2737
for _, v := range essential_vars {
2838
if v == "" {
@@ -131,7 +141,7 @@ func Extract_schema() core.Schema {
131141
defer db.Close()
132142

133143
// Retrieve the schema from the database
134-
rows := information_schema_from(db, viper.GetString("D2_TARGET_DB_NAME"))
144+
rows := information_schema_from(db, viper.GetString(env_name))
135145

136146
// Build the data structure
137147
schema := structured_schema_from(rows)

pkg/virtual/virtual_connections.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func augment_columns_source(_table core.Table, _links VirtualLink) core.Table {
5252
Table: _links.ReferencedTable,
5353
Column: _links.ReferencedColumn,
5454
}
55-
column.Key = "VIRTUAL"
55+
column.Key = core.Virtual_key
5656
column.Extra = "Virtual link to " + _links.ReferencedTable + "." + _links.ReferencedColumn
5757
}
5858
new_columns = append(new_columns, column)
@@ -65,7 +65,7 @@ func augment_columns_reference(_table core.Table, _links VirtualLink) core.Table
6565
new_columns := []core.Column{}
6666
for _, column := range _table.Columns {
6767
if column.Name == _links.ReferencedColumn {
68-
column.Key = "VIRTUAL"
68+
column.Key = core.Virtual_key
6969
column.Extra = "Virtual link from " + _links.SourceTable + "." + _links.SourceColumn
7070
}
7171
new_columns = append(new_columns, column)

pkg/virtual/virtual_connections_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestAugment_schema(t *testing.T) {
5050
Columns: []core.Column{
5151
{
5252
Name: "col1",
53-
Key: "VIRTUAL",
53+
Key: core.Virtual_key,
5454
Reference: &core.Reference{
5555
Table: "table2",
5656
Column: "col1",
@@ -67,7 +67,7 @@ func TestAugment_schema(t *testing.T) {
6767
Columns: []core.Column{
6868
{
6969
Name: "col1",
70-
Key: "VIRTUAL",
70+
Key: core.Virtual_key,
7171
Extra: "Virtual link from table1.col1",
7272
},
7373
{

0 commit comments

Comments
 (0)