Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b6d3753

Browse files
committed
Add Int() and fix tests
1 parent 0be2910 commit b6d3753

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

config/config.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,27 @@ func (c *Config) Service(service string) *Service {
137137
}
138138

139139
// String returns the string key of the Service
140-
func (s *Service) String(str string) (string, error) {
140+
func (s *Service) String(key string) (string, error) {
141141
if s.config == nil {
142-
return "", fmt.Errorf("missing config")
142+
return "", ErrMissingConfig
143143
}
144-
out, ok := s.config.Get(str).(string)
144+
out, ok := s.config.Get(key).(string)
145145
if ok {
146146
return out, nil
147147
}
148-
return "", fmt.Errorf("not found")
148+
return "", ErrNotFound
149+
}
150+
151+
// Int returns the int key of the Service
152+
func (s *Service) Int(key string) (int, error) {
153+
if s.config == nil {
154+
return 0, ErrMissingConfig
155+
}
156+
out, ok := s.config.Get(key).(int)
157+
if ok {
158+
return out, nil
159+
}
160+
return 0, ErrNotFound
149161
}
150162

151163
// Available returns true if the Service exists and has data

config/config_test.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,38 @@ import (
99
)
1010

1111
func TestNew(t *testing.T) {
12-
config, err := config.New()
12+
c, err := config.New()
1313
if !assert.Nil(t, err) {
1414
return
1515
}
16-
if !assert.NotNil(t, config) {
16+
if !assert.NotNil(t, c) {
1717
return
1818
}
1919

20-
iamService := config.
20+
iamService := c.
2121
Region("us-east").
2222
Env("client-test").
2323
Service("iam")
2424
if !assert.NotNil(t, iamService) {
2525
return
2626
}
27-
url, err := iamService.String("url")
27+
url, err := iamService.String("iam_url")
2828
if !assert.Nil(t, err) {
2929
return
3030
}
3131
assert.Equal(t, "https://iam-client-test.us-east.philips-healthsuite.com", url)
3232
}
3333

3434
func TestCartel(t *testing.T) {
35-
config, err := config.New()
35+
c, err := config.New()
3636
if !assert.Nil(t, err) {
3737
return
3838
}
39-
if !assert.NotNil(t, config) {
39+
if !assert.NotNil(t, c) {
4040
return
4141
}
4242

43-
cartelService := config.
43+
cartelService := c.
4444
Region("us-east").
4545
Service("cartel")
4646
if !assert.NotNil(t, cartelService) {
@@ -81,14 +81,14 @@ func TestOpts(t *testing.T) {
8181
}
8282

8383
func TestMissing(t *testing.T) {
84-
config, err := config.New()
84+
c, err := config.New()
8585
if !assert.Nil(t, err) {
8686
return
8787
}
88-
if !assert.NotNil(t, config) {
88+
if !assert.NotNil(t, c) {
8989
return
9090
}
91-
missingService := config.
91+
missingService := c.
9292
Region("us-east").
9393
Service("bogus")
9494
assert.False(t, missingService.Available())
@@ -97,33 +97,36 @@ func TestMissing(t *testing.T) {
9797
}
9898

9999
func TestServices(t *testing.T) {
100-
config, err := config.New(
100+
c, err := config.New(
101101
config.WithRegion("us-east"),
102102
config.WithEnv("client-test"))
103103
if !assert.Nil(t, err) {
104104
return
105105
}
106-
if !assert.NotNil(t, config) {
106+
if !assert.NotNil(t, c) {
107107
return
108108
}
109-
services := config.Services()
109+
services := c.Services()
110110
assert.Less(t, 0, len(services))
111111
}
112112

113113
func TestKeys(t *testing.T) {
114-
config, err := config.New(
114+
c, err := config.New(
115115
config.WithRegion("us-east"),
116116
config.WithEnv("client-test"))
117117
if !assert.Nil(t, err) {
118118
return
119119
}
120-
if !assert.NotNil(t, config) {
120+
if !assert.NotNil(t, c) {
121121
return
122122
}
123-
cartel := config.Service("cartel")
123+
cartel := c.Service("cartel")
124124
assert.True(t, cartel.Available())
125125
keys := cartel.Keys()
126126
assert.Less(t, 0, len(keys))
127127
_, err = cartel.String("bogus")
128128
assert.NotNil(t, err)
129+
port, err := cartel.Int("port")
130+
assert.Equal(t, config.ErrNotFound, err)
131+
assert.Equal(t, 0, port)
129132
}

config/errors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
import (
4+
errors "golang.org/x/xerrors"
5+
)
6+
7+
var (
8+
ErrMissingConfig = errors.New("missing config")
9+
ErrNotFound = errors.New("not found")
10+
)

0 commit comments

Comments
 (0)