Goify makes a valid Go identifier out of any string. It does that by removing any non letter and non digit character and by making sure the first character is a letter or "_". Goify produces a "CamelCase" version of the string, if firstUpper is true the first character of the identifier is uppercase
(str string, firstUpper bool)
| 14 | // firstUpper is true the first character of the identifier is uppercase |
| 15 | // otherwise it's lowercase. |
| 16 | func Goify(str string, firstUpper bool) string { |
| 17 | // Optimize trivial case |
| 18 | if str == "" { |
| 19 | return "" |
| 20 | } |
| 21 | |
| 22 | // Remove optional suffix that defines corresponding transport specific |
| 23 | // name. |
| 24 | idx := strings.Index(str, ":") |
| 25 | if idx > 0 { |
| 26 | str = str[:idx] |
| 27 | } |
| 28 | |
| 29 | str = CamelCase(str, firstUpper, true) |
| 30 | if str == "" { |
| 31 | // All characters are invalid. Produce a default value. |
| 32 | if firstUpper { |
| 33 | return "Val" |
| 34 | } |
| 35 | return "val" |
| 36 | } |
| 37 | return fixReservedGo(str) |
| 38 | } |
| 39 | |
| 40 | // GoifyAtt honors any struct:field:name meta set on the attribute and calls |
| 41 | // Goify with the tag value if present or the given name otherwise. |