From b6eb43451910e4fdbaf81f7d34f84c32b06370a2 Mon Sep 17 00:00:00 2001 From: Alex Malone Date: Thu, 27 Aug 2020 10:46:14 -0500 Subject: [PATCH] feat: add cppType and cppTypeWithPackage Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- helpers/helpers.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/helpers/helpers.go b/helpers/helpers.go index b1c6d1c..cdfc975 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -163,6 +163,8 @@ var ProtoHelpersFuncMap = template.FuncMap{ "getStore": getStore, "goPkg": goPkg, "goPkgLastElement": goPkgLastElement, + "cppType": cppType, + "cppTypeWithPackage": cppTypeWithPackage, } var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location @@ -909,6 +911,57 @@ func haskellType(pkg string, f *descriptor.FieldDescriptorProto) string { } } +// Warning does not handle message embedded like goTypeWithGoPackage does. +func cppTypeWithPackage(f *descriptor.FieldDescriptorProto) string { + pkg := "" + if *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE || *f.Type == descriptor.FieldDescriptorProto_TYPE_ENUM { + if isTimestampPackage(*f.TypeName) { + pkg = "timestamp" + } else { + pkg = getPackageTypeName(*f.TypeName) + } + } + return cppType(pkg, f) +} + +func cppType(pkg string, f *descriptor.FieldDescriptorProto) string { + isRepeat := *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED + typeName := "???" + switch *f.Type { + case descriptor.FieldDescriptorProto_TYPE_DOUBLE: + typeName = "double" + case descriptor.FieldDescriptorProto_TYPE_FLOAT: + typeName = "float" + case descriptor.FieldDescriptorProto_TYPE_INT64: + typeName = "int64_t" + case descriptor.FieldDescriptorProto_TYPE_UINT64: + typeName = "uint64_t" + case descriptor.FieldDescriptorProto_TYPE_INT32: + typeName = "int32_t" + case descriptor.FieldDescriptorProto_TYPE_UINT32: + typeName = "uint32_t" + case descriptor.FieldDescriptorProto_TYPE_BOOL: + typeName = "bool" + case descriptor.FieldDescriptorProto_TYPE_STRING: + typeName = "std::string" + case descriptor.FieldDescriptorProto_TYPE_MESSAGE: + if pkg != "" { + pkg = pkg + "." + } + typeName = fmt.Sprintf("%s%s", pkg, shortType(*f.TypeName)) + case descriptor.FieldDescriptorProto_TYPE_BYTES: + typeName = "std::vector" + case descriptor.FieldDescriptorProto_TYPE_ENUM: + typeName = fmt.Sprintf("%s%s", pkg, shortType(*f.TypeName)) + default: + break + } + if isRepeat { + return "std::vector<" + typeName + ">" + } + return typeName +} + func goTypeWithEmbedded(pkg string, f *descriptor.FieldDescriptorProto, p *descriptor.FileDescriptorProto) string { if pkg != "" { pkg = pkg + "."