-
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving mustParse functions into new typeconv package
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
- Loading branch information
1 parent
9c284c7
commit 23f2bf8
Showing
6 changed files
with
87 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |