Remove dependencies, improve documentation.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package util
|
||||
|
||||
import "fmt"
|
||||
|
||||
// PanicIfNil will panic with the message provided if the check is nil. This is
|
||||
// a convieniance method to avoid many similar if statements.
|
||||
func PanicIfNil(check interface{}, message string) {
|
||||
if check == nil {
|
||||
panic(message)
|
||||
}
|
||||
}
|
||||
|
||||
// PanicOnError will panic with the message and error if the error is not nil.
|
||||
// If the error is nil (no error) then nothing happens.
|
||||
func PanicOnError(err error, message string) {
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("%s: %s", message, err.Error()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// GroupsFromRegex gets RegExp groups after matching it on a line
|
||||
func GroupsFromRegex(rx, line string) map[string]string {
|
||||
// We remove tabs and newlines from the regex. This is purely cosmetic,
|
||||
// as the regex input can be quite long and it's nice for the caller to
|
||||
// be able to format it in a more readable way.
|
||||
rx = strings.Replace(rx, "\r", "", -1)
|
||||
rx = strings.Replace(rx, "\n", "", -1)
|
||||
rx = strings.Replace(rx, "\t", "", -1)
|
||||
re := GetRegex(rx)
|
||||
|
||||
match := re.FindStringSubmatch(line)
|
||||
if len(match) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make(map[string]string)
|
||||
for i, name := range re.SubexpNames() {
|
||||
if i != 0 {
|
||||
result[name] = match[i]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// cachedRegex - structure for saving regexp`s
|
||||
type cachedRegex struct {
|
||||
sync.RWMutex
|
||||
m map[string]*regexp.Regexp
|
||||
}
|
||||
|
||||
// Global variable
|
||||
var cr = cachedRegex{m: map[string]*regexp.Regexp{}}
|
||||
|
||||
// GetRegex return regexp
|
||||
// added for minimaze regexp compilation
|
||||
func GetRegex(rx string) *regexp.Regexp {
|
||||
cr.RLock()
|
||||
v, ok := cr.m[rx]
|
||||
cr.RUnlock()
|
||||
if ok {
|
||||
return v
|
||||
}
|
||||
// if regexp is not in map
|
||||
cr.Lock()
|
||||
cr.m[rx] = regexp.MustCompile(rx)
|
||||
cr.Unlock()
|
||||
return GetRegex(rx)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ShowDiff will print two strings vertically next to each other so that line
|
||||
// differences are easier to read.
|
||||
func ShowDiff(a, b string) string {
|
||||
aLines := strings.Split(a, "\n")
|
||||
bLines := strings.Split(b, "\n")
|
||||
maxLines := int(math.Max(float64(len(aLines)), float64(len(bLines))))
|
||||
out := "\n"
|
||||
|
||||
for lineNumber := 0; lineNumber < maxLines; lineNumber++ {
|
||||
aLine := ""
|
||||
bLine := ""
|
||||
|
||||
// Replace NULL characters with a dot. Otherwise the strings will look
|
||||
// exactly the same but have different length (and therfore not be
|
||||
// equal).
|
||||
if lineNumber < len(aLines) {
|
||||
aLine = strconv.Quote(aLines[lineNumber])
|
||||
}
|
||||
if lineNumber < len(bLines) {
|
||||
bLine = strconv.Quote(bLines[lineNumber])
|
||||
}
|
||||
|
||||
diffFlag := " "
|
||||
if aLine != bLine {
|
||||
diffFlag = "*"
|
||||
}
|
||||
out += fmt.Sprintf("%s %3d %-40s%-40s\n", diffFlag, lineNumber+1, aLine, bLine)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// InStrings returns true if item exists in items. It must be an exact string
|
||||
// match.
|
||||
func InStrings(item string, items []string) bool {
|
||||
for _, v := range items {
|
||||
if item == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Ucfirst returns the word with the first letter uppercased; none of the other
|
||||
// letters in the word are modified. For example "fooBar" would return "FooBar".
|
||||
func Ucfirst(word string) string {
|
||||
if word == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if len(word) == 1 {
|
||||
return strings.ToUpper(word)
|
||||
}
|
||||
|
||||
return strings.ToUpper(string(word[0])) + word[1:]
|
||||
}
|
||||
|
||||
// Atoi converts a string to an integer in cases where we are sure that s will
|
||||
// be a valid integer, otherwise it will panic.
|
||||
func Atoi(s string) int {
|
||||
i, err := strconv.Atoi(s)
|
||||
PanicOnError(err, "bad integer")
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
// GetExportedName returns a deterministic and Go safe name for a C type. For
|
||||
// example, "*__foo[]" will return "FooSlice".
|
||||
func GetExportedName(field string) string {
|
||||
if strings.Contains(field, "interface{}") ||
|
||||
strings.Contains(field, "Interface{}") {
|
||||
return "Interface"
|
||||
}
|
||||
|
||||
// Convert "[]byte" into "byteSlice". This also works with multiple slices,
|
||||
// like "[][]byte" to "byteSliceSlice".
|
||||
for len(field) > 2 && field[:2] == "[]" {
|
||||
field = field[2:] + "Slice"
|
||||
}
|
||||
|
||||
// NotFunc(int)()
|
||||
field = strings.Replace(field, "(", "_", -1)
|
||||
field = strings.Replace(field, ")", "_", -1)
|
||||
|
||||
return Ucfirst(strings.TrimLeft(field, "*_"))
|
||||
}
|
||||
Reference in New Issue
Block a user