Skip to content

Commit

Permalink
fix 3800 (#6)
Browse files Browse the repository at this point in the history
* fix(net/goai): change default value of RequestBody.Required from true to false, add required tag support for RequestBody (gogf#3796)

* fix(database/gdb): support OrderRandom feature in different databases (gogf#3794)

* up

* fix issue 3800

---------

Co-authored-by: oldme <45782393+oldme-git@users.noreply.github.com>
  • Loading branch information
gqcn and oldme-git authored Sep 24, 2024
1 parent f1b39af commit 5ccd90b
Show file tree
Hide file tree
Showing 21 changed files with 432 additions and 40 deletions.
4 changes: 2 additions & 2 deletions container/glist/glist_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func TestRemove(t *testing.T) {
})
}

func TestIssue4103(t *testing.T) {
func Test_Issue4103(t *testing.T) {
l1 := New()
l1.PushBack(1)
l1.PushBack(2)
Expand All @@ -312,7 +312,7 @@ func TestIssue4103(t *testing.T) {
}
}

func TestIssue6349(t *testing.T) {
func Test_Issue6349(t *testing.T) {
l := New()
l.PushBack(1)
l.PushBack(2)
Expand Down
12 changes: 12 additions & 0 deletions contrib/drivers/oracle/oracle_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package oracle

// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "DBMS_RANDOM.VALUE()"
}
11 changes: 11 additions & 0 deletions contrib/drivers/oracle/oracle_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,17 @@ func Test_Model_Replace(t *testing.T) {
})
}

func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}

