|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "google.golang.org/protobuf/types/descriptorpb" |
| 10 | + "google.golang.org/protobuf/types/pluginpb" |
| 11 | +) |
| 12 | + |
| 13 | +type responseBuilder struct { |
| 14 | + request *pluginpb.CodeGeneratorRequest |
| 15 | +} |
| 16 | + |
| 17 | +func buildResponseError(err error) *pluginpb.CodeGeneratorResponse { |
| 18 | + errorMessage := err.Error() |
| 19 | + return &pluginpb.CodeGeneratorResponse{Error: &errorMessage} |
| 20 | +} |
| 21 | + |
| 22 | +func (b responseBuilder) build() *pluginpb.CodeGeneratorResponse { |
| 23 | + resp := new(pluginpb.CodeGeneratorResponse) |
| 24 | + for _, fileName := range b.request.GetFileToGenerate() { |
| 25 | + respFile, err := b.buildFile(fileName) |
| 26 | + if err != nil { |
| 27 | + errorMessage := err.Error() |
| 28 | + resp.Error = &errorMessage |
| 29 | + break |
| 30 | + } |
| 31 | + resp.File = append(resp.File, respFile) |
| 32 | + } |
| 33 | + return resp |
| 34 | +} |
| 35 | + |
| 36 | +func (b responseBuilder) buildFile(reqFileName string) (*pluginpb.CodeGeneratorResponse_File, error) { |
| 37 | + respFileName := regexp.MustCompile(`.proto$`).ReplaceAllString(reqFileName, ".pubsub.proto") |
| 38 | + content, err := b.buildContent(b.findProtoFileByName(reqFileName)) |
| 39 | + return &pluginpb.CodeGeneratorResponse_File{ |
| 40 | + Name: &respFileName, |
| 41 | + Content: &content, |
| 42 | + }, err |
| 43 | +} |
| 44 | + |
| 45 | +func (b responseBuilder) findProtoFileByName(desiredName string) *descriptorpb.FileDescriptorProto { |
| 46 | + for _, protoFile := range b.request.GetProtoFile() { |
| 47 | + if protoFile.GetName() == desiredName { |
| 48 | + return protoFile |
| 49 | + } |
| 50 | + } |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func (b responseBuilder) findMessageByName(desiredName string) *descriptorpb.DescriptorProto { |
| 55 | + for _, protoFile := range b.request.GetProtoFile() { |
| 56 | + packageName := strings.TrimSuffix("."+protoFile.GetPackage(), ".") |
| 57 | + nestedResult := b.findNestedMessageByName(desiredName, protoFile.GetMessageType(), packageName) |
| 58 | + if nestedResult != nil { |
| 59 | + return nestedResult |
| 60 | + } |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (b responseBuilder) findNestedMessageByName(desiredName string, messages []*descriptorpb.DescriptorProto, prefix string) *descriptorpb.DescriptorProto { |
| 66 | + for _, message := range messages { |
| 67 | + fullMessageName := prefix + "." + message.GetName() |
| 68 | + if fullMessageName == desiredName { |
| 69 | + return message |
| 70 | + } |
| 71 | + |
| 72 | + nestedResult := b.findNestedMessageByName(desiredName, message.GetNestedType(), fullMessageName) |
| 73 | + if nestedResult != nil { |
| 74 | + return nestedResult |
| 75 | + } |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (b responseBuilder) buildContent(protoFile *descriptorpb.FileDescriptorProto) (string, error) { |
| 81 | + if len(protoFile.GetMessageType()) != 1 { |
| 82 | + return "", fmt.Errorf( |
| 83 | + "only one top-level type may be defined in the file %s. use nested type instead (https://developers.google.com/protocol-buffers/docs/proto3#nested)", |
| 84 | + protoFile.GetName(), |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + contentBuilder := new(strings.Builder) |
| 89 | + fmt.Fprint(contentBuilder, "syntax = \"proto3\";\n\n") |
| 90 | + b.buildMessage(contentBuilder, protoFile.GetMessageType()[0], 0) |
| 91 | + return contentBuilder.String(), nil |
| 92 | +} |
| 93 | + |
| 94 | +func (b responseBuilder) buildMessage(output io.Writer, message *descriptorpb.DescriptorProto, level int) { |
| 95 | + fmt.Fprintf(output, "%smessage %s {\n", buildIndent(level), message.GetName()) |
| 96 | + for _, field := range message.GetField() { |
| 97 | + b.buildField(output, field, level+1) |
| 98 | + } |
| 99 | + fmt.Fprintf(output, "%s}\n", buildIndent(level)) |
| 100 | +} |
| 101 | + |
| 102 | +func (b responseBuilder) buildField(output io.Writer, field *descriptorpb.FieldDescriptorProto, level int) { |
| 103 | + fieldType := strings.ToLower(strings.Replace(field.GetType().String(), "TYPE_", "", 1)) |
| 104 | + if field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_MESSAGE { |
| 105 | + fieldTypeName := field.GetTypeName() |
| 106 | + b.buildMessage(output, b.findMessageByName(fieldTypeName), level) |
| 107 | + fieldType = fieldTypeName[strings.LastIndexByte(fieldTypeName, '.')+1:] |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Fprint(output, buildIndent(level)) |
| 111 | + if field.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REPEATED { |
| 112 | + fmt.Fprint(output, "repeated ") |
| 113 | + } |
| 114 | + fmt.Fprintf(output, "%s %s = %d;\n", fieldType, field.GetName(), field.GetNumber()) |
| 115 | +} |
| 116 | + |
| 117 | +func buildIndent(level int) string { |
| 118 | + return strings.Repeat(" ", level) |
| 119 | +} |
0 commit comments