mirror of
https://gitee.com/johng/gf
synced 2026-06-08 02:27:42 +08:00
Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4fb739615b | |||
| ba39323d30 | |||
| 383937fe69 | |||
| f919f90bf5 | |||
| b4f6f06ab5 | |||
| 73fbca40ca | |||
| e8d3fcac6b | |||
| f87182d5b8 | |||
| 951f8921cd | |||
| d26b7c5437 | |||
| 9407fda197 | |||
| 905913f7be | |||
| 4b8eaac73f | |||
| ca242ff401 | |||
| 42e3c5f39a | |||
| 4f4d2c2f8e | |||
| 5a01798481 | |||
| 8af1eb693e | |||
| 335ccb32af | |||
| e1f2666499 | |||
| 22fcfdf755 | |||
| 1e21b61a19 | |||
| 6abd8bd864 | |||
| 4876ae0e2b | |||
| 63bdf41d40 | |||
| 984cca8b82 | |||
| 9f7ce42c74 | |||
| 16a43bcb6c | |||
| 85d95e3e27 | |||
| 4cce8557e6 | |||
| d60d7674d7 | |||
| 7f9467d1f8 | |||
| d08e3ef838 | |||
| 645c5ff5b5 |
38
.github/ISSUE_TEMPLATE.MD
vendored
38
.github/ISSUE_TEMPLATE.MD
vendored
@ -1,38 +0,0 @@
|
||||
<!-- Please answer these questions before submitting your issue. Thanks! -->
|
||||
|
||||
<!-- 为高效处理您的疑问,如果觉得是BUG类问题,请您务必提供可复现该问题的最小可运行代码!否则issue可能会被延期处理! -->
|
||||
<!-- 为高效处理您的疑问,如果觉得是BUG类问题,请您务必提供可复现该问题的最小可运行代码!否则issue可能会被延期处理! -->
|
||||
<!-- 为高效处理您的疑问,如果觉得是BUG类问题,请您务必提供可复现该问题的最小可运行代码!否则issue可能会被延期处理! -->
|
||||
<!-- 重要的事情说三遍! -->
|
||||
|
||||
### 1. What version of `Go` and system type/arch are you using?
|
||||
<!--
|
||||
Please paste the output of command `go version` from your terminal.
|
||||
What expect to see is like: `go 1.12, linux/amd64`
|
||||
-->
|
||||
|
||||
|
||||
### 2. What version of `GoFrame` are you using?
|
||||
<!-- You can find the GF version from your `go.mod`, or from the `version.go` in `GF` -->
|
||||
|
||||
|
||||
### 3. Can this issue be re-produced with the latest release?
|
||||
|
||||
|
||||
|
||||
### 4. What did you do?
|
||||
<!--
|
||||
If possible, provide a copy of shortest codes for reproducing the error.
|
||||
A complete runnable program is best.
|
||||
-->
|
||||
|
||||
|
||||
|
||||
### 5. What did you expect to see?
|
||||
|
||||
|
||||
|
||||
### 6. What did you see instead?
|
||||
|
||||
|
||||
|
||||
41
.github/ISSUE_TEMPLATE/bug-report.md
vendored
Normal file
41
.github/ISSUE_TEMPLATE/bug-report.md
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: bug
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
<!-- Please answer these questions before submitting your issue. Thanks! -->
|
||||
|
||||
<!-- 为高效处理 Bug,请您务必提供可复现该问题的最小可运行代码!否则 issue 可能会被延期处理! -->
|
||||
<!-- 为高效处理 Bug,请您务必提供可复现该问题的最小可运行代码!否则 issue 可能会被延期处理! -->
|
||||
<!-- 为高效处理 Bug,如请您务必提供可复现该问题的最小可运行代码!否则 issue 可能会被延期处理! -->
|
||||
<!-- 重要的事情说三遍! -->
|
||||
|
||||
**What version of `Go` and system type/arch are you using?**
|
||||
<!--
|
||||
Please paste the output of command `go version` from your terminal.
|
||||
What expects to see is like: `go 1.12, linux/amd64`
|
||||
-->
|
||||
|
||||
|
||||
**What version of `GoFrame` are you using?**
|
||||
<!-- You can find the GF version from your `go.mod`, or from the `version.go` in `GF` -->
|
||||
|
||||
|
||||
**Can this bug be re-produced with the latest release?**
|
||||
|
||||
|
||||
**What did you do?**
|
||||
<!--
|
||||
If possible, provide a copy of shortest codes for reproducing the error.
|
||||
A complete runnable program is best.
|
||||
-->
|
||||
|
||||
|
||||
**What did you expect to see?**
|
||||
|
||||
|
||||
**What did you see instead?**
|
||||
16
.github/ISSUE_TEMPLATE/enhancement-request.md
vendored
Normal file
16
.github/ISSUE_TEMPLATE/enhancement-request.md
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
---
|
||||
name: Enhancement request
|
||||
about: Enhance an idea for this project
|
||||
title: ''
|
||||
labels: enhancement
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Package that You wish to enhance**
|
||||
|
||||
|
||||
**Enhancement description**
|
||||
|
||||
|
||||
**Additional**
|
||||
19
.github/ISSUE_TEMPLATE/feature-request.md
vendored
Normal file
19
.github/ISSUE_TEMPLATE/feature-request.md
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: feature
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
|
||||
|
||||
**Describe the solution you'd like**
|
||||
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
|
||||
|
||||
**Additional**
|
||||
11
.github/ISSUE_TEMPLATE/question.md
vendored
Normal file
11
.github/ISSUE_TEMPLATE/question.md
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
---
|
||||
name: Question
|
||||
about: I want to ask a question
|
||||
title: ''
|
||||
labels: question
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
<!-- 请优先仔细阅读文档 -->
|
||||
**What do you want to ask**
|
||||
6
.github/workflows/gitee-sync.yml
vendored
6
.github/workflows/gitee-sync.yml
vendored
@ -1,7 +1,7 @@
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
tags:
|
||||
- "*"
|
||||
|
||||
@ -13,8 +13,8 @@ jobs:
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
uses: actions/checkout@v4
|
||||
- name: Mirror Github to Gitee
|
||||
uses: Yikun/hub-mirror-action@v1.2
|
||||
- name: Mirror GitHub to Gitee
|
||||
uses: Yikun/hub-mirror-action@v1.3
|
||||
with:
|
||||
src: github/gogf
|
||||
dst: gitee/johng
|
||||
|
||||
16
README.MD
16
README.MD
@ -7,16 +7,16 @@
|
||||
[](https://github.com/gogf/gf/actions/workflows/ci-main.yml)
|
||||
[](https://goreportcard.com/report/github.com/gogf/gf/v2)
|
||||
[](https://codecov.io/gh/gogf/gf)
|
||||
[](https://github.com/gogf/gf)
|
||||
[](https://github.com/gogf/gf)
|
||||
[](https://github.com/gogf/gf)
|
||||
|
||||
[](https://github.com/gogf/gf/releases)
|
||||
[](https://github.com/gogf/gf/pulls)
|
||||
[](https://github.com/gogf/gf/pulls?q=is%3Apr+is%3Aclosed)
|
||||
[](https://github.com/gogf/gf/issues)
|
||||
[](https://github.com/gogf/gf/issues?q=is%3Aissue+is%3Aclosed)
|
||||

|
||||

|
||||
[](https://github.com/gogf/gf/releases)
|
||||
[](https://github.com/gogf/gf/pulls)
|
||||
[](https://github.com/gogf/gf/pulls?q=is%3Apr+is%3Aclosed)
|
||||
[](https://github.com/gogf/gf/issues)
|
||||
[](https://github.com/gogf/gf/issues?q=is%3Aissue+is%3Aclosed)
|
||||

|
||||

|
||||
|
||||
</div>
|
||||
|
||||
|
||||
@ -3,13 +3,13 @@ module github.com/gogf/gf/cmd/gf/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.2
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f
|
||||
github.com/olekukonko/tablewriter v0.0.5
|
||||
golang.org/x/mod v0.9.0
|
||||
|
||||
@ -44,8 +44,9 @@ type cBuild struct {
|
||||
}
|
||||
|
||||
const (
|
||||
cBuildBrief = `cross-building go project for lots of platforms`
|
||||
cBuildEg = `
|
||||
cBuildDefaultFile = "main.go"
|
||||
cBuildBrief = `cross-building go project for lots of platforms`
|
||||
cBuildEg = `
|
||||
gf build main.go
|
||||
gf build main.go --ps public,template
|
||||
gf build main.go --cgo
|
||||
@ -123,7 +124,7 @@ type cBuildInput struct {
|
||||
Arch string `short:"a" name:"arch" brief:"output binary architecture, multiple arch separated with ','"`
|
||||
System string `short:"s" name:"system" brief:"output binary system, multiple os separated with ','"`
|
||||
Output string `short:"o" name:"output" brief:"output binary path, used when building single binary file"`
|
||||
Path string `short:"p" name:"path" brief:"output binary directory path, default is './temp'" d:"./temp"`
|
||||
Path string `short:"p" name:"path" brief:"output binary directory path, default is '.'" d:"."`
|
||||
Extra string `short:"e" name:"extra" brief:"extra custom \"go build\" options"`
|
||||
Mod string `short:"m" name:"mod" brief:"like \"-mod\" option of \"go build\", use \"-m none\" to disable go module"`
|
||||
Cgo bool `short:"c" name:"cgo" brief:"enable or disable cgo feature, it's disabled in default" orphan:"true"`
|
||||
@ -152,12 +153,13 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
|
||||
|
||||
var (
|
||||
parser = gcmd.ParserFromCtx(ctx)
|
||||
file = parser.GetArg(2).String()
|
||||
file = in.File
|
||||
)
|
||||
if len(file) < 1 {
|
||||
if file == "" {
|
||||
file = parser.GetArg(2).String()
|
||||
// Check and use the main.go file.
|
||||
if gfile.Exists("main.go") {
|
||||
file = "main.go"
|
||||
if gfile.Exists(cBuildDefaultFile) {
|
||||
file = cBuildDefaultFile
|
||||
} else {
|
||||
mlog.Fatal("build file path is empty or main.go not found in current working directory")
|
||||
}
|
||||
@ -239,13 +241,7 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
|
||||
} else {
|
||||
genv.MustSet("CGO_ENABLED", "0")
|
||||
}
|
||||
var (
|
||||
cmd = ""
|
||||
ext = ""
|
||||
)
|
||||
for system, item := range platformMap {
|
||||
cmd = ""
|
||||
ext = ""
|
||||
if len(customSystems) > 0 && customSystems[0] != "all" && !gstr.InArray(customSystems, system) {
|
||||
continue
|
||||
}
|
||||
@ -258,58 +254,22 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
|
||||
// For example:
|
||||
// `gf build`
|
||||
// `gf build -o main.exe`
|
||||
if runtime.GOOS == "windows" {
|
||||
ext = ".exe"
|
||||
}
|
||||
var outputPath string
|
||||
if len(in.Output) > 0 {
|
||||
outputPath = "-o " + in.Output
|
||||
} else {
|
||||
outputPath = "-o " + in.Name + ext
|
||||
}
|
||||
cmd = fmt.Sprintf(
|
||||
`go build %s -ldflags "%s" %s %s`,
|
||||
outputPath, ldFlags, in.Extra, file,
|
||||
c.doBinaryBuild(
|
||||
ctx, file,
|
||||
in.Output, in.Path,
|
||||
runtime.GOOS, runtime.GOARCH, in.Name, ldFlags, in.Extra,
|
||||
in.ExitWhenError,
|
||||
true,
|
||||
)
|
||||
} else {
|
||||
// Cross-building, output the compiled binary to specified path.
|
||||
if system == "windows" {
|
||||
ext = ".exe"
|
||||
}
|
||||
genv.MustSet("GOOS", system)
|
||||
genv.MustSet("GOARCH", arch)
|
||||
|
||||
var outputPath string
|
||||
if len(in.Output) > 0 {
|
||||
outputPath = "-o " + in.Output
|
||||
} else {
|
||||
outputPath = fmt.Sprintf(
|
||||
"-o %s/%s/%s%s",
|
||||
in.Path, system+"_"+arch, in.Name, ext,
|
||||
)
|
||||
}
|
||||
cmd = fmt.Sprintf(
|
||||
`go build %s -ldflags "%s" %s%s`,
|
||||
outputPath, ldFlags, in.Extra, file,
|
||||
c.doBinaryBuild(
|
||||
ctx, file,
|
||||
in.Output, in.Path,
|
||||
system, arch, in.Name, ldFlags, in.Extra,
|
||||
in.ExitWhenError,
|
||||
false,
|
||||
)
|
||||
}
|
||||
mlog.Debug(fmt.Sprintf("build for GOOS=%s GOARCH=%s", system, arch))
|
||||
mlog.Debug(cmd)
|
||||
// It's not necessary printing the complete command string.
|
||||
cmdShow, _ := gregex.ReplaceString(`\s+(-ldflags ".+?")\s+`, " ", cmd)
|
||||
mlog.Print(cmdShow)
|
||||
if result, err := gproc.ShellExec(ctx, cmd); err != nil {
|
||||
mlog.Printf(
|
||||
"failed to build, os:%s, arch:%s, error:\n%s\n\n%s\n",
|
||||
system, arch, gstr.Trim(result),
|
||||
`you may use command option "--debug" to enable debug info and check the details`,
|
||||
)
|
||||
if in.ExitWhenError {
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
mlog.Debug(gstr.Trim(result))
|
||||
}
|
||||
// single binary building.
|
||||
if len(customSystems) == 0 && len(customArches) == 0 {
|
||||
goto buildDone
|
||||
@ -322,6 +282,68 @@ buildDone:
|
||||
return
|
||||
}
|
||||
|
||||
func (c cBuild) doBinaryBuild(
|
||||
ctx context.Context,
|
||||
filePath string,
|
||||
outputPath, dirPath string,
|
||||
system, arch, name, ldFlags, extra string,
|
||||
exitWhenError bool,
|
||||
singleBuild bool,
|
||||
) {
|
||||
var (
|
||||
cmd string
|
||||
ext string
|
||||
)
|
||||
// Cross-building, output the compiled binary to specified path.
|
||||
if system == "windows" {
|
||||
ext = ".exe"
|
||||
}
|
||||
genv.MustSet("GOOS", system)
|
||||
genv.MustSet("GOARCH", arch)
|
||||
|
||||
if outputPath != "" {
|
||||
outputPath = "-o " + outputPath
|
||||
} else {
|
||||
if dirPath == "" {
|
||||
dirPath = "."
|
||||
} else {
|
||||
dirPath = gstr.TrimRight(dirPath, "/")
|
||||
}
|
||||
if singleBuild {
|
||||
outputPath = fmt.Sprintf(
|
||||
"-o %s/%s%s",
|
||||
dirPath, name, ext,
|
||||
)
|
||||
} else {
|
||||
outputPath = fmt.Sprintf(
|
||||
"-o %s/%s/%s%s",
|
||||
dirPath, system+"_"+arch, name, ext,
|
||||
)
|
||||
}
|
||||
}
|
||||
cmd = fmt.Sprintf(
|
||||
`go build %s -ldflags "%s" %s%s`,
|
||||
outputPath, ldFlags, extra, filePath,
|
||||
)
|
||||
mlog.Debug(fmt.Sprintf("build for GOOS=%s GOARCH=%s", system, arch))
|
||||
mlog.Debug(cmd)
|
||||
// It's not necessary printing the complete command string, filtering ldFlags.
|
||||
cmdShow, _ := gregex.ReplaceString(`\s+(-ldflags ".+?")\s+`, " ", cmd)
|
||||
mlog.Print(cmdShow)
|
||||
if result, err := gproc.ShellExec(ctx, cmd); err != nil {
|
||||
mlog.Printf(
|
||||
"failed to build, os:%s, arch:%s, error:\n%s\n\n%s\n",
|
||||
system, arch, gstr.Trim(result),
|
||||
`you may use command option "--debug" to enable debug info and check the details`,
|
||||
)
|
||||
if exitWhenError {
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
mlog.Debug(gstr.Trim(result))
|
||||
}
|
||||
}
|
||||
|
||||
// getBuildInVarStr retrieves and returns the custom build-in variables in configuration
|
||||
// file as json.
|
||||
func (c cBuild) getBuildInVarStr(ctx context.Context, in cBuildInput) string {
|
||||
|
||||
@ -6,6 +6,33 @@
|
||||
|
||||
package cmd
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
var ctx = context.Background()
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
var (
|
||||
ctx = context.Background()
|
||||
testDB gdb.DB
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
testDB, err = gdb.New(gdb.ConfigNode{
|
||||
Link: link,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func dropTableWithDb(db gdb.DB, table string) {
|
||||
dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)
|
||||
if _, err := db.Exec(ctx, dropTableStmt); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
151
cmd/gf/internal/cmd/cmd_z_unit_build_test.go
Normal file
151
cmd/gf/internal/cmd/cmd_z_unit_build_test.go
Normal file
@ -0,0 +1,151 @@
|
||||
// Copyright GoFrame gf 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 cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gproc"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
func Test_Build_Single(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
buildPath = gtest.DataPath(`build`, `single`)
|
||||
pwd = gfile.Pwd()
|
||||
binaryName = `t.test`
|
||||
binaryPath = gtest.DataPath(`build`, `single`, binaryName)
|
||||
f = cBuild{}
|
||||
)
|
||||
defer gfile.Chdir(pwd)
|
||||
defer gfile.Remove(binaryPath)
|
||||
err := gfile.Chdir(buildPath)
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(gfile.Exists(binaryPath), false)
|
||||
_, err = f.Index(ctx, cBuildInput{
|
||||
File: cBuildDefaultFile,
|
||||
Name: binaryName,
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(gfile.Exists(binaryPath), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Build_Single_Output(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
buildPath = gtest.DataPath(`build`, `single`)
|
||||
pwd = gfile.Pwd()
|
||||
binaryName = `tt`
|
||||
binaryDirPath = gtest.DataPath(`build`, `single`, `tt`)
|
||||
binaryPath = gtest.DataPath(`build`, `single`, `tt`, binaryName)
|
||||
f = cBuild{}
|
||||
)
|
||||
defer gfile.Chdir(pwd)
|
||||
defer gfile.Remove(binaryDirPath)
|
||||
err := gfile.Chdir(buildPath)
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(gfile.Exists(binaryPath), false)
|
||||
_, err = f.Index(ctx, cBuildInput{
|
||||
Output: "./tt/tt",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(gfile.Exists(binaryPath), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Build_Single_Path(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
buildPath = gtest.DataPath(`build`, `single`)
|
||||
pwd = gfile.Pwd()
|
||||
dirName = "ttt"
|
||||
binaryName = `main`
|
||||
binaryDirPath = gtest.DataPath(`build`, `single`, dirName)
|
||||
binaryPath = gtest.DataPath(`build`, `single`, dirName, binaryName)
|
||||
f = cBuild{}
|
||||
)
|
||||
defer gfile.Chdir(pwd)
|
||||
defer gfile.Remove(binaryDirPath)
|
||||
err := gfile.Chdir(buildPath)
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(gfile.Exists(binaryPath), false)
|
||||
_, err = f.Index(ctx, cBuildInput{
|
||||
Path: "ttt",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(gfile.Exists(binaryPath), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Build_Single_VarMap(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
buildPath = gtest.DataPath(`build`, `varmap`)
|
||||
pwd = gfile.Pwd()
|
||||
binaryName = `main`
|
||||
binaryPath = gtest.DataPath(`build`, `varmap`, binaryName)
|
||||
f = cBuild{}
|
||||
)
|
||||
defer gfile.Chdir(pwd)
|
||||
defer gfile.Remove(binaryPath)
|
||||
err := gfile.Chdir(buildPath)
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(gfile.Exists(binaryPath), false)
|
||||
_, err = f.Index(ctx, cBuildInput{
|
||||
VarMap: map[string]interface{}{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
},
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(gfile.Exists(binaryPath), true)
|
||||
|
||||
result, err := gproc.ShellExec(ctx, binaryPath)
|
||||
t.AssertNil(err)
|
||||
t.Assert(gstr.Contains(result, `a: 1`), true)
|
||||
t.Assert(gstr.Contains(result, `b: 2`), true)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Build_Multiple(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
buildPath = gtest.DataPath(`build`, `multiple`)
|
||||
pwd = gfile.Pwd()
|
||||
binaryDirPath = gtest.DataPath(`build`, `multiple`, `temp`)
|
||||
binaryPathLinux = gtest.DataPath(`build`, `multiple`, `temp`, `v1.1`, `linux_amd64`, `ttt`)
|
||||
binaryPathWindows = gtest.DataPath(`build`, `multiple`, `temp`, `v1.1`, `windows_amd64`, `ttt.exe`)
|
||||
f = cBuild{}
|
||||
)
|
||||
defer gfile.Chdir(pwd)
|
||||
defer gfile.Remove(binaryDirPath)
|
||||
err := gfile.Chdir(buildPath)
|
||||
t.AssertNil(err)
|
||||
|
||||
t.Assert(gfile.Exists(binaryPathLinux), false)
|
||||
t.Assert(gfile.Exists(binaryPathWindows), false)
|
||||
_, err = f.Index(ctx, cBuildInput{
|
||||
File: "multiple.go",
|
||||
Name: "ttt",
|
||||
Version: "v1.1",
|
||||
Arch: "amd64",
|
||||
System: "linux, windows",
|
||||
Path: "temp",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(gfile.Exists(binaryPathLinux), true)
|
||||
t.Assert(gfile.Exists(binaryPathWindows), true)
|
||||
})
|
||||
}
|
||||
@ -7,13 +7,14 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/genctrl"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Gen_Ctrl_Default(t *testing.T) {
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
@ -20,22 +19,11 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
)
|
||||
|
||||
func dropTableWithDb(db gdb.DB, table string) {
|
||||
dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)
|
||||
if _, err := db.Exec(ctx, dropTableStmt); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Gen_Dao_Default(t *testing.T) {
|
||||
link := "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
|
||||
db, err := gdb.New(gdb.ConfigNode{
|
||||
Link: link,
|
||||
})
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
err error
|
||||
db = testDB
|
||||
table = "table_user"
|
||||
sqlContent = fmt.Sprintf(
|
||||
gtest.DataContent(`gendao`, `user.tpl.sql`),
|
||||
@ -123,14 +111,10 @@ func Test_Gen_Dao_Default(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Gen_Dao_TypeMapping(t *testing.T) {
|
||||
link := "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
|
||||
db, err := gdb.New(gdb.ConfigNode{
|
||||
Link: link,
|
||||
})
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
err error
|
||||
db = testDB
|
||||
table = "table_user"
|
||||
sqlContent = fmt.Sprintf(
|
||||
gtest.DataContent(`gendao`, `user.tpl.sql`),
|
||||
|
||||
70
cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go
Normal file
70
cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go
Normal file
@ -0,0 +1,70 @@
|
||||
// Copyright GoFrame gf 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 cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
)
|
||||
|
||||
func Test_Gen_Pbentity_NameCase(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
err error
|
||||
db = testDB
|
||||
table = "table_user"
|
||||
sqlContent = fmt.Sprintf(
|
||||
gtest.DataContent(`genpbentity`, `user.tpl.sql`),
|
||||
table,
|
||||
)
|
||||
)
|
||||
dropTableWithDb(db, table)
|
||||
array := gstr.SplitAndTrim(sqlContent, ";")
|
||||
for _, v := range array {
|
||||
if _, err = db.Exec(ctx, v); err != nil {
|
||||
t.AssertNil(err)
|
||||
}
|
||||
}
|
||||
defer dropTableWithDb(db, table)
|
||||
var path = gfile.Temp(guid.S())
|
||||
err = gfile.Mkdir(path)
|
||||
t.AssertNil(err)
|
||||
defer gfile.Remove(path)
|
||||
|
||||
root, err := gcmd.NewFromObject(GF)
|
||||
t.AssertNil(err)
|
||||
err = root.AddObject(
|
||||
Gen,
|
||||
)
|
||||
t.AssertNil(err)
|
||||
os.Args = []string{"gf", "gen", "pbentity", "-l", link, "-p", path, "-package=unittest", "-nameCase=SnakeScreaming"}
|
||||
|
||||
err = root.RunWithError(ctx)
|
||||
t.AssertNil(err)
|
||||
|
||||
files := []string{
|
||||
filepath.FromSlash(path + "/table_user.proto"),
|
||||
}
|
||||
|
||||
testPath := gtest.DataPath("genpbentity", "generated_user")
|
||||
expectFiles := []string{
|
||||
filepath.FromSlash(testPath + "/table_user.proto"),
|
||||
}
|
||||
// check files content
|
||||
for i := range files {
|
||||
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -7,13 +7,14 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/genservice"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Gen_Service_Default(t *testing.T) {
|
||||
|
||||
@ -40,7 +40,7 @@ type (
|
||||
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenPbEntityBriefRemovePrefix}"`
|
||||
RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenPbEntityBriefRemoveFieldPrefix}"`
|
||||
NameCase string `name:"nameCase" short:"n" brief:"{CGenPbEntityBriefNameCase}" d:"Camel"`
|
||||
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"CamelLower"`
|
||||
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"none"`
|
||||
Option string `name:"option" short:"o" brief:"{CGenPbEntityBriefOption}"`
|
||||
}
|
||||
CGenPbEntityOutput struct{}
|
||||
@ -342,6 +342,7 @@ func generateMessageFieldForPbEntity(index int, field *gdb.TableField, in CGenPb
|
||||
comment = gstr.Replace(comment, `\n`, " ")
|
||||
comment, _ = gregex.ReplaceString(`\s{2,}`, ` `, comment)
|
||||
if jsonTagName := formatCase(field.Name, in.JsonCase); jsonTagName != "" {
|
||||
jsonTagStr = fmt.Sprintf(`[json_name = "%s"]`, jsonTagName)
|
||||
// beautiful indent.
|
||||
if index < 10 {
|
||||
// 3 spaces
|
||||
@ -378,32 +379,10 @@ func getTplPbEntityContent(tplEntityPath string) string {
|
||||
|
||||
// formatCase call gstr.Case* function to convert the s to specified case.
|
||||
func formatCase(str, caseStr string) string {
|
||||
switch gstr.ToLower(caseStr) {
|
||||
case gstr.ToLower("Camel"):
|
||||
return gstr.CaseCamel(str)
|
||||
|
||||
case gstr.ToLower("CamelLower"):
|
||||
return gstr.CaseCamelLower(str)
|
||||
|
||||
case gstr.ToLower("Kebab"):
|
||||
return gstr.CaseKebab(str)
|
||||
|
||||
case gstr.ToLower("KebabScreaming"):
|
||||
return gstr.CaseKebabScreaming(str)
|
||||
|
||||
case gstr.ToLower("Snake"):
|
||||
return gstr.CaseSnake(str)
|
||||
|
||||
case gstr.ToLower("SnakeFirstUpper"):
|
||||
return gstr.CaseSnakeFirstUpper(str)
|
||||
|
||||
case gstr.ToLower("SnakeScreaming"):
|
||||
return gstr.CaseSnakeScreaming(str)
|
||||
|
||||
case "none":
|
||||
if caseStr == "none" {
|
||||
return ""
|
||||
}
|
||||
return str
|
||||
return gstr.CaseConvert(str, gstr.CaseTypeMatch(caseStr))
|
||||
}
|
||||
|
||||
func sortFieldKeyForPbEntity(fieldMap map[string]*gdb.TableField) []string {
|
||||
|
||||
@ -174,7 +174,7 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
// Parse single logic package folder.
|
||||
var (
|
||||
// StructName => FunctionDefinitions
|
||||
srcPkgInterfaceMap = make(map[string]*garray.StrArray)
|
||||
srcPkgInterfaceMap = gmap.NewListMap()
|
||||
srcImportedPackages = garray.NewSortedStrArray().SetUnique(true)
|
||||
importAliasToPathMap = gmap.NewStrStrMap() // for conflict imports check. alias => import path(with `"`)
|
||||
importPathToAliasMap = gmap.NewStrStrMap() // for conflict imports check. import path(with `"`) => alias
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"go/token"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
@ -99,10 +100,9 @@ func (c CGenService) calculateCodeCommented(in CGenServiceInput, fileContent str
|
||||
}
|
||||
|
||||
func (c CGenService) calculateInterfaceFunctions(
|
||||
in CGenServiceInput, fileContent string, srcPkgInterfaceMap map[string]*garray.StrArray,
|
||||
in CGenServiceInput, fileContent string, srcPkgInterfaceMap *gmap.ListMap,
|
||||
) (err error) {
|
||||
var (
|
||||
ok bool
|
||||
matches [][]string
|
||||
srcPkgInterfaceFuncArray *garray.StrArray
|
||||
)
|
||||
@ -142,9 +142,11 @@ func (c CGenService) calculateInterfaceFunctions(
|
||||
continue
|
||||
}
|
||||
structName = gstr.CaseCamel(structMatch[1])
|
||||
if srcPkgInterfaceFuncArray, ok = srcPkgInterfaceMap[structName]; !ok {
|
||||
srcPkgInterfaceMap[structName] = garray.NewStrArray()
|
||||
srcPkgInterfaceFuncArray = srcPkgInterfaceMap[structName]
|
||||
if !srcPkgInterfaceMap.Contains(structName) {
|
||||
srcPkgInterfaceFuncArray = garray.NewStrArray()
|
||||
srcPkgInterfaceMap.Set(structName, srcPkgInterfaceFuncArray)
|
||||
} else {
|
||||
srcPkgInterfaceFuncArray = srcPkgInterfaceMap.Get(structName).(*garray.StrArray)
|
||||
}
|
||||
srcPkgInterfaceFuncArray.Append(functionHead)
|
||||
}
|
||||
@ -165,8 +167,8 @@ func (c CGenService) calculateInterfaceFunctions(
|
||||
continue
|
||||
}
|
||||
structName = gstr.CaseCamel(structMatch[1])
|
||||
if srcPkgInterfaceFuncArray, ok = srcPkgInterfaceMap[structName]; !ok {
|
||||
srcPkgInterfaceMap[structName] = garray.NewStrArray()
|
||||
if !srcPkgInterfaceMap.Contains(structName) {
|
||||
srcPkgInterfaceMap.Set(structName, garray.NewStrArray())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
@ -23,7 +24,7 @@ import (
|
||||
type generateServiceFilesInput struct {
|
||||
CGenServiceInput
|
||||
DstFilePath string // Absolute file path for generated service go file.
|
||||
SrcStructFunctions map[string]*garray.StrArray
|
||||
SrcStructFunctions *gmap.ListMap
|
||||
SrcImportedPackages []string
|
||||
SrcPackageName string
|
||||
DstPackageName string
|
||||
@ -46,7 +47,8 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
|
||||
// Type definitions.
|
||||
generatedContent += "type("
|
||||
generatedContent += "\n"
|
||||
for structName, funcArray := range in.SrcStructFunctions {
|
||||
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
structName, funcArray := key.(string), value.(*garray.StrArray)
|
||||
allFuncArray.Append(funcArray.Slice()...)
|
||||
// Add comments to a method.
|
||||
for index, funcName := range funcArray.Slice() {
|
||||
@ -60,7 +62,8 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
|
||||
"{FuncDefinition}": funcArray.Join("\n\t"),
|
||||
}))
|
||||
generatedContent += "\n"
|
||||
}
|
||||
return true
|
||||
})
|
||||
generatedContent += ")"
|
||||
generatedContent += "\n"
|
||||
|
||||
@ -70,17 +73,19 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
|
||||
generatingInterfaceCheck string
|
||||
)
|
||||
// Variable definitions.
|
||||
for structName := range in.SrcStructFunctions {
|
||||
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
structName := key.(string)
|
||||
generatingInterfaceCheck = fmt.Sprintf(`[^\w\d]+%s.I%s[^\w\d]`, in.DstPackageName, structName)
|
||||
if gregex.IsMatchString(generatingInterfaceCheck, generatedContent) {
|
||||
continue
|
||||
return true
|
||||
}
|
||||
variableContent += gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentVariable, g.MapStrStr{
|
||||
"{StructName}": structName,
|
||||
"{InterfaceName}": "I" + structName,
|
||||
}))
|
||||
variableContent += "\n"
|
||||
}
|
||||
return true
|
||||
})
|
||||
if variableContent != "" {
|
||||
generatedContent += "var("
|
||||
generatedContent += "\n"
|
||||
@ -89,17 +94,19 @@ func (c CGenService) generateServiceFile(in generateServiceFilesInput) (ok bool,
|
||||
generatedContent += "\n"
|
||||
}
|
||||
// Variable register function definitions.
|
||||
for structName := range in.SrcStructFunctions {
|
||||
in.SrcStructFunctions.Iterator(func(key, value interface{}) bool {
|
||||
structName := key.(string)
|
||||
generatingInterfaceCheck = fmt.Sprintf(`[^\w\d]+%s.I%s[^\w\d]`, in.DstPackageName, structName)
|
||||
if gregex.IsMatchString(generatingInterfaceCheck, generatedContent) {
|
||||
continue
|
||||
return true
|
||||
}
|
||||
generatedContent += gstr.Trim(gstr.ReplaceByMap(consts.TemplateGenServiceContentRegister, g.MapStrStr{
|
||||
"{StructName}": structName,
|
||||
"{InterfaceName}": "I" + structName,
|
||||
}))
|
||||
generatedContent += "\n\n"
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
// Replace empty braces that have new line.
|
||||
generatedContent, _ = gregex.ReplaceString(`{[\s\t]+}`, `{}`, generatedContent)
|
||||
|
||||
5
cmd/gf/internal/cmd/testdata/build/multiple/multiple.go
vendored
Normal file
5
cmd/gf/internal/cmd/testdata/build/multiple/multiple.go
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
5
cmd/gf/internal/cmd/testdata/build/single/main.go
vendored
Normal file
5
cmd/gf/internal/cmd/testdata/build/single/main.go
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
12
cmd/gf/internal/cmd/testdata/build/varmap/go.mod
vendored
Normal file
12
cmd/gf/internal/cmd/testdata/build/varmap/go.mod
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
module github.com/gogf/gf/cmd/gf/cmd/gf/testdata/vardump/v2
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/gogf/gf/v2 v2.6.1
|
||||
|
||||
require (
|
||||
go.opentelemetry.io/otel v1.14.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.14.0 // indirect
|
||||
)
|
||||
|
||||
replace github.com/gogf/gf/v2 => ../../../../../../../
|
||||
27
cmd/gf/internal/cmd/testdata/build/varmap/go.sum
vendored
Normal file
27
cmd/gf/internal/cmd/testdata/build/varmap/go.sum
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0=
|
||||
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
|
||||
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
|
||||
go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
|
||||
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
|
||||
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
|
||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
13
cmd/gf/internal/cmd/testdata/build/varmap/main.go
vendored
Normal file
13
cmd/gf/internal/cmd/testdata/build/varmap/main.go
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gbuild"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for k, v := range gbuild.Data() {
|
||||
fmt.Printf("%s: %v\n", k, v)
|
||||
}
|
||||
}
|
||||
21
cmd/gf/internal/cmd/testdata/genpbentity/generated_user/table_user.proto
vendored
Normal file
21
cmd/gf/internal/cmd/testdata/genpbentity/generated_user/table_user.proto
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
// ==========================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// ==========================================================================
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package unittest;
|
||||
|
||||
option go_package = "unittest";
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
message TableUser {
|
||||
uint32 ID = 1; // User ID
|
||||
string PASSPORT = 2; // User Passport
|
||||
string PASSWORD = 3; // User Password
|
||||
string NICKNAME = 4; // User Nickname
|
||||
string SCORE = 5; // Total score amount.
|
||||
google.protobuf.Timestamp CREATE_AT = 6; // Created Time
|
||||
google.protobuf.Timestamp UPDATE_AT = 7; // Updated Time
|
||||
}
|
||||
10
cmd/gf/internal/cmd/testdata/genpbentity/user.tpl.sql
vendored
Normal file
10
cmd/gf/internal/cmd/testdata/genpbentity/user.tpl.sql
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
CREATE TABLE `%s` (
|
||||
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
|
||||
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
|
||||
`password` varchar(45) NOT NULL COMMENT 'User Password',
|
||||
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
|
||||
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
|
||||
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
|
||||
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
@ -8,6 +8,7 @@ package article
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service"
|
||||
)
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ package utils
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/tools/imports"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/consts"
|
||||
|
||||
@ -173,7 +173,11 @@ func (a *IntArray) InsertBefore(index int, values ...int) error {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if index < 0 || index >= len(a.array) {
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
|
||||
return gerror.NewCodef(
|
||||
gcode.CodeInvalidParameter,
|
||||
"index %d out of array range %d",
|
||||
index, len(a.array),
|
||||
)
|
||||
}
|
||||
rear := append([]int{}, a.array[index:]...)
|
||||
a.array = append(a.array[0:index], values...)
|
||||
@ -186,7 +190,11 @@ func (a *IntArray) InsertAfter(index int, values ...int) error {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if index < 0 || index >= len(a.array) {
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
|
||||
return gerror.NewCodef(
|
||||
gcode.CodeInvalidParameter,
|
||||
"index %d out of array range %d",
|
||||
index, len(a.array),
|
||||
)
|
||||
}
|
||||
rear := append([]int{}, a.array[index+1:]...)
|
||||
a.array = append(a.array[0:index+1], values...)
|
||||
@ -583,7 +591,11 @@ func (a *IntArray) Fill(startIndex int, num int, value int) error {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if startIndex < 0 || startIndex > len(a.array) {
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", startIndex, len(a.array))
|
||||
return gerror.NewCodef(
|
||||
gcode.CodeInvalidParameter,
|
||||
"index %d out of array range %d",
|
||||
startIndex, len(a.array),
|
||||
)
|
||||
}
|
||||
for i := startIndex; i < startIndex+num; i++ {
|
||||
if i > len(a.array)-1 {
|
||||
|
||||
@ -75,7 +75,8 @@ func TestQueue_Close(t *testing.T) {
|
||||
q1 := gqueue.New()
|
||||
q1.Push(1)
|
||||
q1.Push(2)
|
||||
time.Sleep(time.Millisecond)
|
||||
// wait sync to channel
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
t.Assert(q1.Len(), 2)
|
||||
q1.Close()
|
||||
})
|
||||
@ -83,7 +84,8 @@ func TestQueue_Close(t *testing.T) {
|
||||
q1 := gqueue.New(2)
|
||||
q1.Push(1)
|
||||
q1.Push(2)
|
||||
time.Sleep(time.Millisecond)
|
||||
// wait sync to channel
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
t.Assert(q1.Len(), 2)
|
||||
q1.Close()
|
||||
})
|
||||
|
||||
@ -42,7 +42,7 @@ func ExampleIntSet_Add() {
|
||||
fmt.Println(intSet.Slice())
|
||||
fmt.Println(intSet.AddIfNotExist(1))
|
||||
|
||||
// Mya Output:
|
||||
// May Output:
|
||||
// [1 2 3]
|
||||
// false
|
||||
}
|
||||
@ -56,7 +56,7 @@ func ExampleIntSet_AddIfNotExist() {
|
||||
fmt.Println(intSet.Slice())
|
||||
fmt.Println(intSet.AddIfNotExist(1))
|
||||
|
||||
// Mya Output:
|
||||
// May Output:
|
||||
// [1 2 3]
|
||||
// false
|
||||
}
|
||||
|
||||
@ -42,13 +42,13 @@ func ExampleStrSet_Add() {
|
||||
fmt.Println(strSet.Slice())
|
||||
fmt.Println(strSet.AddIfNotExist("str"))
|
||||
|
||||
// Mya Output:
|
||||
// May Output:
|
||||
// [str str1 str2 str3]
|
||||
// false
|
||||
}
|
||||
|
||||
// AddIfNotExist checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set,
|
||||
// it adds the item to set and returns true if it does not exist in the set,
|
||||
// or else it does nothing and returns false.
|
||||
func ExampleStrSet_AddIfNotExist() {
|
||||
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
||||
@ -56,13 +56,13 @@ func ExampleStrSet_AddIfNotExist() {
|
||||
fmt.Println(strSet.Slice())
|
||||
fmt.Println(strSet.AddIfNotExist("str"))
|
||||
|
||||
// Mya Output:
|
||||
// May Output:
|
||||
// [str str1 str2 str3]
|
||||
// false
|
||||
}
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
|
||||
// it adds the item to set and returns true if it does not exist in the set and function `f` returns true,
|
||||
// or else it does nothing and returns false.
|
||||
// Note that, the function `f` is executed without writing lock.
|
||||
func ExampleStrSet_AddIfNotExistFunc() {
|
||||
@ -79,7 +79,7 @@ func ExampleStrSet_AddIfNotExistFunc() {
|
||||
}
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
|
||||
// it adds the item to set and returns true if it does not exist in the set and function `f` returns true,
|
||||
// or else it does nothing and returns false.
|
||||
// Note that, the function `f` is executed without writing lock.
|
||||
func ExampleStrSet_AddIfNotExistFuncLock() {
|
||||
|
||||
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/apolloconfig/agollo/v4 v4.3.1
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/consul/v2
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/hashicorp/consul/api v1.24.0
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2
|
||||
)
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/kubecm/v2
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
k8s.io/api v0.27.4
|
||||
k8s.io/apimachinery v0.27.4
|
||||
k8s.io/client-go v0.27.4
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/nacos/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/nacos-group/nacos-sdk-go v1.1.4
|
||||
)
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/polaris/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/polarismesh/polaris-go v1.5.5
|
||||
)
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.0.15
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/shopspring/decimal v1.3.1
|
||||
)
|
||||
|
||||
@ -74,9 +74,16 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
|
||||
}
|
||||
// Data Source Name of DM8:
|
||||
// dm://userName:password@ip:port/dbname
|
||||
// dm://userName:password@DW/dbname?DW=(192.168.1.1:5236,192.168.1.2:5236)
|
||||
var domain string
|
||||
if config.Port != "" {
|
||||
domain = fmt.Sprintf("%s:%s", config.Host, config.Port)
|
||||
} else {
|
||||
domain = config.Host
|
||||
}
|
||||
source = fmt.Sprintf(
|
||||
"dm://%s:%s@%s:%s/%s?charset=%s&schema=%s",
|
||||
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset, config.Name,
|
||||
"dm://%s:%s@%s/%s?charset=%s&schema=%s",
|
||||
config.User, config.Pass, domain, config.Name, config.Charset, config.Name,
|
||||
)
|
||||
// Demo of timezone setting:
|
||||
// &loc=Asia/Shanghai
|
||||
@ -210,7 +217,11 @@ func (d *Driver) DoFilter(ctx context.Context, link gdb.Link, sql string, args [
|
||||
// TODO The current approach is too rough. We should deal with the GROUP_CONCAT function and the parsing of the index field from within the select from match.
|
||||
// (GROUP_CONCAT DM does not approve; index cannot be used as a query column name, and security characters need to be added, such as "index")
|
||||
l, r := d.GetChars()
|
||||
newSql = gstr.ReplaceI(newSql, "INDEX", l+"INDEX"+r)
|
||||
if strings.Contains(newSql, "INDEX") || strings.Contains(newSql, "index") {
|
||||
if !(strings.Contains(newSql, "_INDEX") || strings.Contains(newSql, "_index")) {
|
||||
newSql = gstr.ReplaceI(newSql, "INDEX", l+"INDEX"+r)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO i tried to do but it never work:
|
||||
// array, err := gregex.MatchAllString(`SELECT (.*INDEX.*) FROM .*`, newSql)
|
||||
|
||||
@ -21,38 +21,44 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
db gdb.DB
|
||||
dblink gdb.DB
|
||||
dbErr gdb.DB
|
||||
ctx context.Context
|
||||
)
|
||||
|
||||
const (
|
||||
db gdb.DB
|
||||
dblink gdb.DB
|
||||
dbErr gdb.DB
|
||||
ctx context.Context
|
||||
TableSize = 10
|
||||
|
||||
// TableName = "inf_group"
|
||||
// TableNamePrefix = "t_"
|
||||
// TestSchema = "SYSDBADP"
|
||||
)
|
||||
|
||||
const (
|
||||
TestDbIP = "127.0.0.1"
|
||||
TestDbPort = "5236"
|
||||
TestDbUser = "SYSDBA"
|
||||
TestDbPass = "SYSDBA001"
|
||||
TestDbName = "SYSDBA"
|
||||
TestDbType = "dm"
|
||||
TestDBHost = "127.0.0.1"
|
||||
TestDBPort = "5236"
|
||||
TestDBUser = "SYSDBA"
|
||||
TestDBPass = "SYSDBA001"
|
||||
TestDBName = "SYSDBA"
|
||||
TestDBType = "dm"
|
||||
TestCharset = "utf8"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int64 `orm:"id"`
|
||||
AccountName string `orm:"account_name"`
|
||||
PwdReset int64 `orm:"pwd_reset"`
|
||||
AttrIndex int64 `orm:"attr_index"`
|
||||
Enabled int64 `orm:"enabled"`
|
||||
Deleted int64 `orm:"deleted"`
|
||||
CreatedBy string `orm:"created_by"`
|
||||
CreatedTime time.Time `orm:"created_time"`
|
||||
UpdatedBy string `orm:"updated_by"`
|
||||
UpdatedTime time.Time `orm:"updated_time"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
node := gdb.ConfigNode{
|
||||
Host: TestDbIP,
|
||||
Port: TestDbPort,
|
||||
User: TestDbUser,
|
||||
Pass: TestDbPass,
|
||||
Name: TestDbName,
|
||||
Type: TestDbType,
|
||||
Host: TestDBHost,
|
||||
Port: TestDBPort,
|
||||
User: TestDBUser,
|
||||
Pass: TestDBPass,
|
||||
Name: TestDBName,
|
||||
Type: TestDBType,
|
||||
Role: "master",
|
||||
Charset: TestCharset,
|
||||
Weight: 1,
|
||||
@ -62,22 +68,23 @@ func init() {
|
||||
UpdatedAt: "updated_time",
|
||||
}
|
||||
|
||||
// todo
|
||||
nodeLink := gdb.ConfigNode{
|
||||
Type: TestDbType,
|
||||
Name: TestDbName,
|
||||
Type: TestDBType,
|
||||
Name: TestDBName,
|
||||
Link: fmt.Sprintf(
|
||||
"dm:%s:%s@tcp(%s:%s)/%s?charset=%s",
|
||||
TestDbUser, TestDbPass, TestDbIP, TestDbPort, TestDbName, TestCharset,
|
||||
TestDBUser, TestDBPass, TestDBHost, TestDBPort, TestDBName, TestCharset,
|
||||
),
|
||||
}
|
||||
|
||||
nodeErr := gdb.ConfigNode{
|
||||
Host: TestDbIP,
|
||||
Port: TestDbPort,
|
||||
User: TestDbUser,
|
||||
Host: TestDBHost,
|
||||
Port: TestDBPort,
|
||||
User: TestDBUser,
|
||||
Pass: "1234",
|
||||
Name: TestDbName,
|
||||
Type: TestDbType,
|
||||
Name: TestDBName,
|
||||
Type: TestDBType,
|
||||
Role: "master",
|
||||
Charset: TestCharset,
|
||||
Weight: 1,
|
||||
@ -107,6 +114,23 @@ func init() {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
func dropTable(table string) {
|
||||
count, err := db.GetCount(
|
||||
ctx,
|
||||
"SELECT COUNT(*) FROM all_tables WHERE owner = ? And table_name= ?", TestDBName, strings.ToUpper(table),
|
||||
)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
return
|
||||
}
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func createTable(table ...string) (name string) {
|
||||
if len(table) > 0 {
|
||||
name = table[0]
|
||||
@ -124,6 +148,7 @@ func createTable(table ...string) (name string) {
|
||||
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
|
||||
"ENABLED" INT DEFAULT 1 NOT NULL,
|
||||
"DELETED" INT DEFAULT 0 NOT NULL,
|
||||
"ATTR_INDEX" INT DEFAULT 0 ,
|
||||
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
||||
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
||||
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
||||
@ -136,18 +161,6 @@ NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
|
||||
return
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int64 `orm:"id"`
|
||||
AccountName string `orm:"account_name"`
|
||||
PwdReset int64 `orm:"pwd_reset"`
|
||||
Enabled int64 `orm:"enabled"`
|
||||
Deleted int64 `orm:"deleted"`
|
||||
CreatedBy string `orm:"created_by"`
|
||||
CreatedTime time.Time `orm:"created_time"`
|
||||
UpdatedBy string `orm:"updated_by"`
|
||||
UpdatedTime time.Time `orm:"updated_time"`
|
||||
}
|
||||
|
||||
func createInitTable(table ...string) (name string) {
|
||||
name = createTable(table...)
|
||||
array := garray.New(true)
|
||||
@ -156,10 +169,11 @@ func createInitTable(table ...string) (name string) {
|
||||
"id": i,
|
||||
"account_name": fmt.Sprintf(`name_%d`, i),
|
||||
"pwd_reset": 0,
|
||||
"attr_index": i,
|
||||
"create_time": gtime.Now().String(),
|
||||
})
|
||||
}
|
||||
result, err := db.Schema(TestDbName).Insert(context.Background(), name, array.Slice())
|
||||
result, err := db.Schema(TestDBName).Insert(context.Background(), name, array.Slice())
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
n, e := result.RowsAffected()
|
||||
@ -168,19 +182,34 @@ func createInitTable(table ...string) (name string) {
|
||||
return
|
||||
}
|
||||
|
||||
func dropTable(table string) {
|
||||
count, err := db.GetCount(
|
||||
ctx,
|
||||
"SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = ?", strings.ToUpper(table),
|
||||
)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
func createTableFalse(table ...string) (name string, err error) {
|
||||
if len(table) > 0 {
|
||||
name = table[0]
|
||||
} else {
|
||||
name = fmt.Sprintf("random_%d", gtime.Timestamp())
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
return
|
||||
}
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
|
||||
gtest.Fatal(err)
|
||||
dropTable(name)
|
||||
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE "%s"
|
||||
(
|
||||
"ID" BIGINT NOT NULL,
|
||||
"ACCOUNT_NAME" VARCHAR(128) DEFAULT '' NOT NULL,
|
||||
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
|
||||
"ENABLED" INT DEFAULT 1 NOT NULL,
|
||||
"DELETED" INT DEFAULT 0 NOT NULL,
|
||||
"INDEX" INT DEFAULT 0 ,
|
||||
"ATTR_INDEX" INT DEFAULT 0 ,
|
||||
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
||||
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
||||
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
||||
"UPDATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
||||
NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
|
||||
`, name)); err != nil {
|
||||
// gtest.Fatal(err)
|
||||
return name, fmt.Errorf("createTableFalse")
|
||||
}
|
||||
|
||||
return name, nil
|
||||
}
|
||||
|
||||
@ -27,13 +27,11 @@ func Test_DB_Ping(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTables(t *testing.T) {
|
||||
tables := []string{"A_tables", "A_tables2"}
|
||||
for _, v := range tables {
|
||||
createInitTable(v)
|
||||
}
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tables := []string{"A_tables", "A_tables2"}
|
||||
|
||||
for _, v := range tables {
|
||||
createInitTable(v)
|
||||
// createTable(v)
|
||||
}
|
||||
result, err := db.Tables(ctx)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
@ -63,13 +61,31 @@ func TestTables(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestTableFields(t *testing.T) {
|
||||
// The test scenario index of this test case (exact matching field) is a keyword in the Dameng database and cannot exist as a field name.
|
||||
// If the data structure previously migrated from mysql has an index (completely matching field), it will also be allowed.
|
||||
// However, when processing the index (completely matching field), the adapter will automatically add security character
|
||||
// In principle, such problems will not occur if you directly use Dameng database initialization instead of migrating the data structure from mysql.
|
||||
// If so, the adapter has also taken care of it.
|
||||
func TestTablesFalse(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tables := []string{"A_tables", "A_tables2"}
|
||||
for _, v := range tables {
|
||||
_, err := createTableFalse(v)
|
||||
gtest.Assert(err, fmt.Errorf("createTableFalse"))
|
||||
// createTable(v)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestTableFields(t *testing.T) {
|
||||
tables := "A_tables"
|
||||
createInitTable(tables)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tables := "A_tables"
|
||||
var expect = map[string][]interface{}{
|
||||
"ID": {"BIGINT", false},
|
||||
"ACCOUNT_NAME": {"VARCHAR", false},
|
||||
"PWD_RESET": {"TINYINT", false},
|
||||
"ATTR_INDEX": {"INT", true},
|
||||
"DELETED": {"INT", false},
|
||||
"CREATED_TIME": {"TIMESTAMP", false},
|
||||
}
|
||||
@ -98,8 +114,9 @@ func TestTableFields(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_DB_Query(t *testing.T) {
|
||||
tableName := "A_tables"
|
||||
createInitTable(tableName)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tableName := "A_tables"
|
||||
// createTable(tableName)
|
||||
_, err := db.Query(ctx, fmt.Sprintf("SELECT * from %s", tableName))
|
||||
t.AssertNil(err)
|
||||
@ -121,16 +138,18 @@ func Test_DB_Query(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestModelSave(t *testing.T) {
|
||||
table := "A_tables"
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// createTable("A_tables")
|
||||
data := []User{
|
||||
{
|
||||
ID: 100,
|
||||
AccountName: "user_100",
|
||||
AttrIndex: 100,
|
||||
CreatedTime: time.Now(),
|
||||
},
|
||||
}
|
||||
_, err := db.Model("A_tables").Data(data).Save()
|
||||
_, err := db.Model(table).Data(data).Save()
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
data2 := []User{
|
||||
@ -139,7 +158,7 @@ func TestModelSave(t *testing.T) {
|
||||
AccountName: "user_101",
|
||||
},
|
||||
}
|
||||
_, err = db.Model("A_tables").Data(&data2).Save()
|
||||
_, err = db.Model(table).Data(&data2).Save()
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
data3 := []User{
|
||||
@ -149,7 +168,7 @@ func TestModelSave(t *testing.T) {
|
||||
PwdReset: 10,
|
||||
},
|
||||
}
|
||||
_, err = db.Model("A_tables").Save(data3)
|
||||
_, err = db.Model(table).Save(data3)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
data4 := []User{
|
||||
@ -159,63 +178,68 @@ func TestModelSave(t *testing.T) {
|
||||
CreatedTime: time.Now(),
|
||||
},
|
||||
}
|
||||
_, err = db.Model("A_tables").Save(&data4)
|
||||
_, err = db.Model(table).Save(&data4)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
// TODO:: Should be Supported 'Replace' Operation
|
||||
// _, err = db.Schema(TestDbName).Replace(ctx, "DoInsert", data, 10)
|
||||
// _, err = db.Schema(TestDBName).Replace(ctx, "DoInsert", data, 10)
|
||||
// gtest.Assert(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestModelInsert(t *testing.T) {
|
||||
// g.Model.insert not lost default not null coloumn
|
||||
table := "A_tables"
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// createTable("A_tables")
|
||||
i := 200
|
||||
data := User{
|
||||
ID: int64(i),
|
||||
AccountName: fmt.Sprintf(`A%dtwo`, i),
|
||||
PwdReset: 0,
|
||||
AttrIndex: 99,
|
||||
// CreatedTime: time.Now(),
|
||||
UpdatedTime: time.Now(),
|
||||
}
|
||||
// _, err := db.Schema(TestDbName).Model("A_tables").Data(data).Insert()
|
||||
_, err := db.Model("A_tables").Insert(&data)
|
||||
// _, err := db.Schema(TestDBName).Model(table).Data(data).Insert()
|
||||
_, err := db.Model(table).Insert(&data)
|
||||
gtest.Assert(err, nil)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// createTable("A_tables")
|
||||
i := 201
|
||||
data := User{
|
||||
ID: int64(i),
|
||||
AccountName: fmt.Sprintf(`A%dtwoONE`, i),
|
||||
PwdReset: 1,
|
||||
CreatedTime: time.Now(),
|
||||
AttrIndex: 98,
|
||||
// UpdatedTime: time.Now(),
|
||||
}
|
||||
// _, err := db.Schema(TestDbName).Model("A_tables").Data(data).Insert()
|
||||
_, err := db.Model("A_tables").Data(&data).Insert()
|
||||
// _, err := db.Schema(TestDBName).Model(table).Data(data).Insert()
|
||||
_, err := db.Model(table).Data(&data).Insert()
|
||||
gtest.Assert(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDBInsert(t *testing.T) {
|
||||
table := "A_tables"
|
||||
createInitTable("A_tables")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// createTable("A_tables")
|
||||
i := 300
|
||||
data := g.Map{
|
||||
"ID": i,
|
||||
"ACCOUNT_NAME": fmt.Sprintf(`A%dthress`, i),
|
||||
"PWD_RESET": 3,
|
||||
"ATTR_INDEX": 98,
|
||||
}
|
||||
_, err := db.Insert(ctx, "A_tables", &data)
|
||||
_, err := db.Insert(ctx, table, &data)
|
||||
gtest.Assert(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DB_Exec(t *testing.T) {
|
||||
createInitTable("A_tables")
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Exec(ctx, "SELECT ? from dual", 1)
|
||||
t.AssertNil(err)
|
||||
@ -226,18 +250,18 @@ func Test_DB_Exec(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_DB_Insert(t *testing.T) {
|
||||
// table := createTable()
|
||||
// defer dropTable(table)
|
||||
table := "A_tables"
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// normal map
|
||||
_, err := db.Insert(ctx, "A_tables", g.Map{
|
||||
_, err := db.Insert(ctx, table, g.Map{
|
||||
"ID": 1000,
|
||||
"ACCOUNT_NAME": "map1",
|
||||
"CREATED_TIME": gtime.Now(),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
result, err := db.Insert(ctx, "A_tables", g.Map{
|
||||
result, err := db.Insert(ctx, table, g.Map{
|
||||
"ID": "2000",
|
||||
"ACCOUNT_NAME": "map2",
|
||||
"CREATED_TIME": gtime.Now(),
|
||||
@ -246,7 +270,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
result, err = db.Insert(ctx, "A_tables", g.Map{
|
||||
result, err = db.Insert(ctx, table, g.Map{
|
||||
"ID": 3000,
|
||||
"ACCOUNT_NAME": "map3",
|
||||
// "CREATED_TIME": gtime.Now(),
|
||||
@ -256,7 +280,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
t.Assert(n, 1)
|
||||
|
||||
// struct
|
||||
result, err = db.Insert(ctx, "A_tables", User{
|
||||
result, err = db.Insert(ctx, table, User{
|
||||
ID: 4000,
|
||||
AccountName: "struct_4",
|
||||
// CreatedTime: timeStr,
|
||||
@ -266,7 +290,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
n, _ = result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
ones, err := db.Model("A_tables").Where("ID", 4000).All()
|
||||
ones, err := db.Model(table).Where("ID", 4000).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(ones[0]["ID"].Int(), 4000)
|
||||
t.Assert(ones[0]["ACCOUNT_NAME"].String(), "struct_4")
|
||||
@ -276,7 +300,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
|
||||
// *struct
|
||||
timeStr := time.Now()
|
||||
result, err = db.Insert(ctx, "A_tables", &User{
|
||||
result, err = db.Insert(ctx, table, &User{
|
||||
ID: 5000,
|
||||
AccountName: "struct_5",
|
||||
CreatedTime: timeStr,
|
||||
@ -286,13 +310,13 @@ func Test_DB_Insert(t *testing.T) {
|
||||
n, _ = result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model("A_tables").Where("ID", 5000).One()
|
||||
one, err := db.Model(table).Where("ID", 5000).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["ID"].Int(), 5000)
|
||||
t.Assert(one["ACCOUNT_NAME"].String(), "struct_5")
|
||||
|
||||
// batch with Insert
|
||||
r, err := db.Insert(ctx, "A_tables", g.Slice{
|
||||
r, err := db.Insert(ctx, table, g.Slice{
|
||||
g.Map{
|
||||
"ID": 6000,
|
||||
"ACCOUNT_NAME": "t6000",
|
||||
@ -306,7 +330,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
one, err = db.Model("A_tables").Where("ID", 6000).One()
|
||||
one, err = db.Model(table).Where("ID", 6000).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["ID"].Int(), 6000)
|
||||
t.Assert(one["ACCOUNT_NAME"].String(), "t6000")
|
||||
@ -314,8 +338,9 @@ func Test_DB_Insert(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_DB_BatchInsert(t *testing.T) {
|
||||
table := "A_tables"
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := "A_tables"
|
||||
r, err := db.Insert(ctx, table, g.List{
|
||||
{
|
||||
"ID": 400,
|
||||
@ -334,9 +359,6 @@ func Test_DB_BatchInsert(t *testing.T) {
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := "A_tables"
|
||||
// table := createTable()
|
||||
// defer dropTable(table)
|
||||
// []interface{}
|
||||
r, err := db.Insert(ctx, table, g.Slice{
|
||||
g.Map{
|
||||
@ -357,9 +379,6 @@ func Test_DB_BatchInsert(t *testing.T) {
|
||||
|
||||
// batch insert map
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := "A_tables"
|
||||
// table := createTable()
|
||||
// defer dropTable(table)
|
||||
result, err := db.Insert(ctx, table, g.Map{
|
||||
"ID": 600,
|
||||
"ACCOUNT_NAME": "600_batch_600",
|
||||
@ -373,10 +392,9 @@ func Test_DB_BatchInsert(t *testing.T) {
|
||||
|
||||
func Test_DB_BatchInsert_Struct(t *testing.T) {
|
||||
// batch insert struct
|
||||
table := "A_tables"
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := "A_tables"
|
||||
// table := createTable()
|
||||
// defer dropTable(table)
|
||||
user := &User{
|
||||
ID: 700,
|
||||
AccountName: "BatchInsert_Struct_700",
|
||||
@ -391,26 +409,25 @@ func Test_DB_BatchInsert_Struct(t *testing.T) {
|
||||
|
||||
func Test_DB_Update(t *testing.T) {
|
||||
table := "A_tables"
|
||||
// table := createInitTable()
|
||||
createInitTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Update(ctx, table, "pwd_reset=7", "id=700")
|
||||
result, err := db.Update(ctx, table, "pwd_reset=7", "id=7")
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).Where("ID", 700).One()
|
||||
one, err := db.Model(table).Where("ID", 7).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["ID"].Int(), 700)
|
||||
t.Assert(one["ACCOUNT_NAME"].String(), "BatchInsert_Struct_700")
|
||||
t.Assert(one["ID"].Int(), 7)
|
||||
t.Assert(one["ACCOUNT_NAME"].String(), "name_7")
|
||||
t.Assert(one["PWD_RESET"].String(), "7")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DB_GetAll(t *testing.T) {
|
||||
table := "A_tables"
|
||||
// table := createInitTable()
|
||||
// defer dropTable(table)
|
||||
createInitTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
@ -459,41 +476,38 @@ func Test_DB_GetAll(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_DB_GetOne(t *testing.T) {
|
||||
// table := createInitTable()
|
||||
table := "A_tables"
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE account_name=?", table), "struct_4")
|
||||
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE account_name=?", table), "name_4")
|
||||
t.AssertNil(err)
|
||||
t.Assert(record["ACCOUNT_NAME"].String(), "struct_4")
|
||||
t.Assert(record["ACCOUNT_NAME"].String(), "name_4")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DB_GetValue(t *testing.T) {
|
||||
table := "A_tables"
|
||||
// table := createInitTable()
|
||||
// defer dropTable(table)
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE account_name=?", table), "map2")
|
||||
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE account_name=?", table), "name_2")
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.Int(), 2000)
|
||||
t.Assert(value.Int(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DB_GetCount(t *testing.T) {
|
||||
table := "A_tables"
|
||||
// table := createInitTable()
|
||||
// defer dropTable(table)
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
count, err := db.GetCount(ctx, fmt.Sprintf("SELECT * FROM %s", table))
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 28)
|
||||
t.Assert(count, 10)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DB_GetStruct(t *testing.T) {
|
||||
table := "A_tables"
|
||||
// table := createInitTable()
|
||||
// defer dropTable(table)
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := new(User)
|
||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
@ -502,33 +516,31 @@ func Test_DB_GetStruct(t *testing.T) {
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := new(User)
|
||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 200)
|
||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 2)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.AccountName, "A200two")
|
||||
t.Assert(user.AccountName, "name_2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DB_GetStructs(t *testing.T) {
|
||||
table := "A_tables"
|
||||
// table := createInitTable()
|
||||
// defer dropTable(table)
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var users []User
|
||||
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 4000)
|
||||
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 4)
|
||||
t.AssertNil(err)
|
||||
t.Assert(users[0].ID, 5000)
|
||||
t.Assert(users[1].ID, 6000)
|
||||
t.Assert(users[2].ID, 6001)
|
||||
t.Assert(users[0].AccountName, "struct_5")
|
||||
t.Assert(users[1].AccountName, "t6000")
|
||||
t.Assert(users[2].AccountName, "t6001")
|
||||
t.Assert(users[0].ID, 5)
|
||||
t.Assert(users[1].ID, 6)
|
||||
t.Assert(users[2].ID, 7)
|
||||
t.Assert(users[0].AccountName, "name_5")
|
||||
t.Assert(users[1].AccountName, "name_6")
|
||||
t.Assert(users[2].AccountName, "name_7")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DB_GetScan(t *testing.T) {
|
||||
table := "A_tables"
|
||||
// table := createInitTable()
|
||||
// defer dropTable(table)
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := new(User)
|
||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
@ -555,17 +567,17 @@ func Test_DB_GetScan(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_DB_Delete(t *testing.T) {
|
||||
// table := createInitTable()
|
||||
// defer dropTable(table)
|
||||
table := "A_tables"
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Delete(ctx, "A_tables", "id=32")
|
||||
result, err := db.Delete(ctx, table, "id=32")
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 0)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model("A_tables").Where("id", 33).Delete()
|
||||
result, err := db.Model(table).Where("id", 33).Delete()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 0)
|
||||
@ -574,8 +586,7 @@ func Test_DB_Delete(t *testing.T) {
|
||||
|
||||
func Test_Empty_Slice_Argument(t *testing.T) {
|
||||
table := "A_tables"
|
||||
// table := createInitTable()
|
||||
// defer dropTable(table)
|
||||
createInitTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
|
||||
t.AssertNil(err)
|
||||
|
||||
@ -6,7 +6,7 @@ replace github.com/gogf/gf/v2 => ../../../
|
||||
|
||||
require (
|
||||
gitee.com/chunanyong/dm v1.8.12
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/denisenkom/go-mssqldb v0.12.3
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.7.1
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@ -55,7 +55,6 @@ func init() {
|
||||
nodeInvalid := gdb.ConfigNode{
|
||||
Link: fmt.Sprintf("mysql:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
|
||||
}
|
||||
|
||||
gdb.AddConfigNode("test", nodeDefault)
|
||||
gdb.AddConfigNode("prefix", nodePrefix)
|
||||
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package mysql_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
@ -886,7 +887,8 @@ func Test_Issue3086(t *testing.T) {
|
||||
func Test_Issue3204(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
db.SetDebug(true)
|
||||
|
||||
// where
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
@ -905,4 +907,95 @@ func Test_Issue3204(t *testing.T) {
|
||||
t.Assert(len(all), 1)
|
||||
t.Assert(all[0]["id"], 2)
|
||||
})
|
||||
// data
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id interface{} `orm:"id,omitempty"`
|
||||
Passport interface{} `orm:"passport,omitempty"`
|
||||
Password interface{} `orm:"password,omitempty"`
|
||||
Nickname interface{} `orm:"nickname,omitempty"`
|
||||
CreateTime interface{} `orm:"create_time,omitempty"`
|
||||
}
|
||||
var (
|
||||
err error
|
||||
sqlArray []string
|
||||
insertId int64
|
||||
data = User{
|
||||
Id: 20,
|
||||
Passport: "passport_20",
|
||||
Password: "",
|
||||
}
|
||||
)
|
||||
sqlArray, err = gdb.CatchSQL(ctx, func(ctx context.Context) error {
|
||||
insertId, err = db.Ctx(ctx).Model(table).Data(data).InsertAndGetId()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(insertId, 20)
|
||||
t.Assert(
|
||||
gstr.Contains(sqlArray[len(sqlArray)-1], "(`id`,`passport`) VALUES(20,'passport_20')"),
|
||||
true,
|
||||
)
|
||||
})
|
||||
// update data
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
g.Meta `orm:"do:true"`
|
||||
Id interface{} `orm:"id,omitempty"`
|
||||
Passport interface{} `orm:"passport,omitempty"`
|
||||
Password interface{} `orm:"password,omitempty"`
|
||||
Nickname interface{} `orm:"nickname,omitempty"`
|
||||
CreateTime interface{} `orm:"create_time,omitempty"`
|
||||
}
|
||||
var (
|
||||
err error
|
||||
sqlArray []string
|
||||
data = User{
|
||||
Passport: "passport_1",
|
||||
Password: "",
|
||||
Nickname: "",
|
||||
}
|
||||
)
|
||||
sqlArray, err = gdb.CatchSQL(ctx, func(ctx context.Context) error {
|
||||
_, err = db.Ctx(ctx).Model(table).Data(data).WherePri(1).Update()
|
||||
return err
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(
|
||||
gstr.Contains(sqlArray[len(sqlArray)-1], "SET `passport`='passport_1' WHERE `id`=1"),
|
||||
true,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/3218
|
||||
func Test_Issue3218(t *testing.T) {
|
||||
table := "issue3218_sys_config"
|
||||
array := gstr.SplitAndTrim(gtest.DataContent(`issue3218.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type SysConfigInfo struct {
|
||||
Name string `json:"name"`
|
||||
Value map[string]string `json:"value"`
|
||||
}
|
||||
var configData *SysConfigInfo
|
||||
err := db.Model(table).Scan(&configData)
|
||||
t.AssertNil(err)
|
||||
t.Assert(configData, &SysConfigInfo{
|
||||
Name: "site",
|
||||
Value: map[string]string{
|
||||
"fixed_page": "",
|
||||
"site_name": "22",
|
||||
"version": "22",
|
||||
"banned_ip": "22",
|
||||
"filings": "2222",
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
14
contrib/drivers/mysql/testdata/issue3218.sql
vendored
Normal file
14
contrib/drivers/mysql/testdata/issue3218.sql
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
CREATE TABLE `issue3218_sys_config` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '配置名称',
|
||||
`value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '配置值',
|
||||
`created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
|
||||
`updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `name`(`name`(191)) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of sys_config
|
||||
-- ----------------------------
|
||||
INSERT INTO `issue3218_sys_config` VALUES (49, 'site', '{\"banned_ip\":\"22\",\"filings\":\"2222\",\"fixed_page\":\"\",\"site_name\":\"22\",\"version\":\"22\"}', '2023-12-19 14:08:25', '2023-12-19 14:08:25');
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/oracle/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/sijms/go-ora/v2 v2.7.10
|
||||
)
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/pgsql/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/lib/pq v1.10.9
|
||||
)
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/glebarez/go-sqlite v1.21.2
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/sqlitecgo/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/mattn/go-sqlite3 v1.14.17
|
||||
)
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/nosql/redis/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/redis/go-redis/v9 v9.2.1
|
||||
go.opentelemetry.io/otel v1.14.0
|
||||
go.opentelemetry.io/otel/trace v1.14.0
|
||||
|
||||
@ -8,20 +8,19 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
// Redis is an implement of Adapter using go-redis.
|
||||
type Redis struct {
|
||||
gredis.AdapterOperation
|
||||
|
||||
client redis.UniversalClient
|
||||
config *gredis.Config
|
||||
}
|
||||
@ -75,40 +74,12 @@ func New(config *gredis.Config) *Redis {
|
||||
client = redis.NewClient(opts.Simple())
|
||||
}
|
||||
|
||||
return &Redis{
|
||||
r := &Redis{
|
||||
client: client,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// Do send a command to the server and returns the received reply.
|
||||
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
|
||||
func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
|
||||
conn, err := r.Conn(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = conn.Close(ctx)
|
||||
}()
|
||||
return conn.Do(ctx, command, args...)
|
||||
}
|
||||
|
||||
// Close closes the redis connection pool, which will release all connections reserved by this pool.
|
||||
// It is commonly not necessary to call Close manually.
|
||||
func (r *Redis) Close(ctx context.Context) (err error) {
|
||||
if err = r.client.Close(); err != nil {
|
||||
err = gerror.Wrap(err, `Redis Client Close failed`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Conn retrieves and returns a connection object for continuous operations.
|
||||
// Note that you should call Close function manually if you do not use this connection any further.
|
||||
func (r *Redis) Conn(ctx context.Context) (gredis.Conn, error) {
|
||||
return &Conn{
|
||||
redis: r,
|
||||
}, nil
|
||||
r.AdapterOperation = r
|
||||
return r
|
||||
}
|
||||
|
||||
func fillWithDefaultConfiguration(config *gredis.Config) {
|
||||
|
||||
@ -18,13 +18,13 @@ import (
|
||||
|
||||
// GroupGeneric provides generic functions of redis.
|
||||
type GroupGeneric struct {
|
||||
redis *Redis
|
||||
Operation gredis.AdapterOperation
|
||||
}
|
||||
|
||||
// GroupGeneric creates and returns GroupGeneric.
|
||||
func (r *Redis) GroupGeneric() gredis.IGroupGeneric {
|
||||
return GroupGeneric{
|
||||
redis: r,
|
||||
Operation: r.AdapterOperation,
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ func (r GroupGeneric) Copy(ctx context.Context, source, destination string, opti
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "Copy", mustMergeOptionToArgs(
|
||||
v, err := r.Operation.Do(ctx, "Copy", mustMergeOptionToArgs(
|
||||
[]interface{}{source, destination}, usedOption,
|
||||
)...)
|
||||
return v.Int64(), err
|
||||
@ -59,7 +59,7 @@ func (r GroupGeneric) Copy(ctx context.Context, source, destination string, opti
|
||||
//
|
||||
// https://redis.io/commands/exists/
|
||||
func (r GroupGeneric) Exists(ctx context.Context, keys ...string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Exists", gconv.Interfaces(keys)...)
|
||||
v, err := r.Operation.Do(ctx, "Exists", gconv.Interfaces(keys)...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ func (r GroupGeneric) Exists(ctx context.Context, keys ...string) (int64, error)
|
||||
//
|
||||
// https://redis.io/commands/type/
|
||||
func (r GroupGeneric) Type(ctx context.Context, key string) (string, error) {
|
||||
v, err := r.redis.Do(ctx, "Type", key)
|
||||
v, err := r.Operation.Do(ctx, "Type", key)
|
||||
return v.String(), err
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ func (r GroupGeneric) Type(ctx context.Context, key string) (string, error) {
|
||||
//
|
||||
// https://redis.io/commands/unlink/
|
||||
func (r GroupGeneric) Unlink(ctx context.Context, keys ...string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Unlink", gconv.Interfaces(keys)...)
|
||||
v, err := r.Operation.Do(ctx, "Unlink", gconv.Interfaces(keys)...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -96,7 +96,7 @@ func (r GroupGeneric) Unlink(ctx context.Context, keys ...string) (int64, error)
|
||||
//
|
||||
// https://redis.io/commands/rename/
|
||||
func (r GroupGeneric) Rename(ctx context.Context, key, newKey string) error {
|
||||
_, err := r.redis.Do(ctx, "Rename", key, newKey)
|
||||
_, err := r.Operation.Do(ctx, "Rename", key, newKey)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -111,7 +111,7 @@ func (r GroupGeneric) Rename(ctx context.Context, key, newKey string) error {
|
||||
//
|
||||
// https://redis.io/commands/renamenx/
|
||||
func (r GroupGeneric) RenameNX(ctx context.Context, key, newKey string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "RenameNX", key, newKey)
|
||||
v, err := r.Operation.Do(ctx, "RenameNX", key, newKey)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -126,7 +126,7 @@ func (r GroupGeneric) RenameNX(ctx context.Context, key, newKey string) (int64,
|
||||
//
|
||||
// https://redis.io/commands/move/
|
||||
func (r GroupGeneric) Move(ctx context.Context, key string, db int) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Move", key, db)
|
||||
v, err := r.Operation.Do(ctx, "Move", key, db)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -137,7 +137,7 @@ func (r GroupGeneric) Move(ctx context.Context, key string, db int) (int64, erro
|
||||
//
|
||||
// https://redis.io/commands/del/
|
||||
func (r GroupGeneric) Del(ctx context.Context, keys ...string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Del", gconv.Interfaces(keys)...)
|
||||
v, err := r.Operation.Do(ctx, "Del", gconv.Interfaces(keys)...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -147,7 +147,7 @@ func (r GroupGeneric) Del(ctx context.Context, keys ...string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/randomkey/
|
||||
func (r GroupGeneric) RandomKey(ctx context.Context) (string, error) {
|
||||
v, err := r.redis.Do(ctx, "RandomKey")
|
||||
v, err := r.Operation.Do(ctx, "RandomKey")
|
||||
return v.String(), err
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ func (r GroupGeneric) RandomKey(ctx context.Context) (string, error) {
|
||||
//
|
||||
// https://redis.io/commands/dbsize/
|
||||
func (r GroupGeneric) DBSize(ctx context.Context) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "DBSize")
|
||||
v, err := r.Operation.Do(ctx, "DBSize")
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -166,7 +166,7 @@ func (r GroupGeneric) DBSize(ctx context.Context) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/keys/
|
||||
func (r GroupGeneric) Keys(ctx context.Context, pattern string) ([]string, error) {
|
||||
v, err := r.redis.Do(ctx, "Keys", pattern)
|
||||
v, err := r.Operation.Do(ctx, "Keys", pattern)
|
||||
return v.Strings(), err
|
||||
}
|
||||
|
||||
@ -174,7 +174,7 @@ func (r GroupGeneric) Keys(ctx context.Context, pattern string) ([]string, error
|
||||
//
|
||||
// https://redis.io/commands/flushdb/
|
||||
func (r GroupGeneric) FlushDB(ctx context.Context, option ...gredis.FlushOp) error {
|
||||
_, err := r.redis.Do(ctx, "FlushDB", gconv.Interfaces(option)...)
|
||||
_, err := r.Operation.Do(ctx, "FlushDB", gconv.Interfaces(option)...)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -191,7 +191,7 @@ func (r GroupGeneric) FlushDB(ctx context.Context, option ...gredis.FlushOp) err
|
||||
//
|
||||
// https://redis.io/commands/flushall/
|
||||
func (r GroupGeneric) FlushAll(ctx context.Context, option ...gredis.FlushOp) error {
|
||||
_, err := r.redis.Do(ctx, "FlushAll", gconv.Interfaces(option)...)
|
||||
_, err := r.Operation.Do(ctx, "FlushAll", gconv.Interfaces(option)...)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -208,7 +208,7 @@ func (r GroupGeneric) Expire(ctx context.Context, key string, seconds int64, opt
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "Expire", mustMergeOptionToArgs(
|
||||
v, err := r.Operation.Do(ctx, "Expire", mustMergeOptionToArgs(
|
||||
[]interface{}{key, seconds}, usedOption,
|
||||
)...)
|
||||
return v.Int64(), err
|
||||
@ -229,7 +229,7 @@ func (r GroupGeneric) ExpireAt(ctx context.Context, key string, time time.Time,
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "ExpireAt", mustMergeOptionToArgs(
|
||||
v, err := r.Operation.Do(ctx, "ExpireAt", mustMergeOptionToArgs(
|
||||
[]interface{}{key, gtime.New(time).Timestamp()}, usedOption,
|
||||
)...)
|
||||
return v.Int64(), err
|
||||
@ -243,7 +243,7 @@ func (r GroupGeneric) ExpireAt(ctx context.Context, key string, time time.Time,
|
||||
//
|
||||
// https://redis.io/commands/expiretime/
|
||||
func (r GroupGeneric) ExpireTime(ctx context.Context, key string) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "ExpireTime", key)
|
||||
return r.Operation.Do(ctx, "ExpireTime", key)
|
||||
}
|
||||
|
||||
// TTL returns the remaining time to live of a key that has a timeout.
|
||||
@ -263,7 +263,7 @@ func (r GroupGeneric) ExpireTime(ctx context.Context, key string) (*gvar.Var, er
|
||||
//
|
||||
// https://redis.io/commands/ttl/
|
||||
func (r GroupGeneric) TTL(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "TTL", key)
|
||||
v, err := r.Operation.Do(ctx, "TTL", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -276,7 +276,7 @@ func (r GroupGeneric) TTL(ctx context.Context, key string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/persist/
|
||||
func (r GroupGeneric) Persist(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Persist", key)
|
||||
v, err := r.Operation.Do(ctx, "Persist", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -293,7 +293,7 @@ func (r GroupGeneric) PExpire(ctx context.Context, key string, milliseconds int6
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "PExpire", mustMergeOptionToArgs(
|
||||
v, err := r.Operation.Do(ctx, "PExpire", mustMergeOptionToArgs(
|
||||
[]interface{}{key, milliseconds}, usedOption,
|
||||
)...)
|
||||
return v.Int64(), err
|
||||
@ -308,7 +308,7 @@ func (r GroupGeneric) PExpireAt(ctx context.Context, key string, time time.Time,
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "PExpireAt", mustMergeOptionToArgs(
|
||||
v, err := r.Operation.Do(ctx, "PExpireAt", mustMergeOptionToArgs(
|
||||
[]interface{}{key, gtime.New(time).TimestampMilli()}, usedOption,
|
||||
)...)
|
||||
return v.Int64(), err
|
||||
@ -322,7 +322,7 @@ func (r GroupGeneric) PExpireAt(ctx context.Context, key string, time time.Time,
|
||||
//
|
||||
// https://redis.io/commands/pexpiretime/
|
||||
func (r GroupGeneric) PExpireTime(ctx context.Context, key string) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "PExpireTime", key)
|
||||
return r.Operation.Do(ctx, "PExpireTime", key)
|
||||
}
|
||||
|
||||
// PTTL like TTL this command returns the remaining time to live of a key that has an expired set,
|
||||
@ -336,6 +336,6 @@ func (r GroupGeneric) PExpireTime(ctx context.Context, key string) (*gvar.Var, e
|
||||
//
|
||||
// https://redis.io/commands/pttl/
|
||||
func (r GroupGeneric) PTTL(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "PTTL", key)
|
||||
v, err := r.Operation.Do(ctx, "PTTL", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -16,13 +16,13 @@ import (
|
||||
|
||||
// GroupHash is the redis group object for hash operations.
|
||||
type GroupHash struct {
|
||||
redis *Redis
|
||||
Operation gredis.AdapterOperation
|
||||
}
|
||||
|
||||
// GroupHash creates and returns a redis group object for hash operations.
|
||||
func (r *Redis) GroupHash() gredis.IGroupHash {
|
||||
return GroupHash{
|
||||
redis: r,
|
||||
Operation: r.AdapterOperation,
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ func (r GroupHash) HSet(ctx context.Context, key string, fields map[string]inter
|
||||
for k, v := range fields {
|
||||
s = append(s, k, v)
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "HSet", s...)
|
||||
v, err := r.Operation.Do(ctx, "HSet", s...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ func (r GroupHash) HSet(ctx context.Context, key string, fields map[string]inter
|
||||
//
|
||||
// https://redis.io/commands/hsetnx/
|
||||
func (r GroupHash) HSetNX(ctx context.Context, key, field string, value interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "HSetNX", key, field, value)
|
||||
v, err := r.Operation.Do(ctx, "HSetNX", key, field, value)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ func (r GroupHash) HSetNX(ctx context.Context, key, field string, value interfac
|
||||
//
|
||||
// https://redis.io/commands/hget/
|
||||
func (r GroupHash) HGet(ctx context.Context, key, field string) (*gvar.Var, error) {
|
||||
v, err := r.redis.Do(ctx, "HGet", key, field)
|
||||
v, err := r.Operation.Do(ctx, "HGet", key, field)
|
||||
return v, err
|
||||
}
|
||||
|
||||
@ -74,7 +74,7 @@ func (r GroupHash) HGet(ctx context.Context, key, field string) (*gvar.Var, erro
|
||||
//
|
||||
// https://redis.io/commands/hstrlen/
|
||||
func (r GroupHash) HStrLen(ctx context.Context, key, field string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "HSTRLEN", key, field)
|
||||
v, err := r.Operation.Do(ctx, "HSTRLEN", key, field)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ func (r GroupHash) HStrLen(ctx context.Context, key, field string) (int64, error
|
||||
//
|
||||
// https://redis.io/commands/hexists/
|
||||
func (r GroupHash) HExists(ctx context.Context, key, field string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "HExists", key, field)
|
||||
v, err := r.Operation.Do(ctx, "HExists", key, field)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ func (r GroupHash) HExists(ctx context.Context, key, field string) (int64, error
|
||||
//
|
||||
// https://redis.io/commands/hdel/
|
||||
func (r GroupHash) HDel(ctx context.Context, key string, fields ...string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "HDel", append([]interface{}{key}, gconv.Interfaces(fields)...)...)
|
||||
v, err := r.Operation.Do(ctx, "HDel", append([]interface{}{key}, gconv.Interfaces(fields)...)...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -106,7 +106,7 @@ func (r GroupHash) HDel(ctx context.Context, key string, fields ...string) (int6
|
||||
//
|
||||
// https://redis.io/commands/hlen/
|
||||
func (r GroupHash) HLen(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "HLen", key)
|
||||
v, err := r.Operation.Do(ctx, "HLen", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -118,7 +118,7 @@ func (r GroupHash) HLen(ctx context.Context, key string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/hincrby/
|
||||
func (r GroupHash) HIncrBy(ctx context.Context, key, field string, increment int64) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "HIncrBy", key, field, increment)
|
||||
v, err := r.Operation.Do(ctx, "HIncrBy", key, field, increment)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -138,7 +138,7 @@ func (r GroupHash) HIncrBy(ctx context.Context, key, field string, increment int
|
||||
//
|
||||
// https://redis.io/commands/hincrbyfloat/
|
||||
func (r GroupHash) HIncrByFloat(ctx context.Context, key, field string, increment float64) (float64, error) {
|
||||
v, err := r.redis.Do(ctx, "HIncrByFloat", key, field, increment)
|
||||
v, err := r.Operation.Do(ctx, "HIncrByFloat", key, field, increment)
|
||||
return v.Float64(), err
|
||||
}
|
||||
|
||||
@ -152,7 +152,7 @@ func (r GroupHash) HMSet(ctx context.Context, key string, fields map[string]inte
|
||||
for k, v := range fields {
|
||||
s = append(s, k, v)
|
||||
}
|
||||
_, err := r.redis.Do(ctx, "HMSet", s...)
|
||||
_, err := r.Operation.Do(ctx, "HMSet", s...)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -163,7 +163,7 @@ func (r GroupHash) HMSet(ctx context.Context, key string, fields map[string]inte
|
||||
//
|
||||
// https://redis.io/commands/hmget/
|
||||
func (r GroupHash) HMGet(ctx context.Context, key string, fields ...string) (gvar.Vars, error) {
|
||||
v, err := r.redis.Do(ctx, "HMGet", append([]interface{}{key}, gconv.Interfaces(fields)...)...)
|
||||
v, err := r.Operation.Do(ctx, "HMGet", append([]interface{}{key}, gconv.Interfaces(fields)...)...)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -171,7 +171,7 @@ func (r GroupHash) HMGet(ctx context.Context, key string, fields ...string) (gva
|
||||
//
|
||||
// https://redis.io/commands/hkeys/
|
||||
func (r GroupHash) HKeys(ctx context.Context, key string) ([]string, error) {
|
||||
v, err := r.redis.Do(ctx, "HKeys", key)
|
||||
v, err := r.Operation.Do(ctx, "HKeys", key)
|
||||
return v.Strings(), err
|
||||
}
|
||||
|
||||
@ -179,7 +179,7 @@ func (r GroupHash) HKeys(ctx context.Context, key string) ([]string, error) {
|
||||
//
|
||||
// https://redis.io/commands/hvals/
|
||||
func (r GroupHash) HVals(ctx context.Context, key string) (gvar.Vars, error) {
|
||||
v, err := r.redis.Do(ctx, "HVals", key)
|
||||
v, err := r.Operation.Do(ctx, "HVals", key)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -189,6 +189,6 @@ func (r GroupHash) HVals(ctx context.Context, key string) (gvar.Vars, error) {
|
||||
//
|
||||
// https://redis.io/commands/hgetall/
|
||||
func (r GroupHash) HGetAll(ctx context.Context, key string) (*gvar.Var, error) {
|
||||
v, err := r.redis.Do(ctx, "HGetAll", key)
|
||||
v, err := r.Operation.Do(ctx, "HGetAll", key)
|
||||
return v, err
|
||||
}
|
||||
|
||||
@ -16,13 +16,13 @@ import (
|
||||
|
||||
// GroupList is the redis group list object.
|
||||
type GroupList struct {
|
||||
redis *Redis
|
||||
Operation gredis.AdapterOperation
|
||||
}
|
||||
|
||||
// GroupList creates and returns a redis group object for list operations.
|
||||
func (r *Redis) GroupList() gredis.IGroupList {
|
||||
return GroupList{
|
||||
redis: r,
|
||||
Operation: r.AdapterOperation,
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ func (r *Redis) GroupList() gredis.IGroupList {
|
||||
//
|
||||
// https://redis.io/commands/lpush/
|
||||
func (r GroupList) LPush(ctx context.Context, key string, values ...interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "LPush", append([]interface{}{key}, values...)...)
|
||||
v, err := r.Operation.Do(ctx, "LPush", append([]interface{}{key}, values...)...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -48,7 +48,7 @@ func (r GroupList) LPush(ctx context.Context, key string, values ...interface{})
|
||||
//
|
||||
// https://redis.io/commands/lpushx
|
||||
func (r GroupList) LPushX(ctx context.Context, key string, element interface{}, elements ...interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "LPushX", append([]interface{}{key, element}, elements...)...)
|
||||
v, err := r.Operation.Do(ctx, "LPushX", append([]interface{}{key, element}, elements...)...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ func (r GroupList) LPushX(ctx context.Context, key string, element interface{},
|
||||
//
|
||||
// https://redis.io/commands/rpush
|
||||
func (r GroupList) RPush(ctx context.Context, key string, values ...interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "RPush", append([]interface{}{key}, values...)...)
|
||||
v, err := r.Operation.Do(ctx, "RPush", append([]interface{}{key}, values...)...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -81,7 +81,7 @@ func (r GroupList) RPush(ctx context.Context, key string, values ...interface{})
|
||||
//
|
||||
// https://redis.io/commands/rpushx
|
||||
func (r GroupList) RPushX(ctx context.Context, key string, value interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "RPushX", key, value)
|
||||
v, err := r.Operation.Do(ctx, "RPushX", key, value)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -103,9 +103,9 @@ func (r GroupList) RPushX(ctx context.Context, key string, value interface{}) (i
|
||||
// https://redis.io/commands/lpop
|
||||
func (r GroupList) LPop(ctx context.Context, key string, count ...int) (*gvar.Var, error) {
|
||||
if len(count) > 0 {
|
||||
return r.redis.Do(ctx, "LPop", key, count[0])
|
||||
return r.Operation.Do(ctx, "LPop", key, count[0])
|
||||
}
|
||||
return r.redis.Do(ctx, "LPop", key)
|
||||
return r.Operation.Do(ctx, "LPop", key)
|
||||
}
|
||||
|
||||
// RPop remove and returns the last element of the list stored at key.
|
||||
@ -126,9 +126,9 @@ func (r GroupList) LPop(ctx context.Context, key string, count ...int) (*gvar.Va
|
||||
// https://redis.io/commands/rpop
|
||||
func (r GroupList) RPop(ctx context.Context, key string, count ...int) (*gvar.Var, error) {
|
||||
if len(count) > 0 {
|
||||
return r.redis.Do(ctx, "RPop", key, count[0])
|
||||
return r.Operation.Do(ctx, "RPop", key, count[0])
|
||||
}
|
||||
return r.redis.Do(ctx, "RPop", key)
|
||||
return r.Operation.Do(ctx, "RPop", key)
|
||||
}
|
||||
|
||||
// LRem removes the first count occurrences of elements equal to value from the list stored at key.
|
||||
@ -137,7 +137,7 @@ func (r GroupList) RPop(ctx context.Context, key string, count ...int) (*gvar.Va
|
||||
//
|
||||
// https://redis.io/commands/lrem/
|
||||
func (r GroupList) LRem(ctx context.Context, key string, count int64, value interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "LRem", key, count, value)
|
||||
v, err := r.Operation.Do(ctx, "LRem", key, count, value)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -148,7 +148,7 @@ func (r GroupList) LRem(ctx context.Context, key string, count int64, value inte
|
||||
//
|
||||
// https://redis.io/commands/llen
|
||||
func (r GroupList) LLen(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "LLen", key)
|
||||
v, err := r.Operation.Do(ctx, "LLen", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -163,7 +163,7 @@ func (r GroupList) LLen(ctx context.Context, key string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/lindex
|
||||
func (r GroupList) LIndex(ctx context.Context, key string, index int64) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "LIndex", key, index)
|
||||
return r.Operation.Do(ctx, "LIndex", key, index)
|
||||
}
|
||||
|
||||
// LInsert inserts element in the list stored at key either before or after the reference value pivot.
|
||||
@ -174,7 +174,7 @@ func (r GroupList) LIndex(ctx context.Context, key string, index int64) (*gvar.V
|
||||
//
|
||||
// https://redis.io/commands/linsert/
|
||||
func (r GroupList) LInsert(ctx context.Context, key string, op gredis.LInsertOp, pivot, value interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "LInsert", key, string(op), pivot, value)
|
||||
v, err := r.Operation.Do(ctx, "LInsert", key, string(op), pivot, value)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -184,7 +184,7 @@ func (r GroupList) LInsert(ctx context.Context, key string, op gredis.LInsertOp,
|
||||
//
|
||||
// https://redis.io/commands/lset/
|
||||
func (r GroupList) LSet(ctx context.Context, key string, index int64, value interface{}) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "LSet", key, index, value)
|
||||
return r.Operation.Do(ctx, "LSet", key, index, value)
|
||||
}
|
||||
|
||||
// LRange returns the specified elements of the list stored at key.
|
||||
@ -196,7 +196,7 @@ func (r GroupList) LSet(ctx context.Context, key string, index int64, value inte
|
||||
//
|
||||
// https://redis.io/commands/lrange/
|
||||
func (r GroupList) LRange(ctx context.Context, key string, start, stop int64) (gvar.Vars, error) {
|
||||
v, err := r.redis.Do(ctx, "LRange", key, start, stop)
|
||||
v, err := r.Operation.Do(ctx, "LRange", key, start, stop)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -206,7 +206,7 @@ func (r GroupList) LRange(ctx context.Context, key string, start, stop int64) (g
|
||||
//
|
||||
// https://redis.io/commands/ltrim/
|
||||
func (r GroupList) LTrim(ctx context.Context, key string, start, stop int64) error {
|
||||
_, err := r.redis.Do(ctx, "LTrim", key, start, stop)
|
||||
_, err := r.Operation.Do(ctx, "LTrim", key, start, stop)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -221,7 +221,7 @@ func (r GroupList) LTrim(ctx context.Context, key string, start, stop int64) err
|
||||
//
|
||||
// https://redis.io/commands/blpop/
|
||||
func (r GroupList) BLPop(ctx context.Context, timeout int64, keys ...string) (gvar.Vars, error) {
|
||||
v, err := r.redis.Do(ctx, "BLPop", append(gconv.Interfaces(keys), timeout)...)
|
||||
v, err := r.Operation.Do(ctx, "BLPop", append(gconv.Interfaces(keys), timeout)...)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -235,7 +235,7 @@ func (r GroupList) BLPop(ctx context.Context, timeout int64, keys ...string) (gv
|
||||
//
|
||||
// https://redis.io/commands/brpop/
|
||||
func (r GroupList) BRPop(ctx context.Context, timeout int64, keys ...string) (gvar.Vars, error) {
|
||||
v, err := r.redis.Do(ctx, "BRPop", append(gconv.Interfaces(keys), timeout)...)
|
||||
v, err := r.Operation.Do(ctx, "BRPop", append(gconv.Interfaces(keys), timeout)...)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -244,7 +244,7 @@ func (r GroupList) BRPop(ctx context.Context, timeout int64, keys ...string) (gv
|
||||
//
|
||||
// https://redis.io/commands/rpoplpush/
|
||||
func (r GroupList) RPopLPush(ctx context.Context, source, destination string) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "RPopLPush", source, destination)
|
||||
return r.Operation.Do(ctx, "RPopLPush", source, destination)
|
||||
}
|
||||
|
||||
// BRPopLPush is the blocking variant of RPopLPush.
|
||||
@ -259,5 +259,5 @@ func (r GroupList) RPopLPush(ctx context.Context, source, destination string) (*
|
||||
//
|
||||
// https://redis.io/commands/brpoplpush/
|
||||
func (r GroupList) BRPopLPush(ctx context.Context, source, destination string, timeout int64) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "BRPopLPush", source, destination, timeout)
|
||||
return r.Operation.Do(ctx, "BRPopLPush", source, destination, timeout)
|
||||
}
|
||||
|
||||
@ -14,13 +14,13 @@ import (
|
||||
|
||||
// GroupPubSub provides pub/sub functions for redis.
|
||||
type GroupPubSub struct {
|
||||
redis *Redis
|
||||
Operation gredis.AdapterOperation
|
||||
}
|
||||
|
||||
// GroupPubSub creates and returns GroupPubSub.
|
||||
func (r *Redis) GroupPubSub() gredis.IGroupPubSub {
|
||||
return GroupPubSub{
|
||||
redis: r,
|
||||
Operation: r.AdapterOperation,
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ func (r *Redis) GroupPubSub() gredis.IGroupPubSub {
|
||||
//
|
||||
// https://redis.io/commands/publish/
|
||||
func (r GroupPubSub) Publish(ctx context.Context, channel string, message interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Publish", channel, message)
|
||||
v, err := r.Operation.Do(ctx, "Publish", channel, message)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ func (r GroupPubSub) Publish(ctx context.Context, channel string, message interf
|
||||
func (r GroupPubSub) Subscribe(
|
||||
ctx context.Context, channel string, channels ...string,
|
||||
) (gredis.Conn, []*gredis.Subscription, error) {
|
||||
conn, err := r.redis.Conn(ctx)
|
||||
conn, err := r.Operation.Conn(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@ -70,7 +70,7 @@ func (r GroupPubSub) Subscribe(
|
||||
func (r GroupPubSub) PSubscribe(
|
||||
ctx context.Context, pattern string, patterns ...string,
|
||||
) (gredis.Conn, []*gredis.Subscription, error) {
|
||||
conn, err := r.redis.Conn(ctx)
|
||||
conn, err := r.Operation.Conn(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@ -16,13 +16,13 @@ import (
|
||||
|
||||
// GroupScript provides script functions for redis.
|
||||
type GroupScript struct {
|
||||
redis *Redis
|
||||
Operation gredis.AdapterOperation
|
||||
}
|
||||
|
||||
// GroupScript creates and returns GroupScript.
|
||||
func (r *Redis) GroupScript() gredis.IGroupScript {
|
||||
return GroupScript{
|
||||
redis: r,
|
||||
Operation: r.AdapterOperation,
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ func (r GroupScript) Eval(ctx context.Context, script string, numKeys int64, key
|
||||
var s = []interface{}{script, numKeys}
|
||||
s = append(s, gconv.Interfaces(keys)...)
|
||||
s = append(s, args...)
|
||||
v, err := r.redis.Do(ctx, "Eval", s...)
|
||||
v, err := r.Operation.Do(ctx, "Eval", s...)
|
||||
return v, err
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ func (r GroupScript) EvalSha(ctx context.Context, sha1 string, numKeys int64, ke
|
||||
var s = []interface{}{sha1, numKeys}
|
||||
s = append(s, gconv.Interfaces(keys)...)
|
||||
s = append(s, args...)
|
||||
v, err := r.redis.Do(ctx, "EvalSha", s...)
|
||||
v, err := r.Operation.Do(ctx, "EvalSha", s...)
|
||||
return v, err
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ func (r GroupScript) EvalSha(ctx context.Context, sha1 string, numKeys int64, ke
|
||||
//
|
||||
// https://redis.io/commands/script-load/
|
||||
func (r GroupScript) ScriptLoad(ctx context.Context, script string) (string, error) {
|
||||
v, err := r.redis.Do(ctx, "Script", "Load", script)
|
||||
v, err := r.Operation.Do(ctx, "Script", "Load", script)
|
||||
return v.String(), err
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ func (r GroupScript) ScriptExists(ctx context.Context, sha1 string, sha1s ...str
|
||||
)
|
||||
s = append(s, "Exists")
|
||||
s = append(s, sha1Array...)
|
||||
result, err := r.redis.Do(ctx, "Script", s...)
|
||||
result, err := r.Operation.Do(ctx, "Script", s...)
|
||||
var (
|
||||
m = make(map[string]bool)
|
||||
resultArray = result.Vars()
|
||||
@ -99,7 +99,7 @@ func (r GroupScript) ScriptFlush(ctx context.Context, option ...gredis.ScriptFlu
|
||||
s = append(s, mustMergeOptionToArgs(
|
||||
[]interface{}{}, usedOption,
|
||||
)...)
|
||||
_, err := r.redis.Do(ctx, "Script", s...)
|
||||
_, err := r.Operation.Do(ctx, "Script", s...)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -108,6 +108,6 @@ func (r GroupScript) ScriptFlush(ctx context.Context, option ...gredis.ScriptFlu
|
||||
//
|
||||
// https://redis.io/commands/script-kill/
|
||||
func (r GroupScript) ScriptKill(ctx context.Context) error {
|
||||
_, err := r.redis.Do(ctx, "Script", "Kill")
|
||||
_, err := r.Operation.Do(ctx, "Script", "Kill")
|
||||
return err
|
||||
}
|
||||
|
||||
@ -16,13 +16,13 @@ import (
|
||||
|
||||
// GroupSet provides set functions for redis.
|
||||
type GroupSet struct {
|
||||
redis *Redis
|
||||
Operation gredis.AdapterOperation
|
||||
}
|
||||
|
||||
// GroupSet creates and returns GroupSet.
|
||||
func (r *Redis) GroupSet() gredis.IGroupSet {
|
||||
return GroupSet{
|
||||
redis: r,
|
||||
Operation: r.AdapterOperation,
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ func (r GroupSet) SAdd(ctx context.Context, key string, member interface{}, memb
|
||||
var s = []interface{}{key}
|
||||
s = append(s, member)
|
||||
s = append(s, members...)
|
||||
v, err := r.redis.Do(ctx, "SAdd", s...)
|
||||
v, err := r.Operation.Do(ctx, "SAdd", s...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ func (r GroupSet) SAdd(ctx context.Context, key string, member interface{}, memb
|
||||
//
|
||||
// https://redis.io/commands/sismember/
|
||||
func (r GroupSet) SIsMember(ctx context.Context, key string, member interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "SIsMember", key, member)
|
||||
v, err := r.Operation.Do(ctx, "SIsMember", key, member)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ func (r GroupSet) SIsMember(ctx context.Context, key string, member interface{})
|
||||
func (r GroupSet) SPop(ctx context.Context, key string, count ...int) (*gvar.Var, error) {
|
||||
var s = []interface{}{key}
|
||||
s = append(s, gconv.Interfaces(count)...)
|
||||
v, err := r.redis.Do(ctx, "SPop", s...)
|
||||
v, err := r.Operation.Do(ctx, "SPop", s...)
|
||||
return v, err
|
||||
}
|
||||
|
||||
@ -95,7 +95,7 @@ func (r GroupSet) SPop(ctx context.Context, key string, count ...int) (*gvar.Var
|
||||
func (r GroupSet) SRandMember(ctx context.Context, key string, count ...int) (*gvar.Var, error) {
|
||||
var s = []interface{}{key}
|
||||
s = append(s, gconv.Interfaces(count)...)
|
||||
v, err := r.redis.Do(ctx, "SRandMember", s...)
|
||||
v, err := r.Operation.Do(ctx, "SRandMember", s...)
|
||||
return v, err
|
||||
}
|
||||
|
||||
@ -112,7 +112,7 @@ func (r GroupSet) SRem(ctx context.Context, key string, member interface{}, memb
|
||||
var s = []interface{}{key}
|
||||
s = append(s, member)
|
||||
s = append(s, members...)
|
||||
v, err := r.redis.Do(ctx, "SRem", s...)
|
||||
v, err := r.Operation.Do(ctx, "SRem", s...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -131,7 +131,7 @@ func (r GroupSet) SRem(ctx context.Context, key string, member interface{}, memb
|
||||
//
|
||||
// https://redis.io/commands/smove/
|
||||
func (r GroupSet) SMove(ctx context.Context, source, destination string, member interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "SMove", source, destination, member)
|
||||
v, err := r.Operation.Do(ctx, "SMove", source, destination, member)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -141,7 +141,7 @@ func (r GroupSet) SMove(ctx context.Context, source, destination string, member
|
||||
//
|
||||
// https://redis.io/commands/scard/
|
||||
func (r GroupSet) SCard(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "SCard", key)
|
||||
v, err := r.Operation.Do(ctx, "SCard", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -152,7 +152,7 @@ func (r GroupSet) SCard(ctx context.Context, key string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/smembers/
|
||||
func (r GroupSet) SMembers(ctx context.Context, key string) (gvar.Vars, error) {
|
||||
v, err := r.redis.Do(ctx, "SMembers", key)
|
||||
v, err := r.Operation.Do(ctx, "SMembers", key)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -167,7 +167,7 @@ func (r GroupSet) SMembers(ctx context.Context, key string) (gvar.Vars, error) {
|
||||
func (r GroupSet) SMIsMember(ctx context.Context, key, member interface{}, members ...interface{}) ([]int, error) {
|
||||
var s = []interface{}{key, member}
|
||||
s = append(s, members...)
|
||||
v, err := r.redis.Do(ctx, "SMIsMember", s...)
|
||||
v, err := r.Operation.Do(ctx, "SMIsMember", s...)
|
||||
return v.Ints(), err
|
||||
}
|
||||
|
||||
@ -179,7 +179,7 @@ func (r GroupSet) SMIsMember(ctx context.Context, key, member interface{}, membe
|
||||
func (r GroupSet) SInter(ctx context.Context, key string, keys ...string) (gvar.Vars, error) {
|
||||
var s = []interface{}{key}
|
||||
s = append(s, gconv.Interfaces(keys)...)
|
||||
v, err := r.redis.Do(ctx, "SInter", s...)
|
||||
v, err := r.Operation.Do(ctx, "SInter", s...)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -194,7 +194,7 @@ func (r GroupSet) SInter(ctx context.Context, key string, keys ...string) (gvar.
|
||||
func (r GroupSet) SInterStore(ctx context.Context, destination string, key string, keys ...string) (int64, error) {
|
||||
var s = []interface{}{destination, key}
|
||||
s = append(s, gconv.Interfaces(keys)...)
|
||||
v, err := r.redis.Do(ctx, "SInterStore", s...)
|
||||
v, err := r.Operation.Do(ctx, "SInterStore", s...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -206,7 +206,7 @@ func (r GroupSet) SInterStore(ctx context.Context, destination string, key strin
|
||||
func (r GroupSet) SUnion(ctx context.Context, key string, keys ...string) (gvar.Vars, error) {
|
||||
var s = []interface{}{key}
|
||||
s = append(s, gconv.Interfaces(keys)...)
|
||||
v, err := r.redis.Do(ctx, "SUnion", s...)
|
||||
v, err := r.Operation.Do(ctx, "SUnion", s...)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -220,7 +220,7 @@ func (r GroupSet) SUnion(ctx context.Context, key string, keys ...string) (gvar.
|
||||
func (r GroupSet) SUnionStore(ctx context.Context, destination, key string, keys ...string) (int64, error) {
|
||||
var s = []interface{}{destination, key}
|
||||
s = append(s, gconv.Interfaces(keys)...)
|
||||
v, err := r.redis.Do(ctx, "SUnionStore", s...)
|
||||
v, err := r.Operation.Do(ctx, "SUnionStore", s...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -233,7 +233,7 @@ func (r GroupSet) SUnionStore(ctx context.Context, destination, key string, keys
|
||||
func (r GroupSet) SDiff(ctx context.Context, key string, keys ...string) (gvar.Vars, error) {
|
||||
var s = []interface{}{key}
|
||||
s = append(s, gconv.Interfaces(keys)...)
|
||||
v, err := r.redis.Do(ctx, "SDiff", s...)
|
||||
v, err := r.Operation.Do(ctx, "SDiff", s...)
|
||||
return v.Vars(), err
|
||||
}
|
||||
|
||||
@ -247,6 +247,6 @@ func (r GroupSet) SDiff(ctx context.Context, key string, keys ...string) (gvar.V
|
||||
func (r GroupSet) SDiffStore(ctx context.Context, destination string, key string, keys ...string) (int64, error) {
|
||||
var s = []interface{}{destination, key}
|
||||
s = append(s, gconv.Interfaces(keys)...)
|
||||
v, err := r.redis.Do(ctx, "SDiffStore", s...)
|
||||
v, err := r.Operation.Do(ctx, "SDiffStore", s...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -15,13 +15,13 @@ import (
|
||||
|
||||
// GroupSortedSet provides sorted set functions for redis.
|
||||
type GroupSortedSet struct {
|
||||
redis *Redis
|
||||
Operation gredis.AdapterOperation
|
||||
}
|
||||
|
||||
// GroupSortedSet creates and returns GroupSortedSet.
|
||||
func (r *Redis) GroupSortedSet() gredis.IGroupSortedSet {
|
||||
return GroupSortedSet{
|
||||
redis: r,
|
||||
Operation: r.AdapterOperation,
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ func (r GroupSortedSet) ZAdd(
|
||||
for _, item := range members {
|
||||
s = append(s, item.Score, item.Member)
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "ZAdd", s...)
|
||||
v, err := r.Operation.Do(ctx, "ZAdd", s...)
|
||||
return v, err
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ func (r GroupSortedSet) ZAdd(
|
||||
//
|
||||
// https://redis.io/commands/zscore/
|
||||
func (r GroupSortedSet) ZScore(ctx context.Context, key string, member interface{}) (float64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZScore", key, member)
|
||||
v, err := r.Operation.Do(ctx, "ZScore", key, member)
|
||||
return v.Float64(), err
|
||||
}
|
||||
|
||||
@ -84,7 +84,7 @@ func (r GroupSortedSet) ZScore(ctx context.Context, key string, member interface
|
||||
//
|
||||
// https://redis.io/commands/zincrby/
|
||||
func (r GroupSortedSet) ZIncrBy(ctx context.Context, key string, increment float64, member interface{}) (float64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZIncrBy", key, increment, member)
|
||||
v, err := r.Operation.Do(ctx, "ZIncrBy", key, increment, member)
|
||||
return v.Float64(), err
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ func (r GroupSortedSet) ZIncrBy(ctx context.Context, key string, increment float
|
||||
//
|
||||
// https://redis.io/commands/zcard/
|
||||
func (r GroupSortedSet) ZCard(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZCard", key)
|
||||
v, err := r.Operation.Do(ctx, "ZCard", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ func (r GroupSortedSet) ZCard(ctx context.Context, key string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/zcount/
|
||||
func (r GroupSortedSet) ZCount(ctx context.Context, key string, min, max string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZCount", key, min, max)
|
||||
v, err := r.Operation.Do(ctx, "ZCount", key, min, max)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -124,7 +124,7 @@ func (r GroupSortedSet) ZRange(ctx context.Context, key string, start, stop int6
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "ZRange", mustMergeOptionToArgs(
|
||||
v, err := r.Operation.Do(ctx, "ZRange", mustMergeOptionToArgs(
|
||||
[]interface{}{key, start, stop}, usedOption,
|
||||
)...)
|
||||
return v.Vars(), err
|
||||
@ -144,7 +144,7 @@ func (r GroupSortedSet) ZRevRange(ctx context.Context, key string, start, stop i
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
return r.redis.Do(ctx, "ZRevRange", mustMergeOptionToArgs(
|
||||
return r.Operation.Do(ctx, "ZRevRange", mustMergeOptionToArgs(
|
||||
[]interface{}{key, start, stop}, usedOption,
|
||||
)...)
|
||||
}
|
||||
@ -160,7 +160,7 @@ func (r GroupSortedSet) ZRevRange(ctx context.Context, key string, start, stop i
|
||||
//
|
||||
// https://redis.io/commands/zrank/
|
||||
func (r GroupSortedSet) ZRank(ctx context.Context, key string, member interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZRank", key, member)
|
||||
v, err := r.Operation.Do(ctx, "ZRank", key, member)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -175,7 +175,7 @@ func (r GroupSortedSet) ZRank(ctx context.Context, key string, member interface{
|
||||
//
|
||||
// https://redis.io/commands/zrevrank/
|
||||
func (r GroupSortedSet) ZRevRank(ctx context.Context, key string, member interface{}) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZRevRank", key, member)
|
||||
v, err := r.Operation.Do(ctx, "ZRevRank", key, member)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -191,7 +191,7 @@ func (r GroupSortedSet) ZRem(ctx context.Context, key string, member interface{}
|
||||
var s = []interface{}{key}
|
||||
s = append(s, member)
|
||||
s = append(s, members...)
|
||||
v, err := r.redis.Do(ctx, "ZRem", s...)
|
||||
v, err := r.Operation.Do(ctx, "ZRem", s...)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -206,7 +206,7 @@ func (r GroupSortedSet) ZRem(ctx context.Context, key string, member interface{}
|
||||
//
|
||||
// https://redis.io/commands/zremrangebyrank/
|
||||
func (r GroupSortedSet) ZRemRangeByRank(ctx context.Context, key string, start, stop int64) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZRemRangeByRank", key, start, stop)
|
||||
v, err := r.Operation.Do(ctx, "ZRemRangeByRank", key, start, stop)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -217,7 +217,7 @@ func (r GroupSortedSet) ZRemRangeByRank(ctx context.Context, key string, start,
|
||||
//
|
||||
// https://redis.io/commands/zremrangebyscore/
|
||||
func (r GroupSortedSet) ZRemRangeByScore(ctx context.Context, key string, min, max string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZRemRangeByScore", key, min, max)
|
||||
v, err := r.Operation.Do(ctx, "ZRemRangeByScore", key, min, max)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -232,7 +232,7 @@ func (r GroupSortedSet) ZRemRangeByScore(ctx context.Context, key string, min, m
|
||||
//
|
||||
// https://redis.io/commands/zremrangebylex/
|
||||
func (r GroupSortedSet) ZRemRangeByLex(ctx context.Context, key string, min, max string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZRemRangeByLex", key, min, max)
|
||||
v, err := r.Operation.Do(ctx, "ZRemRangeByLex", key, min, max)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -249,6 +249,6 @@ func (r GroupSortedSet) ZRemRangeByLex(ctx context.Context, key string, min, max
|
||||
//
|
||||
// https://redis.io/commands/zlexcount/
|
||||
func (r GroupSortedSet) ZLexCount(ctx context.Context, key, min, max string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "ZLexCount", key, min, max)
|
||||
v, err := r.Operation.Do(ctx, "ZLexCount", key, min, max)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -16,13 +16,13 @@ import (
|
||||
|
||||
// GroupString is the function group manager for string operations.
|
||||
type GroupString struct {
|
||||
redis *Redis
|
||||
Operation gredis.AdapterOperation
|
||||
}
|
||||
|
||||
// GroupString is the redis group object for string operations.
|
||||
func (r *Redis) GroupString() gredis.IGroupString {
|
||||
return GroupString{
|
||||
redis: r,
|
||||
Operation: r.AdapterOperation,
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ func (r GroupString) Set(ctx context.Context, key string, value interface{}, opt
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
return r.redis.Do(ctx, "Set", mustMergeOptionToArgs(
|
||||
return r.Operation.Do(ctx, "Set", mustMergeOptionToArgs(
|
||||
[]interface{}{key, value}, usedOption,
|
||||
)...)
|
||||
}
|
||||
@ -52,7 +52,7 @@ func (r GroupString) Set(ctx context.Context, key string, value interface{}, opt
|
||||
//
|
||||
// https://redis.io/commands/setnx/
|
||||
func (r GroupString) SetNX(ctx context.Context, key string, value interface{}) (bool, error) {
|
||||
v, err := r.redis.Do(ctx, "SetNX", key, value)
|
||||
v, err := r.Operation.Do(ctx, "SetNX", key, value)
|
||||
return v.Bool(), err
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ func (r GroupString) SetNX(ctx context.Context, key string, value interface{}) (
|
||||
//
|
||||
// https://redis.io/commands/setex/
|
||||
func (r GroupString) SetEX(ctx context.Context, key string, value interface{}, ttlInSeconds int64) error {
|
||||
_, err := r.redis.Do(ctx, "SetEX", key, ttlInSeconds, value)
|
||||
_, err := r.Operation.Do(ctx, "SetEX", key, ttlInSeconds, value)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ func (r GroupString) SetEX(ctx context.Context, key string, value interface{}, t
|
||||
//
|
||||
// https://redis.io/commands/get/
|
||||
func (r GroupString) Get(ctx context.Context, key string) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "Get", key)
|
||||
return r.Operation.Do(ctx, "Get", key)
|
||||
}
|
||||
|
||||
// GetDel gets the value of key and delete the key.
|
||||
@ -88,7 +88,7 @@ func (r GroupString) Get(ctx context.Context, key string) (*gvar.Var, error) {
|
||||
//
|
||||
// https://redis.io/commands/getdel/
|
||||
func (r GroupString) GetDel(ctx context.Context, key string) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "GetDel", key)
|
||||
return r.Operation.Do(ctx, "GetDel", key)
|
||||
}
|
||||
|
||||
// GetEX is similar to GET, but is a write command with additional options.
|
||||
@ -99,7 +99,7 @@ func (r GroupString) GetEX(ctx context.Context, key string, option ...gredis.Get
|
||||
if len(option) > 0 {
|
||||
usedOption = option[0]
|
||||
}
|
||||
return r.redis.Do(ctx, "GetEX", mustMergeOptionToArgs(
|
||||
return r.Operation.Do(ctx, "GetEX", mustMergeOptionToArgs(
|
||||
[]interface{}{key}, usedOption,
|
||||
)...)
|
||||
}
|
||||
@ -110,7 +110,7 @@ func (r GroupString) GetEX(ctx context.Context, key string, option ...gredis.Get
|
||||
//
|
||||
// https://redis.io/commands/getset/
|
||||
func (r GroupString) GetSet(ctx context.Context, key string, value interface{}) (*gvar.Var, error) {
|
||||
return r.redis.Do(ctx, "GetSet", key, value)
|
||||
return r.Operation.Do(ctx, "GetSet", key, value)
|
||||
}
|
||||
|
||||
// StrLen returns the length of the string value stored at key.
|
||||
@ -120,7 +120,7 @@ func (r GroupString) GetSet(ctx context.Context, key string, value interface{})
|
||||
//
|
||||
// https://redis.io/commands/strlen/
|
||||
func (r GroupString) StrLen(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "StrLen", key)
|
||||
v, err := r.Operation.Do(ctx, "StrLen", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -130,7 +130,7 @@ func (r GroupString) StrLen(ctx context.Context, key string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/append/
|
||||
func (r GroupString) Append(ctx context.Context, key string, value string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Append", key, value)
|
||||
v, err := r.Operation.Do(ctx, "Append", key, value)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -143,7 +143,7 @@ func (r GroupString) Append(ctx context.Context, key string, value string) (int6
|
||||
//
|
||||
// https://redis.io/commands/setrange/
|
||||
func (r GroupString) SetRange(ctx context.Context, key string, offset int64, value string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "SetRange", key, offset, value)
|
||||
v, err := r.Operation.Do(ctx, "SetRange", key, offset, value)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ func (r GroupString) SetRange(ctx context.Context, key string, offset int64, val
|
||||
//
|
||||
// https://redis.io/commands/getrange/
|
||||
func (r GroupString) GetRange(ctx context.Context, key string, start, end int64) (string, error) {
|
||||
v, err := r.redis.Do(ctx, "GetRange", key, start, end)
|
||||
v, err := r.Operation.Do(ctx, "GetRange", key, start, end)
|
||||
return v.String(), err
|
||||
}
|
||||
|
||||
@ -166,7 +166,7 @@ func (r GroupString) GetRange(ctx context.Context, key string, start, end int64)
|
||||
//
|
||||
// https://redis.io/commands/incr/
|
||||
func (r GroupString) Incr(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Incr", key)
|
||||
v, err := r.Operation.Do(ctx, "Incr", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -178,7 +178,7 @@ func (r GroupString) Incr(ctx context.Context, key string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/incrby/
|
||||
func (r GroupString) IncrBy(ctx context.Context, key string, increment int64) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "IncrBy", key, increment)
|
||||
v, err := r.Operation.Do(ctx, "IncrBy", key, increment)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -186,7 +186,7 @@ func (r GroupString) IncrBy(ctx context.Context, key string, increment int64) (i
|
||||
//
|
||||
// https://redis.io/commands/incrbyfloat/
|
||||
func (r GroupString) IncrByFloat(ctx context.Context, key string, increment float64) (float64, error) {
|
||||
v, err := r.redis.Do(ctx, "IncrByFloat", key, increment)
|
||||
v, err := r.Operation.Do(ctx, "IncrByFloat", key, increment)
|
||||
return v.Float64(), err
|
||||
}
|
||||
|
||||
@ -194,7 +194,7 @@ func (r GroupString) IncrByFloat(ctx context.Context, key string, increment floa
|
||||
//
|
||||
// https://redis.io/commands/decr/
|
||||
func (r GroupString) Decr(ctx context.Context, key string) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "Decr", key)
|
||||
v, err := r.Operation.Do(ctx, "Decr", key)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -202,7 +202,7 @@ func (r GroupString) Decr(ctx context.Context, key string) (int64, error) {
|
||||
//
|
||||
// https://redis.io/commands/decrby/
|
||||
func (r GroupString) DecrBy(ctx context.Context, key string, decrement int64) (int64, error) {
|
||||
v, err := r.redis.Do(ctx, "DecrBy", key, decrement)
|
||||
v, err := r.Operation.Do(ctx, "DecrBy", key, decrement)
|
||||
return v.Int64(), err
|
||||
}
|
||||
|
||||
@ -219,7 +219,7 @@ func (r GroupString) MSet(ctx context.Context, keyValueMap map[string]interface{
|
||||
for k, v := range keyValueMap {
|
||||
args = append(args, k, v)
|
||||
}
|
||||
_, err := r.redis.Do(ctx, "MSet", args...)
|
||||
_, err := r.Operation.Do(ctx, "MSet", args...)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -233,7 +233,7 @@ func (r GroupString) MSetNX(ctx context.Context, keyValueMap map[string]interfac
|
||||
for k, v := range keyValueMap {
|
||||
args = append(args, k, v)
|
||||
}
|
||||
v, err := r.redis.Do(ctx, "MSetNX", args...)
|
||||
v, err := r.Operation.Do(ctx, "MSetNX", args...)
|
||||
return v.Bool(), err
|
||||
}
|
||||
|
||||
@ -242,7 +242,7 @@ func (r GroupString) MSetNX(ctx context.Context, keyValueMap map[string]interfac
|
||||
// https://redis.io/commands/mget/
|
||||
func (r GroupString) MGet(ctx context.Context, keys ...string) (map[string]*gvar.Var, error) {
|
||||
var result = make(map[string]*gvar.Var)
|
||||
v, err := r.redis.Do(ctx, "MGet", gconv.Interfaces(keys)...)
|
||||
v, err := r.Operation.Do(ctx, "MGet", gconv.Interfaces(keys)...)
|
||||
if err == nil {
|
||||
values := v.Vars()
|
||||
for i, key := range keys {
|
||||
|
||||
45
contrib/nosql/redis/redis_operation.go
Normal file
45
contrib/nosql/redis/redis_operation.go
Normal file
@ -0,0 +1,45 @@
|
||||
// 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 redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// Do send a command to the server and returns the received reply.
|
||||
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
|
||||
func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
|
||||
conn, err := r.Conn(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = conn.Close(ctx)
|
||||
}()
|
||||
return conn.Do(ctx, command, args...)
|
||||
}
|
||||
|
||||
// Close closes the redis connection pool, which will release all connections reserved by this pool.
|
||||
// It is commonly not necessary to call Close manually.
|
||||
func (r *Redis) Close(ctx context.Context) (err error) {
|
||||
if err = r.client.Close(); err != nil {
|
||||
err = gerror.Wrap(err, `Operation Client Close failed`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Conn retrieves and returns a connection object for continuous operations.
|
||||
// Note that you should call Close function manually if you do not use this connection any further.
|
||||
func (r *Redis) Conn(ctx context.Context) (gredis.Conn, error) {
|
||||
return &Conn{
|
||||
redis: r,
|
||||
}, nil
|
||||
}
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/etcd/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
go.etcd.io/etcd/client/v3 v3.5.7
|
||||
)
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@ module github.com/gogf/gf/contrib/registry/file/v2
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/gogf/gf/v2 v2.6.0
|
||||
require github.com/gogf/gf/v2 v2.6.2
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.2.0 // indirect
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/nacos/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/joy999/nacos-sdk-go v0.0.0-20231120071639-10a34b3e7288
|
||||
)
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/polaris/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/polarismesh/polaris-go v1.5.5
|
||||
)
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/go-zookeeper/zk v1.0.3
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
golang.org/x/sync v0.4.0
|
||||
)
|
||||
|
||||
|
||||
@ -3,8 +3,8 @@ module github.com/gogf/gf/contrib/rpc/grpcx/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/registry/file/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/registry/file/v2 v2.6.2
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
go.opentelemetry.io/otel v1.14.0
|
||||
go.opentelemetry.io/otel/trace v1.14.0
|
||||
google.golang.org/grpc v1.57.2
|
||||
|
||||
@ -8,6 +8,8 @@ package grpcx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
@ -74,8 +76,26 @@ func (s modServer) NewConfig() *GrpcServerConfig {
|
||||
// Reading configuration file and updating the configured keys.
|
||||
if g.Cfg().Available(ctx) {
|
||||
// Server attributes configuration.
|
||||
if err = g.Cfg().MustGet(ctx, configNodeNameGrpcServer).Struct(&config); err != nil {
|
||||
serverConfigMap := g.Cfg().MustGet(ctx, configNodeNameGrpcServer).Map()
|
||||
if len(serverConfigMap) == 0 {
|
||||
return config
|
||||
}
|
||||
if err = gconv.Struct(serverConfigMap, &config); err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
return config
|
||||
}
|
||||
// Server logger configuration checks.
|
||||
serverLoggerConfigMap := g.Cfg().MustGet(
|
||||
ctx,
|
||||
fmt.Sprintf(`%s.logger`, configNodeNameGrpcServer),
|
||||
).Map()
|
||||
if len(serverLoggerConfigMap) == 0 && len(serverConfigMap) > 0 {
|
||||
serverLoggerConfigMap = gconv.Map(serverConfigMap["logger"])
|
||||
}
|
||||
if len(serverLoggerConfigMap) > 0 {
|
||||
if err = config.Logger.SetConfigWithMap(serverLoggerConfigMap); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return config
|
||||
|
||||
@ -7,9 +7,13 @@
|
||||
package grpcx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/debug/gdebug"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
@ -70,3 +74,28 @@ func Test_Grpcx_Grpc_Server_Config(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Grpcx_Grpc_Server_Config_Logger(t *testing.T) {
|
||||
var (
|
||||
pwd = gfile.Pwd()
|
||||
configDir = gfile.Join(gdebug.CallerDirectory(), "testdata", "configuration")
|
||||
)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
err := gfile.Chdir(configDir)
|
||||
t.AssertNil(err)
|
||||
defer gfile.Chdir(pwd)
|
||||
|
||||
s := Server.New()
|
||||
s.Start()
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
defer s.Stop()
|
||||
|
||||
var (
|
||||
logFilePath = fmt.Sprintf("/tmp/log/%s.log", gtime.Now().Format("Y-m-d"))
|
||||
logFileContent = gfile.GetContents(logFilePath)
|
||||
)
|
||||
t.Assert(gfile.Exists(logFilePath), true)
|
||||
t.Assert(gstr.Contains(logFileContent, "TestLogger "), true)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
14
contrib/rpc/grpcx/testdata/configuration/config.yaml
vendored
Normal file
14
contrib/rpc/grpcx/testdata/configuration/config.yaml
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
grpc:
|
||||
name: "demo" # 服务名称
|
||||
address: ":8000" # 自定义服务监听地址
|
||||
logPath: "./log" # 日志存储目录路径
|
||||
logStdout: true # 日志是否输出到终端
|
||||
errorLogEnabled: true # 是否开启错误日志记录
|
||||
accessLogEnabled: true # 是否开启访问日志记录
|
||||
errorStack: true # 当产生错误时,是否记录错误堆栈
|
||||
logger:
|
||||
path: "/tmp/log/" # 日志文件路径。默认为空,表示关闭,仅输出到终端
|
||||
file: "{Y-m-d}.log" # 日志文件格式。默认为"{Y-m-d}.log"
|
||||
prefix: "TestLogger" # 日志内容输出前缀。默认为空
|
||||
level: "all" # 日志输出级别
|
||||
stdout: false # 日志是否同时输出到终端。默认true
|
||||
@ -2,7 +2,7 @@ module github.com/gogf/gf/contrib/sdk/httpclient/v2
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/gogf/gf/v2 v2.6.0
|
||||
require github.com/gogf/gf/v2 v2.6.2
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.2.0 // indirect
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/jaeger/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
go.opentelemetry.io/otel v1.14.0
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.14.0
|
||||
go.opentelemetry.io/otel/sdk v1.14.0
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/otlpgrpc/v2
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
go.opentelemetry.io/otel v1.19.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0
|
||||
|
||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/otlphttp/v2
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
go.opentelemetry.io/otel v1.19.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0
|
||||
|
||||
@ -60,7 +60,7 @@ func (c *Core) GetFieldType(ctx context.Context, fieldName, table, schema string
|
||||
func (c *Core) ConvertDataForRecord(ctx context.Context, value interface{}, table string) (map[string]interface{}, error) {
|
||||
var (
|
||||
err error
|
||||
data = MapOrStructToMapDeep(value, false)
|
||||
data = MapOrStructToMapDeep(value, true)
|
||||
)
|
||||
for fieldName, fieldValue := range data {
|
||||
data[fieldName], err = c.db.ConvertValueForField(
|
||||
|
||||
@ -96,7 +96,9 @@ func DBFromCtx(ctx context.Context) DB {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToSQL formats and returns the last one of sql statements in given closure function without truly executing it.
|
||||
// ToSQL formats and returns the last one of sql statements in given closure function
|
||||
// WITHOUT TRULY EXECUTING IT.
|
||||
// Be caution that, all the following sql statements should use the context object passing by function `f`.
|
||||
func ToSQL(ctx context.Context, f func(ctx context.Context) error) (sql string, err error) {
|
||||
var manager = &CatchSQLManager{
|
||||
SQLArray: garray.NewStrArray(),
|
||||
@ -108,7 +110,8 @@ func ToSQL(ctx context.Context, f func(ctx context.Context) error) (sql string,
|
||||
return
|
||||
}
|
||||
|
||||
// CatchSQL catches and returns all sql statements that are executed in given closure function.
|
||||
// CatchSQL catches and returns all sql statements that are EXECUTED in given closure function.
|
||||
// Be caution that, all the following sql statements should use the context object passing by function `f`.
|
||||
func CatchSQL(ctx context.Context, f func(ctx context.Context) error) (sqlArray []string, err error) {
|
||||
var manager = &CatchSQLManager{
|
||||
SQLArray: garray.NewStrArray(),
|
||||
@ -210,12 +213,15 @@ func GetInsertOperationByOption(option InsertOption) string {
|
||||
}
|
||||
|
||||
func anyValueToMapBeforeToRecord(value interface{}) map[string]interface{} {
|
||||
return gconv.Map(value, gconv.MapOption{Tags: structTagPriority})
|
||||
return gconv.Map(value, gconv.MapOption{
|
||||
Tags: structTagPriority,
|
||||
OmitEmpty: true, // To be compatible with old version from v2.6.0.
|
||||
})
|
||||
}
|
||||
|
||||
// DaToMapDeep is deprecated, use MapOrStructToMapDeep instead.
|
||||
func DaToMapDeep(value interface{}) map[string]interface{} {
|
||||
return MapOrStructToMapDeep(value, false)
|
||||
return MapOrStructToMapDeep(value, true)
|
||||
}
|
||||
|
||||
// MapOrStructToMapDeep converts `value` to map type recursively(if attribute struct is embedded).
|
||||
|
||||
@ -15,7 +15,24 @@ import (
|
||||
// Adapter is an interface for universal redis operations.
|
||||
type Adapter interface {
|
||||
AdapterGroup
|
||||
AdapterOperation
|
||||
}
|
||||
|
||||
// AdapterGroup is an interface managing group operations for redis.
|
||||
type AdapterGroup interface {
|
||||
GroupGeneric() IGroupGeneric
|
||||
GroupHash() IGroupHash
|
||||
GroupList() IGroupList
|
||||
GroupPubSub() IGroupPubSub
|
||||
GroupScript() IGroupScript
|
||||
GroupSet() IGroupSet
|
||||
GroupSortedSet() IGroupSortedSet
|
||||
GroupString() IGroupString
|
||||
}
|
||||
|
||||
// AdapterOperation is the core operation functions for redis.
|
||||
// These functions can be easily overwritten by custom implements.
|
||||
type AdapterOperation interface {
|
||||
// Do send a command to the server and returns the received reply.
|
||||
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
|
||||
Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error)
|
||||
@ -40,18 +57,6 @@ type Conn interface {
|
||||
Close(ctx context.Context) (err error)
|
||||
}
|
||||
|
||||
// AdapterGroup is an interface managing group operations for redis.
|
||||
type AdapterGroup interface {
|
||||
GroupGeneric() IGroupGeneric
|
||||
GroupHash() IGroupHash
|
||||
GroupList() IGroupList
|
||||
GroupPubSub() IGroupPubSub
|
||||
GroupScript() IGroupScript
|
||||
GroupSet() IGroupSet
|
||||
GroupSortedSet() IGroupSortedSet
|
||||
GroupString() IGroupString
|
||||
}
|
||||
|
||||
// ConnCommand is an interface managing some operations bound to certain connection.
|
||||
type ConnCommand interface {
|
||||
// Subscribe subscribes the client to the specified channels.
|
||||
|
||||
@ -53,6 +53,21 @@ func Test_New(t *testing.T) {
|
||||
t.Assert(j.Get("k2"), "v2")
|
||||
t.Assert(j.Get("k3"), nil)
|
||||
})
|
||||
// https://github.com/gogf/gf/issues/3253
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type TestStruct struct {
|
||||
Result []map[string]string `json:"result"`
|
||||
}
|
||||
ts := &TestStruct{
|
||||
Result: []map[string]string{
|
||||
{
|
||||
"Name": "gf",
|
||||
"Role": "",
|
||||
},
|
||||
},
|
||||
}
|
||||
gjson.New(ts)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Valid(t *testing.T) {
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 boot
|
||||
|
||||
import (
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
|
||||
@ -1,12 +1,19 @@
|
||||
// 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 boot
|
||||
|
||||
import (
|
||||
consul "github.com/gogf/gf/contrib/config/consul/v2"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/hashicorp/consul/api"
|
||||
"github.com/hashicorp/go-cleanhttp"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 boot
|
||||
|
||||
import (
|
||||
|
||||
@ -1,10 +1,17 @@
|
||||
// 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 boot
|
||||
|
||||
import (
|
||||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
"github.com/gogf/gf/contrib/config/kubecm/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
|
||||
@ -1,11 +1,18 @@
|
||||
// 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 boot
|
||||
|
||||
import (
|
||||
"github.com/nacos-group/nacos-sdk-go/common/constant"
|
||||
"github.com/nacos-group/nacos-sdk-go/vo"
|
||||
|
||||
"github.com/gogf/gf/contrib/config/nacos/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/nacos-group/nacos-sdk-go/common/constant"
|
||||
"github.com/nacos-group/nacos-sdk-go/vo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 nacos
|
||||
|
||||
import (
|
||||
|
||||
58
example/converter/alias_type_convert_with_refer/main.go
Normal file
58
example/converter/alias_type_convert_with_refer/main.go
Normal file
@ -0,0 +1,58 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type MyTime = *gtime.Time
|
||||
|
||||
type Src struct {
|
||||
A MyTime
|
||||
}
|
||||
|
||||
type Dst struct {
|
||||
B string
|
||||
}
|
||||
|
||||
type SrcWrap struct {
|
||||
Value Src
|
||||
}
|
||||
|
||||
type DstWrap struct {
|
||||
Value Dst
|
||||
}
|
||||
|
||||
// SrcToDstConverter is custom converting function for custom type.
|
||||
func SrcToDstConverter(src Src) (dst *Dst, err error) {
|
||||
return &Dst{B: src.A.Format("Y-m-d")}, nil
|
||||
}
|
||||
|
||||
// SrcToDstConverter is custom converting function for custom type.
|
||||
func main() {
|
||||
// register custom converter function.
|
||||
err := gconv.RegisterConverter(SrcToDstConverter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// custom struct converting.
|
||||
var src = Src{A: gtime.Now()}
|
||||
dst := gconv.ConvertWithRefer(src, &Dst{})
|
||||
fmt.Println("src:", src)
|
||||
fmt.Println("dst:", dst)
|
||||
|
||||
// custom struct attributes converting.
|
||||
var srcWrap = SrcWrap{Src{A: gtime.Now()}}
|
||||
dstWrap := gconv.ConvertWithRefer(srcWrap, &DstWrap{})
|
||||
fmt.Println("srcWrap:", srcWrap)
|
||||
fmt.Println("dstWrap:", dstWrap)
|
||||
}
|
||||
72
example/converter/alias_type_scan/main.go
Normal file
72
example/converter/alias_type_scan/main.go
Normal file
@ -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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type MyTime = *gtime.Time
|
||||
|
||||
type Src struct {
|
||||
A MyTime
|
||||
}
|
||||
|
||||
type Dst struct {
|
||||
B string
|
||||
}
|
||||
|
||||
type SrcWrap struct {
|
||||
Value Src
|
||||
}
|
||||
|
||||
type DstWrap struct {
|
||||
Value Dst
|
||||
}
|
||||
|
||||
// SrcToDstConverter is custom converting function for custom type.
|
||||
func SrcToDstConverter(src Src) (dst *Dst, err error) {
|
||||
return &Dst{B: src.A.Format("Y-m-d")}, nil
|
||||
}
|
||||
|
||||
// SrcToDstConverter is custom converting function for custom type.
|
||||
func main() {
|
||||
// register custom converter function.
|
||||
err := gconv.RegisterConverter(SrcToDstConverter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// custom struct converting.
|
||||
var (
|
||||
src = Src{A: gtime.Now()}
|
||||
dst *Dst
|
||||
)
|
||||
err = gconv.Scan(src, &dst)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("src:", src)
|
||||
fmt.Println("dst:", dst)
|
||||
|
||||
// custom struct attributes converting.
|
||||
var (
|
||||
srcWrap = SrcWrap{Src{A: gtime.Now()}}
|
||||
dstWrap *DstWrap
|
||||
)
|
||||
err = gconv.Scan(srcWrap, &dstWrap)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("srcWrap:", srcWrap)
|
||||
fmt.Println("dstWrap:", dstWrap)
|
||||
}
|
||||
54
example/converter/struct_convert_with_refer/main.go
Normal file
54
example/converter/struct_convert_with_refer/main.go
Normal file
@ -0,0 +1,54 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type Src struct {
|
||||
A int
|
||||
}
|
||||
|
||||
type Dst struct {
|
||||
B int
|
||||
}
|
||||
|
||||
type SrcWrap struct {
|
||||
Value Src
|
||||
}
|
||||
|
||||
type DstWrap struct {
|
||||
Value Dst
|
||||
}
|
||||
|
||||
// SrcToDstConverter is custom converting function for custom type.
|
||||
func SrcToDstConverter(src Src) (dst *Dst, err error) {
|
||||
return &Dst{B: src.A}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// register custom converter function.
|
||||
err := gconv.RegisterConverter(SrcToDstConverter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// custom struct converting.
|
||||
var src = Src{A: 1}
|
||||
dst := gconv.ConvertWithRefer(src, Dst{})
|
||||
fmt.Println("src:", src)
|
||||
fmt.Println("dst:", dst)
|
||||
|
||||
// custom struct attributes converting.
|
||||
var srcWrap = SrcWrap{Src{A: 1}}
|
||||
dstWrap := gconv.ConvertWithRefer(srcWrap, &DstWrap{})
|
||||
fmt.Println("srcWrap:", srcWrap)
|
||||
fmt.Println("dstWrap:", dstWrap)
|
||||
}
|
||||
68
example/converter/struct_scan/main.go
Normal file
68
example/converter/struct_scan/main.go
Normal file
@ -0,0 +1,68 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type Src struct {
|
||||
A int
|
||||
}
|
||||
|
||||
type Dst struct {
|
||||
B int
|
||||
}
|
||||
|
||||
type SrcWrap struct {
|
||||
Value Src
|
||||
}
|
||||
|
||||
type DstWrap struct {
|
||||
Value Dst
|
||||
}
|
||||
|
||||
func SrcToDstConverter(src Src) (dst *Dst, err error) {
|
||||
return &Dst{B: src.A}, nil
|
||||
}
|
||||
|
||||
// SrcToDstConverter is custom converting function for custom type.
|
||||
func main() {
|
||||
// register custom converter function.
|
||||
err := gconv.RegisterConverter(SrcToDstConverter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// custom struct converting.
|
||||
var (
|
||||
src = Src{A: 1}
|
||||
dst *Dst
|
||||
)
|
||||
err = gconv.Scan(src, &dst)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("src:", src)
|
||||
fmt.Println("dst:", dst)
|
||||
|
||||
// custom struct attributes converting.
|
||||
var (
|
||||
srcWrap = SrcWrap{Src{A: 1}}
|
||||
dstWrap *DstWrap
|
||||
)
|
||||
err = gconv.Scan(srcWrap, &dstWrap)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("srcWrap:", srcWrap)
|
||||
fmt.Println("dstWrap:", dstWrap)
|
||||
}
|
||||
@ -1,27 +1,28 @@
|
||||
module github.com/gogf/gf/example
|
||||
|
||||
go 1.20
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/config/consul/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/config/kubecm/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/config/nacos/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/config/polaris/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/nosql/redis/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/registry/etcd/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/registry/file/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/config/consul/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/config/kubecm/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/config/nacos/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/config/polaris/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/nosql/redis/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/registry/etcd/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/registry/file/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/registry/nacos/v2 v2.5.6
|
||||
github.com/gogf/gf/contrib/registry/polaris/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/rpc/grpcx/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/trace/otlphttp/v2 v2.6.0
|
||||
github.com/gogf/gf/v2 v2.6.0
|
||||
github.com/gogf/gf/contrib/registry/polaris/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/rpc/grpcx/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 v2.6.2
|
||||
github.com/gogf/gf/contrib/trace/otlphttp/v2 v2.6.2
|
||||
github.com/gogf/gf/v2 v2.6.2
|
||||
github.com/hashicorp/consul/api v1.24.0
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2
|
||||
github.com/nacos-group/nacos-sdk-go v1.1.4
|
||||
github.com/polarismesh/polaris-go v1.5.5
|
||||
github.com/prometheus/client_golang v1.17.0
|
||||
golang.org/x/time v0.3.0
|
||||
google.golang.org/grpc v1.59.0
|
||||
google.golang.org/protobuf v1.31.0
|
||||
@ -93,7 +94,6 @@ require (
|
||||
github.com/pelletier/go-toml v1.9.3 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/polarismesh/specification v1.4.1 // indirect
|
||||
github.com/prometheus/client_golang v1.17.0 // indirect
|
||||
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
|
||||
github.com/prometheus/common v0.44.0 // indirect
|
||||
github.com/prometheus/procfs v0.11.1 // indirect
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
|
||||
@ -1,12 +1,19 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
type HelloReq struct {
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
// 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 main
|
||||
|
||||
import (
|
||||
|
||||
45
example/httpserver/serve_file/main.go
Normal file
45
example/httpserver/serve_file/main.go
Normal file
@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
// pathMap is used for URL mapping
|
||||
var pathMap = map[string]string{
|
||||
"/aaa/": "/tmp/",
|
||||
}
|
||||
|
||||
// ServeFile serves the file to the response.
|
||||
func ServeFile(r *ghttp.Request) {
|
||||
truePath := r.URL.Path
|
||||
hasPrefix := false
|
||||
// Replace the path prefix.
|
||||
for k, v := range pathMap {
|
||||
if strings.HasPrefix(truePath, k) {
|
||||
truePath = strings.Replace(truePath, k, v, 1) // Replace only once.
|
||||
hasPrefix = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !hasPrefix {
|
||||
r.Response.WriteStatus(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
r.Response.ServeFile(truePath)
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Use(ghttp.MiddlewareHandlerResponse)
|
||||
s.BindHandler("/*", ServeFile)
|
||||
s.SetPort(8080)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
// http://127.0.0.1:8080/aaa/main.go
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user