This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
123 lines (113 loc) · 3.37 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package z
import (
"fmt"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
/*
Creation Time: 2021 - Jan - 12
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
func Constructor(file *protogen.File, g *protogen.GeneratedFile, desc protoreflect.MessageDescriptor) string {
p, t := DescName(file, g, desc)
if p == "" {
return fmt.Sprintf("C_%s", t)
} else {
return fmt.Sprintf("%s.C_%s", p, t)
}
}
func Name(file *protogen.File, g *protogen.GeneratedFile, desc protoreflect.MessageDescriptor) string {
p, t := DescName(file, g, desc)
if p == "" {
return fmt.Sprintf("%s", t)
} else {
return fmt.Sprintf("%s.%s", p, t)
}
}
func PackageName(file *protogen.File, g *protogen.GeneratedFile, desc protoreflect.MessageDescriptor) string {
pkg, _ := DescName(file, g, desc)
return pkg
}
// descName returns the package and ident name
func DescName(file *protogen.File, g *protogen.GeneratedFile, desc protoreflect.MessageDescriptor) (string, string) {
if desc == nil {
return "", ""
}
if string(desc.FullName().Parent()) == string(file.GoPackageName) {
return "", string(desc.Name())
} else {
fd, ok := desc.ParentFile().Options().(*descriptorpb.FileOptions)
if ok {
g.QualifiedGoIdent(protogen.GoImportPath(fd.GetGoPackage()).Ident(string(desc.Name())))
}
return string(desc.ParentFile().Package()), string(desc.Name())
}
}
// zeroValue returns the equal zero value based on the input type
func ZeroValue(f protoreflect.FieldDescriptor) string {
switch f.Kind() {
case protoreflect.BoolKind:
return "false"
case protoreflect.StringKind:
return "\"\""
case protoreflect.MessageKind:
return "nil"
case protoreflect.BytesKind:
return "nil"
default:
return "0"
}
}
func GoKind(file *protogen.File, g *protogen.GeneratedFile, d protoreflect.FieldDescriptor) string {
pkg := PackageName(file, g, d.Message())
switch d.Kind() {
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
return "int32"
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
return "uint32"
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
return "int64"
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return "uint64"
case protoreflect.DoubleKind:
return "float64"
case protoreflect.FloatKind:
return "float32"
case protoreflect.StringKind:
return "string"
case protoreflect.BytesKind:
return "[]byte"
case protoreflect.BoolKind:
return "bool"
case protoreflect.EnumKind:
if pkg == "" {
return string(d.Name())
} else {
return fmt.Sprintf("%s.%s", pkg, d.Name())
}
}
return "unsupported"
}
func CqlKind(d protoreflect.FieldDescriptor) string {
switch d.Kind() {
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
return "int"
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return "bigint"
case protoreflect.DoubleKind:
return "double"
case protoreflect.FloatKind:
return "float"
case protoreflect.BytesKind, protoreflect.StringKind:
return "blob"
case protoreflect.BoolKind:
return "boolean"
}
return "unsupported"
// panic(fmt.Sprintf("unsupported kindCql: %v", k.String()))
}