chore(): strcase usage generalization

This commit is contained in:
Nicolas Carlier 2020-02-04 21:27:07 +00:00
parent 6a011272fd
commit efa525be49
6 changed files with 34 additions and 41 deletions

View File

@ -12,7 +12,7 @@ import (
"time"
"github.com/ncarlier/webhookd/pkg/logger"
"github.com/ncarlier/webhookd/pkg/tools"
"github.com/ncarlier/webhookd/pkg/strcase"
)
var workID uint64
@ -58,7 +58,7 @@ func NewWorkRequest(name, script, payload, output string, args []string, timeout
MessageChan: make(chan []byte),
Status: Idle,
}
w.LogFilename = path.Join(output, fmt.Sprintf("%s_%d_%s.txt", tools.ToSnakeCase(w.Name), w.ID, time.Now().Format("20060102_1504")))
w.LogFilename = path.Join(output, fmt.Sprintf("%s_%d_%s.txt", strcase.ToSnake(w.Name), w.ID, time.Now().Format("20060102_1504")))
return w
}

View File

@ -77,7 +77,7 @@ func ToScreamingDelimited(s string, del uint8, screaming bool) string {
} else if v >= 'a' && v <= 'z' {
n += string(v) + string(del)
}
} else if v == ' ' || v == '_' || v == '-' {
} else if v == ' ' || v == '_' || v == '-' || v == '/' {
// replace spaces/underscores with delimiters
n += string(del)
} else {

View File

@ -0,0 +1,25 @@
package test
import (
"testing"
"github.com/ncarlier/webhookd/pkg/assert"
"github.com/ncarlier/webhookd/pkg/strcase"
)
func TestToSnakeCase(t *testing.T) {
testCases := []struct {
value string
expected string
}{
{"hello-world", "hello_world"},
{"helloWorld", "hello_world"},
{"HelloWorld", "hello_world"},
{"Hello/_World", "hello__world"},
{"Hello/world", "hello_world"},
}
for _, tc := range testCases {
value := strcase.ToSnake(tc.value)
assert.Equal(t, tc.expected, value, "")
}
}

View File

@ -5,25 +5,10 @@ import (
"net/http"
"net/url"
"strings"
"unicode"
"github.com/ncarlier/webhookd/pkg/strcase"
)
// ToSnakeCase convert string to snakecase.
func ToSnakeCase(in string) string {
runes := []rune(in)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '_')
}
out = append(out, unicode.ToLower(runes[i]))
}
result := strings.Replace(string(out), "/", "_", -1)
return strings.Replace(result, "-", "", -1)
}
// QueryParamsToShellVars convert URL query parameters to shell vars.
func QueryParamsToShellVars(q url.Values) []string {
var params []string
@ -33,7 +18,7 @@ func QueryParamsToShellVars(q url.Values) []string {
if err != nil {
continue
}
buf.WriteString(ToSnakeCase(k))
buf.WriteString(strcase.ToSnake(k))
buf.WriteString("=")
buf.WriteString(value)
params = append(params, buf.String())
@ -50,7 +35,7 @@ func HTTPHeadersToShellVars(h http.Header) []string {
if err != nil {
continue
}
buf.WriteString(ToSnakeCase(k))
buf.WriteString(strcase.ToSnake(k))
buf.WriteString("=")
buf.WriteString(value)
params = append(params, buf.String())

View File

@ -9,23 +9,6 @@ import (
"github.com/ncarlier/webhookd/pkg/tools"
)
func TestToSnakeCase(t *testing.T) {
testCases := []struct {
value string
expected string
}{
{"hello-world", "helloworld"},
{"helloWorld", "hello_world"},
{"HelloWorld", "hello_world"},
{"Hello/World", "hello__world"},
{"Hello/world", "hello_world"},
}
for _, tc := range testCases {
value := tools.ToSnakeCase(tc.value)
assert.Equal(t, tc.expected, value, "")
}
}
func TestQueryParamsToShellVars(t *testing.T) {
tc := url.Values{
"string": []string{"foo"},

View File

@ -6,12 +6,12 @@ import (
"path"
"path/filepath"
"github.com/ncarlier/webhookd/pkg/tools"
"github.com/ncarlier/webhookd/pkg/strcase"
)
// RetrieveLogFile retrieve work log with its name and id
func RetrieveLogFile(id, name, base string) (*os.File, error) {
logPattern := path.Join(base, fmt.Sprintf("%s_%s_*.txt", tools.ToSnakeCase(name), id))
logPattern := path.Join(base, fmt.Sprintf("%s_%s_*.txt", strcase.ToSnake(name), id))
files, err := filepath.Glob(logPattern)
if err != nil {
return nil, err