|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + |
| 7 | + "go.temporal.io/server/common/log" |
| 8 | + "go.temporal.io/server/common/log/tag" |
| 9 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + // CurrentVersion means to emit code for the current version of protos. |
| 14 | + CurrentVersion Mode = iota |
| 15 | + |
| 16 | + // Gogo122Version means to emit code for the older gogo-based protos |
| 17 | + // from Temporal v1.22. The way this works is to walk type hierarchies |
| 18 | + // for current version of protos, but convert the current protos back |
| 19 | + // to the corresponding gogo-based types/packages. |
| 20 | + Gogo122Version |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + defaultPackageName = "main_test" |
| 25 | + defaultFuncSignature = "func VisitMessage(vAny any)" |
| 26 | +) |
| 27 | + |
| 28 | +type ( |
| 29 | + Emitter struct { |
| 30 | + logger log.Logger |
| 31 | + mode Mode |
| 32 | + packageName string |
| 33 | + funcSignature string |
| 34 | + funcTrailer string |
| 35 | + handlers []*Handler |
| 36 | + imports map[string]struct{} |
| 37 | + extraImports map[string]struct{} |
| 38 | + root *Tree |
| 39 | + inScopeVars map[string]struct{} |
| 40 | + } |
| 41 | + |
| 42 | + // Handler matches a field in the type hierarchy to a function that generates code. |
| 43 | + Handler struct { |
| 44 | + // Include returns whether to include this path during code generation. |
| 45 | + Include func(VisitType, VisitPath) bool |
| 46 | + // Invocation returns a snippet of generated code. |
| 47 | + // It is passed a variable name that can be used for code generation. |
| 48 | + Invocation func(string) string |
| 49 | + } |
| 50 | + |
| 51 | + Mode int |
| 52 | +) |
| 53 | + |
| 54 | +func NewEmitter(logger log.Logger, mode Mode) *Emitter { |
| 55 | + return &Emitter{ |
| 56 | + logger: logger, |
| 57 | + mode: mode, |
| 58 | + packageName: defaultPackageName, |
| 59 | + funcSignature: defaultFuncSignature, |
| 60 | + imports: make(map[string]struct{}), |
| 61 | + extraImports: make(map[string]struct{}), |
| 62 | + root: NewTree(), |
| 63 | + inScopeVars: map[string]struct{}{}, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func (e *Emitter) SetPackageName(name string) { e.packageName = name } |
| 68 | +func (e *Emitter) SetFunctionSignature(sig string) { e.funcSignature = sig } |
| 69 | +func (e *Emitter) SetFunctionTrailer(trailer string) { e.funcTrailer = trailer } |
| 70 | + |
| 71 | +func (e *Emitter) AddHandler(include func(vt VisitType, path VisitPath) bool, invocation func(string) string) { |
| 72 | + e.handlers = append(e.handlers, &Handler{ |
| 73 | + Include: include, |
| 74 | + Invocation: invocation, |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +func (e *Emitter) AddImport(s string) { |
| 79 | + e.extraImports[s] = struct{}{} |
| 80 | +} |
| 81 | + |
| 82 | +func (e *Emitter) Visit(mt protoreflect.MessageType) { |
| 83 | + Visit(mt.Descriptor(), e.visit) |
| 84 | +} |
| 85 | + |
| 86 | +func (e *Emitter) visit(obj VisitType, path VisitPath) bool { |
| 87 | + if e.mode == Gogo122Version && shouldIgnoreTypeIfDoesntExistIn122(obj.Descriptor) { |
| 88 | + return false |
| 89 | + } |
| 90 | + |
| 91 | + e.logger.Debug("Emitter.visit", |
| 92 | + tag.NewStringTag("obj", string(obj.FullName())), |
| 93 | + tag.NewStringTag("path", path.String()), |
| 94 | + ) |
| 95 | + for _, handler := range e.handlers { |
| 96 | + if handler.Include(obj, path) { |
| 97 | + pathCopy := make(VisitPath, len(path)) |
| 98 | + copy(pathCopy, path) // path is reused during the visitor / changes as it goes. |
| 99 | + e.root.Insert(pathCopy, handler) |
| 100 | + e.discoverImports(pathCopy) |
| 101 | + } |
| 102 | + } |
| 103 | + return true |
| 104 | +} |
| 105 | + |
| 106 | +func (e *Emitter) discoverImports(path VisitPath) { |
| 107 | + for _, obj := range path { |
| 108 | + e.imports[obj.GoImportPath()] = struct{}{} |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (e *Emitter) Generate(out io.Writer) { |
| 113 | + e.genPreamble(out) |
| 114 | + |
| 115 | + writef(out, "%s {\n", e.funcSignature) |
| 116 | + writeln(out, "switch root := vAny.(type) {") |
| 117 | + for _, typ := range e.root.SortedTypes() { |
| 118 | + writef(out, "case *%s:\n", typ.GoQualifiedName()) |
| 119 | + if child := e.root.Children[typ.GoName()]; child != nil { |
| 120 | + e.emit(out, "root", child) |
| 121 | + } |
| 122 | + } |
| 123 | + writeln(out, "}") |
| 124 | + writeln(out, e.funcTrailer) |
| 125 | + writeln(out, "}") |
| 126 | +} |
| 127 | + |
| 128 | +func (e *Emitter) genPreamble(out io.Writer) { |
| 129 | + writeln(out, `// Code generated by cmd/tools/genvisitor. DO NOT EDIT.`) |
| 130 | + writef(out, "package %s\n", e.packageName) |
| 131 | + writeln(out, "import (") |
| 132 | + |
| 133 | + for imp := range e.imports { |
| 134 | + alias := getImportAlias(imp) |
| 135 | + if e.mode == Gogo122Version { |
| 136 | + imp = replaceWith122Import(imp) |
| 137 | + } |
| 138 | + writef(out, "%s \"%s\"\n", alias, imp) |
| 139 | + } |
| 140 | + |
| 141 | + for imp := range e.extraImports { |
| 142 | + writef(out, "\"%s\"\n", imp) |
| 143 | + |
| 144 | + } |
| 145 | + writeln(out, `)`) |
| 146 | +} |
| 147 | + |
| 148 | +func (e *Emitter) emit(out io.Writer, parentVar string, node *Tree) { |
| 149 | + if node == nil { |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + for _, vt := range node.SortedTypes() { |
| 154 | + switch desc := vt.Descriptor.(type) { |
| 155 | + case protoreflect.FieldDescriptor: |
| 156 | + if desc.IsMap() { |
| 157 | + varName, freeVar := e.makeVar("val") |
| 158 | + defer freeVar() |
| 159 | + writef(out, "for _, %s := range %s.%s {\n", varName, parentVar, vt.GoGetter()) |
| 160 | + e.emit(out, varName, node.Children[vt.GoName()]) |
| 161 | + writeln(out, "}") |
| 162 | + } else if desc.IsList() { |
| 163 | + varName, freeVar := e.makeVar("item") |
| 164 | + defer freeVar() |
| 165 | + writef(out, "for _, %s := range %s.%s {\n", varName, parentVar, vt.GoGetter()) |
| 166 | + e.emit(out, varName, node.Children[vt.GoName()]) |
| 167 | + writeln(out, "}") |
| 168 | + } else { |
| 169 | + varName, freeVar := e.makeVar("y") |
| 170 | + defer freeVar() |
| 171 | + writef(out, "%s := %s.%s\n", varName, parentVar, vt.GoGetter()) |
| 172 | + e.emit(out, varName, node.Children[vt.GoName()]) |
| 173 | + } |
| 174 | + case protoreflect.OneofDescriptor: |
| 175 | + writef(out, "switch oneof := %s.%s.(type) {\n", parentVar, vt.GoGetter()) |
| 176 | + e.emitOneOfCases(out, "oneof", vt, node.Children[vt.GoName()]) |
| 177 | + writeln(out, "}") |
| 178 | + default: |
| 179 | + e.emit(out, parentVar, node.Children[vt.GoName()]) |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + for _, handler := range node.Handlers { |
| 184 | + writeln(out, handler.Invocation(parentVar)) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func (e *Emitter) emitOneOfCases(out io.Writer, parentVar string, oneof VisitType, node *Tree) { |
| 189 | + for _, vt := range node.SortedTypes() { |
| 190 | + writef(out, "case *%s.%s:\n", oneof.GoPackageName(), getOneofWrapperType(oneof, vt)) |
| 191 | + varName, freeVar := e.makeVar("x") |
| 192 | + name := vt.GoName() |
| 193 | + writef(out, "%s := %s.%s\n", varName, parentVar, name) |
| 194 | + e.emit(out, varName, node.Children[vt.GoName()]) |
| 195 | + freeVar() |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func (e *Emitter) makeVar(name string) (string, func()) { |
| 200 | + i := 0 |
| 201 | + for { |
| 202 | + i++ |
| 203 | + name := fmt.Sprintf("%s%d", name, i) |
| 204 | + if _, ok := e.inScopeVars[name]; !ok { |
| 205 | + e.inScopeVars[name] = struct{}{} |
| 206 | + return name, func() { e.freeVar(name) } |
| 207 | + } |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func (e *Emitter) freeVar(name string) { |
| 212 | + delete(e.inScopeVars, name) |
| 213 | +} |
| 214 | + |
| 215 | +// Return the "wrapper" Golang interface for `oneof` fields. |
| 216 | +// |
| 217 | +// Protobuf `oneof` fields are generated as an interface: |
| 218 | +// |
| 219 | +// type ReplicationTask struct { |
| 220 | +// Attributes isReplicationTask_Attributes `protobuf_oneof:"attributes"` |
| 221 | +// ... |
| 222 | +// } |
| 223 | +// |
| 224 | +// The interface is implemented by "wrapper" types which seemingly do not appear |
| 225 | +// in the protobuf reflection registry, so we do not enounter these "wrapper" |
| 226 | +// type names while visiting the protobuf type hierachy. |
| 227 | +// |
| 228 | +// type ReplicationTask_SyncVersionedTransitionTaskAttributes struct { |
| 229 | +// SyncVersionedTransitionTaskAttributes *SyncVersionedTransitionTaskAttributes |
| 230 | +// } |
| 231 | +// |
| 232 | +// This returns the implementing type, e.g. "ReplicationTask_SyncVersionedTransitionTaskAttributes", |
| 233 | +// given the interface field (e.g. `Attributes`) and the wrapped field (e.g. `SyncVersionedTransitionTaskAttributes`) |
| 234 | +func getOneofWrapperType(oneof, typ VisitType) string { |
| 235 | + return string(oneof.Parent().Name()) + "_" + snakeToPascalCase(typ.Name()) |
| 236 | +} |
0 commit comments