forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from suyuan32/feat-validator-gen
Feat: validator gen
- Loading branch information
Showing
5 changed files
with
376 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2023 The Ryan SU Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ConvertValidateTagToSwagger converts the validator tag to swagger comments. | ||
func ConvertValidateTagToSwagger(tagData string) ([]string, error) { | ||
if tagData == "" || !strings.Contains(tagData, "validate") { | ||
return nil, nil | ||
} | ||
|
||
validateData := ExtractValidateString(tagData) | ||
|
||
return ConvertTagToComment(validateData) | ||
} | ||
|
||
// ExtractValidateString extracts the validator's string. | ||
func ExtractValidateString(data string) string { | ||
beginIndex := strings.Index(data, "validate") | ||
if beginIndex == -1 { | ||
return "" | ||
} | ||
firstQuotationMark := 0 | ||
for i := beginIndex; i < len(data); i++ { | ||
if data[i] == '"' && firstQuotationMark == 0 { | ||
firstQuotationMark = i | ||
} else if data[i] == '"' && firstQuotationMark != 0 { | ||
return data[firstQuotationMark+1 : i] | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
// ConvertTagToComment converts validator tag to comments. | ||
func ConvertTagToComment(tagString string) ([]string, error) { | ||
var result []string | ||
vals := strings.Split(tagString, ",") | ||
for _, v := range vals { | ||
if strings.Contains(v, "required") { | ||
result = append(result, "// required : true\n") | ||
} | ||
|
||
if strings.Contains(v, "min") || strings.Contains(v, "max") { | ||
result = append(result, fmt.Sprintf("// %s\n", strings.Replace(v, "=", " length : ", -1))) | ||
} | ||
|
||
if strings.Contains(v, "len") { | ||
tagSplit := strings.Split(v, "=") | ||
_, tagNum := tagSplit[0], tagSplit[1] | ||
result = append(result, fmt.Sprintf("// max length : %s\n", tagNum)) | ||
result = append(result, fmt.Sprintf("// min length : %s\n", tagNum)) | ||
} | ||
|
||
if strings.Contains(v, "gt") || strings.Contains(v, "gte") || | ||
strings.Contains(v, "lt") || strings.Contains(v, "lte") { | ||
tagSplit := strings.Split(v, "=") | ||
tag, tagNum := tagSplit[0], tagSplit[1] | ||
if strings.Contains(tagNum, ".") { | ||
bitSize := len(tagNum) - strings.Index(tagNum, ".") - 1 | ||
n, err := strconv.ParseFloat(tagNum, bitSize) | ||
if err != nil { | ||
return nil, errors.New("failed to convert the number in validate tag") | ||
} | ||
|
||
switch tag { | ||
case "gte": | ||
result = append(result, fmt.Sprintf("// min : %.*f\n", bitSize, n)) | ||
case "gt": | ||
result = append(result, fmt.Sprintf("// min : %.*f\n", bitSize, n+1/math.Pow(10, float64(bitSize)))) | ||
case "lte": | ||
result = append(result, fmt.Sprintf("// max : %.*f\n", bitSize, n)) | ||
case "lt": | ||
result = append(result, fmt.Sprintf("// max : %.*f\n", bitSize, n-1/math.Pow(10, float64(bitSize)))) | ||
} | ||
} else { | ||
n, err := strconv.Atoi(tagNum) | ||
if err != nil { | ||
return nil, errors.New("failed to convert the number in validate tag") | ||
} | ||
|
||
switch tag { | ||
case "gte": | ||
result = append(result, fmt.Sprintf("// min : %d\n", n)) | ||
case "gt": | ||
result = append(result, fmt.Sprintf("// min : %d\n", n)) | ||
case "lte": | ||
result = append(result, fmt.Sprintf("// max : %d\n", n)) | ||
case "lt": | ||
result = append(result, fmt.Sprintf("// max : %d\n", n)) | ||
} | ||
} | ||
|
||
} | ||
} | ||
return result, nil | ||
} | ||
|
||
// HasCustomValidation returns true if the comment has validations. | ||
func HasCustomValidation(data string) bool { | ||
lowerCase := strings.ToLower(data) | ||
if strings.Contains(lowerCase, "max") || strings.Contains(lowerCase, "min") || | ||
strings.Contains(lowerCase, "required") { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package util | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestConvertValidateTagToSwagger(t *testing.T) { | ||
type args struct { | ||
tagData string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "testString", | ||
args: args{"json:\"path,optional\" validate:\"omitempty,min=1,max=50\""}, | ||
want: []string{"// min length : 1\n", "// max length : 50\n"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "testLen", | ||
args: args{"json:\"path,optional\" validate:\"omitempty,len=50\""}, | ||
want: []string{"// max length : 50\n", "// min length : 50\n"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "testNum", | ||
args: args{"json:\"path,optional\" validate:\"omitempty,gte=1,lte=50\""}, | ||
want: []string{"// min : 1\n", "// max : 50\n"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "testFloat", | ||
args: args{"json:\"path,optional\" validate:\"omitempty,gte=1.1,lte=50.0\""}, | ||
want: []string{"// min : 1.1\n", "// max : 50.0\n"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "testFloat2", | ||
args: args{"json:\"path,optional\" validate:\"omitempty,gt=1.11,lt=50.01\""}, | ||
want: []string{"// min : 1.12\n", "// max : 50.00\n"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "testRequired", | ||
args: args{"json:\"path,optional\" validate:\"required,gte=1.1,lte=50.0\""}, | ||
want: []string{"// required : true\n", "// min : 1.1\n", "// max : 50.0\n"}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ConvertValidateTagToSwagger(tt.args.tagData) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ConvertValidateTagToSwagger() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ConvertValidateTagToSwagger() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.