Skip to content

Instantly share code, notes, and snippets.

@shanev
Last active October 9, 2018 05:01
Show Gist options
  • Save shanev/b2c56c79e40d51ecdf589c75ff0c7349 to your computer and use it in GitHub Desktop.
Save shanev/b2c56c79e40d51ecdf589c75ff0c7349 to your computer and use it in GitHub Desktop.
package types
import (
"path"
"reflect"
"regexp"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GetType returns the package name of the containing `Msg`
func GetType(msg sdk.Msg) string {
pkgPath := reflect.TypeOf(msg).PkgPath()
return path.Base(pkgPath)
}
// GetName returns the name of the `Msg` in camel case
func GetName(msg sdk.Msg) string {
name := reflect.TypeOf(msg).Name()
prefix := strings.Split(toSnakeCase(name), "_")
return strings.Join(prefix[:len(prefix)-1], "_")
}
func toSnakeCase(str string) string {
matchFirstCap := regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap := regexp.MustCompile("([a-z0-9])([A-Z])")
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment