66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
|
package jsonapi
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
"unicode"
|
||
|
|
||
|
"github.com/gedex/inflector"
|
||
|
)
|
||
|
|
||
|
// https://github.com/golang/lint/blob/3d26dc39376c307203d3a221bada26816b3073cf/lint.go#L482
|
||
|
var commonInitialisms = map[string]bool{
|
||
|
"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,
|
||
|
"SSH": true,
|
||
|
"TLS": true,
|
||
|
"TTL": true,
|
||
|
"UI": true,
|
||
|
"UID": true,
|
||
|
"UUID": true,
|
||
|
"URI": true,
|
||
|
"URL": true,
|
||
|
"UTF8": true,
|
||
|
"VM": true,
|
||
|
"XML": true,
|
||
|
"JWT": true,
|
||
|
}
|
||
|
|
||
|
// Jsonify returns a JSON formatted key name from a go struct field name.
|
||
|
func Jsonify(s string) string {
|
||
|
if s == "" {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
if commonInitialisms[s] {
|
||
|
return strings.ToLower(s)
|
||
|
}
|
||
|
|
||
|
rs := []rune(s)
|
||
|
rs[0] = unicode.ToLower(rs[0])
|
||
|
|
||
|
return string(rs)
|
||
|
}
|
||
|
|
||
|
// Pluralize returns the pluralization of a noun.
|
||
|
func Pluralize(word string) string {
|
||
|
return inflector.Pluralize(word)
|
||
|
}
|