Merge pull request #517 from wuxingzhong/swagger_validate_comment

add validate comment
pull/532/head
Terry.Mao 5 years ago committed by GitHub
commit e51cfe1f24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      tool/protobuf/protoc-gen-bswagger/generator.go

@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"net/http"
"reflect"
"regexp"
"strings"
@ -184,6 +185,12 @@ func (t *swaggerGen) getQueryParameter(file *descriptor.FileDescriptorProto,
cleanComment := tag.GetCommentWithoutTag(fComment.Leading)
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.Required = generator.GetFieldRequired(field, t.Reg, input)
typ, isArray, format := getFieldSwaggerType(field)
@ -208,6 +215,12 @@ func (t *swaggerGen) schemaForField(file *descriptor.FileDescriptorProto,
gen.Error(err, "comment not found err %+v")
}
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)
if !generator.IsScalar(field) {
if generator.IsMap(field, t.Reg) {
@ -303,3 +316,26 @@ func getFieldSwaggerType(field *descriptor.FieldDescriptorProto) (typeName strin
}
return
}
func getValidateComment(field *descriptor.FieldDescriptorProto) string {
var (
tags []reflect.StructTag
)
//get required info from gogoproto.moretags
moretags := tag.GetMoreTags(field)
if moretags != nil {
tags = []reflect.StructTag{reflect.StructTag(*moretags)}
}
validateTag := tag.GetTagValue("validate", tags)
// trim
regStr := []string{
"required *,*",
"omitempty *,*",
}
for _, v := range regStr {
re, _ := regexp.Compile(v)
validateTag = re.ReplaceAllString(validateTag, "")
}
return validateTag
}

Loading…
Cancel
Save