/
funcs.go
44 lines (39 loc) · 1.1 KB
/
funcs.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
package resource
import (
"fmt"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
)
// GoType returns corresponding go type of the field
func GoType(f *protogen.Field) string {
if f.Desc.IsList() {
return "[]" + getUnderlingTypeName(f)
}
return getUnderlingTypeName(f)
}
func getUnderlingTypeName(f *protogen.Field) string {
switch f.Desc.Kind() {
// scalars first
case protoreflect.DoubleKind:
return "float64"
case protoreflect.FloatKind:
return "float32"
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
return "int32"
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
return "int64"
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
return "uint32"
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return "uint64"
case protoreflect.BoolKind:
return "bool"
case protoreflect.StringKind:
return "string"
case protoreflect.BytesKind:
return "[]byte"
// scalars end
default:
panic(fmt.Sprintf("type '%+v' is not supported for conversion to go type"))
}
}