Skip to content

Commit 761d8bc

Browse files
committed
Refactor proto comment extractor
1 parent df545dd commit 761d8bc

File tree

1 file changed

+54
-47
lines changed

1 file changed

+54
-47
lines changed

protoc-gen-swagger/genswagger/template.go

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -559,54 +559,9 @@ func protoComments(reg *descriptor.Registry, file *descriptor.File, outers []str
559559
outerPaths[i] = int32(msg.Index)
560560
}
561561

562-
messageProtoPath := protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), "MessageType")
563-
nestedProtoPath := protoPathIndex(reflect.TypeOf((*pbdescriptor.DescriptorProto)(nil)), "NestedType")
564-
packageProtoPath := protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), "Package")
565-
L1:
566562
for _, loc := range file.SourceCodeInfo.Location {
567-
if typeName != "Package" || typeIndex != packageProtoPath {
568-
if len(loc.Path) < len(outerPaths)*2+2+len(fieldPaths) {
569-
continue
570-
}
571-
for i, v := range outerPaths {
572-
if i == 0 && loc.Path[i*2+0] != messageProtoPath {
573-
continue L1
574-
}
575-
if i != 0 && loc.Path[i*2+0] != nestedProtoPath {
576-
continue L1
577-
}
578-
if loc.Path[i*2+1] != v {
579-
continue L1
580-
}
581-
}
582-
583-
outerOffset := len(outerPaths) * 2
584-
if outerOffset == 0 && loc.Path[outerOffset] != protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), typeName) {
585-
continue
586-
}
587-
if outerOffset != 0 {
588-
if typeName == "MessageType" {
589-
typeName = "NestedType"
590-
}
591-
if loc.Path[outerOffset] != protoPathIndex(reflect.TypeOf((*pbdescriptor.DescriptorProto)(nil)), typeName) {
592-
continue
593-
}
594-
}
595-
if loc.Path[outerOffset+1] != typeIndex {
596-
continue
597-
}
598-
599-
for i, v := range fieldPaths {
600-
if loc.Path[outerOffset+2+i] != v {
601-
continue L1
602-
}
603-
}
604-
} else {
605-
// path for package comments is just [2], and all the other processing
606-
// is too complex for it.
607-
if len(loc.Path) == 0 || typeIndex != loc.Path[0] {
608-
continue
609-
}
563+
if !isProtoPathMatches(loc.Path, outerPaths, typeName, typeIndex, fieldPaths) {
564+
continue
610565
}
611566
comments := ""
612567
if loc.LeadingComments != nil {
@@ -625,6 +580,58 @@ L1:
625580
return ""
626581
}
627582

583+
var messageProtoPath = protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), "MessageType")
584+
var nestedProtoPath = protoPathIndex(reflect.TypeOf((*pbdescriptor.DescriptorProto)(nil)), "NestedType")
585+
var packageProtoPath = protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), "Package")
586+
587+
func isProtoPathMatches(paths []int32, outerPaths []int32, typeName string, typeIndex int32, fieldPaths []int32) bool {
588+
if typeName == "Package" && typeIndex == packageProtoPath {
589+
// path for package comments is just [2], and all the other processing
590+
// is too complex for it.
591+
if len(paths) == 0 || typeIndex != paths[0] {
592+
return false
593+
}
594+
return true
595+
}
596+
597+
if len(paths) != len(outerPaths)*2+2+len(fieldPaths) {
598+
return false
599+
}
600+
601+
typeNameDescriptor := reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil))
602+
if len(outerPaths) > 0 {
603+
if paths[0] != messageProtoPath || paths[1] != outerPaths[0] {
604+
return false
605+
}
606+
paths = paths[2:]
607+
outerPaths = outerPaths[1:]
608+
609+
for i, v := range outerPaths {
610+
if paths[i*2] != nestedProtoPath || paths[i*2+1] != v {
611+
return false
612+
}
613+
}
614+
paths = paths[len(outerPaths)*2:]
615+
616+
if typeName == "MessageType" {
617+
typeName = "NestedType"
618+
}
619+
typeNameDescriptor = reflect.TypeOf((*pbdescriptor.DescriptorProto)(nil))
620+
}
621+
622+
if paths[0] != protoPathIndex(typeNameDescriptor, typeName) || paths[1] != typeIndex {
623+
return false
624+
}
625+
paths = paths[2:]
626+
627+
for i, v := range fieldPaths {
628+
if paths[i] != v {
629+
return false
630+
}
631+
}
632+
return true
633+
}
634+
628635
// protoPathIndex returns a path component for google.protobuf.descriptor.SourceCode_Location.
629636
//
630637
// Specifically, it returns an id as generated from descriptor proto which

0 commit comments

Comments
 (0)