dev
This commit is contained in:
59
internal/plugin/commentgen.go
Normal file
59
internal/plugin/commentgen.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-tools/protoc-gen-ts/internal/codegen"
|
||||
"google.golang.org/genproto/googleapis/api/annotations"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
)
|
||||
|
||||
type commentGenerator struct {
|
||||
descriptor protoreflect.Descriptor
|
||||
}
|
||||
|
||||
func (c commentGenerator) generateLeading(f *codegen.File, indent int) {
|
||||
loc := c.descriptor.ParentFile().SourceLocations().ByDescriptor(c.descriptor)
|
||||
var comments string
|
||||
if loc.TrailingComments != "" {
|
||||
comments = comments + loc.TrailingComments
|
||||
}
|
||||
if loc.LeadingComments != "" {
|
||||
comments = comments + loc.LeadingComments
|
||||
}
|
||||
lines := strings.Split(comments, "\n")
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
f.P(t(indent), "/** "+strings.TrimSpace(line)+" */ ")
|
||||
}
|
||||
if field, ok := c.descriptor.(protoreflect.FieldDescriptor); ok {
|
||||
if behaviorComment := fieldBehaviorComment(field); len(behaviorComment) > 0 {
|
||||
f.P(t(indent), "/** "+behaviorComment+" */")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fieldBehaviorComment(field protoreflect.FieldDescriptor) string {
|
||||
behaviors := getFieldBehaviors(field)
|
||||
if len(behaviors) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
behaviorStrings := make([]string, 0, len(behaviors))
|
||||
for _, b := range behaviors {
|
||||
behaviorStrings = append(behaviorStrings, b.String())
|
||||
}
|
||||
return "Behaviors: " + strings.Join(behaviorStrings, ", ")
|
||||
}
|
||||
|
||||
func getFieldBehaviors(field protoreflect.FieldDescriptor) []annotations.FieldBehavior {
|
||||
if behaviors, ok := proto.GetExtension(
|
||||
field.Options(), annotations.E_FieldBehavior,
|
||||
).([]annotations.FieldBehavior); ok {
|
||||
return behaviors
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user