mirror of
https://gitee.com/johng/gf
synced 2026-06-09 19:13:58 +08:00
Compare commits
6 Commits
contrib/dr
...
v2.1.4
| Author | SHA1 | Date | |
|---|---|---|---|
| a0619f7ff0 | |||
| 37aee19bfa | |||
| 27609d8da8 | |||
| c083b333d8 | |||
| ee376883d1 | |||
| 98169784b1 |
@ -331,6 +331,10 @@ func (c cGenService) generateServiceFiles(
|
||||
mlog.Printf(`not overwrite, ignore generating service go file: %s`, filePath)
|
||||
continue
|
||||
}
|
||||
if !utils.IsFileDoNotEdit(filePath) {
|
||||
mlog.Printf(`ignore file as it is manually maintained: %s`, filePath)
|
||||
continue
|
||||
}
|
||||
if !c.isToGenerateServiceGoFile(filePath, funcArray) {
|
||||
mlog.Printf(`not dirty, ignore generating service go file: %s`, filePath)
|
||||
continue
|
||||
@ -347,10 +351,6 @@ func (c cGenService) generateServiceFiles(
|
||||
|
||||
// isToGenerateServiceGoFile checks and returns whether the service content dirty.
|
||||
func (c cGenService) isToGenerateServiceGoFile(filePath string, funcArray *garray.StrArray) bool {
|
||||
if !utils.IsFileDoNotEdit(filePath) {
|
||||
mlog.Debugf(`ignore file as it is manually maintained: %s`, filePath)
|
||||
return false
|
||||
}
|
||||
var (
|
||||
fileContent = gfile.GetContents(filePath)
|
||||
generatedFuncArray = garray.NewSortedStrArrayFrom(funcArray.Slice())
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
package consts
|
||||
|
||||
const TemplateGenServiceContent = `
|
||||
// ==========================================================================
|
||||
// ================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// ==========================================================================
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package {PackageName}
|
||||
|
||||
|
||||
@ -132,7 +132,7 @@ func (c *Core) CheckLocalTypeForField(ctx context.Context, fieldType string, fie
|
||||
typeName = gstr.Trim(match[1])
|
||||
typePattern = gstr.Trim(match[2])
|
||||
} else {
|
||||
typeName = fieldType
|
||||
typeName = gstr.Split(fieldType, " ")[0]
|
||||
}
|
||||
typeName = strings.ToLower(typeName)
|
||||
switch typeName {
|
||||
@ -234,6 +234,9 @@ func (c *Core) CheckLocalTypeForField(ctx context.Context, fieldType string, fie
|
||||
return LocalTypeBytes, nil
|
||||
|
||||
case strings.Contains(typeName, "int"):
|
||||
if gstr.ContainsI(fieldType, "unsigned") {
|
||||
return LocalTypeUint, nil
|
||||
}
|
||||
return LocalTypeInt, nil
|
||||
|
||||
case strings.Contains(typeName, "time"):
|
||||
|
||||
@ -100,6 +100,7 @@ func doPrint(ctx context.Context, content string, stack bool) {
|
||||
buffer.WriteString(content)
|
||||
buffer.WriteString("\n")
|
||||
if stack {
|
||||
buffer.WriteString("Caller Stack:\n")
|
||||
buffer.WriteString(gdebug.StackWithFilter([]string{stackFilterKey}))
|
||||
}
|
||||
fmt.Print(buffer.String())
|
||||
|
||||
@ -202,6 +202,9 @@ func (oai *OpenApiV3) golangTypeToOAIFormat(t reflect.Type) string {
|
||||
return FormatBinary
|
||||
|
||||
default:
|
||||
if oai.isEmbeddedStructDefinition(t) {
|
||||
return `EmbeddedStructDefinition`
|
||||
}
|
||||
return format
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"reflect"
|
||||
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
type SchemaRefs []SchemaRef
|
||||
@ -19,8 +20,25 @@ type SchemaRef struct {
|
||||
Value *Schema
|
||||
}
|
||||
|
||||
// isEmbeddedStructDefine checks and returns whether given golang type is embedded struct definition, like:
|
||||
// struct A struct{
|
||||
// B struct{
|
||||
// // ...
|
||||
// }
|
||||
// }
|
||||
// The `B` in `A` is called `embedded struct definition`.
|
||||
func (oai *OpenApiV3) isEmbeddedStructDefinition(golangType reflect.Type) bool {
|
||||
s := golangType.String()
|
||||
if gstr.Contains(s, `struct {`) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// newSchemaRefWithGolangType creates a new Schema and returns its SchemaRef.
|
||||
func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap map[string]string) (*SchemaRef, error) {
|
||||
var (
|
||||
err error
|
||||
oaiType = oai.golangTypeToOAIType(golangType)
|
||||
oaiFormat = oai.golangTypeToOAIFormat(golangType)
|
||||
schemaRef = &SchemaRef{}
|
||||
@ -85,15 +103,24 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap
|
||||
schemaRef.Value = nil
|
||||
|
||||
default:
|
||||
// Normal struct object.
|
||||
var structTypeName = oai.golangTypeToSchemaName(golangType)
|
||||
if oai.Components.Schemas.Get(structTypeName) == nil {
|
||||
if err := oai.addSchema(reflect.New(golangType).Elem().Interface()); err != nil {
|
||||
golangTypeInstance := reflect.New(golangType).Elem().Interface()
|
||||
if oai.isEmbeddedStructDefinition(golangType) {
|
||||
schema, err = oai.structToSchema(golangTypeInstance)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
schemaRef.Ref = ""
|
||||
schemaRef.Value = schema
|
||||
} else {
|
||||
var structTypeName = oai.golangTypeToSchemaName(golangType)
|
||||
if oai.Components.Schemas.Get(structTypeName) == nil {
|
||||
if err := oai.addSchema(golangTypeInstance); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
schemaRef.Ref = structTypeName
|
||||
schemaRef.Value = nil
|
||||
}
|
||||
schemaRef.Ref = structTypeName
|
||||
schemaRef.Value = nil
|
||||
}
|
||||
}
|
||||
return schemaRef, nil
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/net/goai"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gmeta"
|
||||
@ -937,7 +938,7 @@ func Test_EnumOfSchemaItems(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AliasNameOfAtrribute(t *testing.T) {
|
||||
func Test_AliasNameOfAttribute(t *testing.T) {
|
||||
type CreateResourceReq struct {
|
||||
gmeta.Meta `path:"/CreateResourceReq" method:"POST"`
|
||||
Name string `p:"n"`
|
||||
@ -973,3 +974,29 @@ func Test_AliasNameOfAtrribute(t *testing.T) {
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_EmbeddedStructAttribute(t *testing.T) {
|
||||
type CreateResourceReq struct {
|
||||
gmeta.Meta `path:"/CreateResourceReq" method:"POST"`
|
||||
Name string `dc:"This is name."`
|
||||
Embedded struct {
|
||||
Age uint `dc:"This is embedded age."`
|
||||
}
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
err error
|
||||
oai = goai.New()
|
||||
req = new(CreateResourceReq)
|
||||
)
|
||||
err = oai.Add(goai.AddInput{
|
||||
Object: req,
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
b, err := json.Marshal(oai)
|
||||
t.AssertNil(err)
|
||||
t.Assert(b, `{"openapi":"3.0.0","components":{"schemas":{"github.com.gogf.gf.v2.net.goai_test.CreateResourceReq":{"properties":{"Name":{"description":"This is name.","format":"string","properties":{},"type":"string"},"Embedded":{"properties":{"Age":{"description":"This is embedded age.","format":"uint","properties":{},"type":"integer"}},"type":"object"}},"type":"object"}}},"info":{"title":"","version":""},"paths":null}`)
|
||||
})
|
||||
}
|
||||
|
||||
@ -113,7 +113,7 @@ func Uint64(any interface{}) uint64 {
|
||||
if valueFloat64 := Float64(value); math.IsNaN(valueFloat64) {
|
||||
return 0
|
||||
} else {
|
||||
return uint64(Float64(value))
|
||||
return uint64(valueFloat64)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -560,8 +560,13 @@ func (v *Validator) doCheckValueRecursively(ctx context.Context, in doCheckValue
|
||||
// Ignore data, assoc, rules and messages from parent.
|
||||
var (
|
||||
validator = v.Clone()
|
||||
toBeValidatedObject = reflect.New(in.Type).Interface()
|
||||
toBeValidatedObject interface{}
|
||||
)
|
||||
if in.Type.Kind() == reflect.Ptr {
|
||||
toBeValidatedObject = reflect.New(in.Type.Elem()).Interface()
|
||||
} else {
|
||||
toBeValidatedObject = reflect.New(in.Type).Interface()
|
||||
}
|
||||
validator.assoc = nil
|
||||
validator.rules = nil
|
||||
validator.messages = nil
|
||||
|
||||
@ -399,3 +399,25 @@ func Test_Issue1983(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/1921
|
||||
func Test_Issue1921(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type SearchOption struct {
|
||||
Size int `v:"max:100"`
|
||||
}
|
||||
type SearchReq struct {
|
||||
Option *SearchOption `json:"option,omitempty"`
|
||||
}
|
||||
|
||||
var (
|
||||
req = SearchReq{
|
||||
Option: &SearchOption{
|
||||
Size: 10000,
|
||||
},
|
||||
}
|
||||
)
|
||||
err := g.Validator().Data(req).Run(ctx)
|
||||
t.Assert(err, "The Size value `10000` must be equal or lesser than 100")
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package gf
|
||||
|
||||
const VERSION = "v2.1.3"
|
||||
const VERSION = "v2.1.4"
|
||||
const AUTHORS = "john<john@goframe.org>"
|
||||
|
||||
Reference in New Issue
Block a user