Compare commits

...

6 Commits

Author SHA1 Message Date
a0619f7ff0 remove uint repeat conversion (#2096)
Co-authored-by: houseme <housemecn@gmail.com>
2022-08-26 15:45:41 +08:00
37aee19bfa new release v2.1.4 (#2095)
v2.1.4
2022-08-26 15:05:45 +08:00
27609d8da8 fix issue #1921 (#2091)
* CI updates

* fix issue in OpenAPI json marshaling of embedded struct definition; improve command gen service

* improve logging content printing for internal log

* fix issue #1921
2022-08-26 14:30:49 +08:00
c083b333d8 fix field type check for package gdb (#2086)
* CI updates

* fix field type check for package gdb
2022-08-26 14:30:33 +08:00
ee376883d1 improve logging content printing for internal log (#2090)
* CI updates

* fix issue in OpenAPI json marshaling of embedded struct definition; improve command gen service

* improve logging content printing for internal log
2022-08-26 14:30:12 +08:00
98169784b1 fix issue in OpenAPI json marshaling of embedded struct definition; improve command gen service (#2089)
* CI updates

* fix issue in OpenAPI json marshaling of embedded struct definition; improve command gen service
2022-08-24 21:20:17 +08:00
11 changed files with 106 additions and 17 deletions

View File

@ -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())

View File

@ -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}

View File

@ -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"):

View File

@ -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())

View File

@ -202,6 +202,9 @@ func (oai *OpenApiV3) golangTypeToOAIFormat(t reflect.Type) string {
return FormatBinary
default:
if oai.isEmbeddedStructDefinition(t) {
return `EmbeddedStructDefinition`
}
return format
}
}

View File

@ -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

View File

@ -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}`)
})
}

View File

@ -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)
}
}
}

View File

@ -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

View File

@ -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")
})
}

View File

@ -1,4 +1,4 @@
package gf
const VERSION = "v2.1.3"
const VERSION = "v2.1.4"
const AUTHORS = "john<john@goframe.org>"