mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-18 21:09:38 +00:00
Upgraded from 99c19923, branch release-3.0. This required fetching or upgrading the following: * k8s.io/api to kubernetes-1.9.1 * k8s.io/apimachinery to kubernetes-1.9.1 * github.com/juju/ratelimit to 1.0.1 * github.com/spf13/pflag to 4c012f6d Also, update Scope's imports/function calls to be compatible with the new client.
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package extra
|
|
|
|
import (
|
|
"github.com/json-iterator/go"
|
|
"unicode"
|
|
)
|
|
|
|
// SetNamingStrategy rename struct fields uniformly
|
|
func SetNamingStrategy(translate func(string) string) {
|
|
jsoniter.RegisterExtension(&namingStrategyExtension{jsoniter.DummyExtension{}, translate})
|
|
}
|
|
|
|
type namingStrategyExtension struct {
|
|
jsoniter.DummyExtension
|
|
translate func(string) string
|
|
}
|
|
|
|
func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
|
|
for _, binding := range structDescriptor.Fields {
|
|
binding.ToNames = []string{extension.translate(binding.Field.Name)}
|
|
binding.FromNames = []string{extension.translate(binding.Field.Name)}
|
|
}
|
|
}
|
|
|
|
// LowerCaseWithUnderscores one strategy to SetNamingStrategy for. It will change HelloWorld to hello_world.
|
|
func LowerCaseWithUnderscores(name string) string {
|
|
newName := []rune{}
|
|
for i, c := range name {
|
|
if i == 0 {
|
|
newName = append(newName, unicode.ToLower(c))
|
|
} else {
|
|
if unicode.IsUpper(c) {
|
|
newName = append(newName, '_')
|
|
newName = append(newName, unicode.ToLower(c))
|
|
} else {
|
|
newName = append(newName, c)
|
|
}
|
|
}
|
|
}
|
|
return string(newName)
|
|
}
|