-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nyaruka/housekeeping
Move utility methods and add tests
- Loading branch information
Showing
8 changed files
with
93 additions
and
59 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package courier | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
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,59 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"unicode/utf8" | ||
) | ||
|
||
// MapAsJSON serializes the given map as a JSON string | ||
func MapAsJSON(m map[string]string) []byte { | ||
bytes, err := json.Marshal(m) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return bytes | ||
} | ||
|
||
// JoinNonEmpty takes a vararg of strings and return the join of all the non-empty strings with a delimiter between them | ||
func JoinNonEmpty(delim string, strings ...string) string { | ||
var buf bytes.Buffer | ||
for _, s := range strings { | ||
if s != "" { | ||
if buf.Len() > 0 { | ||
buf.WriteString(delim) | ||
} | ||
buf.WriteString(s) | ||
} | ||
} | ||
return buf.String() | ||
} | ||
|
||
// DecodeUTF8 is equivalent to .decode('utf-8', 'ignore') in Python | ||
func DecodeUTF8(bytes []byte) string { | ||
s := string(bytes) | ||
if !utf8.ValidString(s) { | ||
v := make([]rune, 0, len(s)) | ||
for i, r := range s { | ||
if r == utf8.RuneError { | ||
_, size := utf8.DecodeRuneInString(s[i:]) | ||
if size == 1 { | ||
continue | ||
} | ||
} | ||
v = append(v, r) | ||
} | ||
s = string(v) | ||
} | ||
return s | ||
} | ||
|
||
// StringArrayContains returns whether a given string array contains the given element | ||
func StringArrayContains(s []string, e string) bool { | ||
for _, a := range s { | ||
if a == e { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,23 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMapAsJSON(t *testing.T) { | ||
assert.Equal(t, "{}", string(MapAsJSON(map[string]string{}))) | ||
assert.Equal(t, "{\"foo\":\"bar\"}", string(MapAsJSON(map[string]string{"foo": "bar"}))) | ||
} | ||
|
||
func TestJoinNonEmpty(t *testing.T) { | ||
assert.Equal(t, "", JoinNonEmpty(" ")) | ||
assert.Equal(t, "hello world", JoinNonEmpty(" ", "", "hello", "", "world")) | ||
} | ||
|
||
func TestStringArrayContains(t *testing.T) { | ||
assert.False(t, StringArrayContains([]string{}, "x")) | ||
assert.False(t, StringArrayContains([]string{"a", "b"}, "x")) | ||
assert.True(t, StringArrayContains([]string{"a", "b", "x", "y"}, "x")) | ||
} |