/* not support the "AS"
func Test_Model_Raw(t *testing.T) {
table := createInitTable()
Expand Down
12 changes: 12 additions & 0 deletions contrib/drivers/pgsql/pgsql_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package pgsql

// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "RANDOM()"
}
11 changes: 11 additions & 0 deletions contrib/drivers/pgsql/pgsql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,14 @@ func Test_Model_OnDuplicateEx(t *testing.T) {
t.Assert(one["nickname"], "name_1")
})
}

func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}
12 changes: 12 additions & 0 deletions contrib/drivers/sqlite/sqlite_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package sqlite

// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "RANDOM()"
}
11 changes: 11 additions & 0 deletions contrib/drivers/sqlite/sqlite_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4299,3 +4299,14 @@ func TestResult_Structs1(t *testing.T) {
t.Assert(array[1].Name, "smith")
})
}

func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}
1 change: 1 addition & 0 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type DB interface {
ConvertValueForLocal(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) // See Core.ConvertValueForLocal
CheckLocalTypeForField(ctx context.Context, fieldType string, fieldValue interface{}) (LocalType, error) // See Core.CheckLocalTypeForField
FormatUpsert(columns []string, list List, option DoInsertOption) (string, error) // See Core.DoFormatUpsert
OrderRandomFunction() string // See Core.OrderRandomFunction
}

// TX defines the interfaces for ORM transaction operations.
Expand Down
5 changes: 5 additions & 0 deletions database/gdb/gdb_core_underlying.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ func (c *Core) RowsToResult(ctx context.Context, rows *sql.Rows) (Result, error)
return result, nil
}

// OrderRandomFunction returns the SQL function for random ordering.
func (c *Core) OrderRandomFunction() string {
return "RAND()"
}

func (c *Core) columnValueToLocalValue(ctx context.Context, value interface{}, columnType *sql.ColumnType) (interface{}, error) {
var scanType = columnType.ScanType()
if scanType != nil {
Expand Down
2 changes: 1 addition & 1 deletion database/gdb/gdb_model_order_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (m *Model) OrderDesc(column string) *Model {
// OrderRandom sets the "ORDER BY RANDOM()" statement for the model.
func (m *Model) OrderRandom() *Model {
model := m.getModel()
model.orderBy = "RAND()"
model.orderBy = m.db.OrderRandomFunction()
return model
}

Expand Down
2 changes: 1 addition & 1 deletion internal/deepcopy/deepcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ func TestPointerToStruct(t *testing.T) {
}
}

func TestIssue9(t *testing.T) {
func Test_Issue9(t *testing.T) {
// simple pointer copy
x := 42
testA := map[string]*int{
Expand Down
1 change: 1 addition & 0 deletions net/ghttp/ghttp_server_config_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (s *Server) SetSwaggerUITemplate(swaggerUITemplate string) {
}

// SetOpenApiPath sets the OpenApiPath for server.
// For example: SetOpenApiPath("/api.json")
func (s *Server) SetOpenApiPath(path string) {
s.config.OpenApiPath = path
}
2 changes: 1 addition & 1 deletion net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func (Issue3789) Say(ctx context.Context, req *Issue3789Req) (res *Issue3789Res,
}

// https://github.com/gogf/gf/issues/3789
func TestIssue3789(t *testing.T) {
func Test_Issue3789(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server()
s.Use(ghttp.MiddlewareHandlerResponse)
Expand Down
9 changes: 5 additions & 4 deletions net/goai/goai_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,16 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
if operation.RequestBody.Value == nil {
var (
requestBody = RequestBody{
Required: true,
Content: map[string]MediaType{},
Content: map[string]MediaType{},
}
)
// Supported mime types of request.
var (
contentTypes = oai.Config.ReadContentTypes
tagMimeValue = gmeta.Get(inputObject.Interface(), gtag.Mime).String()
contentTypes = oai.Config.ReadContentTypes
tagMimeValue = gmeta.Get(inputObject.Interface(), gtag.Mime).String()
tagRequiredValue = gmeta.Get(inputObject.Interface(), gtag.Required).Bool()
)
requestBody.Required = tagRequiredValue
if tagMimeValue != "" {
contentTypes = gstr.SplitAndTrim(tagMimeValue, ",")
}
Expand Down
72 changes: 72 additions & 0 deletions net/goai/goai_z_unit_issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package goai_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
)

var ctx = context.Background()

type Issue3664DefaultReq struct {
g.Meta `path:"/default" method:"post"`
Name string
}
type Issue3664DefaultRes struct{}

type Issue3664RequiredTagReq struct {
g.Meta `path:"/required-tag" required:"true" method:"post"`
Name string
}
type Issue3664RequiredTagRes struct{}

type Issue3664 struct{}

func (Issue3664) Default(ctx context.Context, req *Issue3664DefaultReq) (res *Issue3664DefaultRes, err error) {
res = &Issue3664DefaultRes{}
return
}

func (Issue3664) RequiredTag(ctx context.Context, req *Issue3664RequiredTagReq) (res *Issue3664RequiredTagRes, err error) {
res = &Issue3664RequiredTagRes{}
return
}

// https://github.com/gogf/gf/issues/3664
func Test_Issue3664(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server()
s.Use(ghttp.MiddlewareHandlerResponse)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(
new(Issue3664),
)
})
s.SetLogger(nil)
s.SetOpenApiPath("/api.json")
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
apiContent := c.GetContent(ctx, "/api.json")
j, err := gjson.LoadJson(apiContent)
t.AssertNil(err)
t.Assert(j.Get(`paths./default.post.requestBody.required`).String(), "")
t.Assert(j.Get(`paths./required-tag.post.requestBody.required`).String(), "true")
})
}
8 changes: 8 additions & 0 deletions os/glog/glog_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,19 @@ func (l *Logger) getFpFromPool(ctx context.Context, path string) *gfpool.File {

// printStd prints content `s` without stack.
func (l *Logger) printStd(ctx context.Context, level int, values ...interface{}) {
// nil logger, print nothing
if l == nil {
return
}
l.print(ctx, level, "", values...)
}

// printErr prints content `s` with stack check.
func (l *Logger) printErr(ctx context.Context, level int, values ...interface{}) {
// nil logger, print nothing
if l == nil {
return
}
var stack string
if l.config.StStatus == 1 {
stack = l.GetStack()
Expand Down
4 changes: 4 additions & 0 deletions os/glog/glog_logger_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,9 @@ func (l *Logger) Criticalf(ctx context.Context, format string, v ...interface{})

// checkLevel checks whether the given `level` could be output.
func (l *Logger) checkLevel(level int) bool {
// nil logger, print nothing
if l == nil {
return false
}
return l.config.Level&level > 0
}
Loading

0 comments on commit 5ccd90b

Please sign in to comment.