|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "google.golang.org/protobuf/proto" |
| 10 | + "google.golang.org/protobuf/types/descriptorpb" |
| 11 | +) |
| 12 | + |
| 13 | +type methodInfo struct { |
| 14 | + Method string |
| 15 | + RequestName string |
| 16 | + ResponseName string |
| 17 | +} |
| 18 | + |
| 19 | +type protoFieldInfo struct { |
| 20 | + Type string |
| 21 | + Name string |
| 22 | + Number int |
| 23 | +} |
| 24 | + |
| 25 | +type messageDef struct { |
| 26 | + Name string |
| 27 | + Fields []protoFieldInfo |
| 28 | +} |
| 29 | + |
| 30 | +type descriptorSnapshot struct { |
| 31 | + WorkflowServiceMethods map[string]methodInfo |
| 32 | + WorkflowMessages map[string]messageDef |
| 33 | + WorkflowDescriptors map[string]*descriptorpb.DescriptorProto |
| 34 | + WorkflowEnums map[string]map[string]int32 |
| 35 | + WorkflowPackage string |
| 36 | + WorkflowRequestFile *descriptorpb.FileDescriptorProto |
| 37 | + DescriptorFiles []*descriptorpb.FileDescriptorProto |
| 38 | +} |
| 39 | + |
| 40 | +func (g generator) loadSnapshot(apiRepo string, sourceSHA string, stableRoot string) (descriptorSnapshot, error) { |
| 41 | + worktreeDir, err := os.MkdirTemp("", "experimentgen-api-*") |
| 42 | + if err != nil { |
| 43 | + return descriptorSnapshot{}, err |
| 44 | + } |
| 45 | + cleanup := func() { |
| 46 | + _ = g.run("", "git", "-C", apiRepo, "worktree", "remove", "--force", worktreeDir) |
| 47 | + _ = os.RemoveAll(worktreeDir) |
| 48 | + } |
| 49 | + defer cleanup() |
| 50 | + |
| 51 | + if err := g.run("", "git", "-C", apiRepo, "worktree", "add", "--detach", worktreeDir, sourceSHA); err != nil { |
| 52 | + return descriptorSnapshot{}, err |
| 53 | + } |
| 54 | + |
| 55 | + targets := []string{ |
| 56 | + filepath.ToSlash(filepath.Join(stableRoot, "workflowservice/v1/service.proto")), |
| 57 | + filepath.ToSlash(filepath.Join(stableRoot, "workflowservice/v1/request_response.proto")), |
| 58 | + filepath.ToSlash(filepath.Join(stableRoot, "enums/v1/workflow.proto")), |
| 59 | + } |
| 60 | + |
| 61 | + descPath := filepath.Join(worktreeDir, "experimental-descriptor.pb") |
| 62 | + args := append([]string{ |
| 63 | + "-I", worktreeDir, |
| 64 | + "--include_imports", |
| 65 | + "--descriptor_set_out=" + descPath, |
| 66 | + }, targets...) |
| 67 | + if err := g.run(worktreeDir, "protoc", args...); err != nil { |
| 68 | + return descriptorSnapshot{}, err |
| 69 | + } |
| 70 | + |
| 71 | + blob, err := os.ReadFile(descPath) |
| 72 | + if err != nil { |
| 73 | + return descriptorSnapshot{}, err |
| 74 | + } |
| 75 | + var set descriptorpb.FileDescriptorSet |
| 76 | + if err := proto.Unmarshal(blob, &set); err != nil { |
| 77 | + return descriptorSnapshot{}, err |
| 78 | + } |
| 79 | + return snapshotFromDescriptorSet(&set, stableRoot) |
| 80 | +} |
| 81 | + |
| 82 | +func snapshotFromDescriptorSet(set *descriptorpb.FileDescriptorSet, stableRoot string) (descriptorSnapshot, error) { |
| 83 | + snapshot := descriptorSnapshot{ |
| 84 | + WorkflowServiceMethods: make(map[string]methodInfo), |
| 85 | + WorkflowMessages: make(map[string]messageDef), |
| 86 | + WorkflowDescriptors: make(map[string]*descriptorpb.DescriptorProto), |
| 87 | + WorkflowEnums: make(map[string]map[string]int32), |
| 88 | + DescriptorFiles: cloneFileDescriptors(set.File), |
| 89 | + } |
| 90 | + |
| 91 | + serviceName := filepath.ToSlash(filepath.Join(stableRoot, "workflowservice/v1/service.proto")) |
| 92 | + requestResponseName := filepath.ToSlash(filepath.Join(stableRoot, "workflowservice/v1/request_response.proto")) |
| 93 | + enumName := filepath.ToSlash(filepath.Join(stableRoot, "enums/v1/workflow.proto")) |
| 94 | + |
| 95 | + var serviceFile, requestFile, enumFile *descriptorpb.FileDescriptorProto |
| 96 | + for _, file := range set.File { |
| 97 | + switch file.GetName() { |
| 98 | + case serviceName: |
| 99 | + serviceFile = file |
| 100 | + case requestResponseName: |
| 101 | + requestFile = file |
| 102 | + case enumName: |
| 103 | + enumFile = file |
| 104 | + } |
| 105 | + } |
| 106 | + if serviceFile == nil || requestFile == nil || enumFile == nil { |
| 107 | + return descriptorSnapshot{}, fmt.Errorf("descriptor set missing required files") |
| 108 | + } |
| 109 | + |
| 110 | + for _, service := range serviceFile.GetService() { |
| 111 | + if service.GetName() != "WorkflowService" { |
| 112 | + continue |
| 113 | + } |
| 114 | + for _, method := range service.GetMethod() { |
| 115 | + name := method.GetName() |
| 116 | + snapshot.WorkflowServiceMethods[name] = methodInfo{ |
| 117 | + Method: name, |
| 118 | + RequestName: trimDescriptorName(method.GetInputType()), |
| 119 | + ResponseName: trimDescriptorName(method.GetOutputType()), |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + for _, msg := range requestFile.GetMessageType() { |
| 125 | + snapshot.WorkflowMessages[msg.GetName()] = descriptorMessageDef(msg) |
| 126 | + snapshot.WorkflowDescriptors[msg.GetName()] = proto.Clone(msg).(*descriptorpb.DescriptorProto) |
| 127 | + } |
| 128 | + snapshot.WorkflowPackage = requestFile.GetPackage() |
| 129 | + snapshot.WorkflowRequestFile = proto.Clone(requestFile).(*descriptorpb.FileDescriptorProto) |
| 130 | + |
| 131 | + for _, enum := range enumFile.GetEnumType() { |
| 132 | + values := make(map[string]int32, len(enum.GetValue())) |
| 133 | + for _, value := range enum.GetValue() { |
| 134 | + values[value.GetName()] = value.GetNumber() |
| 135 | + } |
| 136 | + snapshot.WorkflowEnums[enum.GetName()] = values |
| 137 | + } |
| 138 | + |
| 139 | + return snapshot, nil |
| 140 | +} |
| 141 | + |
| 142 | +func descriptorMessageDef(msg *descriptorpb.DescriptorProto) messageDef { |
| 143 | + fields := make([]protoFieldInfo, 0, len(msg.GetField())) |
| 144 | + for _, field := range msg.GetField() { |
| 145 | + fields = append(fields, protoFieldInfo{ |
| 146 | + Type: descriptorFieldType(field), |
| 147 | + Name: field.GetName(), |
| 148 | + Number: int(field.GetNumber()), |
| 149 | + }) |
| 150 | + } |
| 151 | + return messageDef{ |
| 152 | + Name: msg.GetName(), |
| 153 | + Fields: fields, |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func descriptorFieldType(field *descriptorpb.FieldDescriptorProto) string { |
| 158 | + var base string |
| 159 | + switch field.GetType() { |
| 160 | + case descriptorpb.FieldDescriptorProto_TYPE_DOUBLE: |
| 161 | + base = "double" |
| 162 | + case descriptorpb.FieldDescriptorProto_TYPE_FLOAT: |
| 163 | + base = "float" |
| 164 | + case descriptorpb.FieldDescriptorProto_TYPE_INT64: |
| 165 | + base = "int64" |
| 166 | + case descriptorpb.FieldDescriptorProto_TYPE_UINT64: |
| 167 | + base = "uint64" |
| 168 | + case descriptorpb.FieldDescriptorProto_TYPE_INT32: |
| 169 | + base = "int32" |
| 170 | + case descriptorpb.FieldDescriptorProto_TYPE_FIXED64: |
| 171 | + base = "fixed64" |
| 172 | + case descriptorpb.FieldDescriptorProto_TYPE_FIXED32: |
| 173 | + base = "fixed32" |
| 174 | + case descriptorpb.FieldDescriptorProto_TYPE_BOOL: |
| 175 | + base = "bool" |
| 176 | + case descriptorpb.FieldDescriptorProto_TYPE_STRING: |
| 177 | + base = "string" |
| 178 | + case descriptorpb.FieldDescriptorProto_TYPE_BYTES: |
| 179 | + base = "bytes" |
| 180 | + case descriptorpb.FieldDescriptorProto_TYPE_UINT32: |
| 181 | + base = "uint32" |
| 182 | + case descriptorpb.FieldDescriptorProto_TYPE_SFIXED32: |
| 183 | + base = "sfixed32" |
| 184 | + case descriptorpb.FieldDescriptorProto_TYPE_SFIXED64: |
| 185 | + base = "sfixed64" |
| 186 | + case descriptorpb.FieldDescriptorProto_TYPE_SINT32: |
| 187 | + base = "sint32" |
| 188 | + case descriptorpb.FieldDescriptorProto_TYPE_SINT64: |
| 189 | + base = "sint64" |
| 190 | + default: |
| 191 | + base = trimDescriptorName(field.GetTypeName()) |
| 192 | + } |
| 193 | + if field.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REPEATED { |
| 194 | + return "repeated " + base |
| 195 | + } |
| 196 | + return base |
| 197 | +} |
| 198 | + |
| 199 | +func trimDescriptorName(name string) string { |
| 200 | + name = strings.TrimPrefix(name, ".") |
| 201 | + if idx := strings.LastIndex(name, "."); idx >= 0 { |
| 202 | + return name[idx+1:] |
| 203 | + } |
| 204 | + return name |
| 205 | +} |
| 206 | + |
| 207 | +func cloneFileDescriptors(files []*descriptorpb.FileDescriptorProto) []*descriptorpb.FileDescriptorProto { |
| 208 | + cloned := make([]*descriptorpb.FileDescriptorProto, 0, len(files)) |
| 209 | + for _, file := range files { |
| 210 | + cloned = append(cloned, proto.Clone(file).(*descriptorpb.FileDescriptorProto)) |
| 211 | + } |
| 212 | + return cloned |
| 213 | +} |
0 commit comments