Skip to content

Commit

Permalink
Moving mustParse functions into new typeconv package
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Aug 5, 2017
1 parent 9c284c7 commit 23f2bf8
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 40 deletions.
5 changes: 3 additions & 2 deletions libkv/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/libkv/store"
"github.com/docker/libkv/store/boltdb"
"github.com/hairyhenderson/gomplate/env"
"github.com/hairyhenderson/gomplate/typeconv"
)

// NewBoltDB - initialize a new BoltDB datasource handler
Expand All @@ -27,10 +28,10 @@ func setupBoltDB(bucket string) *store.Config {
logFatal("missing bucket - must specify BoltDB bucket in URL fragment")
}

t := mustParseInt(env.Getenv("BOLTDB_TIMEOUT"))
t := typeconv.MustParseInt(env.Getenv("BOLTDB_TIMEOUT"), 10, 16)
return &store.Config{
Bucket: bucket,
ConnectionTimeout: time.Duration(t) * time.Second,
PersistConnection: mustParseBool(env.Getenv("BOLTDB_PERSIST")),
PersistConnection: typeconv.MustParseBool(env.Getenv("BOLTDB_PERSIST")),
}
}
7 changes: 4 additions & 3 deletions libkv/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/libkv/store"
"github.com/docker/libkv/store/consul"
"github.com/hairyhenderson/gomplate/env"
"github.com/hairyhenderson/gomplate/typeconv"
consulapi "github.com/hashicorp/consul/api"
)

Expand Down Expand Up @@ -35,7 +36,7 @@ func consulURL(u *url.URL) *url.URL {
case "consul+https", "https":
c.Scheme = "https"
case "consul":
if mustParseBool(env.Getenv("CONSUL_HTTP_SSL")) {
if typeconv.MustParseBool(env.Getenv("CONSUL_HTTP_SSL")) {
c.Scheme = "https"
} else {
c.Scheme = "http"
Expand All @@ -52,7 +53,7 @@ func consulURL(u *url.URL) *url.URL {
}

func consulConfig(useTLS bool) *store.Config {
t := mustParseInt(env.Getenv("CONSUL_TIMEOUT"))
t := typeconv.MustAtoi(env.Getenv("CONSUL_TIMEOUT"))
config := &store.Config{
ConnectionTimeout: time.Duration(t) * time.Second,
}
Expand All @@ -76,7 +77,7 @@ func setupTLS(prefix string) *consulapi.TLSConfig {
KeyFile: env.Getenv(prefix + "_CLIENT_KEY"),
}
if v := env.Getenv(prefix + "_HTTP_SSL_VERIFY"); v != "" {
verify := mustParseBool(v)
verify := typeconv.MustParseBool(v)
tlsConfig.InsecureSkipVerify = !verify
}
return tlsConfig
Expand Down
11 changes: 0 additions & 11 deletions libkv/libkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package libkv

import (
"log"
"strconv"

"github.com/docker/libkv/store"
)
Expand Down Expand Up @@ -33,13 +32,3 @@ func (kv *LibKV) Read(path string) ([]byte, error) {

return data.Value, nil
}

func mustParseBool(s string) bool {
b, _ := strconv.ParseBool(s)
return b
}

func mustParseInt(s string) int64 {
i, _ := strconv.ParseInt(s, 10, 16)
return i
}
24 changes: 0 additions & 24 deletions libkv/libkv_test.go

This file was deleted.

33 changes: 33 additions & 0 deletions typeconv/typeconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package typeconv

import "strconv"

// MustParseBool - wrapper for strconv.ParseBool that returns false in the case of error
func MustParseBool(s string) bool {
b, _ := strconv.ParseBool(s)
return b
}

// MustParseInt - wrapper for strconv.ParseInt that returns 0 in the case of error
func MustParseInt(s string, base, bitSize int) int64 {
i, _ := strconv.ParseInt(s, base, bitSize)
return i
}

// MustParseFloat - wrapper for strconv.ParseFloat that returns 0 in the case of error
func MustParseFloat(s string, bitSize int) float64 {
i, _ := strconv.ParseFloat(s, bitSize)
return i
}

// MustParseUint - wrapper for strconv.ParseUint that returns 0 in the case of error
func MustParseUint(s string, base, bitSize int) uint64 {
i, _ := strconv.ParseUint(s, base, bitSize)
return i
}

// MustAtoi - wrapper for strconv.Atoi that returns 0 in the case of error
func MustAtoi(s string) int {
i, _ := strconv.Atoi(s)
return i
}
47 changes: 47 additions & 0 deletions typeconv/typeconv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package typeconv

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMustParseBool(t *testing.T) {
for _, b := range []string{"1", "t", "T", "true", "TRUE", "True"} {
assert.True(t, MustParseBool(b))
}
for _, b := range []string{"0", "f", "F", "false", "FALSE", "False", "", "gibberish", "12345"} {
assert.False(t, MustParseBool(b))
}
}

func TestMustParseInt(t *testing.T) {
for _, i := range []string{"0", "-0", "foo", "", "*&^%"} {
assert.Equal(t, 0, int(MustParseInt(i, 10, 64)))
}
assert.Equal(t, 1, int(MustParseInt("1", 10, 64)))
assert.Equal(t, -1, int(MustParseInt("-1", 10, 64)))
}

func TestMustAtoi(t *testing.T) {
for _, i := range []string{"0", "-0", "foo", "", "*&^%"} {
assert.Equal(t, 0, MustAtoi(i))
}
assert.Equal(t, 1, MustAtoi("1"))
assert.Equal(t, -1, MustAtoi("-1"))
}

func TestMustParseUint(t *testing.T) {
for _, i := range []string{"0", "-0", "-1", "foo", "", "*&^%"} {
assert.Equal(t, uint64(0), MustParseUint(i, 10, 64))
}
assert.Equal(t, uint64(1), MustParseUint("1", 10, 64))
}

func TestMustParseFloat(t *testing.T) {
for _, i := range []string{"0", "-0", "foo", "", "*&^%"} {
assert.Equal(t, 0.0, MustParseFloat(i, 64))
}
assert.Equal(t, 1.0, MustParseFloat("1", 64))
assert.Equal(t, -1.0, MustParseFloat("-1", 64))
}

0 comments on commit 23f2bf8

Please sign in to comment.