FmtFieldName formats a string as a struct key Example: FmtFieldName("foo_id") Output: FooID
(s string)
| 358 | // FmtFieldName("foo_id") |
| 359 | // Output: FooID |
| 360 | func FmtFieldName(s string) string { |
| 361 | runes := []rune(s) |
| 362 | for len(runes) > 0 && !unicode.IsLetter(runes[0]) && !unicode.IsDigit(runes[0]) { |
| 363 | runes = runes[1:] |
| 364 | } |
| 365 | if len(runes) == 0 { |
| 366 | return "_" |
| 367 | } |
| 368 | |
| 369 | s = stringifyFirstChar(string(runes)) |
| 370 | name := lintFieldName(s) |
| 371 | runes = []rune(name) |
| 372 | for i, c := range runes { |
| 373 | ok := unicode.IsLetter(c) || unicode.IsDigit(c) |
| 374 | if i == 0 { |
| 375 | ok = unicode.IsLetter(c) |
| 376 | } |
| 377 | if !ok { |
| 378 | runes[i] = '_' |
| 379 | } |
| 380 | } |
| 381 | s = string(runes) |
| 382 | s = strings.Trim(s, "_") |
| 383 | if len(s) == 0 { |
| 384 | return "_" |
| 385 | } |
| 386 | return s |
| 387 | } |
| 388 | |
| 389 | func lintFieldName(name string) string { |
| 390 | // Fast path for simple cases: "_" and all lowercase. |