-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
59 lines (54 loc) · 1.26 KB
/
json.go
File metadata and controls
59 lines (54 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package batcha
import (
"strings"
"unicode"
)
// skipConvertKeys are map keys (lowercase) whose children should NOT have
// their keys converted, because they are user-defined (e.g., tag keys, parameters).
var skipConvertKeys = map[string]bool{
"options": true,
"parameters": true,
"tags": true,
}
// walkMap recursively converts map keys using the provided function.
func walkMap(v any, fn func(string) string) any {
switch val := v.(type) {
case map[string]any:
result := make(map[string]any, len(val))
for k, child := range val {
newKey := fn(k)
if skipConvertKeys[strings.ToLower(k)] {
result[newKey] = child
} else {
result[newKey] = walkMap(child, fn)
}
}
return result
case []any:
result := make([]any, len(val))
for i, child := range val {
result[i] = walkMap(child, fn)
}
return result
default:
return v
}
}
// toPascalCase converts a camelCase string to PascalCase.
func toPascalCase(s string) string {
if s == "" {
return s
}
runes := []rune(s)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
// toCamelCase converts a PascalCase string to camelCase.
func toCamelCase(s string) string {
if s == "" {
return s
}
runes := []rune(s)
runes[0] = unicode.ToLower(runes[0])
return string(runes)
}