forked from ddd/req2proto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
82 lines (67 loc) · 2.3 KB
/
process.go
File metadata and controls
82 lines (67 loc) · 2.3 KB
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
package main
import (
"fmt"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)
func processFileDescriptors(fdMap map[string]*descriptorpb.FileDescriptorProto) {
enumMap := make(map[string]bool)
// First pass: Collect all enum types across all packages
for _, fd := range fdMap {
collectEnumTypes(fd, enumMap)
}
// Second pass: Update field types and handle duplicate field names
for _, fd := range fdMap {
updateFieldTypes(fd, enumMap)
}
}
func collectEnumTypes(fd *descriptorpb.FileDescriptorProto, enumMap map[string]bool) {
// Process root-level enums
for _, enum := range fd.EnumType {
fullName := fmt.Sprintf("%s.%s", *fd.Package, *enum.Name)
enumMap[fullName] = true
}
// Process enums in messages
for _, msg := range fd.MessageType {
collectMessageEnumTypes(msg, *fd.Package, enumMap)
}
}
func collectMessageEnumTypes(msg *descriptorpb.DescriptorProto, parentPath string, enumMap map[string]bool) {
currentPath := fmt.Sprintf("%s.%s", parentPath, *msg.Name)
// Process enums in this message
for _, enum := range msg.EnumType {
fullName := fmt.Sprintf("%s.%s", currentPath, *enum.Name)
enumMap[fullName] = true
}
// Process nested messages
for _, nestedMsg := range msg.NestedType {
collectMessageEnumTypes(nestedMsg, currentPath, enumMap)
}
}
func updateFieldTypes(fd *descriptorpb.FileDescriptorProto, enumMap map[string]bool) {
updateMessageFieldTypes(fd.MessageType, *fd.Package, enumMap)
}
func updateMessageFieldTypes(messages []*descriptorpb.DescriptorProto, parentPath string, enumMap map[string]bool) {
for _, msg := range messages {
currentPath := fmt.Sprintf("%s.%s", parentPath, *msg.Name)
fieldNames := make(map[string]bool)
for _, field := range msg.Field {
// Check for duplicate field names
if fieldNames[*field.Name] {
newName := fmt.Sprintf("%s_%s", *field.Name, generateRandomString())
field.Name = proto.String(newName)
}
fieldNames[*field.Name] = true
if field.TypeName != nil {
fullTypeName := strings.TrimPrefix(*field.TypeName, ".")
if enumMap[fullTypeName] {
// Change field type to enum
field.Type = descriptorpb.FieldDescriptorProto_TYPE_ENUM.Enum()
}
}
}
// Recursively update nested messages
updateMessageFieldTypes(msg.NestedType, currentPath, enumMap)
}
}