Skip to content

Commit 0cba1f6

Browse files
committed
feat(config): Added app description config key
1 parent c61f160 commit 0cba1f6

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

config/README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ For the following examples, we will be considering those configuration files:
5151
# ./configs/config.yaml
5252
app:
5353
name: app
54+
description: app description
5455
env: dev
5556
version: 0.1.0
5657
debug: false
@@ -109,10 +110,11 @@ func main() {
109110
cfg, _ := config.NewDefaultConfigFactory().Create()
110111
111112
// helpers
112-
fmt.Printf("name: %s", cfg.AppName()) // name: app
113-
fmt.Printf("env: %s", cfg.AppEnv()) // env: dev
114-
fmt.Printf("version: %s", cfg.AppVersion()) // version: 0.1.0
115-
fmt.Printf("debug: %v", cfg.AppDebug()) // debug: false
113+
fmt.Printf("name: %s", cfg.AppName()) // name: app
114+
fmt.Printf("description: %s", cfg.AppDescription()) // description: app description
115+
fmt.Printf("env: %s", cfg.AppEnv()) // env: dev
116+
fmt.Printf("version: %s", cfg.AppVersion()) // version: 0.1.0
117+
fmt.Printf("debug: %v", cfg.AppDebug()) // debug: false
116118
117119
// others
118120
fmt.Printf("string_value: %s", cfg.GetString("config.values.string_value")) // string_value: default
@@ -146,11 +148,12 @@ func main() {
146148
cfg, _ := config.NewDefaultConfigFactory().Create()
147149
148150
// helpers
149-
fmt.Printf("var: %s", cfg.GetEnvVar("APP_ENV")) // var: test
150-
fmt.Printf("name: %s", cfg.AppName()) // name: app
151-
fmt.Printf("env: %s", cfg.AppEnv()) // env: test
152-
fmt.Printf("version: %s", cfg.AppVersion()) // version: 0.1.0
153-
fmt.Printf("debug: %v", cfg.AppDebug()) // debug: true
151+
fmt.Printf("var: %s", cfg.GetEnvVar("APP_ENV")) // var: test
152+
fmt.Printf("name: %s", cfg.AppName()) // name: app
153+
fmt.Printf("description: %s", cfg.AppDescription()) // description: app description
154+
fmt.Printf("env: %s", cfg.AppEnv()) // env: test
155+
fmt.Printf("version: %s", cfg.AppVersion()) // version: 0.1.0
156+
fmt.Printf("debug: %v", cfg.AppDebug()) // debug: true
154157
155158
// others
156159
fmt.Printf("string_value: %s", cfg.GetString("config.values.string_value")) // string_value: test

config/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func (c *Config) AppName() string {
3232
return c.GetString("app.name")
3333
}
3434

35+
// AppDescription returns the configured application description (from config field app.description or env var APP_DESCRIPTION).
36+
func (c *Config) AppDescription() string {
37+
return c.GetString("app.description")
38+
}
39+
3540
// AppEnv returns the configured application environment (from config field app.env or env var APP_ENV).
3641
func (c *Config) AppEnv() string {
3742
return c.GetString("app.env")

config/config_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,31 @@ func TestAppNameOverrideFromCustomEnvConfig(t *testing.T) {
4141
assert.Equal(t, "custom-app", cfg.AppName())
4242
}
4343

44+
func TestAppDescriptionFromDefaultConfig(t *testing.T) {
45+
cfg, err := createTestConfig()
46+
47+
assert.Nil(t, err)
48+
assert.Equal(t, "default-description", cfg.AppDescription())
49+
}
50+
51+
func TestAppDescriptionOverrideFromTestEnvConfig(t *testing.T) {
52+
t.Setenv("APP_ENV", "test")
53+
54+
cfg, err := createTestConfig()
55+
56+
assert.NoError(t, err)
57+
assert.Equal(t, "test-description", cfg.AppDescription())
58+
}
59+
60+
func TestAppDescriptionOverrideFromCustomEnvConfig(t *testing.T) {
61+
t.Setenv("APP_ENV", "custom")
62+
63+
cfg, err := createTestConfig()
64+
65+
assert.NoError(t, err)
66+
assert.Equal(t, "custom-description", cfg.AppDescription())
67+
}
68+
4469
func TestAppNameOverrideFromInvalidEnvConfig(t *testing.T) {
4570
t.Setenv("APP_ENV", "invalid")
4671

config/testdata/config/valid/config.custom.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
app:
22
name: custom-app
3+
description: custom-description
34
env: custom
45
debug: true
56
config:

config/testdata/config/valid/config.test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
app:
22
name: test-app
3+
description: test-description
34
env: test
45
debug: true
56
config:

config/testdata/config/valid/config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
app:
22
name: default-app
3+
description: default-description
34
env: dev
45
version: 0.1.0
56
debug: false

0 commit comments

Comments
 (0)