From fcfaf82ebd9555f5c434aaa75e18d7cc6892eae2 Mon Sep 17 00:00:00 2001 From: gfanton Date: Thu, 13 Apr 2017 13:15:40 +0200 Subject: [PATCH] Add jsSuffixReservedKeyword helper --- helpers.go | 8 ++++++++ 1 file changed, 8 insertions(+) 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]