From c1f10d57e97f28529a654424fa1a60aedd39b49e Mon Sep 17 00:00:00 2001 From: Valerio Gheri Date: Wed, 1 Feb 2017 14:38:14 +0100 Subject: [PATCH] Added helper to detect if a url contains variables in the form of url parameters with the same name of the fields of a specific protobuf message --- helpers.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/helpers.go b/helpers.go index 8f34beb..ae1d58a 100644 --- a/helpers.go +++ b/helpers.go @@ -60,16 +60,17 @@ var ProtoHelpersFuncMap = template.FuncMap{ "kebabCase": func(s string) string { return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1) }, - "snakeCase": xstrings.ToSnakeCase, - "getMessageType": getMessageType, - "isFieldMessage": isFieldMessage, - "isFieldRepeated": isFieldRepeated, - "goType": goType, - "jsType": jsType, - "namespacedFlowType": namespacedFlowType, - "httpVerb": httpVerb, - "httpPath": httpPath, - "shortType": shortType, + "snakeCase": xstrings.ToSnakeCase, + "getMessageType": getMessageType, + "isFieldMessage": isFieldMessage, + "isFieldRepeated": isFieldRepeated, + "goType": goType, + "jsType": jsType, + "namespacedFlowType": namespacedFlowType, + "httpVerb": httpVerb, + "httpPath": httpPath, + "shortType": shortType, + "urlHasVarsFromMessage": urlHasVarsFromMessage, } func init() { @@ -242,3 +243,15 @@ func httpVerb(m *descriptor.MethodDescriptorProto) string { return t.Custom.Kind } } + +func urlHasVarsFromMessage(path string, d *descriptor.DescriptorProto) bool { + for _, field := range d.Field { + if !isFieldMessage(field) { + if strings.Contains(path, fmt.Sprintf("{%s}", *field.Name)) { + return true + } + } + } + + return false +}