Skip to content

Commit

Permalink
Add Int() and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loafoe committed Jul 1, 2020
1 parent 0be2910 commit b6d3753
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
20 changes: 16 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,27 @@ func (c *Config) Service(service string) *Service {
}

// String returns the string key of the Service
func (s *Service) String(str string) (string, error) {
func (s *Service) String(key string) (string, error) {
if s.config == nil {
return "", fmt.Errorf("missing config")
return "", ErrMissingConfig
}
out, ok := s.config.Get(str).(string)
out, ok := s.config.Get(key).(string)
if ok {
return out, nil
}
return "", fmt.Errorf("not found")
return "", ErrNotFound
}

// Int returns the int key of the Service
func (s *Service) Int(key string) (int, error) {
if s.config == nil {
return 0, ErrMissingConfig
}
out, ok := s.config.Get(key).(int)
if ok {
return out, nil
}
return 0, ErrNotFound
}

// Available returns true if the Service exists and has data
Expand Down
35 changes: 19 additions & 16 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ import (
)

func TestNew(t *testing.T) {
config, err := config.New()
c, err := config.New()
if !assert.Nil(t, err) {
return
}
if !assert.NotNil(t, config) {
if !assert.NotNil(t, c) {
return
}

iamService := config.
iamService := c.
Region("us-east").
Env("client-test").
Service("iam")
if !assert.NotNil(t, iamService) {
return
}
url, err := iamService.String("url")
url, err := iamService.String("iam_url")
if !assert.Nil(t, err) {
return
}
assert.Equal(t, "https://iam-client-test.us-east.philips-healthsuite.com", url)
}

func TestCartel(t *testing.T) {
config, err := config.New()
c, err := config.New()
if !assert.Nil(t, err) {
return
}
if !assert.NotNil(t, config) {
if !assert.NotNil(t, c) {
return
}

cartelService := config.
cartelService := c.
Region("us-east").
Service("cartel")
if !assert.NotNil(t, cartelService) {
Expand Down Expand Up @@ -81,14 +81,14 @@ func TestOpts(t *testing.T) {
}

func TestMissing(t *testing.T) {
config, err := config.New()
c, err := config.New()
if !assert.Nil(t, err) {
return
}
if !assert.NotNil(t, config) {
if !assert.NotNil(t, c) {
return
}
missingService := config.
missingService := c.
Region("us-east").
Service("bogus")
assert.False(t, missingService.Available())
Expand All @@ -97,33 +97,36 @@ func TestMissing(t *testing.T) {
}

func TestServices(t *testing.T) {
config, err := config.New(
c, err := config.New(
config.WithRegion("us-east"),
config.WithEnv("client-test"))
if !assert.Nil(t, err) {
return
}
if !assert.NotNil(t, config) {
if !assert.NotNil(t, c) {
return
}
services := config.Services()
services := c.Services()
assert.Less(t, 0, len(services))
}

func TestKeys(t *testing.T) {
config, err := config.New(
c, err := config.New(
config.WithRegion("us-east"),
config.WithEnv("client-test"))
if !assert.Nil(t, err) {
return
}
if !assert.NotNil(t, config) {
if !assert.NotNil(t, c) {
return
}
cartel := config.Service("cartel")
cartel := c.Service("cartel")
assert.True(t, cartel.Available())
keys := cartel.Keys()
assert.Less(t, 0, len(keys))
_, err = cartel.String("bogus")
assert.NotNil(t, err)
port, err := cartel.Int("port")
assert.Equal(t, config.ErrNotFound, err)
assert.Equal(t, 0, port)
}
10 changes: 10 additions & 0 deletions config/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

import (
errors "golang.org/x/xerrors"
)

var (
ErrMissingConfig = errors.New("missing config")
ErrNotFound = errors.New("not found")
)

0 comments on commit b6d3753

Please sign in to comment.