add validate comment

pull/517/head
wuxingzhong 5 years ago
parent 7c946dc919
commit f3bb1b1ee0
  1. 30
      tool/protobuf/protoc-gen-bswagger/generator.go

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"reflect"
"regexp" "regexp"
"strings" "strings"
@ -184,6 +185,12 @@ func (t *swaggerGen) getQueryParameter(file *descriptor.FileDescriptorProto,
cleanComment := tag.GetCommentWithoutTag(fComment.Leading) cleanComment := tag.GetCommentWithoutTag(fComment.Leading)
p.Description = strings.Trim(strings.Join(cleanComment, "\n"), "\n\r ") p.Description = strings.Trim(strings.Join(cleanComment, "\n"), "\n\r ")
validateComment := getValidateComment(field)
if p.Description != "" && validateComment != "" {
p.Description = p.Description + "," + validateComment
} else if validateComment != "" {
p.Description = validateComment
}
p.In = "query" p.In = "query"
p.Required = generator.GetFieldRequired(field, t.Reg, input) p.Required = generator.GetFieldRequired(field, t.Reg, input)
typ, isArray, format := getFieldSwaggerType(field) typ, isArray, format := getFieldSwaggerType(field)
@ -208,6 +215,12 @@ func (t *swaggerGen) schemaForField(file *descriptor.FileDescriptorProto,
gen.Error(err, "comment not found err %+v") gen.Error(err, "comment not found err %+v")
} }
schema.Description = strings.Trim(fComment.Leading, "\n\r ") schema.Description = strings.Trim(fComment.Leading, "\n\r ")
validateComment := getValidateComment(field)
if schema.Description != "" && validateComment != "" {
schema.Description = schema.Description + "," + validateComment
} else if validateComment != "" {
schema.Description = validateComment
}
typ, isArray, format := getFieldSwaggerType(field) typ, isArray, format := getFieldSwaggerType(field)
if !generator.IsScalar(field) { if !generator.IsScalar(field) {
if generator.IsMap(field, t.Reg) { if generator.IsMap(field, t.Reg) {
@ -303,3 +316,20 @@ func getFieldSwaggerType(field *descriptor.FieldDescriptorProto) (typeName strin
} }
return return
} }
func getValidateComment(field *descriptor.FieldDescriptorProto) string {
var (
tags []reflect.StructTag
)
comment := ""
//get required info from gogoproto.moretags
moretags := tag.GetMoreTags(field)
if moretags != nil {
tags = []reflect.StructTag{reflect.StructTag(*moretags)}
}
validateTag := tag.GetTagValue("validate", tags)
if len(validateTag) > 0 {
comment = validateTag
}
return comment
}

Loading…
Cancel
Save