MCPcopy Index your code
hub / github.com/ChimeraCoder/gojson / FmtFieldName

Function FmtFieldName

json-to-struct.go:360–387  ·  view source on GitHub ↗

FmtFieldName formats a string as a struct key Example: FmtFieldName("foo_id") Output: FooID

(s string)

Source from the content-addressed store, hash-verified

358// FmtFieldName("foo_id")
359// Output: FooID
360func 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
389func lintFieldName(name string) string {
390 // Fast path for simple cases: "_" and all lowercase.

Callers 2

TestFmtFieldNameFunction · 0.85
generateTypesFunction · 0.85

Calls 2

stringifyFirstCharFunction · 0.85
lintFieldNameFunction · 0.85

Tested by 1

TestFmtFieldNameFunction · 0.68