diff --git a/helpers.go b/helpers.go index d1e45c8..f8489ef 100644 --- a/helpers.go +++ b/helpers.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "regexp" "strings" "text/template" @@ -14,6 +15,8 @@ import ( options "google.golang.org/genproto/googleapis/api/annotations" ) +var jsReservedRe *regexp.Regexp = regexp.MustCompile(`(^|[^A-Za-z])(do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)($|[^A-Za-z])`) + var ProtoHelpersFuncMap = template.FuncMap{ "string": func(i interface { String() string @@ -66,6 +69,7 @@ var ProtoHelpersFuncMap = template.FuncMap{ "isFieldRepeated": isFieldRepeated, "goType": goType, "jsType": jsType, + "jsSuffixReserved": jsSuffixReservedKeyword, "namespacedFlowType": namespacedFlowType, "httpVerb": httpVerb, "httpPath": httpPath, @@ -205,6 +209,10 @@ func jsType(f *descriptor.FieldDescriptorProto) string { } } +func jsSuffixReservedKeyword(s string) string { + return jsReservedRe.ReplaceAllString(s, "${1}${2}_${3}") +} + func shortType(s string) string { t := strings.Split(s, ".") return t[len(t)-1]