mirror of
https://gitee.com/johng/gf
synced 2026-06-10 03:23:59 +08:00
Compare commits
130 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b702d98700 | |||
| 99f1d9d0ed | |||
| 707b08c585 | |||
| f44c19868c | |||
| 13ab139afc | |||
| cacb2f142b | |||
| 11f0317e92 | |||
| 9a667c8803 | |||
| c1cce17934 | |||
| 5a92d7de0d | |||
| 53bf378868 | |||
| cd7c45c00c | |||
| f13a5ad82e | |||
| 2e9be609c8 | |||
| 2f2f6e1ffe | |||
| f3bd2b67f7 | |||
| 645cecdffb | |||
| 22e9965629 | |||
| 24ea9f9245 | |||
| 9dbde6e8f1 | |||
| fe0b34544d | |||
| 5e489d59b4 | |||
| 61f49574a9 | |||
| 2fabcb62a8 | |||
| 3bc3b652c1 | |||
| 042a6f12f5 | |||
| 5acce82e63 | |||
| 4732bf46ad | |||
| 5bed5a1532 | |||
| 5b7576430f | |||
| 356f4cd701 | |||
| c444630d1e | |||
| 8e40cded42 | |||
| 0e52d467d3 | |||
| 6665d62e7e | |||
| 5bdf1a71b8 | |||
| a34ca0ff4b | |||
| 7f0163d958 | |||
| 31f19b0eee | |||
| 93d0760898 | |||
| 4863e7a6ae | |||
| a161b44cc7 | |||
| 7072244420 | |||
| f68b66e606 | |||
| 4e7c6c1fb4 | |||
| d8a7e36478 | |||
| 8971ad8445 | |||
| 270af8accb | |||
| b0ef63fc9d | |||
| 6e1f8c3cfc | |||
| e58d7e8dda | |||
| 3a3384cf06 | |||
| ef2a9f6fd1 | |||
| 63f756f731 | |||
| bb1c27c36a | |||
| 87cd0703c0 | |||
| 6317d9de53 | |||
| 7acf16fdba | |||
| a52b454d3e | |||
| 816e075c52 | |||
| 9882b361a8 | |||
| 4415dcf1c1 | |||
| 42fd583bfd | |||
| c70bc7c96a | |||
| 02bd780a33 | |||
| 24a2192ce2 | |||
| d8ef8a1f5d | |||
| 745a913cfb | |||
| 13dba407a2 | |||
| 34e7c5f809 | |||
| d570624caa | |||
| f18312419b | |||
| 89f869dd44 | |||
| 20b64507b1 | |||
| 7443246e05 | |||
| f9e7823c14 | |||
| 52943b283c | |||
| 36403fdc08 | |||
| 0317f6812e | |||
| 5169137069 | |||
| 7d9bccf912 | |||
| 14f56ea18f | |||
| 1736d271d2 | |||
| 19755ad233 | |||
| cfdd043e4e | |||
| 78917ed5cb | |||
| 88684ca00a | |||
| 784983806a | |||
| cdb3b94e22 | |||
| 83dcc4a5e0 | |||
| a6c0b281a3 | |||
| 3120f24553 | |||
| ac9be6134b | |||
| 4c1b4f7858 | |||
| 1e45bf93d8 | |||
| e8dd3979b6 | |||
| 374ee4c0ea | |||
| 95411aff77 | |||
| 1999ef95c1 | |||
| b15075fdfe | |||
| 4d2b244319 | |||
| 4c3af63076 | |||
| 91bbff6ced | |||
| 26aab44ec8 | |||
| 2e10ce421b | |||
| 8eda69b11e | |||
| 7d7b242968 | |||
| 202419202f | |||
| 55078beed1 | |||
| d9f4e6eaa6 | |||
| 2ba0913bea | |||
| 8f2dcf21ff | |||
| 01b06e0745 | |||
| 665b5960c8 | |||
| eb6a7a4728 | |||
| 7df53ff55e | |||
| 8021f39710 | |||
| f2af08270b | |||
| c2f028848c | |||
| d9c7224861 | |||
| f59a1ada88 | |||
| 705ab1d33f | |||
| c07c4d7217 | |||
| b867b2a0bc | |||
| 872d674182 | |||
| 4682abafdf | |||
| b7d194cf52 | |||
| edf2366296 | |||
| 22af5be71f | |||
| f662ff8051 |
119
.example/database/gdb/driver/driver/driver.go
Normal file
119
.example/database/gdb/driver/driver/driver.go
Normal file
@ -0,0 +1,119 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). 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 driver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
)
|
||||
|
||||
type MyDriver struct {
|
||||
*gdb.Core
|
||||
}
|
||||
|
||||
// Open creates and returns a underlying sql.DB object for mysql.
|
||||
func (d *MyDriver) Open(config *gdb.ConfigNode) (*sql.DB, error) {
|
||||
var source string
|
||||
if config.LinkInfo != "" {
|
||||
source = config.LinkInfo
|
||||
} else {
|
||||
source = fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true&loc=Local",
|
||||
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset,
|
||||
)
|
||||
}
|
||||
intlog.Printf("Open: %s", source)
|
||||
if db, err := sql.Open("mysql", source); err == nil {
|
||||
return db, nil
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// getChars returns the security char for this type of database.
|
||||
func (d *MyDriver) GetChars() (charLeft string, charRight string) {
|
||||
return "`", "`"
|
||||
}
|
||||
|
||||
// handleSqlBeforeExec handles the sql before posts it to database.
|
||||
func (d *MyDriver) HandleSqlBeforeExec(sql string) string {
|
||||
return sql
|
||||
}
|
||||
|
||||
// Tables retrieves and returns the tables of current schema.
|
||||
func (d *MyDriver) Tables(schema ...string) (tables []string, err error) {
|
||||
var result gdb.Result
|
||||
link, err := d.DB.GetSlave(schema...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err = d.DB.DoGetAll(link, `SHOW TABLES`)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, m := range result {
|
||||
for _, v := range m {
|
||||
tables = append(tables, v.String())
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// gdb.TableFields retrieves and returns the fields information of specified table of current schema.
|
||||
//
|
||||
// Note that it returns a map containing the field name and its corresponding fields.
|
||||
// As a map is unsorted, the gdb.TableField struct has a "Index" field marks its sequence in the fields.
|
||||
//
|
||||
// It's using cache feature to enhance the performance, which is never expired util the process restarts.
|
||||
func (d *MyDriver) TableFields(table string, schema ...string) (fields map[string]*gdb.TableField, err error) {
|
||||
table = gstr.Trim(table)
|
||||
if gstr.Contains(table, " ") {
|
||||
panic("function gdb.TableFields supports only single table operations")
|
||||
}
|
||||
checkSchema := d.DB.GetSchema()
|
||||
if len(schema) > 0 && schema[0] != "" {
|
||||
checkSchema = schema[0]
|
||||
}
|
||||
v := d.DB.GetCache().GetOrSetFunc(
|
||||
fmt.Sprintf(`mysql_table_fields_%s_%s`, table, checkSchema),
|
||||
func() interface{} {
|
||||
var result gdb.Result
|
||||
var link *sql.DB
|
||||
link, err = d.DB.GetSlave(checkSchema)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
result, err = d.DB.DoGetAll(
|
||||
link,
|
||||
fmt.Sprintf(`SHOW FULL COLUMNS FROM %s`, d.DB.QuoteWord(table)),
|
||||
)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
fields = make(map[string]*gdb.TableField)
|
||||
for i, m := range result {
|
||||
fields[m["Field"].String()] = &gdb.TableField{
|
||||
Index: i,
|
||||
Name: m["Field"].String(),
|
||||
Type: m["Type"].String(),
|
||||
Null: m["Null"].Bool(),
|
||||
Key: m["Key"].String(),
|
||||
Default: m["Default"].Val(),
|
||||
Extra: m["Extra"].String(),
|
||||
Comment: m["Comment"].String(),
|
||||
}
|
||||
}
|
||||
return fields
|
||||
}, 0)
|
||||
if err == nil {
|
||||
fields = v.(map[string]*gdb.TableField)
|
||||
}
|
||||
return
|
||||
}
|
||||
1
.example/database/gdb/driver/main.go
Normal file
1
.example/database/gdb/driver/main.go
Normal file
@ -0,0 +1 @@
|
||||
package main
|
||||
@ -1,8 +1,3 @@
|
||||
|
||||
[database]
|
||||
type = "mssql"
|
||||
host = "127.0.0.1"
|
||||
port = "1451"
|
||||
user = "sa"
|
||||
pass = "eno@123"
|
||||
name = "frpc"
|
||||
linkinfo = "mssql:user id=test;password=test1;server=122.152.202.91;port=1433;database=test;encrypt=disable"
|
||||
@ -2,13 +2,22 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
|
||||
_ "github.com/denisenkom/go-mssqldb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r, err := g.DB().GetAll(`SELECT TOP 10 * FROM KF_PatInfo_Emergency`)
|
||||
fmt.Println(err)
|
||||
g.Dump(r.ToList())
|
||||
type Table2 struct {
|
||||
Id string `orm:"id;pr" json:"id"` //ID
|
||||
Createtime gtime.Time `orm:"createtime" json:"createtime"` //创建时间
|
||||
Updatetime gtime.Time `orm:"updatetime" json:"updatetime"` //更新时间
|
||||
}
|
||||
var table2 Table2
|
||||
err := g.DB().Table("table2").Where("id=?", 1).Struct(&table2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(table2.Createtime)
|
||||
}
|
||||
|
||||
@ -11,11 +11,11 @@ func main() {
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("test").OrderBy("id asc").All()
|
||||
r, e := db.Table("test").Order("id asc").All()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
fmt.Println(e)
|
||||
}
|
||||
if r != nil {
|
||||
fmt.Println(r.ToList())
|
||||
fmt.Println(r.List())
|
||||
}
|
||||
}
|
||||
|
||||
18
.example/database/gdb/mysql/gdb_reconnect.go
Normal file
18
.example/database/gdb/mysql/gdb_reconnect.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
for {
|
||||
r, err := db.Table("user").All()
|
||||
fmt.Println(err)
|
||||
fmt.Println(r)
|
||||
time.Sleep(time.Second * 10)
|
||||
}
|
||||
}
|
||||
@ -8,10 +8,5 @@ func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
|
||||
one, e := db.Table("order.order o").LeftJoin("user.user u", "o.uid=u.id").Where("u.id", 1).One()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
g.Dump(one)
|
||||
|
||||
db.Table("user").Fields("DISTINCT id,nickname").Filter().All()
|
||||
}
|
||||
|
||||
@ -100,53 +100,53 @@ func testConvert() {
|
||||
func testSplitChar() {
|
||||
var v interface{}
|
||||
j := gjson.New(nil)
|
||||
t1 := gtime.Nanosecond()
|
||||
t1 := gtime.TimestampNano()
|
||||
j.Set("a.b.c.d.e.f.g.h.i.j.k", 1)
|
||||
t2 := gtime.Nanosecond()
|
||||
t2 := gtime.TimestampNano()
|
||||
fmt.Println(t2 - t1)
|
||||
|
||||
t5 := gtime.Nanosecond()
|
||||
t5 := gtime.TimestampNano()
|
||||
v = j.Get("a.b.c.d.e.f.g.h.i.j.k")
|
||||
t6 := gtime.Nanosecond()
|
||||
t6 := gtime.TimestampNano()
|
||||
fmt.Println(v)
|
||||
fmt.Println(t6 - t5)
|
||||
|
||||
j.SetSplitChar('#')
|
||||
|
||||
t7 := gtime.Nanosecond()
|
||||
t7 := gtime.TimestampNano()
|
||||
v = j.Get("a#b#c#d#e#f#g#h#i#j#k")
|
||||
t8 := gtime.Nanosecond()
|
||||
t8 := gtime.TimestampNano()
|
||||
fmt.Println(v)
|
||||
fmt.Println(t8 - t7)
|
||||
}
|
||||
|
||||
func testViolenceCheck() {
|
||||
j := gjson.New(nil)
|
||||
t1 := gtime.Nanosecond()
|
||||
t1 := gtime.TimestampNano()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t2 := gtime.Nanosecond()
|
||||
t2 := gtime.TimestampNano()
|
||||
fmt.Println(t2 - t1)
|
||||
|
||||
t3 := gtime.Nanosecond()
|
||||
t3 := gtime.TimestampNano()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t4 := gtime.Nanosecond()
|
||||
t4 := gtime.TimestampNano()
|
||||
fmt.Println(t4 - t3)
|
||||
|
||||
t5 := gtime.Nanosecond()
|
||||
t5 := gtime.TimestampNano()
|
||||
j.Get("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a")
|
||||
t6 := gtime.Nanosecond()
|
||||
t6 := gtime.TimestampNano()
|
||||
fmt.Println(t6 - t5)
|
||||
|
||||
j.SetViolenceCheck(false)
|
||||
|
||||
t7 := gtime.Nanosecond()
|
||||
t7 := gtime.TimestampNano()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t8 := gtime.Nanosecond()
|
||||
t8 := gtime.TimestampNano()
|
||||
fmt.Println(t8 - t7)
|
||||
|
||||
t9 := gtime.Nanosecond()
|
||||
t9 := gtime.TimestampNano()
|
||||
j.Get("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a")
|
||||
t10 := gtime.Nanosecond()
|
||||
t10 := gtime.TimestampNano()
|
||||
fmt.Println(t10 - t9)
|
||||
}
|
||||
|
||||
|
||||
@ -3,34 +3,16 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Upload uploads files to /tmp .
|
||||
func Upload(r *ghttp.Request) {
|
||||
saveDir := "/tmp/"
|
||||
for _, item := range r.GetMultipartFiles("upload-file") {
|
||||
file, err := item.Open()
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
f, err := gfile.Create(saveDir + gfile.Basename(item.Filename))
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := io.Copy(f, file); err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
saveDirPath := "/tmp/"
|
||||
files := r.GetUploadFiles("upload-file")
|
||||
if err := files.Save(saveDirPath); err != nil {
|
||||
r.Response.WriteExit(err)
|
||||
}
|
||||
r.Response.Write("upload successfully")
|
||||
r.Response.WriteExit("upload successfully")
|
||||
}
|
||||
|
||||
// UploadShow shows uploading simgle file page.
|
||||
@ -71,7 +53,7 @@ func UploadShowBatch(r *ghttp.Request) {
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/upload", func(group *ghttp.RouterGroup) {
|
||||
group.ALL("/", Upload)
|
||||
group.POST("/", Upload)
|
||||
group.ALL("/show", UploadShow)
|
||||
group.ALL("/batch", UploadShowBatch)
|
||||
})
|
||||
|
||||
@ -3,34 +3,16 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Upload uploads files to /tmp .
|
||||
func Upload(r *ghttp.Request) {
|
||||
saveDir := "/tmp/"
|
||||
for _, item := range r.GetMultipartFiles("upload-file") {
|
||||
file, err := item.Open()
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
f, err := gfile.Create(saveDir + gfile.Basename(item.Filename))
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := io.Copy(f, file); err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
saveDirPath := "/tmp/"
|
||||
files := r.GetUploadFiles("upload-file")
|
||||
if err := files.Save(saveDirPath); err != nil {
|
||||
r.Response.WriteExit(err)
|
||||
}
|
||||
r.Response.Write("upload successfully")
|
||||
r.Response.WriteExit("upload successfully")
|
||||
}
|
||||
|
||||
// UploadShow shows uploading simgle file page.
|
||||
@ -71,7 +53,7 @@ func UploadShowBatch(r *ghttp.Request) {
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/upload", func(group *ghttp.RouterGroup) {
|
||||
group.ALL("/", Upload)
|
||||
group.POST("/", Upload)
|
||||
group.ALL("/show", UploadShow)
|
||||
group.ALL("/batch", UploadShowBatch)
|
||||
})
|
||||
|
||||
22
.example/net/ghttp/server/body.go
Normal file
22
.example/net/ghttp/server/body.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
body1 := r.GetBody()
|
||||
body2, _ := ioutil.ReadAll(r.Body)
|
||||
fmt.Println(body1)
|
||||
fmt.Println(body2)
|
||||
r.Response.Write("hello world")
|
||||
})
|
||||
s.SetPort(8999)
|
||||
s.Run()
|
||||
}
|
||||
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
@ -11,6 +12,7 @@ func MiddlewareCORS(r *ghttp.Request) {
|
||||
}
|
||||
|
||||
func Order(r *ghttp.Request) {
|
||||
glog.Println("order")
|
||||
r.Response.Write("GET")
|
||||
}
|
||||
|
||||
@ -18,7 +20,7 @@ func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
group.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
|
||||
@ -20,7 +20,7 @@ func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
group.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
|
||||
@ -26,7 +26,7 @@ func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
group.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
|
||||
@ -19,7 +19,7 @@ func MiddlewareAuth(r *ghttp.Request) {
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/admin", func(group *ghttp.RouterGroup) {
|
||||
g.MiddlewarePattern("/*action", func(r *ghttp.Request) {
|
||||
group.Middleware(func(r *ghttp.Request) {
|
||||
if action := r.GetRouterString("action"); action != "" {
|
||||
switch action {
|
||||
case "login":
|
||||
|
||||
@ -25,14 +25,12 @@ func MiddlewareCORS(r *ghttp.Request) {
|
||||
|
||||
func MiddlewareLog(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
glog.Println(r.Response.Status, r.URL.Path)
|
||||
g.Log().Println(r.Response.Status, r.URL.Path)
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareLog)
|
||||
})
|
||||
s.Use(MiddlewareLog)
|
||||
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareAuth, MiddlewareCORS)
|
||||
group.ALL("/user/list", func(r *ghttp.Request) {
|
||||
|
||||
@ -13,20 +13,20 @@ func main() {
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("end")
|
||||
})
|
||||
g.Group("/order", func(group *ghttp.RouterGroup) {
|
||||
g.GET("/list", func(r *ghttp.Request) {
|
||||
group.Group("/order", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/list", func(r *ghttp.Request) {
|
||||
r.Response.Write("list")
|
||||
})
|
||||
})
|
||||
g.Group("/user", func(group *ghttp.RouterGroup) {
|
||||
g.GET("/info", func(r *ghttp.Request) {
|
||||
group.Group("/user", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/info", func(r *ghttp.Request) {
|
||||
r.Response.Write("info")
|
||||
})
|
||||
g.POST("/edit", func(r *ghttp.Request) {
|
||||
group.POST("/edit", func(r *ghttp.Request) {
|
||||
r.Response.Write("edit")
|
||||
})
|
||||
})
|
||||
g.Group("/hook", func(group *ghttp.RouterGroup) {
|
||||
group.Group("/hook", func(group *ghttp.RouterGroup) {
|
||||
group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook any")
|
||||
})
|
||||
|
||||
@ -9,14 +9,14 @@ import (
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
g.GET("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set("time", gtime.Second())
|
||||
group.GET("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set("time", gtime.Timestamp())
|
||||
r.Response.Write("ok")
|
||||
})
|
||||
g.GET("/get", func(r *ghttp.Request) {
|
||||
group.GET("/get", func(r *ghttp.Request) {
|
||||
r.Response.WriteJson(r.Session.Map())
|
||||
})
|
||||
g.GET("/clear", func(r *ghttp.Request) {
|
||||
group.GET("/clear", func(r *ghttp.Request) {
|
||||
r.Session.Clear()
|
||||
})
|
||||
})
|
||||
29
.example/net/ghttp/server/session/redis/redis.go
Normal file
29
.example/net/ghttp/server/session/redis/redis.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gsession"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetSessionMaxAge(2 * time.Minute)
|
||||
s.SetSessionStorage(gsession.NewStorageRedis(g.Redis()))
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set("time", gtime.Timestamp())
|
||||
r.Response.Write("ok")
|
||||
})
|
||||
group.GET("/get", func(r *ghttp.Request) {
|
||||
r.Response.WriteJson(r.Session.Map())
|
||||
})
|
||||
group.GET("/clear", func(r *ghttp.Request) {
|
||||
r.Session.Clear()
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
17
.example/net/ghttp/server/template/config/config.go
Normal file
17
.example/net/ghttp/server/template/config/config.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.WriteTplContent(`${.name}`, g.Map{
|
||||
"name": "john",
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
2
.example/net/ghttp/server/template/config/config.toml
Normal file
2
.example/net/ghttp/server/template/config/config.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[viewer]
|
||||
delimiters = ["${", "}"]
|
||||
@ -27,7 +27,7 @@ func main() {
|
||||
},
|
||||
MemUsed: 15560320,
|
||||
MemTotal: 16333788,
|
||||
Time: int(gtime.Second()),
|
||||
Time: int(gtime.Timestamp()),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
start := gtime.Millisecond()
|
||||
start := gtime.TimestampMilli()
|
||||
wg := sync.WaitGroup{}
|
||||
for i := 0; i < 100000; i++ {
|
||||
wg.Add(1)
|
||||
@ -19,5 +19,5 @@ func main() {
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Println("time spent:", gtime.Millisecond()-start)
|
||||
fmt.Println("time spent:", gtime.TimestampMilli()-start)
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
start := gtime.Millisecond()
|
||||
start := gtime.TimestampMilli()
|
||||
wg := sync.WaitGroup{}
|
||||
for i := 0; i < 100000; i++ {
|
||||
wg.Add(1)
|
||||
@ -21,5 +21,5 @@ func main() {
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Println(grpool.Size())
|
||||
fmt.Println("time spent:", gtime.Millisecond()-start)
|
||||
fmt.Println("time spent:", gtime.TimestampMilli()-start)
|
||||
}
|
||||
|
||||
@ -9,8 +9,8 @@ import (
|
||||
func main() {
|
||||
fmt.Println("Date :", gtime.Date())
|
||||
fmt.Println("Datetime :", gtime.Datetime())
|
||||
fmt.Println("Second :", gtime.Second())
|
||||
fmt.Println("Millisecond:", gtime.Millisecond())
|
||||
fmt.Println("Microsecond:", gtime.Microsecond())
|
||||
fmt.Println("Nanosecond :", gtime.Nanosecond())
|
||||
fmt.Println("Second :", gtime.Timestamp())
|
||||
fmt.Println("Millisecond:", gtime.TimestampMilli())
|
||||
fmt.Println("Microsecond:", gtime.TimestampMicro())
|
||||
fmt.Println("Nanosecond :", gtime.TimestampNano())
|
||||
}
|
||||
|
||||
@ -5,4 +5,8 @@
|
||||
|
||||
[redis]
|
||||
default = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
|
||||
[viewer]
|
||||
delimiters = ["${", "}"]
|
||||
autoencode = true
|
||||
@ -2,13 +2,34 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/os/gtimer"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g.Dump(gfile.ScanDirFile("/Users/john/Temp/test", "Dockerfile", true))
|
||||
//if err := gfile.ReplaceDir("gf-empty", "app", "/Users/john/Temp/test", "*.*", true); err != nil {
|
||||
// glog.Fatal("content replacing failed,", err.Error())
|
||||
//}
|
||||
|
||||
func GetList() {
|
||||
START:
|
||||
for {
|
||||
res, err := g.Redis().DoVar("RPOP", "mill")
|
||||
if err != nil {
|
||||
glog.Debug("Rpop:", err)
|
||||
break
|
||||
}
|
||||
glog.Debug(res)
|
||||
if res.IsEmpty() {
|
||||
glog.Debug("nil")
|
||||
continue START
|
||||
}
|
||||
interval := 50 * time.Second
|
||||
gtimer.AddOnce(interval, func() {
|
||||
glog.Debug("end------:", res, gtime.Now().Format("Y-m-d H:i:s"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
g.Redis().SetMaxActive(2)
|
||||
//g.Redis().SetMaxIdle(100)
|
||||
GetList()
|
||||
}
|
||||
|
||||
@ -4,13 +4,12 @@ import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/util/gpage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/page/demo", func(r *ghttp.Request) {
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String())
|
||||
page := r.GetPage(100, 10)
|
||||
buffer, _ := gview.ParseContent(`
|
||||
<html>
|
||||
<head>
|
||||
|
||||
@ -4,14 +4,13 @@ import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/util/gpage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/page/ajax", func(r *ghttp.Request) {
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String(), r.Router)
|
||||
page.EnableAjax("DoAjax")
|
||||
page := r.GetPage(100, 10)
|
||||
page.AjaxActionName = "DoAjax"
|
||||
buffer, _ := gview.ParseContent(`
|
||||
<html>
|
||||
<head>
|
||||
@ -29,11 +28,17 @@ func main() {
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div>{{.page}}</div>
|
||||
<div>{{.page1}}</div>
|
||||
<div>{{.page2}}</div>
|
||||
<div>{{.page3}}</div>
|
||||
<div>{{.page4}}</div>
|
||||
</body>
|
||||
</html>
|
||||
`, g.Map{
|
||||
"page": page.GetContent(1),
|
||||
"page1": page.GetContent(1),
|
||||
"page2": page.GetContent(2),
|
||||
"page3": page.GetContent(3),
|
||||
"page4": page.GetContent(4),
|
||||
})
|
||||
r.Response.Write(buffer)
|
||||
})
|
||||
|
||||
@ -23,7 +23,7 @@ func wrapContent(page *gpage.Page) string {
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/page/custom1/*page", func(r *ghttp.Request) {
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String(), r.Router)
|
||||
page := r.GetPage(100, 10)
|
||||
content := wrapContent(page)
|
||||
buffer, _ := gview.ParseContent(`
|
||||
<html>
|
||||
|
||||
@ -15,7 +15,7 @@ func pageContent(page *gpage.Page) string {
|
||||
page.LastPageTag = "LastPage"
|
||||
pageStr := page.FirstPage()
|
||||
pageStr += page.PrevPage()
|
||||
pageStr += page.PageBar("current-page")
|
||||
pageStr += page.PageBar()
|
||||
pageStr += page.NextPage()
|
||||
pageStr += page.LastPage()
|
||||
return pageStr
|
||||
@ -24,7 +24,7 @@ func pageContent(page *gpage.Page) string {
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/page/custom2/*page", func(r *ghttp.Request) {
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String(), r.Router)
|
||||
page := r.GetPage(100, 10)
|
||||
buffer, _ := gview.ParseContent(`
|
||||
<html>
|
||||
<head>
|
||||
|
||||
@ -4,13 +4,12 @@ import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/util/gpage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/page/static/*page", func(r *ghttp.Request) {
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String(), r.Router)
|
||||
page := r.GetPage(100, 10)
|
||||
buffer, _ := gview.ParseContent(`
|
||||
<html>
|
||||
<head>
|
||||
|
||||
@ -4,13 +4,12 @@ import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/util/gpage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/:obj/*action/{page}.html", func(r *ghttp.Request) {
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String(), r.Router)
|
||||
page := r.GetPage(100, 10)
|
||||
buffer, _ := gview.ParseContent(`
|
||||
<html>
|
||||
<head>
|
||||
|
||||
@ -4,14 +4,13 @@ import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/util/gpage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/page/template/{page}.html", func(r *ghttp.Request) {
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String())
|
||||
page.SetUrlTemplate("/order/list/{.page}.html")
|
||||
page := r.GetPage(100, 10)
|
||||
page.UrlTemplate = "/order/list/{.page}.html"
|
||||
buffer, _ := gview.ParseContent(`
|
||||
<html>
|
||||
<head>
|
||||
|
||||
@ -4,6 +4,7 @@ go:
|
||||
- "1.11.x"
|
||||
- "1.12.x"
|
||||
- "1.13.x"
|
||||
- "1.14.x"
|
||||
|
||||
branches:
|
||||
only:
|
||||
@ -12,7 +13,7 @@ branches:
|
||||
- staging
|
||||
|
||||
env:
|
||||
- GF_DEV=1 GO111MODULE=on
|
||||
- GF_DEBUG=1 GO111MODULE=on
|
||||
|
||||
services:
|
||||
- mysql
|
||||
|
||||
11
DONATOR.MD
11
DONATOR.MD
@ -2,6 +2,7 @@
|
||||
|
||||
We currently accept donation by Alipay/WechatPay, please note your github/gitee account in your payment bill.
|
||||
|
||||
> If you cannot view the donation image, please click [here](https://goframe.org/images/donate.png).
|
||||
|
||||
| Name | Channel | Amount | Comment
|
||||
|---|---|--- | ---
|
||||
@ -14,7 +15,7 @@ We currently accept donation by Alipay/WechatPay, please note your github/gitee
|
||||
|[zhuhuan12](https://gitee.com/zhuhuan12)|gitee|¥50.00 |
|
||||
|[zfan_codes](https://gitee.com/zfan_codes)|gitee|¥10.00 |
|
||||
|[arden](https://github.com/arden)|alipay|¥10.00 |
|
||||
|[macnie](https://www.macnie.com)|wechat|¥100.00 |
|
||||
|[macnie](https://www.macnie.com)|wechat|¥110.00 |
|
||||
|lah|wechat|¥100.00 |
|
||||
|x*z|wechat|¥20.00 |
|
||||
|潘兄|wechat|¥100.00 |
|
||||
@ -42,6 +43,14 @@ We currently accept donation by Alipay/WechatPay, please note your github/gitee
|
||||
|张炳贤|wechat+qq|¥600.00|
|
||||
|[王哈哈](https://gitee.com/develop1024)|wechat|¥6.66| 希望gf越来越好
|
||||
|夕景|alipay+qq|¥9.96+3.57|
|
||||
|struggler|alipay|¥18.80|
|
||||
|*铁|wechat|¥0.01|
|
||||
|C*e|wechat|¥66.66| GF越来越好,棒👍!
|
||||
|(佚名)|wechat|¥6.66| (名字打不出来,也没备注,捐赠时间2020-02-21 14:24:34)
|
||||
|[王飞](https://gitee.com/wang_2018)|gitee|¥20.00| 感谢您的开源项目!
|
||||
|[Zeroing-ZY](https://gitee.com/yunjieg)|gitee|¥20.00| 感谢您的开源项目!
|
||||
|[katydid酱](https://gitee.com/katydid2005)|gitee|¥50.00| 感谢您的开源项目!框架给予了很大的帮助!谢谢大佬!
|
||||
|[李海峰](https://gitee.com/dlhf)|gitee|¥10.00| 希望GF越来越好,框架很牛逼!
|
||||
|
||||
|
||||
<img src="https://goframe.org/images/donate.png"/>
|
||||
|
||||
54
README.MD
54
README.MD
@ -1,6 +1,6 @@
|
||||
# GoFrame
|
||||
|
||||
[](https://godoc.org/github.com/gogf/gf/g#pkg-subdirectories)
|
||||
[](https://godoc.org/github.com/gogf/gf)
|
||||
[](https://travis-ci.org/gogf/gf)
|
||||
[](https://goreportcard.com/report/github.com/gogf/gf)
|
||||
[](https://codecov.io/gh/gogf/gf/branch/master)
|
||||
@ -9,11 +9,11 @@
|
||||
|
||||
English | [简体中文](README_ZH.MD)
|
||||
|
||||
`GF(GoFrame)` is a modular, full-featured and production-ready application development framework of golang.
|
||||
Providing a series of core components and dozens of practical modules,
|
||||
`GF(GoFrame)` is a modular, loose-coupled, full-featured and production-ready application development framework of golang,
|
||||
providing a series of core components and dozens of practical modules,
|
||||
such as: memcache, configure, validator, logging, array/queue/set/map containers,
|
||||
timer/timing tasks, file/memory lock, object pool, database ORM, etc.
|
||||
Supporting web server integrated with router, cookie, session, middleware, logger,
|
||||
timer/timing tasks, file/memory lock, object pool, database ORM, etc,
|
||||
supporting web server integrated with router, cookie, session, middleware, logger,
|
||||
template, https, hooks, rewrites and many more features.
|
||||
|
||||
|
||||
@ -36,49 +36,41 @@ golang version >= 1.10
|
||||
* [APIDoc](https://godoc.org/github.com/gogf/gf)
|
||||
* [中文文档](https://goframe.org)
|
||||
|
||||
# Discussion
|
||||
- QQ Group:[116707870](//shang.qq.com/wpa/qunwpa?idkey=195f91eceeb5d7fa76009b7cd5a4641f70bf4897b7f5a520635eb26ff17adfe7)
|
||||
- WX Group:Add friend`389961817` in WeChat, commenting `GF`
|
||||
- Issues:https://github.com/gogf/gf/issues
|
||||
|
||||
> It's recommended learning `GoFrame` through its awesome source codes and API reference.
|
||||
|
||||
# Architecture
|
||||
<div align=center>
|
||||
<img src="https://goframe.org/images/arch.png?v=10"/>
|
||||
<img src="https://goframe.org/images/arch.png?v=11"/>
|
||||
</div>
|
||||
|
||||
# Quick Start
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("Hello World")
|
||||
})
|
||||
s.Run()
|
||||
}
|
||||
```
|
||||
|
||||
[More Features...](https://goframe.org/start/index)
|
||||
|
||||
|
||||
# License
|
||||
|
||||
`GF` is licensed under the [MIT License](LICENSE), 100% free and open-source, forever.
|
||||
|
||||
# Contributors
|
||||
This project exists thanks to all the people who contribute. [[Contributors](https://github.com/gogf/gf/graphs/contributors)].
|
||||
<a href="https://github.com/gogf/gf/graphs/contributors"><img src="https://opencollective.com/goframe/contributors.svg?width=890&button=false" /></a>
|
||||
|
||||
# Donators
|
||||
|
||||
We currently accept donation by Alipay/WechatPay, please note your github/gitee account in your payment bill. If you like `GF`, why not [buy developer a cup of coffee](DONATOR.MD)?
|
||||
|
||||
# Sponsors
|
||||
We appreciate any kind of sponsorship for `GF` development. If you've got some interesting, please contact WeChat `389961817` / Email `john@goframe.org`.
|
||||
|
||||
|
||||
# Thanks
|
||||
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://goframe.org/images/jetbrains.png" width="100" alt="JetBrains"/></a>
|
||||
|
||||
|
||||
<!--
|
||||
# Sponsor
|
||||
We appreciate any kind of sponsorship for `GF` development. If you've got some interested, please contact john@goframe.org.
|
||||
-->
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
47
README_ZH.MD
47
README_ZH.MD
@ -1,5 +1,5 @@
|
||||
# GoFrame
|
||||
[](https://godoc.org/github.com/gogf/gf/g#pkg-subdirectories)
|
||||
[](https://godoc.org/github.com/gogf/gf)
|
||||
[](https://travis-ci.org/gogf/gf)
|
||||
[](https://goreportcard.com/report/github.com/gogf/gf)
|
||||
[](https://codecov.io/gh/gogf/gf/branch/master)
|
||||
@ -18,11 +18,16 @@
|
||||
# 特点
|
||||
* 模块化、松耦合设计;
|
||||
* 模块丰富,开箱即用;
|
||||
* 简便及可维护性为宗旨;
|
||||
* 简便易用,易于维护;
|
||||
* 社区活跃,大牛谦逊低调脾气好;
|
||||
* 高代码质量、高单元测试覆盖率;
|
||||
* 详尽的开发文档及示例;
|
||||
* 完善的本地中文化支持;
|
||||
* 更适合企业及团队使用;
|
||||
* 更多请查阅文档及源码;
|
||||
|
||||
# 地址
|
||||
- **主库**:https://github.com/gogf/gf
|
||||
- **码云**:https://gitee.com/johng/gf
|
||||
|
||||
# 安装
|
||||
```html
|
||||
@ -40,7 +45,7 @@ golang版本 >= 1.11
|
||||
|
||||
# 架构
|
||||
<div align=center>
|
||||
<img src="https://goframe.org/images/arch.png?v=10"/>
|
||||
<img src="https://goframe.org/images/arch.png?v=11"/>
|
||||
</div>
|
||||
|
||||
|
||||
@ -51,35 +56,31 @@ golang版本 >= 1.11
|
||||
|
||||
接口文档:[https://godoc.org/github.com/gogf/gf](https://godoc.org/github.com/gogf/gf)
|
||||
|
||||
# 使用
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("Hello World")
|
||||
})
|
||||
s.Run()
|
||||
}
|
||||
```
|
||||
|
||||
[更多..](https://goframe.org/start/index)
|
||||
# 帮助
|
||||
- QQ交流群:[116707870](//shang.qq.com/wpa/qunwpa?idkey=195f91eceeb5d7fa76009b7cd5a4641f70bf4897b7f5a520635eb26ff17adfe7)
|
||||
- WX交流群:微信添加`389961817`备注`GF`
|
||||
- 主库ISSUE:https://github.com/gogf/gf/issues
|
||||
|
||||
> 建议通过阅读`Gorame`的源码以及API文档深度学习`GoFrame`,了解更多的精妙设计。
|
||||
|
||||
# 协议
|
||||
|
||||
`GF` 使用非常友好的 [MIT](LICENSE) 开源协议进行发布,永久`100%`开源免费。
|
||||
|
||||
# 贡献
|
||||
|
||||
感谢所有参与`GoFrame`开发的贡献者。 [[贡献者列表](https://github.com/gogf/gf/graphs/contributors)].
|
||||
<a href="https://github.com/gogf/gf/graphs/contributors"><img src="https://opencollective.com/goframe/contributors.svg?width=890&button=false" /></a>
|
||||
|
||||
|
||||
# 捐赠
|
||||
|
||||
如果您喜欢`GF`,要不[给开发者来杯咖啡吧](DONATOR.MD)!
|
||||
请在捐赠时备注您的`github`/`gitee`账号名称。
|
||||
|
||||
# 赞助
|
||||
|
||||
赞助支持`GF`框架的快速研发,如果您感兴趣,请联系 微信 `389961817` / 邮件 `john@goframe.org`。
|
||||
|
||||
# 感谢
|
||||
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://goframe.org/images/jetbrains.png" width="100" alt="JetBrains"/></a>
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"math"
|
||||
"sort"
|
||||
@ -176,6 +177,9 @@ func (a *Array) InsertAfter(index int, value interface{}) *Array {
|
||||
func (a *Array) Remove(index int) interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if index < 0 || index >= len(a.array) {
|
||||
return nil
|
||||
}
|
||||
// Determine array boundaries when deleting to improve deletion efficiency。
|
||||
if index == 0 {
|
||||
value := a.array[0]
|
||||
@ -194,6 +198,16 @@ func (a *Array) Remove(index int) interface{} {
|
||||
return value
|
||||
}
|
||||
|
||||
// RemoveValue removes an item by value.
|
||||
// It returns true if value is found in the array, or else false if not found.
|
||||
func (a *Array) RemoveValue(value interface{}) bool {
|
||||
if i := a.Search(value); i != -1 {
|
||||
a.Remove(i)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PushLeft pushes one or multiple items to the beginning of array.
|
||||
func (a *Array) PushLeft(value ...interface{}) *Array {
|
||||
a.mu.Lock()
|
||||
@ -418,9 +432,6 @@ func (a *Array) Contains(value interface{}) bool {
|
||||
// Search searches array by <value>, returns the index of <value>,
|
||||
// or returns -1 if not exists.
|
||||
func (a *Array) Search(value interface{}) int {
|
||||
if len(a.array) == 0 {
|
||||
return -1
|
||||
}
|
||||
a.mu.RLock()
|
||||
result := -1
|
||||
for index, v := range a.array {
|
||||
@ -430,7 +441,20 @@ func (a *Array) Search(value interface{}) int {
|
||||
}
|
||||
}
|
||||
a.mu.RUnlock()
|
||||
return result
|
||||
}
|
||||
|
||||
func (a *Array) doSearch(value interface{}) int {
|
||||
if len(a.array) == 0 {
|
||||
return -1
|
||||
}
|
||||
result := -1
|
||||
for index, v := range a.array {
|
||||
if v == value {
|
||||
result = index
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@ -696,3 +720,48 @@ func (a *Array) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for array.
|
||||
func (a *Array) UnmarshalValue(value interface{}) error {
|
||||
if a.mu == nil {
|
||||
a.mu = rwmutex.New()
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
return json.Unmarshal(gconv.Bytes(value), &a.array)
|
||||
default:
|
||||
a.array = gconv.SliceAny(value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FilterNil removes all nil value of the array.
|
||||
func (a *Array) FilterNil() *Array {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for i := 0; i < len(a.array); {
|
||||
if empty.IsNil(a.array[i]) {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// FilterEmpty removes all empty value of the array.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (a *Array) FilterEmpty() *Array {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for i := 0; i < len(a.array); {
|
||||
if empty.IsEmpty(a.array[i]) {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@ -179,6 +179,9 @@ func (a *IntArray) InsertAfter(index int, value int) *IntArray {
|
||||
func (a *IntArray) Remove(index int) int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if index < 0 || index >= len(a.array) {
|
||||
return 0
|
||||
}
|
||||
// Determine array boundaries when deleting to improve deletion efficiency.
|
||||
if index == 0 {
|
||||
value := a.array[0]
|
||||
@ -197,6 +200,16 @@ func (a *IntArray) Remove(index int) int {
|
||||
return value
|
||||
}
|
||||
|
||||
// RemoveValue removes an item by value.
|
||||
// It returns true if value is found in the array, or else false if not found.
|
||||
func (a *IntArray) RemoveValue(value int) bool {
|
||||
if i := a.Search(value); i != -1 {
|
||||
a.Remove(i)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PushLeft pushes one or multiple items to the beginning of array.
|
||||
func (a *IntArray) PushLeft(value ...int) *IntArray {
|
||||
a.mu.Lock()
|
||||
@ -430,9 +443,6 @@ func (a *IntArray) Contains(value int) bool {
|
||||
// Search searches array by <value>, returns the index of <value>,
|
||||
// or returns -1 if not exists.
|
||||
func (a *IntArray) Search(value int) int {
|
||||
if len(a.array) == 0 {
|
||||
return -1
|
||||
}
|
||||
a.mu.RLock()
|
||||
result := -1
|
||||
for index, v := range a.array {
|
||||
@ -442,7 +452,6 @@ func (a *IntArray) Search(value int) int {
|
||||
}
|
||||
}
|
||||
a.mu.RUnlock()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@ -691,3 +700,33 @@ func (a *IntArray) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for array.
|
||||
func (a *IntArray) UnmarshalValue(value interface{}) error {
|
||||
if a.mu == nil {
|
||||
a.mu = rwmutex.New()
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
return json.Unmarshal(gconv.Bytes(value), &a.array)
|
||||
default:
|
||||
a.array = gconv.SliceInt(value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FilterEmpty removes all zero value of the array.
|
||||
func (a *IntArray) FilterEmpty() *IntArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for i := 0; i < len(a.array); {
|
||||
if a.array[i] == 0 {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@ -165,6 +165,9 @@ func (a *StrArray) InsertAfter(index int, value string) *StrArray {
|
||||
func (a *StrArray) Remove(index int) string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if index < 0 || index >= len(a.array) {
|
||||
return ""
|
||||
}
|
||||
// Determine array boundaries when deleting to improve deletion efficiency。
|
||||
if index == 0 {
|
||||
value := a.array[0]
|
||||
@ -183,6 +186,16 @@ func (a *StrArray) Remove(index int) string {
|
||||
return value
|
||||
}
|
||||
|
||||
// RemoveValue removes an item by value.
|
||||
// It returns true if value is found in the array, or else false if not found.
|
||||
func (a *StrArray) RemoveValue(value string) bool {
|
||||
if i := a.Search(value); i != -1 {
|
||||
a.Remove(i)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PushLeft pushes one or multiple items to the beginning of array.
|
||||
func (a *StrArray) PushLeft(value ...string) *StrArray {
|
||||
a.mu.Lock()
|
||||
@ -687,3 +700,33 @@ func (a *StrArray) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for array.
|
||||
func (a *StrArray) UnmarshalValue(value interface{}) error {
|
||||
if a.mu == nil {
|
||||
a.mu = rwmutex.New()
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
return json.Unmarshal(gconv.Bytes(value), &a.array)
|
||||
default:
|
||||
a.array = gconv.SliceStr(value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FilterEmpty removes all empty string value of the array.
|
||||
func (a *StrArray) FilterEmpty() *StrArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for i := 0; i < len(a.array); {
|
||||
if a.array[i] == "" {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"math"
|
||||
@ -160,6 +161,9 @@ func (a *SortedArray) Get(index int) interface{} {
|
||||
func (a *SortedArray) Remove(index int) interface{} {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if index < 0 || index >= len(a.array) {
|
||||
return nil
|
||||
}
|
||||
// Determine array boundaries when deleting to improve deletion efficiency.
|
||||
if index == 0 {
|
||||
value := a.array[0]
|
||||
@ -178,6 +182,16 @@ func (a *SortedArray) Remove(index int) interface{} {
|
||||
return value
|
||||
}
|
||||
|
||||
// RemoveValue removes an item by value.
|
||||
// It returns true if value is found in the array, or else false if not found.
|
||||
func (a *SortedArray) RemoveValue(value interface{}) bool {
|
||||
if i := a.Search(value); i != -1 {
|
||||
a.Remove(i)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PopLeft pops and returns an item from the beginning of array.
|
||||
func (a *SortedArray) PopLeft() interface{} {
|
||||
a.mu.Lock()
|
||||
@ -425,6 +439,9 @@ func (a *SortedArray) SetUnique(unique bool) *SortedArray {
|
||||
func (a *SortedArray) Unique() *SortedArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return a
|
||||
}
|
||||
i := 0
|
||||
for {
|
||||
if i == len(a.array)-1 {
|
||||
@ -642,10 +659,77 @@ func (a *SortedArray) UnmarshalJSON(b []byte) error {
|
||||
if err := json.Unmarshal(b, &a.array); err != nil {
|
||||
return err
|
||||
}
|
||||
if a.comparator != nil {
|
||||
if a.comparator != nil && a.array != nil {
|
||||
sort.Slice(a.array, func(i, j int) bool {
|
||||
return a.comparator(a.array[i], a.array[j]) < 0
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for array.
|
||||
func (a *SortedArray) UnmarshalValue(value interface{}) (err error) {
|
||||
if a.mu == nil {
|
||||
a.mu = rwmutex.New()
|
||||
a.unique = gtype.NewBool()
|
||||
// Note that the comparator is string comparator in default.
|
||||
a.comparator = gutil.ComparatorString
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
err = json.Unmarshal(gconv.Bytes(value), &a.array)
|
||||
default:
|
||||
a.array = gconv.SliceAny(value)
|
||||
}
|
||||
if a.comparator != nil && a.array != nil {
|
||||
sort.Slice(a.array, func(i, j int) bool {
|
||||
return a.comparator(a.array[i], a.array[j]) < 0
|
||||
})
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// FilterNil removes all nil value of the array.
|
||||
func (a *SortedArray) FilterNil() *SortedArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for i := 0; i < len(a.array); {
|
||||
if empty.IsNil(a.array[i]) {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
for i := len(a.array) - 1; i >= 0; {
|
||||
if empty.IsNil(a.array[i]) {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// FilterEmpty removes all empty value of the array.
|
||||
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
|
||||
func (a *SortedArray) FilterEmpty() *SortedArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for i := 0; i < len(a.array); {
|
||||
if empty.IsEmpty(a.array[i]) {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
for i := len(a.array) - 1; i >= 0; {
|
||||
if empty.IsEmpty(a.array[i]) {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@ -146,6 +146,9 @@ func (a *SortedIntArray) Get(index int) int {
|
||||
func (a *SortedIntArray) Remove(index int) int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if index < 0 || index >= len(a.array) {
|
||||
return 0
|
||||
}
|
||||
// Determine array boundaries when deleting to improve deletion efficiency.
|
||||
if index == 0 {
|
||||
value := a.array[0]
|
||||
@ -164,6 +167,16 @@ func (a *SortedIntArray) Remove(index int) int {
|
||||
return value
|
||||
}
|
||||
|
||||
// RemoveValue removes an item by value.
|
||||
// It returns true if value is found in the array, or else false if not found.
|
||||
func (a *SortedIntArray) RemoveValue(value int) bool {
|
||||
if i := a.Search(value); i != -1 {
|
||||
a.Remove(i)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PopLeft pops and returns an item from the beginning of array.
|
||||
func (a *SortedIntArray) PopLeft() int {
|
||||
a.mu.Lock()
|
||||
@ -416,6 +429,10 @@ func (a *SortedIntArray) SetUnique(unique bool) *SortedIntArray {
|
||||
// Unique uniques the array, clear repeated items.
|
||||
func (a *SortedIntArray) Unique() *SortedIntArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return a
|
||||
}
|
||||
i := 0
|
||||
for {
|
||||
if i == len(a.array)-1 {
|
||||
@ -427,7 +444,6 @@ func (a *SortedIntArray) Unique() *SortedIntArray {
|
||||
i++
|
||||
}
|
||||
}
|
||||
a.mu.Unlock()
|
||||
return a
|
||||
}
|
||||
|
||||
@ -616,6 +632,51 @@ func (a *SortedIntArray) UnmarshalJSON(b []byte) error {
|
||||
if err := json.Unmarshal(b, &a.array); err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Ints(a.array)
|
||||
if a.array != nil {
|
||||
sort.Ints(a.array)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for array.
|
||||
func (a *SortedIntArray) UnmarshalValue(value interface{}) (err error) {
|
||||
if a.mu == nil {
|
||||
a.mu = rwmutex.New()
|
||||
a.unique = gtype.NewBool()
|
||||
// Note that the comparator is string comparator in default.
|
||||
a.comparator = defaultComparatorInt
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
err = json.Unmarshal(gconv.Bytes(value), &a.array)
|
||||
default:
|
||||
a.array = gconv.SliceInt(value)
|
||||
}
|
||||
if a.array != nil {
|
||||
sort.Ints(a.array)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// FilterEmpty removes all zero value of the array.
|
||||
func (a *SortedIntArray) FilterEmpty() *SortedIntArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for i := 0; i < len(a.array); {
|
||||
if a.array[i] == 0 {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
for i := len(a.array) - 1; i >= 0; {
|
||||
if a.array[i] == 0 {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@ -131,6 +131,9 @@ func (a *SortedStrArray) Get(index int) string {
|
||||
func (a *SortedStrArray) Remove(index int) string {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if index < 0 || index >= len(a.array) {
|
||||
return ""
|
||||
}
|
||||
// Determine array boundaries when deleting to improve deletion efficiency.
|
||||
if index == 0 {
|
||||
value := a.array[0]
|
||||
@ -149,6 +152,16 @@ func (a *SortedStrArray) Remove(index int) string {
|
||||
return value
|
||||
}
|
||||
|
||||
// RemoveValue removes an item by value.
|
||||
// It returns true if value is found in the array, or else false if not found.
|
||||
func (a *SortedStrArray) RemoveValue(value string) bool {
|
||||
if i := a.Search(value); i != -1 {
|
||||
a.Remove(i)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PopLeft pops and returns an item from the beginning of array.
|
||||
func (a *SortedStrArray) PopLeft() string {
|
||||
a.mu.Lock()
|
||||
@ -401,6 +414,10 @@ func (a *SortedStrArray) SetUnique(unique bool) *SortedStrArray {
|
||||
// Unique uniques the array, clear repeated items.
|
||||
func (a *SortedStrArray) Unique() *SortedStrArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if len(a.array) == 0 {
|
||||
return a
|
||||
}
|
||||
i := 0
|
||||
for {
|
||||
if i == len(a.array)-1 {
|
||||
@ -412,7 +429,6 @@ func (a *SortedStrArray) Unique() *SortedStrArray {
|
||||
i++
|
||||
}
|
||||
}
|
||||
a.mu.Unlock()
|
||||
return a
|
||||
}
|
||||
|
||||
@ -612,6 +628,51 @@ func (a *SortedStrArray) UnmarshalJSON(b []byte) error {
|
||||
if err := json.Unmarshal(b, &a.array); err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Strings(a.array)
|
||||
if a.array != nil {
|
||||
sort.Strings(a.array)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for array.
|
||||
func (a *SortedStrArray) UnmarshalValue(value interface{}) (err error) {
|
||||
if a.mu == nil {
|
||||
a.mu = rwmutex.New()
|
||||
a.unique = gtype.NewBool()
|
||||
// Note that the comparator is string comparator in default.
|
||||
a.comparator = defaultComparatorStr
|
||||
}
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
err = json.Unmarshal(gconv.Bytes(value), &a.array)
|
||||
default:
|
||||
a.array = gconv.SliceStr(value)
|
||||
}
|
||||
if a.array != nil {
|
||||
sort.Strings(a.array)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// FilterEmpty removes all empty string value of the array.
|
||||
func (a *SortedStrArray) FilterEmpty() *SortedStrArray {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for i := 0; i < len(a.array); {
|
||||
if a.array[i] == "" {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
for i := len(a.array) - 1; i >= 0; {
|
||||
if a.array[i] == "" {
|
||||
a.array = append(a.array[:i], a.array[i+1:]...)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@ -13,8 +13,8 @@ import (
|
||||
)
|
||||
|
||||
func Example_basic() {
|
||||
// 创建普通的数组,默认并发安全(带锁)
|
||||
a := garray.New(true)
|
||||
// 创建普通的数组
|
||||
a := garray.New()
|
||||
|
||||
// 添加数据项
|
||||
for i := 0; i < 10; i++ {
|
||||
|
||||
@ -34,6 +34,8 @@ func Test_Array_Basic(t *testing.T) {
|
||||
gtest.Assert(array3.Search(100), -1)
|
||||
gtest.Assert(array.Contains(100), true)
|
||||
gtest.Assert(array.Remove(0), 100)
|
||||
gtest.Assert(array.Remove(-1), nil)
|
||||
gtest.Assert(array.Remove(100000), nil)
|
||||
|
||||
gtest.Assert(array2.Remove(3), 3)
|
||||
gtest.Assert(array2.Remove(1), 1)
|
||||
@ -190,6 +192,34 @@ func TestArray_Chunk(t *testing.T) {
|
||||
gtest.Assert(chunks[2], []interface{}{5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []interface{}{4, 5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2})
|
||||
gtest.Assert(chunks[1], []interface{}{3, 4})
|
||||
gtest.Assert(chunks[2], []interface{}{5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []interface{}{4, 5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_Pad(t *testing.T) {
|
||||
@ -497,3 +527,67 @@ func TestArray_Iterator(t *testing.T) {
|
||||
gtest.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_RemoveValue(t *testing.T) {
|
||||
slice := g.Slice{"a", "b", "d", "c"}
|
||||
array := garray.NewArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue("e"), false)
|
||||
gtest.Assert(array.RemoveValue("b"), true)
|
||||
gtest.Assert(array.RemoveValue("a"), true)
|
||||
gtest.Assert(array.RemoveValue("c"), true)
|
||||
gtest.Assert(array.RemoveValue("f"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Array *garray.Array
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`[1,2,3]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.Slice{1, 2, 3},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_FilterNil(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
values := g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}
|
||||
array := garray.NewArrayFromCopy(values)
|
||||
gtest.Assert(array.FilterNil().Slice(), values)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewArrayFromCopy(g.Slice{nil, 1, 2, 3, 4, nil})
|
||||
gtest.Assert(array.FilterNil(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}})
|
||||
gtest.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewArrayFrom(g.Slice{1, 2, 3, 4})
|
||||
gtest.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
@ -35,6 +35,8 @@ func Test_IntArray_Basic(t *testing.T) {
|
||||
gtest.Assert(array2.Search(100), -1)
|
||||
gtest.Assert(array.Contains(100), true)
|
||||
gtest.Assert(array.Remove(0), 100)
|
||||
gtest.Assert(array.Remove(-1), 0)
|
||||
gtest.Assert(array.Remove(100000), 0)
|
||||
gtest.Assert(array.Contains(100), false)
|
||||
array.Append(4)
|
||||
gtest.Assert(array.Len(), 4)
|
||||
@ -178,6 +180,34 @@ func TestIntArray_Chunk(t *testing.T) {
|
||||
gtest.Assert(chunks[2], []int{5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []int{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []int{4, 5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []int{1, 2})
|
||||
gtest.Assert(chunks[1], []int{3, 4})
|
||||
gtest.Assert(chunks[2], []int{5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []int{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []int{4, 5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_Pad(t *testing.T) {
|
||||
@ -531,3 +561,56 @@ func TestIntArray_Iterator(t *testing.T) {
|
||||
gtest.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_RemoveValue(t *testing.T) {
|
||||
slice := g.SliceInt{10, 20, 30, 40}
|
||||
array := garray.NewIntArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue(99), false)
|
||||
gtest.Assert(array.RemoveValue(20), true)
|
||||
gtest.Assert(array.RemoveValue(10), true)
|
||||
gtest.Assert(array.RemoveValue(20), false)
|
||||
gtest.Assert(array.RemoveValue(88), false)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Array *garray.IntArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`[1,2,3]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.Slice{1, 2, 3},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewIntArrayFrom(g.SliceInt{0, 1, 2, 3, 4, 0})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3, 4})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
@ -34,6 +34,8 @@ func Test_StrArray_Basic(t *testing.T) {
|
||||
gtest.Assert(array.Search("100"), 0)
|
||||
gtest.Assert(array.Contains("100"), true)
|
||||
gtest.Assert(array.Remove(0), 100)
|
||||
gtest.Assert(array.Remove(-1), "")
|
||||
gtest.Assert(array.Remove(100000), "")
|
||||
gtest.Assert(array.Contains("100"), false)
|
||||
array.Append("4")
|
||||
gtest.Assert(array.Len(), 4)
|
||||
@ -177,6 +179,34 @@ func TestStrArray_Chunk(t *testing.T) {
|
||||
gtest.Assert(chunks[2], []string{"5"})
|
||||
gtest.Assert(len(array1.Chunk(0)), 0)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"1", "2", "3", "4", "5"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
gtest.Assert(chunks[1], []string{"4", "5"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []string{"1", "2"})
|
||||
gtest.Assert(chunks[1], []string{"3", "4"})
|
||||
gtest.Assert(chunks[2], []string{"5", "6"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
gtest.Assert(chunks[1], []string{"4", "5", "6"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_Pad(t *testing.T) {
|
||||
@ -535,3 +565,55 @@ func TestStrArray_Iterator(t *testing.T) {
|
||||
gtest.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_RemoveValue(t *testing.T) {
|
||||
slice := g.SliceStr{"a", "b", "d", "c"}
|
||||
array := garray.NewStrArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue("e"), false)
|
||||
gtest.Assert(array.RemoveValue("b"), true)
|
||||
gtest.Assert(array.RemoveValue("a"), true)
|
||||
gtest.Assert(array.RemoveValue("c"), true)
|
||||
gtest.Assert(array.RemoveValue("f"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Array *garray.StrArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`["1","2","3"]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.SliceStr{"1", "2", "3"},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewStrArrayFrom(g.SliceStr{"", "1", "2", "0"})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceStr{"1", "2", "0"})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewStrArrayFrom(g.SliceStr{"1", "2"})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceStr{"1", "2"})
|
||||
})
|
||||
}
|
||||
|
||||
@ -117,6 +117,9 @@ func TestSortedArray_Remove(t *testing.T) {
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
gtest.Assert(array1.Contains("b"), false)
|
||||
|
||||
gtest.Assert(array1.Remove(-1), nil)
|
||||
gtest.Assert(array1.Remove(100000), nil)
|
||||
|
||||
i2 := array1.Remove(0)
|
||||
gtest.Assert(gconv.String(i2), "a")
|
||||
gtest.Assert(array1.Len(), 2)
|
||||
@ -323,6 +326,34 @@ func TestSortedArray_Chunk(t *testing.T) {
|
||||
i1 = array1.Chunk(0)
|
||||
gtest.Assert(len(i1), 0)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorInt)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []interface{}{4, 5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorInt)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2})
|
||||
gtest.Assert(chunks[1], []interface{}{3, 4})
|
||||
gtest.Assert(chunks[2], []interface{}{5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorInt)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []interface{}{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []interface{}{4, 5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_SubSlice(t *testing.T) {
|
||||
@ -634,3 +665,67 @@ func TestSortedArray_Iterator(t *testing.T) {
|
||||
gtest.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_RemoveValue(t *testing.T) {
|
||||
slice := g.Slice{"a", "b", "d", "c"}
|
||||
array := garray.NewSortedArrayFrom(slice, gutil.ComparatorString)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue("e"), false)
|
||||
gtest.Assert(array.RemoveValue("b"), true)
|
||||
gtest.Assert(array.RemoveValue("a"), true)
|
||||
gtest.Assert(array.RemoveValue("c"), true)
|
||||
gtest.Assert(array.RemoveValue("f"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Array *garray.SortedArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`[2,3,1]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.Slice{2, 3, 1},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_FilterNil(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
values := g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}
|
||||
array := garray.NewSortedArrayFromCopy(values, gutil.ComparatorInt)
|
||||
gtest.Assert(array.FilterNil().Slice(), g.Slice{0, "", g.Slice{}, 1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewSortedArrayFromCopy(g.Slice{nil, 1, 2, 3, 4, nil}, gutil.ComparatorInt)
|
||||
gtest.Assert(array.FilterNil(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewSortedArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}, gutil.ComparatorInt)
|
||||
gtest.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewSortedArrayFrom(g.Slice{1, 2, 3, 4}, gutil.ComparatorInt)
|
||||
gtest.Assert(array.FilterEmpty(), g.Slice{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
@ -78,6 +78,10 @@ func TestSortedIntArray_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 3, 5, 0}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
|
||||
gtest.Assert(array1.Remove(-1), 0)
|
||||
gtest.Assert(array1.Remove(100000), 0)
|
||||
|
||||
i1 := array1.Remove(2)
|
||||
gtest.Assert(i1, 3)
|
||||
gtest.Assert(array1.Search(5), 2)
|
||||
@ -264,6 +268,34 @@ func TestSortedIntArray_Chunk(t *testing.T) {
|
||||
gtest.Assert(ns1[2], []int{5})
|
||||
gtest.Assert(len(ns2), 0)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 2, 3, 4, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []int{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []int{4, 5})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []int{1, 2})
|
||||
gtest.Assert(chunks[1], []int{3, 4})
|
||||
gtest.Assert(chunks[2], []int{5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []int{1, 2, 3})
|
||||
gtest.Assert(chunks[1], []int{4, 5, 6})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_SubSlice(t *testing.T) {
|
||||
@ -513,3 +545,56 @@ func TestSortedIntArray_Iterator(t *testing.T) {
|
||||
gtest.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_RemoveValue(t *testing.T) {
|
||||
slice := g.SliceInt{10, 20, 30, 40}
|
||||
array := garray.NewSortedIntArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue(99), false)
|
||||
gtest.Assert(array.RemoveValue(20), true)
|
||||
gtest.Assert(array.RemoveValue(10), true)
|
||||
gtest.Assert(array.RemoveValue(20), false)
|
||||
gtest.Assert(array.RemoveValue(88), false)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Array *garray.SortedIntArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`[2,3,1]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.Slice{2, 3, 1},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewSortedIntArrayFrom(g.SliceInt{0, 1, 2, 3, 4, 0})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewSortedIntArrayFrom(g.SliceInt{1, 2, 3, 4})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceInt{1, 2, 3, 4})
|
||||
})
|
||||
}
|
||||
|
||||
@ -77,6 +77,10 @@ func TestSortedStrArray_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"a", "d", "c", "b"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
|
||||
gtest.Assert(array1.Remove(-1), "")
|
||||
gtest.Assert(array1.Remove(100000), "")
|
||||
|
||||
gtest.Assert(array1.Remove(2), "c")
|
||||
gtest.Assert(array1.Get(2), "d")
|
||||
gtest.Assert(array1.Len(), 3)
|
||||
@ -330,6 +334,34 @@ func TestSortedStrArray_Chunk(t *testing.T) {
|
||||
gtest.Assert(array2[1], []string{"c", "d"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"1", "2", "3", "4", "5"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
gtest.Assert(chunks[1], []string{"4", "5"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(2)
|
||||
gtest.Assert(len(chunks), 3)
|
||||
gtest.Assert(chunks[0], []string{"1", "2"})
|
||||
gtest.Assert(chunks[1], []string{"3", "4"})
|
||||
gtest.Assert(chunks[2], []string{"5", "6"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
chunks := array1.Chunk(3)
|
||||
gtest.Assert(len(chunks), 2)
|
||||
gtest.Assert(chunks[0], []string{"1", "2", "3"})
|
||||
gtest.Assert(chunks[1], []string{"4", "5", "6"})
|
||||
gtest.Assert(array1.Chunk(0), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_SetUnique(t *testing.T) {
|
||||
@ -523,3 +555,55 @@ func TestSortedStrArray_Iterator(t *testing.T) {
|
||||
gtest.Assert(index, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_RemoveValue(t *testing.T) {
|
||||
slice := g.SliceStr{"a", "b", "d", "c"}
|
||||
array := garray.NewSortedStrArrayFrom(slice)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(array.RemoveValue("e"), false)
|
||||
gtest.Assert(array.RemoveValue("b"), true)
|
||||
gtest.Assert(array.RemoveValue("a"), true)
|
||||
gtest.Assert(array.RemoveValue("c"), true)
|
||||
gtest.Assert(array.RemoveValue("f"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Array *garray.SortedStrArray
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": []byte(`["1","3","2"]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"array": g.SliceStr{"1", "3", "2"},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Array.Slice(), g.SliceStr{"1", "2", "3"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_FilterEmpty(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewSortedStrArrayFrom(g.SliceStr{"", "1", "2", "0"})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceStr{"0", "1", "2"})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
array := garray.NewSortedStrArrayFrom(g.SliceStr{"1", "2"})
|
||||
gtest.Assert(array.FilterEmpty(), g.SliceStr{"1", "2"})
|
||||
})
|
||||
}
|
||||
|
||||
@ -438,3 +438,22 @@ func (l *List) UnmarshalJSON(b []byte) error {
|
||||
l.PushBacks(array)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for list.
|
||||
func (l *List) UnmarshalValue(value interface{}) (err error) {
|
||||
if l.mu == nil {
|
||||
l.mu = rwmutex.New()
|
||||
l.list = list.New()
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
var array []interface{}
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
err = json.Unmarshal(gconv.Bytes(value), &array)
|
||||
default:
|
||||
array = gconv.SliceAny(value)
|
||||
}
|
||||
l.PushBacks(array)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -632,3 +632,32 @@ func TestList_Json(t *testing.T) {
|
||||
gtest.Assert(l.FrontAll(), a)
|
||||
})
|
||||
}
|
||||
|
||||
func TestList_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
List *List
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"list": []byte(`[1,2,3]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.List.FrontAll(), []interface{}{1, 2, 3})
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"list": []interface{}{1, 2, 3},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.List.FrontAll(), []interface{}{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
@ -436,3 +436,17 @@ func (m *AnyAnyMap) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (m *AnyAnyMap) UnmarshalValue(value interface{}) (err error) {
|
||||
if m.mu == nil {
|
||||
m.mu = rwmutex.New()
|
||||
m.data = make(map[interface{}]interface{})
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for k, v := range gconv.Map(value) {
|
||||
m.data[k] = v
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -432,3 +432,22 @@ func (m *IntAnyMap) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (m *IntAnyMap) UnmarshalValue(value interface{}) (err error) {
|
||||
if m.mu == nil {
|
||||
m.mu = rwmutex.New()
|
||||
m.data = make(map[int]interface{})
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
return json.Unmarshal(gconv.Bytes(value), &m.data)
|
||||
default:
|
||||
for k, v := range gconv.Map(value) {
|
||||
m.data[gconv.Int(k)] = v
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -409,3 +409,22 @@ func (m *IntIntMap) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (m *IntIntMap) UnmarshalValue(value interface{}) (err error) {
|
||||
if m.mu == nil {
|
||||
m.mu = rwmutex.New()
|
||||
m.data = make(map[int]int)
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
return json.Unmarshal(gconv.Bytes(value), &m.data)
|
||||
default:
|
||||
for k, v := range gconv.Map(value) {
|
||||
m.data[gconv.Int(k)] = gconv.Int(v)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -410,3 +410,22 @@ func (m *IntStrMap) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (m *IntStrMap) UnmarshalValue(value interface{}) (err error) {
|
||||
if m.mu == nil {
|
||||
m.mu = rwmutex.New()
|
||||
m.data = make(map[int]string)
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
return json.Unmarshal(gconv.Bytes(value), &m.data)
|
||||
default:
|
||||
for k, v := range gconv.Map(value) {
|
||||
m.data[gconv.Int(k)] = gconv.String(v)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -428,3 +428,15 @@ func (m *StrAnyMap) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (m *StrAnyMap) UnmarshalValue(value interface{}) (err error) {
|
||||
if m.mu == nil {
|
||||
m.mu = rwmutex.New()
|
||||
m.data = make(map[string]interface{})
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.data = gconv.Map(value)
|
||||
return
|
||||
}
|
||||
|
||||
@ -411,3 +411,22 @@ func (m *StrIntMap) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (m *StrIntMap) UnmarshalValue(value interface{}) (err error) {
|
||||
if m.mu == nil {
|
||||
m.mu = rwmutex.New()
|
||||
m.data = make(map[string]int)
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
return json.Unmarshal(gconv.Bytes(value), &m.data)
|
||||
default:
|
||||
for k, v := range gconv.Map(value) {
|
||||
m.data[k] = gconv.Int(v)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package gmap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/internal/empty"
|
||||
|
||||
@ -412,3 +413,14 @@ func (m *StrStrMap) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (m *StrStrMap) UnmarshalValue(value interface{}) (err error) {
|
||||
if m.mu == nil {
|
||||
m.mu = rwmutex.New()
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.data = gconv.MapStrStr(value)
|
||||
return
|
||||
}
|
||||
|
||||
@ -491,3 +491,22 @@ func (m *ListMap) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (m *ListMap) UnmarshalValue(value interface{}) (err error) {
|
||||
if m.mu == nil {
|
||||
m.mu = rwmutex.New()
|
||||
m.data = make(map[interface{}]*glist.Element)
|
||||
m.list = glist.New()
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for k, v := range gconv.Map(value) {
|
||||
if e, ok := m.data[k]; !ok {
|
||||
m.data[k] = m.list.PushBack(&gListMapNode{k, v})
|
||||
} else {
|
||||
e.Value = &gListMapNode{k, v}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -275,3 +275,39 @@ func Test_AnyAnyMap_Pops(t *testing.T) {
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAnyAnyMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.Map
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":"v1","k2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
@ -257,3 +258,39 @@ func Test_IntAnyMap_Pops(t *testing.T) {
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntAnyMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.IntAnyMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"1":"v1","2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "v1")
|
||||
gtest.Assert(t.Map.Get(2), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "v1")
|
||||
gtest.Assert(t.Map.Get(2), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
@ -260,3 +261,39 @@ func Test_IntIntMap_Pops(t *testing.T) {
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntIntMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.IntIntMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"1":1,"2":2}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "1")
|
||||
gtest.Assert(t.Map.Get(2), "2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.MapIntAny{
|
||||
1: 1,
|
||||
2: 2,
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "1")
|
||||
gtest.Assert(t.Map.Get(2), "2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
@ -261,3 +262,39 @@ func Test_IntStrMap_Pops(t *testing.T) {
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntStrMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.IntStrMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"1":"v1","2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "v1")
|
||||
gtest.Assert(t.Map.Get(2), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get(1), "v1")
|
||||
gtest.Assert(t.Map.Get(2), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -231,3 +231,39 @@ func Test_ListMap_Pops(t *testing.T) {
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestListMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.ListMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"1":"v1","2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("1"), "v1")
|
||||
gtest.Assert(t.Map.Get("2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.MapIntAny{
|
||||
1: "v1",
|
||||
2: "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("1"), "v1")
|
||||
gtest.Assert(t.Map.Get("2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
@ -269,3 +270,39 @@ func Test_StrAnyMap_Pops(t *testing.T) {
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrAnyMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.StrAnyMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":"v1","k2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
@ -272,3 +273,39 @@ func Test_StrIntMap_Pops(t *testing.T) {
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrIntMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.StrIntMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":1,"k2":2}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), 1)
|
||||
gtest.Assert(t.Map.Get("k2"), 2)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": 1,
|
||||
"k2": 2,
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), 1)
|
||||
gtest.Assert(t.Map.Get("k2"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
@ -269,3 +270,39 @@ func Test_StrStrMap_Pops(t *testing.T) {
|
||||
gtest.Assert(vArray.Unique().Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrStrMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.StrStrMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":"v1","k2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -148,3 +148,39 @@ func Test_TreeMap_Json(t *testing.T) {
|
||||
gtest.Assert(m.Get("k2"), data["k2"])
|
||||
})
|
||||
}
|
||||
|
||||
func TestTreeMap_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Map *gmap.TreeMap
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": []byte(`{"k1":"v1","k2":"v2"}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"map": g.Map{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Map.Size(), 2)
|
||||
gtest.Assert(t.Map.Get("k1"), "v1")
|
||||
gtest.Assert(t.Map.Get("k2"), "v2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ func (p *Pool) Put(value interface{}) {
|
||||
if p.Expire == 0 {
|
||||
item.expire = 0
|
||||
} else {
|
||||
item.expire = gtime.Millisecond() + p.Expire
|
||||
item.expire = gtime.TimestampMilli() + p.Expire
|
||||
}
|
||||
p.list.PushBack(item)
|
||||
}
|
||||
@ -86,7 +86,7 @@ func (p *Pool) Get() (interface{}, error) {
|
||||
for !p.closed.Val() {
|
||||
if r := p.list.PopFront(); r != nil {
|
||||
f := r.(*poolItem)
|
||||
if f.expire == 0 || f.expire > gtime.Millisecond() {
|
||||
if f.expire == 0 || f.expire > gtime.TimestampMilli() {
|
||||
return f.value, nil
|
||||
}
|
||||
} else {
|
||||
@ -130,7 +130,7 @@ func (p *Pool) checkExpire() {
|
||||
// TODO Do not use Pop and Push mechanism, which is not graceful.
|
||||
if r := p.list.PopFront(); r != nil {
|
||||
item := r.(*poolItem)
|
||||
if item.expire == 0 || item.expire > gtime.Millisecond() {
|
||||
if item.expire == 0 || item.expire > gtime.TimestampMilli() {
|
||||
p.list.PushFront(item)
|
||||
break
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ func BenchmarkSyncPoolPut(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGpoolGet(b *testing.B) {
|
||||
func BenchmarkSyncPoolGet(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
syncp.Get()
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gqueue provides a dynamic/static concurrent-safe queue.
|
||||
// Package gqueue provides dynamic/static concurrent-safe queue.
|
||||
//
|
||||
// Features:
|
||||
//
|
||||
@ -25,6 +25,7 @@ import (
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
)
|
||||
|
||||
// Queue is a concurrent-safe queue built on doubly linked list and channel.
|
||||
type Queue struct {
|
||||
limit int // Limit for queue size.
|
||||
list *glist.List // Underlying list structure for data maintaining.
|
||||
@ -54,14 +55,14 @@ func New(limit ...int) *Queue {
|
||||
q.list = glist.New(true)
|
||||
q.events = make(chan struct{}, math.MaxInt32)
|
||||
q.C = make(chan interface{}, gDEFAULT_QUEUE_SIZE)
|
||||
go q.startAsyncLoop()
|
||||
go q.asyncLoopFromListToChannel()
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
// startAsyncLoop starts an asynchronous goroutine,
|
||||
// asyncLoopFromListToChannel starts an asynchronous goroutine,
|
||||
// which handles the data synchronization from list <q.list> to channel <q.C>.
|
||||
func (q *Queue) startAsyncLoop() {
|
||||
func (q *Queue) asyncLoopFromListToChannel() {
|
||||
defer func() {
|
||||
if q.closed.Val() {
|
||||
_ = recover()
|
||||
|
||||
@ -430,3 +430,24 @@ func (set *Set) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for set.
|
||||
func (set *Set) UnmarshalValue(value interface{}) (err error) {
|
||||
if set.mu == nil {
|
||||
set.mu = rwmutex.New()
|
||||
set.data = make(map[interface{}]struct{})
|
||||
}
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
var array []interface{}
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
err = json.Unmarshal(gconv.Bytes(value), &array)
|
||||
default:
|
||||
array = gconv.SliceAny(value)
|
||||
}
|
||||
for _, v := range array {
|
||||
set.data[v] = struct{}{}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -401,3 +401,24 @@ func (set *IntSet) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for set.
|
||||
func (set *IntSet) UnmarshalValue(value interface{}) (err error) {
|
||||
if set.mu == nil {
|
||||
set.mu = rwmutex.New()
|
||||
set.data = make(map[int]struct{})
|
||||
}
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
var array []int
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
err = json.Unmarshal(gconv.Bytes(value), &array)
|
||||
default:
|
||||
array = gconv.SliceInt(value)
|
||||
}
|
||||
for _, v := range array {
|
||||
set.data[v] = struct{}{}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -417,3 +417,24 @@ func (set *StrSet) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for set.
|
||||
func (set *StrSet) UnmarshalValue(value interface{}) (err error) {
|
||||
if set.mu == nil {
|
||||
set.mu = rwmutex.New()
|
||||
set.data = make(map[string]struct{})
|
||||
}
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
var array []string
|
||||
switch value.(type) {
|
||||
case string, []byte:
|
||||
err = json.Unmarshal(gconv.Bytes(value), &array)
|
||||
default:
|
||||
array = gconv.SliceStr(value)
|
||||
}
|
||||
for _, v := range array {
|
||||
set.data[v] = struct{}{}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -10,6 +10,8 @@ package gset_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
@ -361,3 +363,40 @@ func TestSet_AddIfNotExistFunc(t *testing.T) {
|
||||
gtest.Assert(s.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Set *gset.Set
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": []byte(`["k1","k2","k3"]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains("k1"), true)
|
||||
gtest.Assert(t.Set.Contains("k2"), true)
|
||||
gtest.Assert(t.Set.Contains("k3"), true)
|
||||
gtest.Assert(t.Set.Contains("k4"), false)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": g.Slice{"k1", "k2", "k3"},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains("k1"), true)
|
||||
gtest.Assert(t.Set.Contains("k2"), true)
|
||||
gtest.Assert(t.Set.Contains("k3"), true)
|
||||
gtest.Assert(t.Set.Contains("k4"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,8 @@ package gset_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -324,3 +326,40 @@ func TestIntSet_AddIfNotExistFunc(t *testing.T) {
|
||||
gtest.Assert(s.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Set *gset.IntSet
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": []byte(`[1,2,3]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains(1), true)
|
||||
gtest.Assert(t.Set.Contains(2), true)
|
||||
gtest.Assert(t.Set.Contains(3), true)
|
||||
gtest.Assert(t.Set.Contains(4), false)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": g.Slice{1, 2, 3},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains(1), true)
|
||||
gtest.Assert(t.Set.Contains(2), true)
|
||||
gtest.Assert(t.Set.Contains(3), true)
|
||||
gtest.Assert(t.Set.Contains(4), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,6 +10,8 @@ package gset_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -360,3 +362,40 @@ func TestStrSet_AddIfNotExistFunc(t *testing.T) {
|
||||
gtest.Assert(s.Contains("4"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Set *gset.StrSet
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": []byte(`["1","2","3"]`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains("1"), true)
|
||||
gtest.Assert(t.Set.Contains("2"), true)
|
||||
gtest.Assert(t.Set.Contains("3"), true)
|
||||
gtest.Assert(t.Set.Contains("4"), false)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"set": g.SliceStr{"1", "2", "3"},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Set.Size(), 3)
|
||||
gtest.Assert(t.Set.Contains("1"), true)
|
||||
gtest.Assert(t.Set.Contains("2"), true)
|
||||
gtest.Assert(t.Set.Contains("3"), true)
|
||||
gtest.Assert(t.Set.Contains("4"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -942,3 +942,17 @@ func (tree *RedBlackTree) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for map.
|
||||
func (tree *RedBlackTree) UnmarshalValue(value interface{}) (err error) {
|
||||
if tree.mu == nil {
|
||||
tree.mu = rwmutex.New()
|
||||
tree.comparator = gutil.ComparatorString
|
||||
}
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
for k, v := range gconv.Map(value) {
|
||||
tree.doSet(k, v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ func (v *Bool) Val() bool {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Bool) Cas(old, new bool) bool {
|
||||
func (v *Bool) Cas(old, new bool) (swapped bool) {
|
||||
var oldInt32, newInt32 int32
|
||||
if old {
|
||||
oldInt32 = 1
|
||||
@ -90,3 +90,9 @@ func (v *Bool) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Bool(bytes.Trim(b, `"`)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Bool) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Bool(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func (v *Byte) Add(delta byte) (new byte) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Byte) Cas(old, new byte) bool {
|
||||
func (v *Byte) Cas(old, new byte) (swapped bool) {
|
||||
return atomic.CompareAndSwapInt32(&v.value, int32(old), int32(new))
|
||||
}
|
||||
|
||||
@ -68,3 +68,9 @@ func (v *Byte) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Uint8(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Byte) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Byte(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -72,3 +72,9 @@ func (v *Bytes) UnmarshalJSON(b []byte) error {
|
||||
v.Set(src[:n])
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Bytes) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Bytes(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ func (v *Float32) Add(delta float32) (new float32) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Float32) Cas(old, new float32) bool {
|
||||
func (v *Float32) Cas(old, new float32) (swapped bool) {
|
||||
return atomic.CompareAndSwapUint32(&v.value, math.Float32bits(old), math.Float32bits(new))
|
||||
}
|
||||
|
||||
@ -81,3 +81,9 @@ func (v *Float32) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Float32(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Float32) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Float32(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ func (v *Float64) Add(delta float64) (new float64) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Float64) Cas(old, new float64) bool {
|
||||
func (v *Float64) Cas(old, new float64) (swapped bool) {
|
||||
return atomic.CompareAndSwapUint64(&v.value, math.Float64bits(old), math.Float64bits(new))
|
||||
}
|
||||
|
||||
@ -81,3 +81,9 @@ func (v *Float64) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Float64(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Float64) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Float64(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func (v *Int) Add(delta int) (new int) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Int) Cas(old, new int) bool {
|
||||
func (v *Int) Cas(old, new int) (swapped bool) {
|
||||
return atomic.CompareAndSwapInt64(&v.value, int64(old), int64(new))
|
||||
}
|
||||
|
||||
@ -68,3 +68,9 @@ func (v *Int) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Int(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Int) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Int(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func (v *Int32) Add(delta int32) (new int32) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Int32) Cas(old, new int32) bool {
|
||||
func (v *Int32) Cas(old, new int32) (swapped bool) {
|
||||
return atomic.CompareAndSwapInt32(&v.value, old, new)
|
||||
}
|
||||
|
||||
@ -68,3 +68,9 @@ func (v *Int32) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Int32(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Int32) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Int32(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func (v *Int64) Add(delta int64) (new int64) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Int64) Cas(old, new int64) bool {
|
||||
func (v *Int64) Cas(old, new int64) (swapped bool) {
|
||||
return atomic.CompareAndSwapInt64(&v.value, old, new)
|
||||
}
|
||||
|
||||
@ -68,3 +68,9 @@ func (v *Int64) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Int64(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Int64) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Int64(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -65,3 +65,9 @@ func (v *Interface) UnmarshalJSON(b []byte) error {
|
||||
v.Set(i)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Interface) UnmarshalValue(value interface{}) error {
|
||||
v.Set(value)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -63,3 +63,9 @@ func (v *String) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.UnsafeBytesToStr(bytes.Trim(b, `"`)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *String) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.String(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func (v *Uint) Add(delta uint) (new uint) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Uint) Cas(old, new uint) bool {
|
||||
func (v *Uint) Cas(old, new uint) (swapped bool) {
|
||||
return atomic.CompareAndSwapUint64(&v.value, uint64(old), uint64(new))
|
||||
}
|
||||
|
||||
@ -68,3 +68,9 @@ func (v *Uint) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Uint(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Uint) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Uint(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func (v *Uint32) Add(delta uint32) (new uint32) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Uint32) Cas(old, new uint32) bool {
|
||||
func (v *Uint32) Cas(old, new uint32) (swapped bool) {
|
||||
return atomic.CompareAndSwapUint32(&v.value, old, new)
|
||||
}
|
||||
|
||||
@ -68,3 +68,9 @@ func (v *Uint32) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Uint32(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Uint32) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Uint32(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ func (v *Uint64) Add(delta uint64) (new uint64) {
|
||||
}
|
||||
|
||||
// Cas executes the compare-and-swap operation for value.
|
||||
func (v *Uint64) Cas(old, new uint64) bool {
|
||||
func (v *Uint64) Cas(old, new uint64) (swapped bool) {
|
||||
return atomic.CompareAndSwapUint64(&v.value, old, new)
|
||||
}
|
||||
|
||||
@ -68,3 +68,9 @@ func (v *Uint64) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Uint64(gconv.UnsafeBytesToStr(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
func (v *Uint64) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Uint64(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -49,6 +49,12 @@ func BenchmarkInt_Add(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkInt_Cas(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
it.Cas(i, i)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkInt32_Set(b *testing.B) {
|
||||
for i := int32(0); i < int32(b.N); i++ {
|
||||
it32.Set(i)
|
||||
|
||||
125
container/gtype/z_unit_bool_test.go
Normal file
125
container/gtype/z_unit_bool_test.go
Normal file
@ -0,0 +1,125 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). 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 gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Bool(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
i := gtype.NewBool(true)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(false), true)
|
||||
gtest.AssertEQ(iClone.Val(), false)
|
||||
|
||||
i1 := gtype.NewBool(false)
|
||||
iClone1 := i1.Clone()
|
||||
gtest.AssertEQ(iClone1.Set(true), false)
|
||||
gtest.AssertEQ(iClone1.Val(), true)
|
||||
|
||||
//空参测试
|
||||
i2 := gtype.NewBool()
|
||||
gtest.AssertEQ(i2.Val(), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bool_JSON(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.Case(t, func() {
|
||||
i := gtype.NewBool(true)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
i := gtype.NewBool(false)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
var err error
|
||||
i := gtype.NewBool()
|
||||
err = json.Unmarshal([]byte("true"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), true)
|
||||
err = json.Unmarshal([]byte("false"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), false)
|
||||
err = json.Unmarshal([]byte("1"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), true)
|
||||
err = json.Unmarshal([]byte("0"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
i := gtype.NewBool(true)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewBool()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), i.Val())
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
i := gtype.NewBool(false)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewBool()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), i.Val())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bool_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Var *gtype.Bool
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "true",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), true)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "false",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), false)
|
||||
})
|
||||
}
|
||||
77
container/gtype/z_unit_byte_test.go
Normal file
77
container/gtype/z_unit_byte_test.go
Normal file
@ -0,0 +1,77 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). 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 gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Byte(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var wg sync.WaitGroup
|
||||
addTimes := 127
|
||||
i := gtype.NewByte(byte(0))
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(byte(1)), byte(0))
|
||||
gtest.AssertEQ(iClone.Val(), byte(1))
|
||||
for index := 0; index < addTimes; index++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
i.Add(1)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
gtest.AssertEQ(byte(addTimes), i.Val())
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewByte()
|
||||
gtest.AssertEQ(i1.Val(), byte(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Byte_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
i := gtype.NewByte(49)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.Case(t, func() {
|
||||
var err error
|
||||
i := gtype.NewByte()
|
||||
err = json.Unmarshal([]byte("49"), &i)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i.Val(), "49")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Byte_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Var *gtype.Byte
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "2",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "2")
|
||||
})
|
||||
}
|
||||
63
container/gtype/z_unit_bytes_test.go
Normal file
63
container/gtype/z_unit_bytes_test.go
Normal file
@ -0,0 +1,63 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). 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 gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Bytes(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
i := gtype.NewBytes([]byte("abc"))
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set([]byte("123")), []byte("abc"))
|
||||
gtest.AssertEQ(iClone.Val(), []byte("123"))
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewBytes()
|
||||
gtest.AssertEQ(i1.Val(), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bytes_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
b := []byte("i love gf")
|
||||
i := gtype.NewBytes(b)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewBytes()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), b)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Bytes_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Var *gtype.Bytes
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123")
|
||||
})
|
||||
}
|
||||
64
container/gtype/z_unit_float32_test.go
Normal file
64
container/gtype/z_unit_float32_test.go
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). 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 gtype_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Float32(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
i := gtype.NewFloat32(0)
|
||||
iClone := i.Clone()
|
||||
gtest.AssertEQ(iClone.Set(0.1), float32(0))
|
||||
gtest.AssertEQ(iClone.Val(), float32(0.1))
|
||||
|
||||
//空参测试
|
||||
i1 := gtype.NewFloat32()
|
||||
gtest.AssertEQ(i1.Val(), float32(0))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Float32_JSON(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
v := float32(math.MaxFloat32)
|
||||
i := gtype.NewFloat32(v)
|
||||
b1, err1 := json.Marshal(i)
|
||||
b2, err2 := json.Marshal(i.Val())
|
||||
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.Assert(b1, b2)
|
||||
|
||||
i2 := gtype.NewFloat32()
|
||||
err := json.Unmarshal(b2, &i2)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(i2.Val(), v)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Float32_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Var *gtype.Float32
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(map[string]interface{}{
|
||||
"name": "john",
|
||||
"var": "123.456",
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Var.Val(), "123.456")
|
||||
})
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user