vendor!
This commit is contained in:
19
vendor/github.com/serenize/snaker/LICENSE.txt
generated
vendored
Normal file
19
vendor/github.com/serenize/snaker/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2015 Serenize UG (haftungsbeschränkt)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
25
vendor/github.com/serenize/snaker/README.md
generated
vendored
Normal file
25
vendor/github.com/serenize/snaker/README.md
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
# snaker
|
||||
|
||||
[](https://travis-ci.org/serenize/snaker)
|
||||
[](https://godoc.org/github.com/serenize/snaker)
|
||||
|
||||
This is a small utility to convert camel cased strings to snake case and back, except some defined words.
|
||||
|
||||
## QBS Usage
|
||||
|
||||
To replace the original toSnake and back algorithms for [https://github.com/coocood/qbs](https://github.com/coocood/qbs)
|
||||
you can easily use snaker:
|
||||
|
||||
Import snaker
|
||||
```go
|
||||
import (
|
||||
github.com/coocood/qbs
|
||||
github.com/serenize/snaker
|
||||
)
|
||||
```
|
||||
|
||||
Register the snaker methods to qbs
|
||||
```go
|
||||
qbs.ColumnNameToFieldName = snaker.SnakeToCamel
|
||||
qbs.FieldNameToColumnName = snaker.CamelToSnake
|
||||
```
|
123
vendor/github.com/serenize/snaker/snaker.go
generated
vendored
Normal file
123
vendor/github.com/serenize/snaker/snaker.go
generated
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
// Package snaker provides methods to convert CamelCase names to snake_case and back.
|
||||
// It considers the list of allowed initialsms used by github.com/golang/lint/golint (e.g. ID or HTTP)
|
||||
package snaker
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// CamelToSnake converts a given string to snake case
|
||||
func CamelToSnake(s string) string {
|
||||
var result string
|
||||
var words []string
|
||||
var lastPos int
|
||||
rs := []rune(s)
|
||||
|
||||
for i := 0; i < len(rs); i++ {
|
||||
if i > 0 && unicode.IsUpper(rs[i]) {
|
||||
if initialism := startsWithInitialism(s[lastPos:]); initialism != "" {
|
||||
words = append(words, initialism)
|
||||
|
||||
i += len(initialism) - 1
|
||||
lastPos = i
|
||||
continue
|
||||
}
|
||||
|
||||
words = append(words, s[lastPos:i])
|
||||
lastPos = i
|
||||
}
|
||||
}
|
||||
|
||||
// append the last word
|
||||
if s[lastPos:] != "" {
|
||||
words = append(words, s[lastPos:])
|
||||
}
|
||||
|
||||
for k, word := range words {
|
||||
if k > 0 {
|
||||
result += "_"
|
||||
}
|
||||
|
||||
result += strings.ToLower(word)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// SnakeToCamel returns a string converted from snake case to uppercase
|
||||
func SnakeToCamel(s string) string {
|
||||
var result string
|
||||
|
||||
words := strings.Split(s, "_")
|
||||
|
||||
for _, word := range words {
|
||||
if upper := strings.ToUpper(word); commonInitialisms[upper] {
|
||||
result += upper
|
||||
continue
|
||||
}
|
||||
|
||||
if len(word) > 0 {
|
||||
w := []rune(word)
|
||||
w[0] = unicode.ToUpper(w[0])
|
||||
result += string(w)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// startsWithInitialism returns the initialism if the given string begins with it
|
||||
func startsWithInitialism(s string) string {
|
||||
var initialism string
|
||||
// the longest initialism is 5 char, the shortest 2
|
||||
for i := 1; i <= 5; i++ {
|
||||
if len(s) > i-1 && commonInitialisms[s[:i]] {
|
||||
initialism = s[:i]
|
||||
}
|
||||
}
|
||||
return initialism
|
||||
}
|
||||
|
||||
// commonInitialisms, taken from
|
||||
// https://github.com/golang/lint/blob/206c0f020eba0f7fbcfbc467a5eb808037df2ed6/lint.go#L731
|
||||
var commonInitialisms = map[string]bool{
|
||||
"ACL": true,
|
||||
"API": true,
|
||||
"ASCII": true,
|
||||
"CPU": true,
|
||||
"CSS": true,
|
||||
"DNS": true,
|
||||
"EOF": true,
|
||||
"GUID": true,
|
||||
"HTML": true,
|
||||
"HTTP": true,
|
||||
"HTTPS": true,
|
||||
"ID": true,
|
||||
"IP": true,
|
||||
"JSON": true,
|
||||
"LHS": true,
|
||||
"QPS": true,
|
||||
"RAM": true,
|
||||
"RHS": true,
|
||||
"RPC": true,
|
||||
"SLA": true,
|
||||
"SMTP": true,
|
||||
"SQL": true,
|
||||
"SSH": true,
|
||||
"TCP": true,
|
||||
"TLS": true,
|
||||
"TTL": true,
|
||||
"UDP": true,
|
||||
"UI": true,
|
||||
"UID": true,
|
||||
"UUID": true,
|
||||
"URI": true,
|
||||
"URL": true,
|
||||
"UTF8": true,
|
||||
"VM": true,
|
||||
"XML": true,
|
||||
"XMPP": true,
|
||||
"XSRF": true,
|
||||
"XSS": true,
|
||||
}
|
Reference in New Issue
Block a user