From bb1a95fff873d75d8bc940bfd525b2682eea3a2c Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 14 Dec 2021 21:01:36 +0800 Subject: [PATCH 1/7] fix issue in gproc.ShellExec --- os/gproc/gproc.go | 6 ++++-- os/gproc/gproc_z_unit_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 os/gproc/gproc_z_unit_test.go diff --git a/os/gproc/gproc.go b/os/gproc/gproc.go index ca2f777ab..3c9607383 100644 --- a/os/gproc/gproc.go +++ b/os/gproc/gproc.go @@ -103,7 +103,7 @@ func ShellRun(cmd string) error { } // ShellExec executes given command `cmd` synchronously and returns the command result. -func ShellExec(cmd string, environment ...[]string) (string, error) { +func ShellExec(cmd string, environment ...[]string) (result string, err error) { var ( buf = bytes.NewBuffer(nil) p = NewProcess( @@ -114,7 +114,9 @@ func ShellExec(cmd string, environment ...[]string) (string, error) { ) p.Stdout = buf p.Stderr = buf - return buf.String(), p.Run() + err = p.Run() + result = buf.String() + return } // parseCommand parses command `cmd` into slice arguments. diff --git a/os/gproc/gproc_z_unit_test.go b/os/gproc/gproc_z_unit_test.go new file mode 100644 index 000000000..a02087d3b --- /dev/null +++ b/os/gproc/gproc_z_unit_test.go @@ -0,0 +1,29 @@ +// 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. + +// go test *.go -bench=".*" -benchmem + +package gproc_test + +import ( + "testing" + + "github.com/gogf/gf/v2/os/gproc" + "github.com/gogf/gf/v2/test/gtest" +) + +func Test_ShellExec(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + s, err := gproc.ShellExec(`echo 123`) + t.AssertNil(err) + t.Assert(s, "123\n") + }) + // error + gtest.C(t, func(t *gtest.T) { + _, err := gproc.ShellExec(`NoneExistCommandCall`) + t.AssertNE(err, nil) + }) +} From 4c7e409e61a66798fbbd70f974d496054f64289b Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 14 Dec 2021 22:34:06 +0800 Subject: [PATCH 2/7] improve argument/option printing content for package gcmd --- os/gcmd/gcmd_command_help.go | 11 +++++++---- os/gcmd/gcmd_z_unit_feature_object2_test.go | 16 +++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/os/gcmd/gcmd_command_help.go b/os/gcmd/gcmd_command_help.go index 1d86f06dd..7381c01ab 100644 --- a/os/gcmd/gcmd_command_help.go +++ b/os/gcmd/gcmd_command_help.go @@ -65,16 +65,17 @@ func (c *Command) Print() { } } for _, cmd := range c.commands { + brief := gstr.Replace(cmd.Brief, "\n", "") // Add "..." to brief for those commands that also have sub-commands. if len(cmd.commands) > 0 { - cmd.Brief = gstr.TrimRight(cmd.Brief, ".") + "..." + brief = gstr.TrimRight(brief, ".") + "..." } var ( spaceLength = maxSpaceLength - len(cmd.Name) wordwrapPrefix = gstr.Repeat(" ", len(prefix+cmd.Name)+spaceLength+4) lineStr = fmt.Sprintf( "%s%s%s%s\n", - prefix, cmd.Name, gstr.Repeat(" ", spaceLength+4), gstr.Trim(cmd.Brief), + prefix, cmd.Name, gstr.Repeat(" ", spaceLength+4), gstr.Trim(brief), ) ) lineStr = gstr.WordWrap(lineStr, maxLineChars, "\n"+wordwrapPrefix) @@ -102,11 +103,12 @@ func (c *Command) Print() { continue } var ( + brief = gstr.Trim(gstr.Replace(arg.Brief, "\n", "")) spaceLength = maxSpaceLength - len(arg.Name) wordwrapPrefix = gstr.Repeat(" ", len(prefix+arg.Name)+spaceLength+4) lineStr = fmt.Sprintf( "%s%s%s%s\n", - prefix, arg.Name, gstr.Repeat(" ", spaceLength+4), gstr.Trim(arg.Brief), + prefix, arg.Name, gstr.Repeat(" ", spaceLength+4), brief, ) ) lineStr = gstr.WordWrap(lineStr, maxLineChars, "\n"+wordwrapPrefix) @@ -145,11 +147,12 @@ func (c *Command) Print() { nameStr = fmt.Sprintf("-/--%s", arg.Name) } var ( + brief = gstr.Trim(gstr.Replace(arg.Brief, "\n", "")) spaceLength = maxSpaceLength - len(nameStr) wordwrapPrefix = gstr.Repeat(" ", len(prefix+nameStr)+spaceLength+4) lineStr = fmt.Sprintf( "%s%s%s%s\n", - prefix, nameStr, gstr.Repeat(" ", spaceLength+4), gstr.Trim(arg.Brief), + prefix, nameStr, gstr.Repeat(" ", spaceLength+4), brief, ) ) lineStr = gstr.WordWrap(lineStr, maxLineChars, "\n"+wordwrapPrefix) diff --git a/os/gcmd/gcmd_z_unit_feature_object2_test.go b/os/gcmd/gcmd_z_unit_feature_object2_test.go index 2d16b6f09..4e050e047 100644 --- a/os/gcmd/gcmd_z_unit_feature_object2_test.go +++ b/os/gcmd/gcmd_z_unit_feature_object2_test.go @@ -86,15 +86,21 @@ PLATFORMS plan9 386 plan9 amd64 solaris amd64 +` + commandBuildBriefPack = ` +destination file path for packed file. if extension of the filename is ".go" and "-n" option is given, +it enables packing SRC to go file, or else it packs SRC into a binary file. + ` ) func init() { gtag.Sets(map[string]string{ - `commandBuildBrief`: commandBuildBrief, - `commandBuildDc`: commandBuildDc, - `commandBuildEg`: commandBuildEg, - `commandBuildAd`: commandBuildAd, + `commandBuildBrief`: commandBuildBrief, + `commandBuildDc`: commandBuildDc, + `commandBuildEg`: commandBuildEg, + `commandBuildAd`: commandBuildAd, + `commandBuildBriefPack`: commandBuildBriefPack, }) } @@ -109,7 +115,7 @@ type commandBuildInput struct { 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"` - Pack string `name:"pack" brief:"pack specified folder into temporary go file before building and removes it after built"` + Pack string `name:"pack" brief:"{commandBuildBriefPack}"` } type commandBuildOutput struct{} From bc2ce19876bebc998ecd8dbaaf3af451fe2f0f97 Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 14 Dec 2021 23:01:20 +0800 Subject: [PATCH 3/7] add Export feature for package gres --- os/gres/gres.go | 5 +++++ os/gres/gres_resource.go | 28 +++++++++++++++++++++++++++- os/gres/gres_z_unit_test.go | 23 +++++++++++++++++++++-- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/os/gres/gres.go b/os/gres/gres.go index c7d9b6911..123dd7e0a 100644 --- a/os/gres/gres.go +++ b/os/gres/gres.go @@ -77,6 +77,11 @@ func ScanDirFile(path string, pattern string, recursive ...bool) []*File { return defaultResource.ScanDirFile(path, pattern, recursive...) } +// Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively. +func Export(src, dst string) error { + return defaultResource.Export(src, dst) +} + // Dump prints the files of the default resource object. func Dump() { defaultResource.Dump() diff --git a/os/gres/gres_resource.go b/os/gres/gres_resource.go index 9c8547264..74a10686a 100644 --- a/os/gres/gres_resource.go +++ b/os/gres/gres_resource.go @@ -224,12 +224,38 @@ func (r *Resource) doScanDir(path string, pattern string, recursive bool, onlyFi return files } +// Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively. +func (r *Resource) Export(src, dst string) error { + var ( + err error + path string + files = r.doScanDir(src, "*", true, false) + ) + for _, file := range files { + path = gfile.Join(dst, file.Name()) + if file.FileInfo().IsDir() { + err = gfile.Mkdir(path) + } else { + err = gfile.PutBytes(path, file.Content()) + } + if err != nil { + return err + } + } + return nil +} + // Dump prints the files of current resource object. func (r *Resource) Dump() { var info os.FileInfo r.tree.Iterator(func(key, value interface{}) bool { info = value.(*File).FileInfo() - fmt.Printf("%v %7s %s\n", gtime.New(info.ModTime()).ISO8601(), gfile.FormatSize(info.Size()), key) + fmt.Printf( + "%v %7s %s\n", + gtime.New(info.ModTime()).ISO8601(), + gfile.FormatSize(info.Size()), + key, + ) return true }) fmt.Printf("TOTAL FILES: %d\n", r.tree.Size()) diff --git a/os/gres/gres_z_unit_test.go b/os/gres/gres_z_unit_test.go index 0438013e2..0e3e514fb 100644 --- a/os/gres/gres_z_unit_test.go +++ b/os/gres/gres_z_unit_test.go @@ -7,11 +7,11 @@ package gres_test import ( - _ "github.com/gogf/gf/v2/os/gres/testdata/data" - "strings" "testing" + _ "github.com/gogf/gf/v2/os/gres/testdata/data" + "github.com/gogf/gf/v2/debug/gdebug" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gfile" @@ -231,3 +231,22 @@ func Test_ScanDirFile(t *testing.T) { t.Assert(files[0].Content(), "sub-test2 content") }) } + +func Test_Export(t *testing.T) { + gres.Dump() + gtest.C(t, func(t *gtest.T) { + var ( + src = `template` + dst = gfile.TempDir(gtime.TimestampNanoStr()) + err = gres.Export(src, dst) + ) + defer gfile.Remove(dst) + t.AssertNil(err) + files, err := gfile.ScanDir(dst, "*", true) + t.AssertNil(err) + t.Assert(len(files), 14) + + name := `template/index.html` + t.Assert(gfile.GetContents(gfile.Join(dst, name)), gres.GetContent(name)) + }) +} From 36e05561cc0e0d038dbc84c21357b972bacaf5ba Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 14 Dec 2021 23:49:08 +0800 Subject: [PATCH 4/7] improve Export feature for package gres --- os/gres/gres.go | 4 ++-- os/gres/gres_resource.go | 31 +++++++++++++++++++++++++------ os/gres/gres_z_unit_test.go | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/os/gres/gres.go b/os/gres/gres.go index 123dd7e0a..7bb9e815d 100644 --- a/os/gres/gres.go +++ b/os/gres/gres.go @@ -78,8 +78,8 @@ func ScanDirFile(path string, pattern string, recursive ...bool) []*File { } // Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively. -func Export(src, dst string) error { - return defaultResource.Export(src, dst) +func Export(src, dst string, option ...ExportOption) error { + return defaultResource.Export(src, dst, option...) } // Dump prints the files of the default resource object. diff --git a/os/gres/gres_resource.go b/os/gres/gres_resource.go index 74a10686a..1a925d444 100644 --- a/os/gres/gres_resource.go +++ b/os/gres/gres_resource.go @@ -17,6 +17,7 @@ import ( "github.com/gogf/gf/v2/internal/intlog" "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/os/gtime" + "github.com/gogf/gf/v2/text/gstr" ) type Resource struct { @@ -224,15 +225,33 @@ func (r *Resource) doScanDir(path string, pattern string, recursive bool, onlyFi return files } -// Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively. -func (r *Resource) Export(src, dst string) error { +// ExportOption is the option for function Export. +type ExportOption struct { + RemovePrefix string // Remove the prefix of file name from resource. +} + +// Export exports and saves specified path `srcPath` and all its sub files to specified system path `dstPath` recursively. +func (r *Resource) Export(src, dst string, option ...ExportOption) error { var ( - err error - path string - files = r.doScanDir(src, "*", true, false) + err error + name string + path string + exportOption ExportOption + files = r.doScanDir(src, "*", true, false) ) + if len(option) > 0 { + exportOption = option[0] + } for _, file := range files { - path = gfile.Join(dst, file.Name()) + name = file.Name() + if exportOption.RemovePrefix != "" { + name = gstr.TrimLeftStr(name, exportOption.RemovePrefix) + } + name = gstr.Trim(name, `\/`) + if name == "" { + continue + } + path = gfile.Join(dst, name) if file.FileInfo().IsDir() { err = gfile.Mkdir(path) } else { diff --git a/os/gres/gres_z_unit_test.go b/os/gres/gres_z_unit_test.go index 0e3e514fb..6ace54c95 100644 --- a/os/gres/gres_z_unit_test.go +++ b/os/gres/gres_z_unit_test.go @@ -249,4 +249,22 @@ func Test_Export(t *testing.T) { name := `template/index.html` t.Assert(gfile.GetContents(gfile.Join(dst, name)), gres.GetContent(name)) }) + gtest.C(t, func(t *gtest.T) { + var ( + src = `template` + dst = gfile.TempDir(gtime.TimestampNanoStr()) + err = gres.Export(src, dst, gres.ExportOption{ + RemovePrefix: `template`, + }) + ) + defer gfile.Remove(dst) + t.AssertNil(err) + files, err := gfile.ScanDir(dst, "*", true) + t.AssertNil(err) + t.Assert(len(files), 13) + + nameInRes := `template/index.html` + nameInSys := `index.html` + t.Assert(gfile.GetContents(gfile.Join(dst, nameInSys)), gres.GetContent(nameInRes)) + }) } From e28be8d1149e194abbd214aadc570d6c16d7962c Mon Sep 17 00:00:00 2001 From: huangqian Date: Tue, 14 Dec 2021 23:54:15 +0800 Subject: [PATCH 5/7] reorder the sample function --- text/gstr/gstr_z_example_test.go | 1267 +++++++++++++++--------------- 1 file changed, 653 insertions(+), 614 deletions(-) diff --git a/text/gstr/gstr_z_example_test.go b/text/gstr/gstr_z_example_test.go index c21805713..465997ce4 100644 --- a/text/gstr/gstr_z_example_test.go +++ b/text/gstr/gstr_z_example_test.go @@ -12,139 +12,144 @@ import ( "github.com/gogf/gf/v2/text/gstr" ) -func ExampleAddSlashes() { +func ExampleCount() { var ( - str = `'aa'"bb"cc\r\n\d\t` - result = gstr.AddSlashes(str) + str = `goframe is very, very easy to use` + substr1 = "goframe" + substr2 = "very" + result1 = gstr.Count(str, substr1) + result2 = gstr.Count(str, substr2) ) + fmt.Println(result1) + fmt.Println(result2) + // Output: + // 1 + // 2 +} + +func ExampleCountI() { + var ( + str = `goframe is very, very easy to use` + substr1 = "GOFRAME" + substr2 = "VERY" + result1 = gstr.CountI(str, substr1) + result2 = gstr.CountI(str, substr2) + ) + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 1 + // 2 +} + +func ExampleToLower() { + var ( + s = `GOFRAME` + result = gstr.ToLower(s) + ) fmt.Println(result) // Output: - // \'aa\'\"bb\"cc\\r\\n\\d\\t + // goframe } -func ExampleCaseCamel() { +func ExampleToUpper() { + var ( + s = `goframe` + result = gstr.ToUpper(s) + ) + fmt.Println(result) + + // Output: + // GOFRAME +} + +func ExampleUcFirst() { + var ( + s = `hello` + result = gstr.UcFirst(s) + ) + fmt.Println(result) + + // Output: + // Hello +} + +func ExampleLcFirst() { + var ( + str = `Goframe` + result = gstr.LcFirst(str) + ) + fmt.Println(result) + + // Output: + // goframe +} + +func ExampleUcWords() { var ( str = `hello world` - result = gstr.CaseCamel(str) + result = gstr.UcWords(str) ) fmt.Println(result) // Output: - // HelloWorld + // Hello World } -func ExampleCaseCamelLower() { +func ExampleIsLetterLower() { + fmt.Println(gstr.IsLetterLower('a')) + fmt.Println(gstr.IsLetterLower('A')) + + // Output: + // true + // false +} + +func ExampleIsLetterUpper() { + fmt.Println(gstr.IsLetterUpper('A')) + fmt.Println(gstr.IsLetterUpper('a')) + + // Output: + // true + // false +} + +func ExampleIsNumeric() { + fmt.Println(gstr.IsNumeric("88")) + fmt.Println(gstr.IsNumeric("3.1415926")) + fmt.Println(gstr.IsNumeric("abc")) + // Output: + // true + // true + // false +} + +func ExampleReverse() { var ( - str = `hello world` - result = gstr.CaseCamelLower(str) + str = `123456` + result = gstr.Reverse(str) ) fmt.Println(result) // Output: - // helloWorld + // 654321 } -func ExampleCaseDelimited() { +func ExampleNumberFormat() { var ( - str = `hello world` - del = byte('-') - result = gstr.CaseDelimited(str, del) + number float64 = 123456 + decimals = 2 + decPoint = "." + thousandsSep = "," + result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep) ) fmt.Println(result) // Output: - // hello-world -} - -func ExampleCaseDelimitedScreaming() { - { - var ( - str = `hello world` - del = byte('-') - result = gstr.CaseDelimitedScreaming(str, del, true) - ) - fmt.Println(result) - } - { - var ( - str = `hello world` - del = byte('-') - result = gstr.CaseDelimitedScreaming(str, del, false) - ) - fmt.Println(result) - } - - // Output: - // HELLO-WORLD - // hello-world -} - -func ExampleCaseKebab() { - var ( - str = `hello world` - result = gstr.CaseKebab(str) - ) - fmt.Println(result) - - // Output: - // hello-world -} - -func ExampleCaseKebabScreaming() { - var ( - str = `hello world` - result = gstr.CaseKebabScreaming(str) - ) - fmt.Println(result) - - // Output: - // HELLO-WORLD -} - -func ExampleCaseSnake() { - var ( - str = `hello world` - result = gstr.CaseSnake(str) - ) - fmt.Println(result) - - // Output: - // hello_world -} - -func ExampleCaseSnakeFirstUpper() { - var ( - str = `RGBCodeMd5` - result = gstr.CaseSnakeFirstUpper(str) - ) - fmt.Println(result) - - // Output: - // rgb_code_md5 -} - -func ExampleCaseSnakeScreaming() { - var ( - str = `hello world` - result = gstr.CaseSnakeScreaming(str) - ) - fmt.Println(result) - - // Output: - // HELLO_WORLD -} - -func ExampleChr() { - var ( - ascii = 65 // A - result = gstr.Chr(ascii) - ) - fmt.Println(result) - - // Output: - // A + // 123,456.00 } func ExampleChunkSplit() { @@ -171,147 +176,6 @@ func ExampleCompare() { // 1 } -func ExampleCompareVersion() { - fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8")) - fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7")) - fmt.Println(gstr.CompareVersion("2.8.beta", "2.8")) - - // Output: - // 1 - // -1 - // 0 -} - -func ExampleCompareVersionGo() { - fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8")) - fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible")) - fmt.Println(gstr.CompareVersionGo( - "v0.0.2-20180626092158-b2ccc119800e", - "v1.0.1-20190626092158-b2ccc519800e", - )) - - // Output: - // 1 - // 1 - // -1 -} - -func ExampleContains() { - { - var ( - str = `Hello World` - substr = `Hello` - result = gstr.Contains(str, substr) - ) - fmt.Println(result) - } - { - var ( - str = `Hello World` - substr = `hello` - result = gstr.Contains(str, substr) - ) - fmt.Println(result) - } - - // Output: - // true - // false -} - -func ExampleContainsAny() { - { - var ( - s = `goframe` - chars = "g" - result = gstr.ContainsAny(s, chars) - ) - fmt.Println(result) - } - { - var ( - s = `goframe` - chars = "G" - result = gstr.ContainsAny(s, chars) - ) - fmt.Println(result) - } - - // Output: - // true - // false -} - -func ExampleContainsI() { - var ( - str = `Hello World` - substr = "hello" - result1 = gstr.Contains(str, substr) - result2 = gstr.ContainsI(str, substr) - ) - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // false - // true -} - -func ExampleCount() { - var ( - str = `goframe is very, very easy to use` - substr1 = "goframe" - substr2 = "very" - result1 = gstr.Count(str, substr1) - result2 = gstr.Count(str, substr2) - ) - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // 1 - // 2 -} - -func ExampleCountChars() { - var ( - str = `goframe` - result = gstr.CountChars(str) - ) - fmt.Println(result) - - // May Output: - // map[a:1 e:1 f:1 g:1 m:1 o:1 r:1] - -} - -func ExampleCountI() { - var ( - str = `goframe is very, very easy to use` - substr1 = "GOFRAME" - substr2 = "VERY" - result1 = gstr.CountI(str, substr1) - result2 = gstr.CountI(str, substr2) - ) - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // 1 - // 2 -} - -func ExampleCountWords() { - var ( - str = `goframe is very, very easy to use!` - result = gstr.CountWords(str) - ) - fmt.Printf(`%#v`, result) - - // Output: - // map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1} -} - func ExampleEqual() { fmt.Println(gstr.Equal(`A`, `a`)) fmt.Println(gstr.Equal(`A`, `A`)) @@ -323,18 +187,6 @@ func ExampleEqual() { // false } -func ExampleExplode() { - var ( - str = `Hello World` - delimiter = " " - result = gstr.Explode(delimiter, str) - ) - fmt.Printf(`%#v`, result) - - // Output: - // []string{"Hello", "World"} -} - func ExampleFields() { var ( str = `Hello World` @@ -370,81 +222,115 @@ func ExampleHasSuffix() { // true } -func ExampleHideStr() { +func ExampleCountWords() { var ( - str = `13800138000` - percent = 40 - hide = `*` - result = gstr.HideStr(str, percent, hide) + str = `goframe is very, very easy to use!` + result = gstr.CountWords(str) + ) + fmt.Printf(`%#v`, result) + + // Output: + // map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1} +} + +func ExampleCountChars() { + var ( + str = `goframe` + result = gstr.CountChars(str) + ) + fmt.Println(result) + + // May Output: + // map[a:1 e:1 f:1 g:1 m:1 o:1 r:1] +} + +func ExampleWordWrap() { + { + var ( + str = `A very long woooooooooooooooooord. and something` + width = 8 + br = "\n" + result = gstr.WordWrap(str, width, br) + ) + fmt.Println(result) + } + { + var ( + str = `The quick brown fox jumped over the lazy dog.` + width = 20 + br = "
\n" + result = gstr.WordWrap(str, width, br) + ) + fmt.Printf("%v", result) + } + + // Output: + // A very + // long + // woooooooooooooooooord. + // and + // something + // The quick brown fox
+ // jumped over the lazy
+ // dog. +} + +func ExampleLenRune() { + var ( + str = `GoFrame框架` + result = gstr.LenRune(str) ) fmt.Println(result) // Output: - // 138****8000 + // 9 } -func ExampleImplode() { +func ExampleRepeat() { var ( - pieces = []string{"goframe", "is", "very", "easy", "to", "use"} - glue = " " - result = gstr.Implode(glue, pieces) + input = `goframe ` + multiplier = 3 + result = gstr.Repeat(input, multiplier) ) fmt.Println(result) // Output: - // goframe is very easy to use + // goframe goframe goframe } -func ExampleInArray() { +func ExampleShuffle() { var ( - a = []string{"goframe", "is", "very", "easy", "to", "use"} - s = "goframe" - result = gstr.InArray(a, s) + str = `123456` + result = gstr.Shuffle(str) ) fmt.Println(result) - // Output: - // true + // May Output: + // 563214 } -func ExampleIsLetterLower() { - fmt.Println(gstr.IsLetterLower('a')) - fmt.Println(gstr.IsLetterLower('A')) - - // Output: - // true - // false -} - -func ExampleIsLetterUpper() { - fmt.Println(gstr.IsLetterUpper('A')) - fmt.Println(gstr.IsLetterUpper('a')) - - // Output: - // true - // false -} - -func ExampleIsNumeric() { - fmt.Println(gstr.IsNumeric("88")) - fmt.Println(gstr.IsNumeric("3.1415926")) - fmt.Println(gstr.IsNumeric("abc")) - // Output: - // true - // true - // false -} - -func ExampleIsSubDomain() { +func ExampleSplit() { var ( - subDomain = `s.goframe.org` - mainDomain = `goframe.org` - result = gstr.IsSubDomain(subDomain, mainDomain) + str = `a|b|c|d` + delimiter = `|` + result = gstr.Split(str, delimiter) ) - fmt.Println(result) + fmt.Printf(`%#v`, result) // Output: - // true + // []string{"a", "b", "c", "d"} +} + +func ExampleSplitAndTrim() { + var ( + str = `a|b|||||c|d` + delimiter = `|` + result = gstr.SplitAndTrim(str, delimiter) + ) + fmt.Printf(`%#v`, result) + + // Output: + // []string{"a", "b", "c", "d"} } func ExampleJoin() { @@ -471,41 +357,65 @@ func ExampleJoinAny() { // 99,73,85,66 } -func ExampleLcFirst() { +func ExampleExplode() { var ( - str = `Goframe` - result = gstr.LcFirst(str) + str = `Hello World` + delimiter = " " + result = gstr.Explode(delimiter, str) ) - fmt.Println(result) + fmt.Printf(`%#v`, result) // Output: - // goframe + // []string{"Hello", "World"} } -func ExampleLenRune() { +func ExampleImplode() { var ( - str = `GoFrame框架` - result = gstr.LenRune(str) + pieces = []string{"goframe", "is", "very", "easy", "to", "use"} + glue = " " + result = gstr.Implode(glue, pieces) ) fmt.Println(result) // Output: - // 9 + // goframe is very easy to use } -func ExampleLevenshtein() { +func ExampleChr() { var ( - str1 = "Hello World" - str2 = "hallo World" - costIns = 1 - costRep = 1 - costDel = 1 - result = gstr.Levenshtein(str1, str2, costIns, costRep, costDel) + ascii = 65 // A + result = gstr.Chr(ascii) ) fmt.Println(result) // Output: - // 2 + // A +} + +// '103' is the 'g' in ASCII +func ExampleOrd() { + var ( + str = `goframe` + result = gstr.Ord(str) + ) + + fmt.Println(result) + + // Output: + // 103 +} + +func ExampleHideStr() { + var ( + str = `13800138000` + percent = 40 + hide = `*` + result = gstr.HideStr(str, percent, hide) + ) + fmt.Println(result) + + // Output: + // 138****8000 } func ExampleNl2Br() { @@ -525,20 +435,257 @@ use` // goframe
is
very
easy
to
use } -func ExampleNumberFormat() { +func ExampleAddSlashes() { var ( - number float64 = 123456 - decimals = 2 - decPoint = "." - thousandsSep = "," - result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep) + str = `'aa'"bb"cc\r\n\d\t` + result = gstr.AddSlashes(str) + ) + + fmt.Println(result) + + // Output: + // \'aa\'\"bb\"cc\\r\\n\\d\\t +} + +func ExampleStripSlashes() { + var ( + str = `C:\\windows\\GoFrame\\test` + result = gstr.StripSlashes(str) ) fmt.Println(result) // Output: - // 123,456.00 + // C:\windows\GoFrame\test } +func ExampleQuoteMeta() { + { + var ( + str = `.\+?[^]()` + result = gstr.QuoteMeta(str) + ) + fmt.Println(result) + } + { + var ( + str = `https://goframe.org/pages/viewpage.action?pageId=1114327` + result = gstr.QuoteMeta(str) + ) + fmt.Println(result) + } + + // Output: + // \.\\\+\?\[\^\]\(\) + // https://goframe\.org/pages/viewpage\.action\?pageId=1114327 + +} + +// array +func ExampleSearchArray() { + var ( + array = []string{"goframe", "is", "very", "nice"} + str = `goframe` + result = gstr.SearchArray(array, str) + ) + fmt.Println(result) + + // Output: + // 0 +} + +func ExampleInArray() { + var ( + a = []string{"goframe", "is", "very", "easy", "to", "use"} + s = "goframe" + result = gstr.InArray(a, s) + ) + fmt.Println(result) + + // Output: + // true +} + +func ExamplePrefixArray() { + // Output: + // test +} + +// case +func ExampleCaseCamel() { + var ( + str = `hello world` + result = gstr.CaseCamel(str) + ) + fmt.Println(result) + + // Output: + // HelloWorld +} + +func ExampleCaseCamelLower() { + var ( + str = `hello world` + result = gstr.CaseCamelLower(str) + ) + fmt.Println(result) + + // Output: + // helloWorld +} + +func ExampleCaseSnake() { + var ( + str = `hello world` + result = gstr.CaseSnake(str) + ) + fmt.Println(result) + + // Output: + // hello_world +} + +func ExampleCaseSnakeScreaming() { + var ( + str = `hello world` + result = gstr.CaseSnakeScreaming(str) + ) + fmt.Println(result) + + // Output: + // HELLO_WORLD +} + +func ExampleCaseSnakeFirstUpper() { + var ( + str = `RGBCodeMd5` + result = gstr.CaseSnakeFirstUpper(str) + ) + fmt.Println(result) + + // Output: + // rgb_code_md5 +} + +func ExampleCaseKebab() { + var ( + str = `hello world` + result = gstr.CaseKebab(str) + ) + fmt.Println(result) + + // Output: + // hello-world +} + +func ExampleCaseKebabScreaming() { + var ( + str = `hello world` + result = gstr.CaseKebabScreaming(str) + ) + fmt.Println(result) + + // Output: + // HELLO-WORLD +} + +func ExampleCaseDelimited() { + var ( + str = `hello world` + del = byte('-') + result = gstr.CaseDelimited(str, del) + ) + fmt.Println(result) + + // Output: + // hello-world +} + +func ExampleCaseDelimitedScreaming() { + { + var ( + str = `hello world` + del = byte('-') + result = gstr.CaseDelimitedScreaming(str, del, true) + ) + fmt.Println(result) + } + { + var ( + str = `hello world` + del = byte('-') + result = gstr.CaseDelimitedScreaming(str, del, false) + ) + fmt.Println(result) + } + + // Output: + // HELLO-WORLD + // hello-world +} + +// contain +func ExampleContains() { + { + var ( + str = `Hello World` + substr = `Hello` + result = gstr.Contains(str, substr) + ) + fmt.Println(result) + } + { + var ( + str = `Hello World` + substr = `hello` + result = gstr.Contains(str, substr) + ) + fmt.Println(result) + } + + // Output: + // true + // false +} + +func ExampleContainsI() { + var ( + str = `Hello World` + substr = "hello" + result1 = gstr.Contains(str, substr) + result2 = gstr.ContainsI(str, substr) + ) + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true +} + +func ExampleContainsAny() { + { + var ( + s = `goframe` + chars = "g" + result = gstr.ContainsAny(s, chars) + ) + fmt.Println(result) + } + { + var ( + s = `goframe` + chars = "G" + result = gstr.ContainsAny(s, chars) + ) + fmt.Println(result) + } + + // Output: + // true + // false +} + +// convert func ExampleOctStr() { var ( str = `\346\200\241` @@ -550,19 +697,36 @@ func ExampleOctStr() { // 怡 } -// '103' is the 'g' in ASCII -func ExampleOrd() { +// domain +func ExampleIsSubDomain() { var ( - str = `goframe` - result = gstr.Ord(str) + subDomain = `s.goframe.org` + mainDomain = `goframe.org` + result = gstr.IsSubDomain(subDomain, mainDomain) ) - fmt.Println(result) // Output: - // 103 + // true } +// levenshtein +func ExampleLevenshtein() { + var ( + str1 = "Hello World" + str2 = "hallo World" + costIns = 1 + costRep = 1 + costDel = 1 + result = gstr.Levenshtein(str1, str2, costIns, costRep, costDel) + ) + fmt.Println(result) + + // Output: + // 2 +} + +// parse func ExampleParse() { { var ( @@ -613,6 +777,7 @@ func ExampleParse() { // map[a___[b:c] } +// pos func ExamplePos() { var ( haystack = `Hello World` @@ -625,6 +790,21 @@ func ExamplePos() { // 6 } +func ExamplePosRune() { + var ( + haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` + needle = `Go` + posI = gstr.PosRune(haystack, needle) + posR = gstr.PosRRune(haystack, needle) + ) + fmt.Println(posI) + fmt.Println(posR) + + // Output: + // 0 + // 22 +} + func ExamplePosI() { var ( haystack = `goframe is very, very easy to use` @@ -680,6 +860,21 @@ func ExamplePosR() { // 17 } +func ExamplePosRRune() { + var ( + haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` + needle = `Go` + posI = gstr.PosIRune(haystack, needle) + posR = gstr.PosRRune(haystack, needle) + ) + fmt.Println(posI) + fmt.Println(posR) + + // Output: + // 0 + // 22 +} + func ExamplePosRI() { var ( haystack = `goframe is very, very easy to use` @@ -710,70 +905,7 @@ func ExamplePosRIRune() { // 22 } -func ExamplePosRRune() { - var ( - haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` - needle = `Go` - posI = gstr.PosIRune(haystack, needle) - posR = gstr.PosRRune(haystack, needle) - ) - fmt.Println(posI) - fmt.Println(posR) - - // Output: - // 0 - // 22 -} - -func ExamplePosRune() { - var ( - haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` - needle = `Go` - posI = gstr.PosRune(haystack, needle) - posR = gstr.PosRRune(haystack, needle) - ) - fmt.Println(posI) - fmt.Println(posR) - - // Output: - // 0 - // 22 -} - -func ExampleQuoteMeta() { - { - var ( - str = `.\+?[^]()` - result = gstr.QuoteMeta(str) - ) - fmt.Println(result) - } - { - var ( - str = `https://goframe.org/pages/viewpage.action?pageId=1114327` - result = gstr.QuoteMeta(str) - ) - fmt.Println(result) - } - - // Output: - // \.\\\+\?\[\^\]\(\) - // https://goframe\.org/pages/viewpage\.action\?pageId=1114327 - -} - -func ExampleRepeat() { - var ( - input = `goframe ` - multiplier = 3 - result = gstr.Repeat(input, multiplier) - ) - fmt.Println(result) - - // Output: - // goframe goframe goframe -} - +// replace func ExampleReplace() { var ( origin = `golang is very nice!` @@ -787,6 +919,19 @@ func ExampleReplace() { // goframe is very nice! } +func ExampleReplaceI() { + var ( + origin = `golang is very nice!` + search = `GOLANG` + replace = `goframe` + result = gstr.ReplaceI(origin, search, replace) + ) + fmt.Println(result) + + // Output: + // goframe is very nice! +} + func ExampleReplaceByArray() { { var ( @@ -810,6 +955,19 @@ func ExampleReplaceByArray() { // goframe is very nice } +func ExampleReplaceIByArray() { + var ( + origin = `golang is very Good` + array = []string{"Golang", "goframe", "GOOD", "nice"} + result = gstr.ReplaceIByArray(origin, array) + ) + + fmt.Println(result) + + // Output: + // goframe is very nice +} + func ExampleReplaceByMap() { { var ( @@ -838,32 +996,6 @@ func ExampleReplaceByMap() { // goframe is very nice } -func ExampleReplaceI() { - var ( - origin = `golang is very nice!` - search = `GOLANG` - replace = `goframe` - result = gstr.ReplaceI(origin, search, replace) - ) - fmt.Println(result) - - // Output: - // goframe is very nice! -} - -func ExampleReplaceIByArray() { - var ( - origin = `golang is very Good` - array = []string{"Golang", "goframe", "GOOD", "nice"} - result = gstr.ReplaceIByArray(origin, array) - ) - - fmt.Println(result) - - // Output: - // goframe is very nice -} - func ExampleReplaceIByMap() { var ( origin = `golang is very nice` @@ -878,40 +1010,7 @@ func ExampleReplaceIByMap() { // goframe is very nice } -func ExampleReverse() { - var ( - str = `123456` - result = gstr.Reverse(str) - ) - fmt.Println(result) - - // Output: - // 654321 -} - -func ExampleSearchArray() { - var ( - array = []string{"goframe", "is", "very", "nice"} - str = `goframe` - result = gstr.SearchArray(array, str) - ) - fmt.Println(result) - - // Output: - // 0 -} - -func ExampleShuffle() { - var ( - str = `123456` - result = gstr.Shuffle(str) - ) - fmt.Println(result) - - // May Output: - // 563214 -} - +// similartext func ExampleSimilarText() { var ( first = `AaBbCcDd` @@ -925,6 +1024,7 @@ func ExampleSimilarText() { // 2 } +// soundex func ExampleSoundex() { var ( str1 = `Hello` @@ -938,30 +1038,7 @@ func ExampleSoundex() { // H400 H400 } -func ExampleSplit() { - var ( - str = `a|b|c|d` - delimiter = `|` - result = gstr.Split(str, delimiter) - ) - fmt.Printf(`%#v`, result) - - // Output: - // []string{"a", "b", "c", "d"} -} - -func ExampleSplitAndTrim() { - var ( - str = `a|b|||||c|d` - delimiter = `|` - result = gstr.SplitAndTrim(str, delimiter) - ) - fmt.Printf(`%#v`, result) - - // Output: - // []string{"a", "b", "c", "d"} -} - +// str func ExampleStr() { var ( haystack = `xxx.jpg` @@ -986,6 +1063,57 @@ func ExampleStrEx() { // a=1&b=2 } +func ExampleStrTill() { + var ( + haystack = `https://goframe.org/index.html?test=123456` + needle = `?` + result = gstr.StrTill(haystack, needle) + ) + fmt.Println(result) + + // Output: + // https://goframe.org/index.html? +} + +func ExampleStrTillEx() { + var ( + haystack = `https://goframe.org/index.html?test=123456` + needle = `?` + result = gstr.StrTillEx(haystack, needle) + ) + fmt.Println(result) + + // Output: + // https://goframe.org/index.html +} + +// substr +func ExampleSubStr() { + var ( + str = `1234567890` + start = 0 + length = 4 + subStr = gstr.SubStr(str, start, length) + ) + fmt.Println(subStr) + + // Output: + // 1234 +} + +func ExampleSubStrRune() { + var ( + str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` + start = 14 + length = 3 + subStr = gstr.SubStrRune(str, start, length) + ) + fmt.Println(subStr) + + // Output: + // 高性能 +} + func ExampleStrLimit() { var ( str = `123456789` @@ -1012,89 +1140,27 @@ func ExampleStrLimitRune() { // GoFrame是一款模块化、高性能... } -func ExampleStrTill() { - var ( - haystack = `https://goframe.org/index.html?test=123456` - needle = `?` - result = gstr.StrTill(haystack, needle) - ) - fmt.Println(result) - +func ExampleSubStrFrom() { // Output: - // https://goframe.org/index.html? + // test } -func ExampleStrTillEx() { - var ( - haystack = `https://goframe.org/index.html?test=123456` - needle = `?` - result = gstr.StrTillEx(haystack, needle) - ) - fmt.Println(result) - +func ExampleSubStrFromEx() { // Output: - // https://goframe.org/index.html + // test } -func ExampleStripSlashes() { - var ( - str = `C:\\windows\\GoFrame\\test` - result = gstr.StripSlashes(str) - ) - fmt.Println(result) - +func ExampleSubStrFromR() { // Output: - // C:\windows\GoFrame\test + // test } -func ExampleSubStr() { - var ( - str = `1234567890` - start = 0 - length = 4 - subStr = gstr.SubStr(str, start, length) - ) - fmt.Println(subStr) - +func ExampleSubStrFromREx() { // Output: - // 1234 -} - -func ExampleSubStrRune() { - var ( - str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` - start = 14 - length = 3 - subStr = gstr.SubStrRune(str, start, length) - ) - fmt.Println(subStr) - - // Output: - // 高性能 -} - -func ExampleToLower() { - var ( - s = `GOFRAME` - result = gstr.ToLower(s) - ) - fmt.Println(result) - - // Output: - // goframe -} - -func ExampleToUpper() { - var ( - s = `goframe` - result = gstr.ToUpper(s) - ) - fmt.Println(result) - - // Output: - // GOFRAME + // test } +// trim func ExampleTrim() { var ( str = `*Hello World*` @@ -1107,16 +1173,17 @@ func ExampleTrim() { // Hello World } -func ExampleTrimAll() { +func ExampleTrimStr() { var ( - str = `*Hello World*` - characterMask = "*" - result = gstr.TrimAll(str, characterMask) + str = `Hello World` + cut = "World" + count = -1 + result = gstr.TrimStr(str, cut, count) ) fmt.Println(result) // Output: - // HelloWorld + // Hello } func ExampleTrimLeft() { @@ -1169,68 +1236,40 @@ func ExampleTrimRightStr() { // Hello World } -func ExampleTrimStr() { +func ExampleTrimAll() { var ( - str = `Hello World` - cut = "World" - count = -1 - result = gstr.TrimStr(str, cut, count) + str = `*Hello World*` + characterMask = "*" + result = gstr.TrimAll(str, characterMask) ) fmt.Println(result) // Output: - // Hello + // HelloWorld } -func ExampleUcFirst() { - var ( - s = `hello` - result = gstr.UcFirst(s) - ) - fmt.Println(result) +// version +func ExampleCompareVersion() { + fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8")) + fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7")) + fmt.Println(gstr.CompareVersion("2.8.beta", "2.8")) // Output: - // Hello + // 1 + // -1 + // 0 } -func ExampleUcWords() { - var ( - str = `hello world` - result = gstr.UcWords(str) - ) - fmt.Println(result) +func ExampleCompareVersionGo() { + fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8")) + fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible")) + fmt.Println(gstr.CompareVersionGo( + "v0.0.2-20180626092158-b2ccc119800e", + "v1.0.1-20190626092158-b2ccc519800e", + )) // Output: - // Hello World -} - -func ExampleWordWrap() { - { - var ( - str = `A very long woooooooooooooooooord. and something` - width = 8 - br = "\n" - result = gstr.WordWrap(str, width, br) - ) - fmt.Println(result) - } - { - var ( - str = `The quick brown fox jumped over the lazy dog.` - width = 20 - br = "
\n" - result = gstr.WordWrap(str, width, br) - ) - fmt.Printf("%v", result) - } - - // Output: - // A very - // long - // woooooooooooooooooord. - // and - // something - // The quick brown fox
- // jumped over the lazy
- // dog. + // 1 + // 1 + // -1 } From 19c9f0a4884514f5d435e7ada6037c9ef4c05034 Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 15 Dec 2021 00:01:59 +0800 Subject: [PATCH 6/7] add DTO feature for package gdb --- database/gdb/gdb_func.go | 25 +++- database/gdb/gdb_model_insert.go | 8 +- ... => gdb_z_mysql_feature_model_dto_test.go} | 138 ++++++++++++++++++ 3 files changed, 163 insertions(+), 8 deletions(-) rename database/gdb/{gdb_z_mysql_feature_model_for_dao_test.go => gdb_z_mysql_feature_model_dto_test.go} (52%) diff --git a/database/gdb/gdb_func.go b/database/gdb/gdb_func.go index d55885f5e..386047350 100644 --- a/database/gdb/gdb_func.go +++ b/database/gdb/gdb_func.go @@ -60,6 +60,7 @@ const ( OrmTagForWith = "with" OrmTagForWithWhere = "where" OrmTagForWithOrder = "order" + OrmTagForDto = "dto" ) var ( @@ -70,9 +71,25 @@ var ( structTagPriority = append([]string{OrmTagForStruct}, gconv.StructTagPriority...) ) -// isForDaoModel checks and returns whether given type is for dao model. -func isForDaoModel(t reflect.Type) bool { - return gstr.HasSuffix(t.String(), modelForDaoSuffix) +// isDtoStruct checks and returns whether given type is a DTO struct. +func isDtoStruct(object interface{}) bool { + // It checks by struct name like "XxxForDao", to be compatible with old version. + // TODO remove this compatible codes in future. + reflectType := reflect.TypeOf(object) + if gstr.HasSuffix(reflectType.String(), modelForDaoSuffix) { + return true + } + // It checks by struct meta for DTO struct in version. + if ormTag := gmeta.Get(object, OrmTagForStruct); !ormTag.IsEmpty() { + match, _ := gregex.MatchString( + fmt.Sprintf(`%s\s*:\s*([^,]+)`, OrmTagForDto), + ormTag.String(), + ) + if len(match) > 1 { + return gconv.Bool(match[1]) + } + } + return false } // getTableNameFromOrmTag retrieves and returns the table name from struct object. @@ -424,7 +441,7 @@ func formatWhereHolder(db DB, in formatWhereHolderInput) (newWhere string, newAr case reflect.Struct: // If the `where` parameter is defined like `xxxForDao`, it then adds `OmitNil` option for this condition, // which will filter all nil parameters in `where`. - if isForDaoModel(reflect.TypeOf(in.Where)) { + if isDtoStruct(in.Where) { in.OmitNil = true } // If `where` struct implements iIterator interface, diff --git a/database/gdb/gdb_model_insert.go b/database/gdb/gdb_model_insert.go index 0333c2445..7c8679e52 100644 --- a/database/gdb/gdb_model_insert.go +++ b/database/gdb/gdb_model_insert.go @@ -73,10 +73,10 @@ func (m *Model) Data(data ...interface{}) *Model { switch reflectInfo.OriginKind { case reflect.Slice, reflect.Array: if reflectInfo.OriginValue.Len() > 0 { - // If the `data` parameter is defined like `xxxForDao`, + // If the `data` parameter is a DTO struct, // it then adds `OmitNilData` option for this condition, // which will filter all nil parameters in `data`. - if isForDaoModel(reflectInfo.OriginValue.Index(0).Elem().Type()) { + if isDtoStruct(reflectInfo.OriginValue.Index(0).Interface()) { model = model.OmitNilData() model.option |= optionOmitNilDataInternal } @@ -88,10 +88,10 @@ func (m *Model) Data(data ...interface{}) *Model { model.data = list case reflect.Struct: - // If the `data` parameter is defined like `xxxForDao`, + // If the `data` parameter is a DTO struct, // it then adds `OmitNilData` option for this condition, // which will filter all nil parameters in `data`. - if isForDaoModel(reflect.TypeOf(value)) { + if isDtoStruct(value) { model = model.OmitNilData() } if v, ok := data[0].(iInterfaces); ok { diff --git a/database/gdb/gdb_z_mysql_feature_model_for_dao_test.go b/database/gdb/gdb_z_mysql_feature_model_dto_test.go similarity index 52% rename from database/gdb/gdb_z_mysql_feature_model_for_dao_test.go rename to database/gdb/gdb_z_mysql_feature_model_dto_test.go index 5d4dc7934..e722595fd 100644 --- a/database/gdb/gdb_z_mysql_feature_model_for_dao_test.go +++ b/database/gdb/gdb_z_mysql_feature_model_dto_test.go @@ -13,6 +13,144 @@ import ( "github.com/gogf/gf/v2/test/gtest" ) +func Test_Model_Insert_Data_DTO(t *testing.T) { + table := createTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + type User struct { + g.Meta `orm:"dto:true"` + Id interface{} + Passport interface{} + Password interface{} + Nickname interface{} + CreateTime interface{} + } + data := User{ + Id: 1, + Passport: "user_1", + Password: "pass_1", + } + result, err := db.Model(table).Data(data).Insert() + t.AssertNil(err) + n, _ := result.LastInsertId() + t.Assert(n, 1) + + one, err := db.Model(table).WherePri(1).One() + t.AssertNil(err) + t.Assert(one[`id`], `1`) + t.Assert(one[`passport`], `user_1`) + t.Assert(one[`password`], `pass_1`) + t.Assert(one[`nickname`], ``) + t.Assert(one[`create_time`], ``) + }) +} + +func Test_Model_Insert_Data_LIst_DTO(t *testing.T) { + table := createTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + type User struct { + g.Meta `orm:"dto:true"` + Id interface{} + Passport interface{} + Password interface{} + Nickname interface{} + CreateTime interface{} + } + data := g.Slice{ + User{ + Id: 1, + Passport: "user_1", + Password: "pass_1", + }, + User{ + Id: 2, + Passport: "user_2", + Password: "pass_2", + }, + } + result, err := db.Model(table).Data(data).Insert() + t.AssertNil(err) + n, _ := result.LastInsertId() + t.Assert(n, 2) + + one, err := db.Model(table).WherePri(1).One() + t.AssertNil(err) + t.Assert(one[`id`], `1`) + t.Assert(one[`passport`], `user_1`) + t.Assert(one[`password`], `pass_1`) + t.Assert(one[`nickname`], ``) + t.Assert(one[`create_time`], ``) + + one, err = db.Model(table).WherePri(2).One() + t.AssertNil(err) + t.Assert(one[`id`], `2`) + t.Assert(one[`passport`], `user_2`) + t.Assert(one[`password`], `pass_2`) + t.Assert(one[`nickname`], ``) + t.Assert(one[`create_time`], ``) + }) +} + +func Test_Model_Update_Data_DTO(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + type User struct { + g.Meta `orm:"dto:true"` + Id interface{} + Passport interface{} + Password interface{} + Nickname interface{} + CreateTime interface{} + } + data := User{ + Id: 1, + Passport: "user_100", + Password: "pass_100", + } + _, err := db.Model(table).Data(data).WherePri(1).Update() + t.AssertNil(err) + + one, err := db.Model(table).WherePri(1).One() + t.AssertNil(err) + t.Assert(one[`id`], `1`) + t.Assert(one[`passport`], `user_100`) + t.Assert(one[`password`], `pass_100`) + t.Assert(one[`nickname`], `name_1`) + }) +} + +func Test_Model_Where_DTO(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + type User struct { + g.Meta `orm:"dto:true"` + Id interface{} + Passport interface{} + Password interface{} + Nickname interface{} + CreateTime interface{} + } + where := User{ + Id: 1, + Passport: "user_1", + Password: "pass_1", + } + one, err := db.Model(table).Where(where).One() + t.AssertNil(err) + t.Assert(one[`id`], `1`) + t.Assert(one[`passport`], `user_1`) + t.Assert(one[`password`], `pass_1`) + t.Assert(one[`nickname`], `name_1`) + }) +} + func Test_Model_Insert_Data_ForDao(t *testing.T) { table := createTable() defer dropTable(table) From ff2e9b568b14d3898cd8f7cbdc30be424807894d Mon Sep 17 00:00:00 2001 From: huangqian Date: Wed, 15 Dec 2021 21:15:34 +0800 Subject: [PATCH 7/7] Implemented gstr Example 1. PrefixArray 2.SubStrFrom 3.SubStrFromEx 4.SubStrFromR 5.SubStrFromREx --- text/gstr/gstr_z_example_test.go | 46 ++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/text/gstr/gstr_z_example_test.go b/text/gstr/gstr_z_example_test.go index 465997ce4..c3499dda5 100644 --- a/text/gstr/gstr_z_example_test.go +++ b/text/gstr/gstr_z_example_test.go @@ -506,8 +506,16 @@ func ExampleInArray() { } func ExamplePrefixArray() { + var ( + strArray = []string{"tom", "lily", "john"} + ) + + gstr.PrefixArray(strArray, "classA_") + + fmt.Println(strArray) + // Output: - // test + // [classA_tom classA_lily classA_john] } // case @@ -1141,23 +1149,51 @@ func ExampleStrLimitRune() { } func ExampleSubStrFrom() { + var ( + str = "我爱GoFrameGood" + need = `爱` + ) + + fmt.Println(gstr.SubStrFrom(str, need)) + // Output: - // test + // 爱GoFrameGood } func ExampleSubStrFromEx() { + var ( + str = "我爱GoFrameGood" + need = `爱` + ) + + fmt.Println(gstr.SubStrFromEx(str, need)) + // Output: - // test + // GoFrameGood } func ExampleSubStrFromR() { + var ( + str = "我爱GoFrameGood" + need = `Go` + ) + + fmt.Println(gstr.SubStrFromR(str, need)) + // Output: - // test + // Good } func ExampleSubStrFromREx() { + var ( + str = "我爱GoFrameGood" + need = `Go` + ) + + fmt.Println(gstr.SubStrFromREx(str, need)) + // Output: - // test + // od } // trim