/
fieldtype.go
132 lines (115 loc) · 3.06 KB
/
fieldtype.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
124
125
126
127
128
129
130
131
132
package gen
import (
"fmt"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
)
type FieldType struct {
field *Field
Go protogen.GoIdent
Firestore protogen.GoIdent
IsPointer bool // TODO: unused?
IsEnum bool
IsCustom bool
IsExternal bool
IsList bool
}
func NewFieldType(field *Field) (*FieldType, error) {
types := &FieldType{
field: field,
Go: field.proto.GoIdent,
}
if err := types.init(); err != nil {
return nil, err
}
return types, nil
}
func (t *FieldType) init() error {
if t.field.proto.Desc.IsMap() {
panic(fmt.Sprintf("TODO: Map type: %+v", t.Go))
}
t.IsList = t.field.proto.Desc.IsList()
switch t.field.proto.Desc.Kind() {
case protoreflect.BoolKind:
t.Firestore.GoName = "bool"
case protoreflect.EnumKind:
t.Firestore.GoName = "int32"
t.IsEnum = true
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
t.Firestore.GoName = "int32"
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
t.Firestore.GoName = "uint32"
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
t.Firestore.GoName = "int64"
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
panic(fmt.Sprintf("UNSUPPORTED: uint64|fixed64: %+v", t.Go))
case protoreflect.FloatKind:
t.Firestore.GoName = "float32"
case protoreflect.DoubleKind:
t.Firestore.GoName = "float64"
case protoreflect.StringKind:
t.Firestore.GoName = "string"
case protoreflect.BytesKind:
t.Firestore.GoName = "[]byte"
case protoreflect.MessageKind, protoreflect.GroupKind:
if t.isTimestamp() {
t.Firestore.GoName += "Time"
t.Firestore.GoImportPath = "time"
return nil
}
nested := t.field.proto.Message
t.Go = nested.GoIdent
filePkg := t.field.msg.file.proto.GoImportPath
fieldPkg := nested.GoIdent.GoImportPath
t.IsExternal = filePkg != fieldPkg
}
if unmapped := t.Firestore.GoName == ""; unmapped {
t.IsCustom = true
}
switch {
case t.IsCustom && t.IsExternal:
panic(fmt.Sprintf("TODO: External custom types: %+v", t.Go))
case t.IsCustom && !t.IsExternal:
log.Info().
Str("parent", t.field.msg.ProtoName()).
Str("msg", t.Go.GoName).
Msg("nested")
t.IsPointer = true
t.Firestore.GoName = "Firestore" + t.Go.GoName
t.field.msg.file.nested[t.Go.GoName] = struct{}{}
// panic(fmt.Sprintf("TODO: Internal custom types: %+v", t.Go))
}
return nil
}
func (t *FieldType) String() string {
if t.Firestore.GoImportPath != "" {
id := t.field.msg.file.out.QualifiedGoIdent(t.Firestore)
if t.IsList {
id = "[]" + id
}
if t.IsPointer {
id = "*" + id
}
return id
}
if t.IsPointer {
return "*" + t.Firestore.GoName
}
id := t.Firestore.GoName
if t.IsList {
id = "[]" + id
}
return id
}
func (t *FieldType) isTimestamp() bool {
if t.field.proto.Message == nil {
return false
}
var (
path = t.field.proto.Message.GoIdent.GoImportPath
name = t.field.proto.Message.GoIdent.GoName
)
return path == "google.golang.org/protobuf/types/known/timestamppb" &&
name == "Timestamp"
}