mirror of
https://gitee.com/johng/gf
synced 2026-06-08 18:47:45 +08:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 02e467fb57 | |||
| ec994f3080 | |||
| 1181ab499c | |||
| 5424935fd9 | |||
| b9fbfb91bd | |||
| 62f66b4fec | |||
| eef9da9a41 | |||
| 7d32be3b6c | |||
| c2ad9f5fb9 | |||
| 4847fecdaa | |||
| f75383e0c5 | |||
| f683dccb6f | |||
| 6fb106b618 | |||
| 97956ad903 | |||
| 5d72a5b5ae | |||
| 1d7ded562c |
34
.example/database/gdb/mysql/gdb_update_field.go
Normal file
34
.example/database/gdb/mysql/gdb_update_field.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
table := "medicine_clinics_upload_yinchuan"
|
||||
list, err := db.Table(table).All()
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
panic(err)
|
||||
}
|
||||
content := ""
|
||||
for _, item := range list {
|
||||
if j, err := gjson.DecodeToJson(item["upload_data"].String()); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
s, _ := j.ToJsonIndentString()
|
||||
content += item["id"].String() + "\t" + item["medicine_clinic_id"].String() + "\t"
|
||||
content += s
|
||||
content += "\n\n"
|
||||
//if _, err := db.Table(table).Data("data_decode", s).Where("id", item["id"].Int()).Update(); err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
}
|
||||
}
|
||||
gfile.PutContents("/Users/john/Temp/medicine_clinics_upload_yinchuan.txt", content)
|
||||
}
|
||||
@ -8,7 +8,6 @@ import (
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("test").Where("id IN (?)", []interface{}{1, 2}).All()
|
||||
|
||||
44
.example/encoding/gparser/config.yaml
Normal file
44
.example/encoding/gparser/config.yaml
Normal file
@ -0,0 +1,44 @@
|
||||
|
||||
broker:
|
||||
ip: "127.0.0.1"
|
||||
tcpport: "4222"
|
||||
httpport: "8222"
|
||||
user: "alfxnats"
|
||||
pwd: "alfxnats"
|
||||
clusterid: "alfxnats"
|
||||
database:
|
||||
ip: "110.1.1.227"
|
||||
port: "27017"
|
||||
user: "alfxdev"
|
||||
pwd: "alfxdev"
|
||||
dbname: "alfxdev"
|
||||
scheduler:
|
||||
a2rparallel: 4
|
||||
sleeptime: 300
|
||||
worker:
|
||||
name: "worker1"
|
||||
domains:
|
||||
officenet: 3
|
||||
producnet: 3
|
||||
free: 3
|
||||
temppath: "/home/alfx/proc"
|
||||
common:
|
||||
filepath: "/home/alfx/file"
|
||||
logpath: "home/alfx/log"
|
||||
logdebug: true
|
||||
logtrace: true
|
||||
report:
|
||||
localip: "127.0.0.1"
|
||||
localport: ""
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
77
.example/encoding/gparser/gparser_yaml_issue336.go
Normal file
77
.example/encoding/gparser/gparser_yaml_issue336.go
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/encoding/gparser"
|
||||
)
|
||||
|
||||
type Conf struct {
|
||||
Broker Broker
|
||||
Database Database
|
||||
Scheduler Scheduler
|
||||
Worker Worker
|
||||
Common Common
|
||||
Report Report
|
||||
}
|
||||
|
||||
type Broker struct {
|
||||
Ip string
|
||||
Tcport string
|
||||
Httpport string
|
||||
User string
|
||||
Pwd string
|
||||
Clusterid string
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
Ip string
|
||||
Port string
|
||||
User string
|
||||
Pwd string
|
||||
Dbname string
|
||||
}
|
||||
|
||||
type Scheduler struct {
|
||||
SleepTime int
|
||||
Pidfilepath string
|
||||
A2rparallel int
|
||||
}
|
||||
|
||||
type Worker struct {
|
||||
Name string
|
||||
Domains map[string]interface{}
|
||||
Temppath string
|
||||
Pidfilepath string
|
||||
}
|
||||
|
||||
type Common struct {
|
||||
Filepath string
|
||||
Logpath string
|
||||
Logdebug bool
|
||||
Logtrace bool
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
Localip string
|
||||
Localport string //暂不启用
|
||||
}
|
||||
|
||||
func main() {
|
||||
_, err := gparser.Load("config.yaml")
|
||||
if err != nil {
|
||||
fmt.Println("oops,read config.yaml err:", err)
|
||||
}
|
||||
|
||||
//fmt.Println("yaml.v3读取yaml文件")
|
||||
//f, err := os.Open("config.yaml")
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//var conf Conf
|
||||
//err = yaml.NewDecoder(f).Decode(&conf)
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//fmt.Println(conf)
|
||||
}
|
||||
16
.example/net/ghttp/server/form/form.go
Normal file
16
.example/net/ghttp/server/form/form.go
Normal file
@ -0,0 +1,16 @@
|
||||
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) {
|
||||
g.Dump(r.GetPostMap())
|
||||
r.Response.WriteTpl("form.html")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
30
.example/net/ghttp/server/form/form.html
Normal file
30
.example/net/ghttp/server/form/form.html
Normal file
@ -0,0 +1,30 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>form test</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>form1</h1>
|
||||
<form action="?" method="post" enctype="multipart/form-data">
|
||||
<p><input type="text" name="input1" value="1"/></p>
|
||||
<p><input type="text" name="input2" value="2"/></p>
|
||||
<p><input type="text" name="array1" value="3"/></p>
|
||||
<p><input type="text" name="array1" value="4"/></p>
|
||||
<p><input type="text" name="array2[]" value="5"/></p>
|
||||
<p><input type="text" name="array2[]" value="6"/></p>
|
||||
<p><input type="text" name="map[a]" value="7"/></p>
|
||||
<p><input type="text" name="map[b]" value="8"/></p>
|
||||
<p><input type="password" name="password1" value="9"/></p>
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
|
||||
<h1>form2</h1>
|
||||
<form action="?" method="post" enctype="application/x-www-form-urlencoded">
|
||||
<p><input type="text" name="input[a]" value="1"/></p>
|
||||
<p><input type="text" name="input[b]" value="2"/></p>
|
||||
<p><input type="text" name="array" value="3"/></p>
|
||||
<p><input type="text" name="array" value="4"/></p>
|
||||
<p><input type="password" name="password2" value="5"/></p>
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
</body>
|
||||
41
.example/net/ghttp/server/middleware/auth_exception.go
Normal file
41
.example/net/ghttp/server/middleware/auth_exception.go
Normal file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareAuth(r *ghttp.Request) {
|
||||
token := r.Get("token")
|
||||
if token == "123456" {
|
||||
r.Middleware.Next()
|
||||
} else {
|
||||
r.Response.WriteStatus(http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/admin", func(g *ghttp.RouterGroup) {
|
||||
g.MiddlewarePattern("/*action", func(r *ghttp.Request) {
|
||||
if action := r.GetRouterString("action"); action != "" {
|
||||
switch action {
|
||||
case "login":
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
MiddlewareAuth(r)
|
||||
})
|
||||
g.ALL("/login", func(r *ghttp.Request) {
|
||||
r.Response.Write("login")
|
||||
})
|
||||
g.ALL("/dashboard", func(r *ghttp.Request) {
|
||||
r.Response.Write("dashboard")
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
42
.example/net/ghttp/server/middleware/error_handling.go
Normal file
42
.example/net/ghttp/server/middleware/error_handling.go
Normal file
@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareAuth(r *ghttp.Request) {
|
||||
token := r.Get("token")
|
||||
if token == "123456" {
|
||||
r.Middleware.Next()
|
||||
} else {
|
||||
r.Response.WriteStatus(http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func MiddlewareError(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
if r.Response.Status >= http.StatusInternalServerError {
|
||||
r.Response.ClearBuffer()
|
||||
r.Response.Write("Internal error occurred, please try again later.")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareAuth, MiddlewareCORS, MiddlewareError)
|
||||
g.ALL("/user/list", func(r *ghttp.Request) {
|
||||
panic("db error: sql is xxxxxxx")
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
"github.com/gogf/gf/os/gcmd"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
file := "xxx/github.com/hg-hh/ww/gf/.example/"
|
||||
fmt.Println(gregex.IsMatchString(`/github.com/[^/]+/gf/\.example/`, file))
|
||||
glog.SetFlags(glog.F_TIME_DATE | glog.F_TIME_TIME | glog.F_FILE_SHORT)
|
||||
glog.Debug("dd")
|
||||
glog.Println("timeout", gcmd.GetOpt("timeout"))
|
||||
}
|
||||
|
||||
31
RELEASE.2.MD
31
RELEASE.2.MD
@ -1,30 +1,18 @@
|
||||
# `v1.9.0`
|
||||
|
||||
该版本实际为`v2.0.0`的大版本发布,为避免`go module`机制严格要求`v2`版本以上需要修改`import`并加上`v2`后缀,因此使用了`v1.9.0`进行发布。
|
||||
|
||||
## 新特性
|
||||
|
||||
1. 增加`gf`命令行开发辅助工具:https://goframe.org/toolchain/cli
|
||||
- 支持`GF`框架下载更新;
|
||||
- 支持初始化新建项目命令;
|
||||
- 支持跨平台交叉编译命令;
|
||||
- 命令行工具支持自动更新命令;
|
||||
- 支持二进制文件打包,生成二进制文件或者Go程序文件;
|
||||
- 支持指定数据库生成数据表模型,支持本地配置文件读取数据库配置;
|
||||
1. 新增`gi18n`国际化管理模块:https://goframe.org/i18n/gi18n/index
|
||||
1. 新增`gf`命令行开发辅助工具:https://goframe.org/toolchain/cli
|
||||
1. 新增`gres`资源管理器模块:https://goframe.org/os/gres/index
|
||||
- 资源管理器支持虚拟的文件/目录操作方法;
|
||||
- 默认整合支持到了WebServer、配置管理、模板引擎中;
|
||||
- 可将任意的文件打包为`Go`内容,支持开发者自定义加解密;
|
||||
- 任意文件如网站静态文件、配置文件等可编译到二进制文件中,也可编译到发布的可执行文件中;
|
||||
- 开发者可只需编译发布一个可执行文件,除了方便了软件分发,也为保护软件知识产权内容提供了可能;
|
||||
1. 新增`gini`模块:https://goframe.org/encoding/gini/index
|
||||
- 支持`ini`文件的读取/生成;
|
||||
- 同时配置管理模块也增加了对`ini`文件的支持;
|
||||
- 配置管理模块目前自动识别支持`ini/xml/json/toml/yaml`五种数据格式;
|
||||
1. `Session`功能重构,新增`gsession`模块,`WebServer`默认使用文件存储`Session`:https://goframe.org/net/ghttp/session
|
||||
1. 重构`Session`功能,新增`gsession`模块,`WebServer`默认使用文件存储`Session`:https://goframe.org/net/ghttp/session
|
||||
1. `WebServer`新增中间件特性,并保留原有的HOOK设计,两者都可实现请求拦截、预处理等等特性:https://goframe.org/net/ghttp/router/middleware
|
||||
1. 新增`gi18n`国际化管理模块:https://goframe.org/i18n/gi18n/index
|
||||
1. 新增`gini`模块:https://goframe.org/encoding/gini/index
|
||||
1. `WebServer`新增更便捷的层级路由注册方式:https://goframe.org/net/ghttp/group/level
|
||||
1. `gcmd`命令行参数解析模块重构,增加`Parser`解析对象;
|
||||
1. 新增`gdebug`模块,用于堆栈获取/打印;
|
||||
1. `gcmd`命令行参数解析模块重构,增加`Parser`解析对象:https://goframe.org/os/gcmd/index
|
||||
1. 新增`gdebug`模块,用于堆栈信息获取/打印:https://goframe.org/debug/gdebug/index
|
||||
|
||||
|
||||
## 重大调整
|
||||
@ -40,6 +28,7 @@
|
||||
## 功能改进
|
||||
|
||||
1. `ghttp`
|
||||
- 改进`Request`参数解析方式:https://goframe.org/net/ghttp/request
|
||||
- `Cookie`及`Session`的`TTL`配置数据类型修改为`time.Duration`;
|
||||
- 新增允许同时通过`Header/Cookie`传递`SessionId`;
|
||||
- 新增`ConfigFromMap/SetConfigWithMap`方法,支持通过`map`参数设置WebServer;
|
||||
@ -53,7 +42,7 @@
|
||||
- 增加`SetLogger`方法用于开发者自定义数据库的日志打印;
|
||||
- 增加`Master/Slave`方法,开发者可自主选择数据库操作执行的主从节点;
|
||||
- 增加对`mssql/pgsql/oracle`的单元测试;
|
||||
- `debug`模式支持完整带参数整合的SQL语句调试打印;
|
||||
- 在`debug`模式支持完整带参数整合的SQL语句调试打印;
|
||||
- 增加了更多的功能方法;
|
||||
1. `glog`
|
||||
- 新增`Default`方法用于获取默认的`Logger`对象;
|
||||
|
||||
@ -68,6 +68,17 @@ func (m *AnyAnyMap) Map() map[interface{}]interface{} {
|
||||
return data
|
||||
}
|
||||
|
||||
// MapStrAny returns a copy of the data of the map as map[string]interface{}.
|
||||
func (m *AnyAnyMap) MapStrAny() map[string]interface{} {
|
||||
m.mu.RLock()
|
||||
data := make(map[string]interface{}, len(m.data))
|
||||
for k, v := range m.data {
|
||||
data[gconv.String(k)] = v
|
||||
}
|
||||
m.mu.RUnlock()
|
||||
return data
|
||||
}
|
||||
|
||||
// Set sets key-value to the hash map.
|
||||
func (m *AnyAnyMap) Set(key interface{}, val interface{}) {
|
||||
m.mu.Lock()
|
||||
|
||||
@ -104,6 +104,20 @@ func (m *ListMap) Map() map[interface{}]interface{} {
|
||||
return data
|
||||
}
|
||||
|
||||
// MapStrAny returns a copy of the data of the map as map[string]interface{}.
|
||||
func (m *ListMap) MapStrAny() map[string]interface{} {
|
||||
m.mu.RLock()
|
||||
node := (*gListMapNode)(nil)
|
||||
data := make(map[string]interface{}, len(m.data))
|
||||
m.list.IteratorAsc(func(e *glist.Element) bool {
|
||||
node = e.Value.(*gListMapNode)
|
||||
data[gconv.String(node.key)] = node.value
|
||||
return true
|
||||
})
|
||||
m.mu.RUnlock()
|
||||
return data
|
||||
}
|
||||
|
||||
// Set sets key-value to the map.
|
||||
func (m *ListMap) Set(key interface{}, value interface{}) {
|
||||
m.mu.Lock()
|
||||
|
||||
@ -10,6 +10,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
)
|
||||
@ -408,6 +410,16 @@ func (tree *AVLTree) Map() map[interface{}]interface{} {
|
||||
return m
|
||||
}
|
||||
|
||||
// MapStrAny returns all key-value items as map[string]interface{}.
|
||||
func (tree *AVLTree) MapStrAny() map[string]interface{} {
|
||||
m := make(map[string]interface{}, tree.Size())
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
m[gconv.String(key)] = value
|
||||
return true
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
// Flip exchanges key-value of the tree to value-key.
|
||||
// Note that you should guarantee the value is the same type as key,
|
||||
// or else the comparator would panic.
|
||||
|
||||
@ -12,6 +12,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
)
|
||||
@ -302,6 +304,16 @@ func (tree *BTree) Map() map[interface{}]interface{} {
|
||||
return m
|
||||
}
|
||||
|
||||
// MapStrAny returns all key-value items as map[string]interface{}.
|
||||
func (tree *BTree) MapStrAny() map[string]interface{} {
|
||||
m := make(map[string]interface{}, tree.Size())
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
m[gconv.String(key)] = value
|
||||
return true
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
// Clear removes all nodes from the tree.
|
||||
func (tree *BTree) Clear() {
|
||||
tree.mu.Lock()
|
||||
|
||||
@ -10,6 +10,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
)
|
||||
@ -345,6 +347,16 @@ func (tree *RedBlackTree) Map() map[interface{}]interface{} {
|
||||
return m
|
||||
}
|
||||
|
||||
// MapStrAny returns all key-value items as map[string]interface{}.
|
||||
func (tree *RedBlackTree) MapStrAny() map[string]interface{} {
|
||||
m := make(map[string]interface{}, tree.Size())
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
m[gconv.String(key)] = value
|
||||
return true
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
// Left returns the left-most (min) node or nil if tree is empty.
|
||||
func (tree *RedBlackTree) Left() *RedBlackTreeNode {
|
||||
tree.mu.RLock()
|
||||
|
||||
@ -249,26 +249,26 @@ func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) (er
|
||||
return gconv.StructsDeep(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
// MapStruct converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMap converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of struct/*struct.
|
||||
func (v *Var) MapStruct(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapStruct(v.Val(), pointer, mapping...)
|
||||
func (v *Var) MapToMap(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapToMap(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
// MapStructDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMapDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of struct/*struct.
|
||||
func (v *Var) MapStructDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapStructDeep(v.Val(), pointer, mapping...)
|
||||
func (v *Var) MapToMapDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapToMapDeep(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
// MapStructs converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMaps converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []struct/[]*struct.
|
||||
func (v *Var) MapStructs(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapStructs(v.Val(), pointer, mapping...)
|
||||
func (v *Var) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapToMaps(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
// MapStructsDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMapsDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []struct/[]*struct.
|
||||
func (v *Var) MapStructsDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapStructsDeep(v.Val(), pointer, mapping...)
|
||||
func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapToMapsDeep(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ import (
|
||||
|
||||
const (
|
||||
gDEFAULT_DEBUG_SQL_LENGTH = 1000
|
||||
gPATH_FILTER_KEY = "/gf/database/gdb/gdb"
|
||||
gPATH_FILTER_KEY = "/database/gdb/gdb"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -17,7 +17,7 @@ import (
|
||||
|
||||
const (
|
||||
gMAX_DEPTH = 1000
|
||||
gFILTER_KEY = "/gf/debug/gdebug/gdebug.go"
|
||||
gFILTER_KEY = "/debug/gdebug/gdebug.go"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -296,20 +296,20 @@ func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...ma
|
||||
return gconv.StructsDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) GetMapStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapStruct(j.Get(pattern), pointer, mapping...)
|
||||
func (j *Json) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMap(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) GetMapStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapStructDeep(j.Get(pattern), pointer, mapping...)
|
||||
func (j *Json) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMapDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) GetMapStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapStructs(j.Get(pattern), pointer, mapping...)
|
||||
func (j *Json) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMaps(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) GetMapStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapStructsDeep(j.Get(pattern), pointer, mapping...)
|
||||
func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMapsDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToMap converts current Json object to map[string]interface{}.
|
||||
@ -354,28 +354,28 @@ func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string)
|
||||
return gconv.StructsDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) ToMapStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapStruct(*(j.p), pointer, mapping...)
|
||||
return gconv.MapToMap(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) ToMapStructDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapStructDeep(*(j.p), pointer, mapping...)
|
||||
return gconv.MapToMapDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) ToMapStructs(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapStructs(*(j.p), pointer, mapping...)
|
||||
return gconv.MapToMaps(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) ToMapStructsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapStructsDeep(*(j.p), pointer, mapping...)
|
||||
return gconv.MapToMapsDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// Dump prints current Json object with more manually readable.
|
||||
|
||||
@ -12,7 +12,7 @@ import (
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
yaml3 "gopkg.in/yaml.v3"
|
||||
yaml3 "github.com/gf-third/yaml/v3"
|
||||
)
|
||||
|
||||
func Encode(v interface{}) ([]byte, error) {
|
||||
@ -24,7 +24,7 @@ func Decode(v []byte) (interface{}, error) {
|
||||
if err := yaml3.Unmarshal(v, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gconv.Map(result), nil
|
||||
return gconv.MapDeep(result), nil
|
||||
}
|
||||
|
||||
func DecodeTo(v []byte, result interface{}) error {
|
||||
|
||||
@ -22,7 +22,7 @@ type Error struct {
|
||||
}
|
||||
|
||||
const (
|
||||
gFILTER_KEY = "/gf/errors/gerror/gerror"
|
||||
gFILTER_KEY = "/errors/gerror/gerror"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
7
go.mod
7
go.mod
@ -6,15 +6,16 @@ require (
|
||||
github.com/fatih/structs v1.1.0
|
||||
github.com/fsnotify/fsnotify v1.4.7
|
||||
github.com/gf-third/mysql v1.4.2
|
||||
github.com/gf-third/yaml/v3 v3.0.0
|
||||
github.com/gofrs/flock v0.7.1 // indirect
|
||||
github.com/gomodule/redigo v2.0.0+incompatible
|
||||
github.com/gorilla/websocket v1.4.1
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190424092004-025bd760b278
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190916062342-6f856a90d556
|
||||
github.com/mattn/go-runewidth v0.0.4 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.1
|
||||
github.com/theckman/go-flock v0.7.1
|
||||
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b // indirect
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 // indirect
|
||||
golang.org/x/text v0.3.2
|
||||
google.golang.org/appengine v1.6.2 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20190905181640-827449938966
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
)
|
||||
|
||||
19
go.sum
19
go.sum
@ -8,42 +8,37 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/gf-third/mysql v1.4.2 h1:f1M5CNFUG3WkE07UOomtu4o0n/KJKeuUUf5Nc9ZFXs4=
|
||||
github.com/gf-third/mysql v1.4.2/go.mod h1:+dd90V663ppI2fV5uQ6+rHk0u8KCyU6FkG8Um8Cx3ms=
|
||||
github.com/gofrs/flock v0.7.1 h1:DP+LD/t0njgoPBvT5MJLeliUIVQR03hiKR6vezdwHlc=
|
||||
github.com/gf-third/yaml/v3 v3.0.0 h1:IMLH3JWFpNraTz5d6jzaNZFvWaVYAwhP9w1lhk9SFUs=
|
||||
github.com/gf-third/yaml/v3 v3.0.0/go.mod h1:En6jd9ZtAhuCiVfRnTo8NYVgbiZvg/F26r8Tj+vsUy4=
|
||||
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
||||
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
|
||||
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
|
||||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190424092004-025bd760b278 h1:DZo48DQFIDo/YWjUeFip1dfJztBhRuaxfUnPd+gAfcs=
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190424092004-025bd760b278/go.mod h1:Xk7G0nwBiIloTMbLddk4WWJOqi4i/JLhadLd0HUXO30=
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190916062342-6f856a90d556 h1:RS7ewakEriTcISIy+USQV9tE37SpWCblCiPE6QWxagk=
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190916062342-6f856a90d556/go.mod h1:Xk7G0nwBiIloTMbLddk4WWJOqi4i/JLhadLd0HUXO30=
|
||||
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
|
||||
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
|
||||
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
||||
github.com/theckman/go-flock v0.7.1 h1:YdJyIjDuQdEU7voZ9YaeXSO4OnrxdI+WejPUwyZ/Txs=
|
||||
github.com/theckman/go-flock v0.7.1/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ=
|
||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b h1:3S2h5FadpNr0zUUCVZjlKIEYF+KaX/OBplTGo89CYHI=
|
||||
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 h1:7TYNF4UdlohbFwpNH04CoPMp1cHUZgO1Ebq5r2hIjfo=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
google.golang.org/appengine v1.6.2 h1:j8RI1yW0SkI+paT6uGwMlrMI/6zwYA6/CFil8rxOzGI=
|
||||
google.golang.org/appengine v1.6.2/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20190905181640-827449938966 h1:B0J02caTR6tpSJozBJyiAzT6CtBzjclw4pgm9gg8Ys0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20190905181640-827449938966/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
@ -38,8 +38,11 @@ type Request struct {
|
||||
hasServeHandler bool // 是否检索到服务函数
|
||||
parsedGet bool // GET参数是否已经解析
|
||||
parsedPost bool // POST参数是否已经解析
|
||||
queryVars map[string][]string // GET参数
|
||||
routerVars map[string][]string // 路由解析参数
|
||||
parsedRaw bool // 原始参数是否已经解析
|
||||
getMap map[string]interface{} // GET解析参数
|
||||
postMap map[string]interface{} // POST解析参数
|
||||
routerMap map[string]interface{} // 路由解析参数
|
||||
rawVarMap map[string]interface{} // 原始数据参数
|
||||
error error // 当前请求执行错误
|
||||
exit bool // 是否退出当前请求流程执行
|
||||
params map[string]interface{} // 开发者自定义参数(请求流程中有效)
|
||||
@ -52,12 +55,12 @@ type Request struct {
|
||||
// 创建一个Request对象
|
||||
func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
|
||||
request := &Request{
|
||||
routerVars: make(map[string][]string),
|
||||
Id: s.servedCount.Add(1),
|
||||
Server: s,
|
||||
Request: r,
|
||||
Response: newResponse(s, w),
|
||||
EnterTime: gtime.Microsecond(),
|
||||
routerMap: make(map[string]interface{}),
|
||||
Id: s.servedCount.Add(1),
|
||||
Server: s,
|
||||
Request: r,
|
||||
Response: newResponse(s, w),
|
||||
EnterTime: gtime.Microsecond(),
|
||||
}
|
||||
// 会话处理
|
||||
request.Cookie = GetCookie(request)
|
||||
@ -82,8 +85,8 @@ func (r *Request) WebSocket() (*WebSocket, error) {
|
||||
|
||||
// 获得指定名称的参数字符串(Router/GET/POST),同 GetRequestString
|
||||
// 这是常用方法的简化别名
|
||||
func (r *Request) Get(key string, def ...interface{}) string {
|
||||
return r.GetRequestString(key, def...)
|
||||
func (r *Request) Get(key string, def ...interface{}) interface{} {
|
||||
return r.GetRequest(key, def...)
|
||||
}
|
||||
|
||||
// 建议都用该参数替代参数获取
|
||||
@ -113,6 +116,10 @@ func (r *Request) GetString(key string, def ...interface{}) string {
|
||||
return r.GetRequestString(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetBool(key string, def ...interface{}) bool {
|
||||
return r.GetRequestBool(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetInt(key string, def ...interface{}) int {
|
||||
return r.GetRequestInt(key, def...)
|
||||
}
|
||||
@ -149,10 +156,14 @@ func (r *Request) GetInterfaces(key string, def ...interface{}) []interface{} {
|
||||
return r.GetRequestInterfaces(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetMap(def ...map[string]string) map[string]string {
|
||||
func (r *Request) GetMap(def ...map[string]interface{}) map[string]interface{} {
|
||||
return r.GetRequestMap(def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetMapStrStr(def ...map[string]interface{}) map[string]string {
|
||||
return r.GetRequestMapStrStr(def...)
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数pointer应当为一个struct对象的指针,
|
||||
// mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
@ -236,14 +247,6 @@ func (r *Request) GetSessionId() string {
|
||||
return id
|
||||
}
|
||||
|
||||
// 生成随机的SESSIONID
|
||||
func (r *Request) MakeSessionId() string {
|
||||
id := gsession.NewSessionId()
|
||||
r.Cookie.SetSessionId(id)
|
||||
r.Response.Header().Set(r.Server.GetSessionIdName(), id)
|
||||
return id
|
||||
}
|
||||
|
||||
// 获得请求来源URL地址
|
||||
func (r *Request) GetReferer() string {
|
||||
return r.Header.Get("Referer")
|
||||
|
||||
@ -38,7 +38,7 @@ func (m *Middleware) Next() {
|
||||
}
|
||||
// 路由参数赋值
|
||||
for k, v := range item.values {
|
||||
m.request.routerVars[k] = v
|
||||
m.request.routerMap[k] = v
|
||||
}
|
||||
m.request.Router = item.handler.router
|
||||
// 执行函数处理
|
||||
|
||||
@ -17,14 +17,17 @@ func (r *Request) SetParam(key string, value interface{}) {
|
||||
}
|
||||
|
||||
// 获取请求流程共享变量
|
||||
func (r *Request) GetParam(key string, def ...interface{}) *gvar.Var {
|
||||
func (r *Request) GetParam(key string, def ...interface{}) interface{} {
|
||||
if r.params != nil {
|
||||
if v, ok := r.params[key]; ok {
|
||||
return gvar.New(v)
|
||||
}
|
||||
return r.params[key]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0])
|
||||
return def[0]
|
||||
}
|
||||
return gvar.New(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取请求流程共享变量
|
||||
func (r *Request) GetParamVar(key string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(r.GetParam(key, def...))
|
||||
}
|
||||
|
||||
@ -7,156 +7,164 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/encoding/gurl"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/structs"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// 初始化POST请求参数
|
||||
func (r *Request) initPost() {
|
||||
if !r.parsedPost {
|
||||
// MultiMedia表单请求解析允许最大使用内存:1GB
|
||||
if r.ParseMultipartForm(1024*1024*1024) == nil {
|
||||
r.parsedPost = true
|
||||
r.parsedPost = true
|
||||
if v := r.Header.Get("Content-Type"); v != "" && gstr.Contains(v, "multipart/") {
|
||||
// multipart/form-data, multipart/mixed
|
||||
r.ParseMultipartForm(r.Server.config.FormParsingMemory)
|
||||
if len(r.PostForm) > 0 {
|
||||
// 重新组织数据格式,使用统一的数据Parse方式
|
||||
params := ""
|
||||
for name, values := range r.PostForm {
|
||||
if len(values) == 1 {
|
||||
if len(params) > 0 {
|
||||
params += "&"
|
||||
}
|
||||
params += name + "=" + gurl.Encode(values[0])
|
||||
} else {
|
||||
if len(name) > 2 && name[len(name)-2:] == "[]" {
|
||||
name = name[:len(name)-2]
|
||||
for _, v := range values {
|
||||
if len(params) > 0 {
|
||||
params += "&"
|
||||
}
|
||||
params += name + "[]=" + gurl.Encode(v)
|
||||
}
|
||||
} else {
|
||||
if len(params) > 0 {
|
||||
params += "&"
|
||||
}
|
||||
params += name + "=" + gurl.Encode(values[len(values)-1])
|
||||
}
|
||||
}
|
||||
}
|
||||
r.postMap, _ = gstr.Parse(params)
|
||||
}
|
||||
} else if strings.EqualFold(r.Method, "POST") {
|
||||
r.parsedRaw = true
|
||||
if raw := r.GetRawString(); len(raw) > 0 {
|
||||
r.postMap, _ = gstr.Parse(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置POST参数,仅在ghttp.Server内有效,**注意并发安全性**
|
||||
func (r *Request) SetPost(key string, value string) {
|
||||
// 设置当前请求的POST参数
|
||||
func (r *Request) SetPost(key string, value interface{}) {
|
||||
r.initPost()
|
||||
r.PostForm[key] = []string{value}
|
||||
r.postMap[key] = value
|
||||
}
|
||||
|
||||
func (r *Request) AddPost(key string, value string) {
|
||||
func (r *Request) GetPost(key string, def ...interface{}) interface{} {
|
||||
r.initPost()
|
||||
r.PostForm[key] = append(r.PostForm[key], value)
|
||||
}
|
||||
|
||||
func (r *Request) GetPost(key string, def ...interface{}) []string {
|
||||
r.initPost()
|
||||
if v, ok := r.PostForm[key]; ok {
|
||||
if v, ok := r.postMap[key]; ok {
|
||||
return v
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gconv.Strings(def[0])
|
||||
return def[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) GetPostVar(key string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(r.GetPostString(key, def...))
|
||||
return gvar.New(r.GetPost(key, def...))
|
||||
}
|
||||
|
||||
func (r *Request) GetPostString(key string, def ...interface{}) string {
|
||||
value := r.GetPost(key, def...)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
return ""
|
||||
return r.GetPostVar(key, def...).String()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostBool(key string, def ...interface{}) bool {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Bool(value)
|
||||
}
|
||||
return false
|
||||
return r.GetPostVar(key, def...).Bool()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostInt(key string, def ...interface{}) int {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Int(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetPostVar(key, def...).Int()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostInts(key string, def ...interface{}) []int {
|
||||
value := r.GetPost(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Ints(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetPostVar(key, def...).Ints()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostUint(key string, def ...interface{}) uint {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Uint(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetPostVar(key, def...).Uint()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloat32(key string, def ...interface{}) float32 {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float32(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetPostVar(key, def...).Float32()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloat64(key string, def ...interface{}) float64 {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float64(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetPostVar(key, def...).Float64()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloats(key string, def ...interface{}) []float64 {
|
||||
value := r.GetPost(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Floats(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetPostVar(key, def...).Floats()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostArray(key string, def ...interface{}) []string {
|
||||
return r.GetPost(key, def...)
|
||||
return r.GetPostVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostStrings(key string, def ...interface{}) []string {
|
||||
return r.GetPost(key, def...)
|
||||
return r.GetPostVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostInterfaces(key string, def ...interface{}) []interface{} {
|
||||
value := r.GetPost(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Interfaces(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetPostVar(key, def...).Interfaces()
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetPostArray获取特定字段内容
|
||||
func (r *Request) GetPostMap(def ...map[string]string) map[string]string {
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值。
|
||||
// 当不指定键值对关联数组时,默认获取POST方式提交的所有的提交键值对数据。
|
||||
func (r *Request) GetPostMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.initPost()
|
||||
m := make(map[string]string)
|
||||
for k, v := range r.PostForm {
|
||||
m[k] = v[0]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
for k, v := range def[0] {
|
||||
if _, ok := m[k]; !ok {
|
||||
m[k] = v
|
||||
if len(kvMap) > 0 {
|
||||
m := make(map[string]interface{})
|
||||
for k, defValue := range kvMap[0] {
|
||||
if postValue, ok := r.postMap[k]; ok {
|
||||
m[k] = postValue
|
||||
} else {
|
||||
m[k] = defValue
|
||||
}
|
||||
}
|
||||
return m
|
||||
} else {
|
||||
return r.postMap
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (r *Request) GetPostMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
postMap := r.GetPostMap(kvMap...)
|
||||
if len(postMap) > 0 {
|
||||
m := make(map[string]string)
|
||||
for k, v := range postMap {
|
||||
m[k] = gconv.String(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetPostToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
r.initPost()
|
||||
tagMap := structs.TagMapName(pointer, paramTagPriority, true)
|
||||
if len(mapping) > 0 {
|
||||
for k, v := range mapping[0] {
|
||||
tagMap[k] = v
|
||||
}
|
||||
}
|
||||
params := make(map[string]interface{})
|
||||
for k, v := range r.GetPostMap() {
|
||||
params[k] = v
|
||||
}
|
||||
return gconv.StructDeep(params, pointer, tagMap)
|
||||
return gconv.StructDeep(r.postMap, pointer, tagMap)
|
||||
}
|
||||
|
||||
@ -9,6 +9,8 @@ package ghttp
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
|
||||
"github.com/gogf/gf/internal/structs"
|
||||
@ -18,156 +20,123 @@ import (
|
||||
// 初始化GET请求参数
|
||||
func (r *Request) initGet() {
|
||||
if !r.parsedGet {
|
||||
r.queryVars = r.URL.Query()
|
||||
if strings.EqualFold(r.Method, "GET") {
|
||||
r.parsedGet = true
|
||||
if r.URL.RawQuery != "" {
|
||||
r.getMap, _ = gstr.Parse(r.URL.RawQuery)
|
||||
} else if strings.EqualFold(r.Method, "GET") {
|
||||
r.parsedRaw = true
|
||||
if raw := r.GetRawString(); len(raw) > 0 {
|
||||
var array []string
|
||||
for _, item := range strings.Split(raw, "&") {
|
||||
array = strings.Split(item, "=")
|
||||
r.queryVars[array[0]] = append(r.queryVars[array[0]], array[1])
|
||||
}
|
||||
r.getMap, _ = gstr.Parse(raw)
|
||||
}
|
||||
}
|
||||
r.parsedGet = true
|
||||
}
|
||||
}
|
||||
|
||||
// 设置GET参数,仅在ghttp.Server内有效,**注意并发安全性**
|
||||
func (r *Request) SetQuery(key string, value string) {
|
||||
// 设置当前请求的GET参数
|
||||
func (r *Request) SetQuery(key string, value interface{}) {
|
||||
r.initGet()
|
||||
r.queryVars[key] = []string{value}
|
||||
}
|
||||
|
||||
// 添加GET参数,构成[]string
|
||||
func (r *Request) AddQuery(key string, value string) {
|
||||
r.initGet()
|
||||
r.queryVars[key] = append(r.queryVars[key], value)
|
||||
r.getMap[key] = value
|
||||
}
|
||||
|
||||
// 获得指定名称的get参数列表
|
||||
func (r *Request) GetQuery(key string, def ...interface{}) []string {
|
||||
func (r *Request) GetQuery(key string, def ...interface{}) interface{} {
|
||||
r.initGet()
|
||||
if v, ok := r.queryVars[key]; ok {
|
||||
if v, ok := r.getMap[key]; ok {
|
||||
return v
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gconv.Strings(def[0])
|
||||
return def[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryVar(key string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(r.GetQueryString(key, def...))
|
||||
return gvar.New(r.GetQuery(key, def...))
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryString(key string, def ...interface{}) string {
|
||||
value := r.GetQuery(key, def...)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
return ""
|
||||
return r.GetQueryVar(key, def...).String()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryBool(key string, def ...interface{}) bool {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Bool(value)
|
||||
}
|
||||
return false
|
||||
return r.GetQueryVar(key, def...).Bool()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryInt(key string, def ...interface{}) int {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Int(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetQueryVar(key, def...).Int()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryInts(key string, def ...interface{}) []int {
|
||||
value := r.GetQuery(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Ints(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetQueryVar(key, def...).Ints()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryUint(key string, def ...interface{}) uint {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Uint(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetQueryVar(key, def...).Uint()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloat32(key string, def ...interface{}) float32 {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float32(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetQueryVar(key, def...).Float32()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloat64(key string, def ...interface{}) float64 {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float64(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetQueryVar(key, def...).Float64()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloats(key string, def ...interface{}) []float64 {
|
||||
value := r.GetQuery(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Floats(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetQueryVar(key, def...).Floats()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryArray(key string, def ...interface{}) []string {
|
||||
return r.GetQuery(key, def...)
|
||||
return r.GetQueryVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryStrings(key string, def ...interface{}) []string {
|
||||
return r.GetQuery(key, def...)
|
||||
return r.GetQueryVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryInterfaces(key string, def ...interface{}) []interface{} {
|
||||
value := r.GetQuery(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Interfaces(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetQueryVar(key, def...).Interfaces()
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
func (r *Request) GetQueryMap(def ...map[string]string) map[string]string {
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值。
|
||||
// 当不指定键值对关联数组时,默认获取GET方式提交的所有的提交键值对数据。
|
||||
func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.initGet()
|
||||
m := make(map[string]string)
|
||||
for k, v := range r.queryVars {
|
||||
m[k] = v[0]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
for k, v := range def[0] {
|
||||
if _, ok := m[k]; !ok {
|
||||
m[k] = v
|
||||
if len(kvMap) > 0 {
|
||||
m := make(map[string]interface{})
|
||||
for k, defValue := range kvMap[0] {
|
||||
if queryValue, ok := r.getMap[k]; ok {
|
||||
m[k] = queryValue
|
||||
} else {
|
||||
m[k] = defValue
|
||||
}
|
||||
}
|
||||
return m
|
||||
} else {
|
||||
return r.getMap
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
queryMap := r.GetQueryMap(kvMap...)
|
||||
if len(queryMap) > 0 {
|
||||
m := make(map[string]string)
|
||||
for k, v := range queryMap {
|
||||
m[k] = gconv.String(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 将所有的get参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetQueryToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
tagmap := structs.TagMapName(pointer, paramTagPriority, true)
|
||||
r.initGet()
|
||||
tagMap := structs.TagMapName(pointer, paramTagPriority, true)
|
||||
if len(mapping) > 0 {
|
||||
for k, v := range mapping[0] {
|
||||
tagmap[k] = v
|
||||
tagMap[k] = v
|
||||
}
|
||||
}
|
||||
params := make(map[string]interface{})
|
||||
for k, v := range r.GetQueryMap() {
|
||||
params[k] = v
|
||||
}
|
||||
return gconv.StructDeep(params, pointer, tagmap)
|
||||
return gconv.StructDeep(r.getMap, pointer, tagMap)
|
||||
}
|
||||
|
||||
@ -9,129 +9,127 @@ package ghttp
|
||||
import (
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/structs"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// 初始化RAW请求参数
|
||||
func (r *Request) initRaw() {
|
||||
if !r.parsedRaw {
|
||||
r.parsedRaw = true
|
||||
if raw := r.GetRawString(); len(raw) > 0 {
|
||||
r.rawVarMap, _ = gstr.Parse(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获得router、post或者get提交的参数,如果有同名参数,那么按照router->get->post优先级进行覆盖
|
||||
func (r *Request) GetRequest(key string, def ...interface{}) []string {
|
||||
v := r.GetRouterArray(key)
|
||||
func (r *Request) GetRequest(key string, def ...interface{}) interface{} {
|
||||
v := r.GetRouterValue(key)
|
||||
if v == nil {
|
||||
v = r.GetQuery(key)
|
||||
}
|
||||
if v == nil {
|
||||
v = r.GetPost(key)
|
||||
}
|
||||
if v != nil {
|
||||
return v
|
||||
}
|
||||
r.initRaw()
|
||||
v = r.rawVarMap[key]
|
||||
if v == nil && len(def) > 0 {
|
||||
return gconv.Strings(def[0])
|
||||
return def[0]
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestVar(key string, def ...interface{}) *gvar.Var {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil {
|
||||
return gvar.New(value[0])
|
||||
}
|
||||
return gvar.New(nil)
|
||||
return gvar.New(r.GetRequest(key, def...))
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestString(key string, def ...interface{}) string {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
return ""
|
||||
return r.GetRequestVar(key, def...).String()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestBool(key string, def ...interface{}) bool {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Bool(value)
|
||||
}
|
||||
return false
|
||||
return r.GetRequestVar(key, def...).Bool()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestInt(key string, def ...interface{}) int {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Int(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetRequestVar(key, def...).Int()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestInts(key string, def ...interface{}) []int {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Ints(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetRequestVar(key, def...).Ints()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestUint(key string, def ...interface{}) uint {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Uint(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetRequestVar(key, def...).Uint()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestFloat32(key string, def ...interface{}) float32 {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float32(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetRequestVar(key, def...).Float32()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestFloat64(key string, def ...interface{}) float64 {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float64(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetRequestVar(key, def...).Float64()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestFloats(key string, def ...interface{}) []float64 {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Floats(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetRequestVar(key, def...).Floats()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestArray(key string, def ...interface{}) []string {
|
||||
return r.GetRequest(key, def...)
|
||||
return r.GetRequestVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestStrings(key string, def ...interface{}) []string {
|
||||
return r.GetRequest(key, def...)
|
||||
return r.GetRequestVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestInterfaces(key string, def ...interface{}) []interface{} {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Interfaces(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetRequestVar(key, def...).Interfaces()
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetRequestArray获取特定字段内容
|
||||
func (r *Request) GetRequestMap(def ...map[string]string) map[string]string {
|
||||
m := r.GetQueryMap()
|
||||
if len(m) == 0 {
|
||||
m = r.GetPostMap()
|
||||
}
|
||||
if len(def) > 0 {
|
||||
for k, v := range def[0] {
|
||||
if _, ok := m[k]; !ok {
|
||||
m[k] = v
|
||||
func (r *Request) GetRequestMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.initRaw()
|
||||
m := r.rawVarMap
|
||||
if len(kvMap) > 0 {
|
||||
m = make(map[string]interface{})
|
||||
for k, defValue := range kvMap[0] {
|
||||
if rawValue, ok := r.rawVarMap[k]; ok {
|
||||
m[k] = rawValue
|
||||
} else {
|
||||
m[k] = defValue
|
||||
}
|
||||
}
|
||||
}
|
||||
if m == nil {
|
||||
m = make(map[string]interface{})
|
||||
}
|
||||
for k, v := range r.GetPostMap(kvMap...) {
|
||||
m[k] = v
|
||||
}
|
||||
for k, v := range r.GetQueryMap(kvMap...) {
|
||||
m[k] = v
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
requestMap := r.GetRequestMap(kvMap...)
|
||||
if len(requestMap) > 0 {
|
||||
m := make(map[string]string)
|
||||
for k, v := range requestMap {
|
||||
m[k] = gconv.String(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetRequestToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
tagMap := structs.TagMapName(pointer, paramTagPriority, true)
|
||||
@ -140,14 +138,5 @@ func (r *Request) GetRequestToStruct(pointer interface{}, mapping ...map[string]
|
||||
tagMap[k] = v
|
||||
}
|
||||
}
|
||||
params := make(map[string]interface{})
|
||||
for k, v := range r.GetRequestMap() {
|
||||
params[k] = v
|
||||
}
|
||||
if len(params) == 0 {
|
||||
if j := r.GetJson(); j != nil {
|
||||
params = j.ToMap()
|
||||
}
|
||||
}
|
||||
return gconv.StructDeep(params, pointer, tagMap)
|
||||
return gconv.StructDeep(r.GetRequestMap(), pointer, tagMap)
|
||||
}
|
||||
|
||||
@ -6,26 +6,28 @@
|
||||
|
||||
package ghttp
|
||||
|
||||
func (r *Request) SetRouterString(key, value string) {
|
||||
r.routerVars[key] = []string{value}
|
||||
}
|
||||
import "github.com/gogf/gf/container/gvar"
|
||||
|
||||
func (r *Request) AddRouterString(key, value string) {
|
||||
r.routerVars[key] = append(r.routerVars[key], value)
|
||||
func (r *Request) SetRouterValue(key string, value interface{}) {
|
||||
r.routerMap[key] = value
|
||||
}
|
||||
|
||||
// 获得路由解析参数
|
||||
func (r *Request) GetRouterString(key string) string {
|
||||
if v := r.GetRouterArray(key); v != nil {
|
||||
return v[0]
|
||||
func (r *Request) GetRouterValue(key string, def ...interface{}) interface{} {
|
||||
if r.routerMap != nil {
|
||||
return r.routerMap[key]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 获得路由解析参数
|
||||
func (r *Request) GetRouterArray(key string) []string {
|
||||
if v, ok := r.routerVars[key]; ok {
|
||||
return v
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获得路由解析参数
|
||||
func (r *Request) GetRouterVar(key string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(r.GetRouterValue(key, def...))
|
||||
}
|
||||
|
||||
func (r *Request) GetRouterString(key string, def ...interface{}) string {
|
||||
return r.GetRouterVar(key, def...).String()
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ func (r *Response) WriteJsonP(content interface{}) error {
|
||||
return err
|
||||
} else {
|
||||
//r.Header().Set("Content-Type", "application/json")
|
||||
if callback := r.request.Get("callback"); callback != "" {
|
||||
if callback := r.request.GetString("callback"); callback != "" {
|
||||
buffer := []byte(callback)
|
||||
buffer = append(buffer, byte('('))
|
||||
buffer = append(buffer, b...)
|
||||
|
||||
@ -75,8 +75,8 @@ type (
|
||||
|
||||
// 根据特定URL.Path解析后的路由检索结果项
|
||||
handlerParsedItem struct {
|
||||
handler *handlerItem // 路由注册项
|
||||
values map[string][]string // 特定URL.Path的Router解析参数
|
||||
handler *handlerItem // 路由注册项
|
||||
values map[string]string // 特定URL.Path的Router解析参数
|
||||
}
|
||||
|
||||
// 控制器服务函数反射信息
|
||||
|
||||
@ -73,6 +73,7 @@ type ServerConfig struct {
|
||||
ErrorStack bool // Logging: 当产生错误时打印调用链详细堆栈
|
||||
ErrorLogEnabled bool // Logging: 是否开启error log(默认开启)
|
||||
AccessLogEnabled bool // Logging: 是否开启access log(默认关闭)
|
||||
FormParsingMemory int64 // Mess: 表单解析内存限制(byte)
|
||||
NameToUriType int // Mess: 服务注册时对象和方法名称转换为URI时的规则
|
||||
GzipContentTypes []string // Mess: 允许进行gzip压缩的文件类型
|
||||
DumpRouteMap bool // Mess: 是否在程序启动时默认打印路由表信息
|
||||
@ -105,6 +106,7 @@ var defaultServerConfig = ServerConfig{
|
||||
ErrorLogEnabled: true,
|
||||
AccessLogEnabled: false,
|
||||
DumpRouteMap: true,
|
||||
FormParsingMemory: 1024 * 1024 * 1024,
|
||||
RouterCacheExpire: 60,
|
||||
Rewrites: make(map[string]string),
|
||||
}
|
||||
@ -287,41 +289,6 @@ func (s *Server) SetServerAgent(agent string) {
|
||||
s.config.ServerAgent = agent
|
||||
}
|
||||
|
||||
func (s *Server) SetGzipContentTypes(types []string) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.GzipContentTypes = types
|
||||
}
|
||||
|
||||
// 服务注册时对象和方法名称转换为URI时的规则
|
||||
func (s *Server) SetNameToUriType(t int) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.NameToUriType = t
|
||||
}
|
||||
|
||||
// 是否在程序启动时打印路由表信息
|
||||
func (s *Server) SetDumpRouteMap(enabled bool) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.DumpRouteMap = enabled
|
||||
}
|
||||
|
||||
// 设置路由缓存过期时间(秒)
|
||||
func (s *Server) SetRouterCacheExpire(expire int) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.RouterCacheExpire = expire
|
||||
}
|
||||
|
||||
// 设置KeepAlive
|
||||
func (s *Server) SetKeepAlive(enabled bool) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
|
||||
52
net/ghttp/ghttp_server_config_mess.go
Normal file
52
net/ghttp/ghttp_server_config_mess.go
Normal file
@ -0,0 +1,52 @@
|
||||
// Copyright 2019 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 ghttp
|
||||
|
||||
import "github.com/gogf/gf/os/glog"
|
||||
|
||||
func (s *Server) SetGzipContentTypes(types []string) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.GzipContentTypes = types
|
||||
}
|
||||
|
||||
// 服务注册时对象和方法名称转换为URI时的规则
|
||||
func (s *Server) SetNameToUriType(t int) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.NameToUriType = t
|
||||
}
|
||||
|
||||
// 是否在程序启动时打印路由表信息
|
||||
func (s *Server) SetDumpRouteMap(enabled bool) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.DumpRouteMap = enabled
|
||||
}
|
||||
|
||||
// 设置路由缓存过期时间(秒)
|
||||
func (s *Server) SetRouterCacheExpire(expire int) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.RouterCacheExpire = expire
|
||||
}
|
||||
|
||||
func (s *Server) SetFormParsingMemory(maxMemory int64) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.FormParsingMemory = maxMemory
|
||||
}
|
||||
@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gPATH_FILTER_KEY = "/gf/net/ghttp/ghttp"
|
||||
gPATH_FILTER_KEY = "/net/ghttp/ghttp"
|
||||
)
|
||||
|
||||
// 处理服务错误信息,主要是panic,http请求的status由access log进行管理
|
||||
|
||||
@ -20,7 +20,7 @@ type utilPprof struct{}
|
||||
|
||||
func (p *utilPprof) Index(r *Request) {
|
||||
profiles := runpprof.Profiles()
|
||||
action := r.Get("action")
|
||||
action := r.GetString("action")
|
||||
data := map[string]interface{}{
|
||||
"uri": strings.TrimRight(r.URL.Path, "/") + "/",
|
||||
"profiles": profiles,
|
||||
|
||||
@ -9,9 +9,10 @@ package ghttp
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
|
||||
"github.com/gogf/gf/container/glist"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
@ -19,7 +20,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gFILTER_KEY = "/gf/net/ghttp/ghttp"
|
||||
gFILTER_KEY = "/net/ghttp/ghttp"
|
||||
)
|
||||
|
||||
// 解析pattern
|
||||
|
||||
@ -216,6 +216,14 @@ func (g *RouterGroup) Middleware(handlers ...HandlerFunc) *RouterGroup {
|
||||
return group
|
||||
}
|
||||
|
||||
func (g *RouterGroup) MiddlewarePattern(pattern string, handlers ...HandlerFunc) *RouterGroup {
|
||||
group := g.Clone()
|
||||
for _, handler := range handlers {
|
||||
group.preBind("MIDDLEWARE", pattern, handler)
|
||||
}
|
||||
return group
|
||||
}
|
||||
|
||||
func (g *RouterGroup) preBind(bindType string, pattern string, object interface{}, params ...interface{}) *RouterGroup {
|
||||
preBindItems = append(preBindItems, groupPreBindItem{
|
||||
group: g,
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"runtime"
|
||||
)
|
||||
@ -34,20 +35,20 @@ func (s *Server) callHookHandler(hook string, r *Request) {
|
||||
hookItems := r.getHookHandlers(hook)
|
||||
if len(hookItems) > 0 {
|
||||
// 备份原有的router变量
|
||||
oldRouterVars := r.routerVars
|
||||
oldRouterVars := r.routerMap
|
||||
for _, item := range hookItems {
|
||||
// hook方法不能更改serve方法的路由参数,其匹配的路由参数只能自己使用,
|
||||
// 且在多个hook方法之间不能共享路由参数,单可以使用匹配的serve方法路由参数。
|
||||
// 当前回调函数的路由参数只在当前回调函数下有效。
|
||||
r.routerVars = make(map[string][]string)
|
||||
r.routerMap = make(map[string]interface{})
|
||||
if len(oldRouterVars) > 0 {
|
||||
for k, v := range oldRouterVars {
|
||||
r.routerVars[k] = v
|
||||
r.routerMap[k] = v
|
||||
}
|
||||
}
|
||||
if len(item.values) > 0 {
|
||||
for k, v := range item.values {
|
||||
r.routerVars[k] = v
|
||||
r.routerMap[k] = v
|
||||
}
|
||||
}
|
||||
// 不使用hook的router对象,保留路由注册服务的router对象,不能覆盖
|
||||
@ -61,12 +62,13 @@ func (s *Server) callHookHandler(hook string, r *Request) {
|
||||
case gEXCEPTION_EXIT_HOOK:
|
||||
return
|
||||
default:
|
||||
r.Response.WriteStatus(http.StatusInternalServerError, err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// 恢复原有的router变量
|
||||
r.routerVars = oldRouterVars
|
||||
r.routerMap = oldRouterVars
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@ func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
||||
}
|
||||
|
||||
// 注册中间件,绑定到全局路由规则("/*")上,中间件参数支持多个。
|
||||
func (s *Server) AddMiddleware(handlers ...HandlerFunc) {
|
||||
func (s *Server) BindMiddlewareDefault(handlers ...HandlerFunc) {
|
||||
for _, handler := range handlers {
|
||||
s.setHandler(gDEFAULT_MIDDLEWARE_PATTERN, &handlerItem{
|
||||
itemType: gHANDLER_TYPE_MIDDLEWARE,
|
||||
|
||||
@ -113,14 +113,10 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
// 如果需要query匹配,那么需要重新正则解析URL
|
||||
if len(item.router.RegNames) > 0 {
|
||||
if len(match) > len(item.router.RegNames) {
|
||||
parsedItem.values = make(map[string][]string)
|
||||
// 如果存在存在同名路由参数名称,那么执行数组追加
|
||||
parsedItem.values = make(map[string]string)
|
||||
// 如果存在存在同名路由参数名称,那么执行覆盖
|
||||
for i, name := range item.router.RegNames {
|
||||
if _, ok := parsedItem.values[name]; ok {
|
||||
parsedItem.values[name] = append(parsedItem.values[name], match[i+1])
|
||||
} else {
|
||||
parsedItem.values[name] = []string{match[i+1]}
|
||||
}
|
||||
parsedItem.values[name] = match[i+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,14 +20,13 @@ func Test_Cookie(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Cookie.Set(r.Get("k"), r.Get("v"))
|
||||
r.Cookie.Set(r.GetString("k"), r.GetString("v"))
|
||||
})
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
//fmt.Println(r.Cookie.Map())
|
||||
r.Response.Write(r.Cookie.Get(r.Get("k")))
|
||||
r.Response.Write(r.Cookie.Get(r.GetString("k")))
|
||||
})
|
||||
s.BindHandler("/remove", func(r *ghttp.Request) {
|
||||
r.Cookie.Remove(r.Get("k"))
|
||||
r.Cookie.Remove(r.GetString("k"))
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
|
||||
@ -130,18 +130,18 @@ func Test_BindMiddleware_Status(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic1(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic1(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("4")
|
||||
@ -162,18 +162,18 @@ func Test_AddMiddleware_Basic1(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic2(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic2(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("PUT:/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("4")
|
||||
@ -196,17 +196,17 @@ func Test_AddMiddleware_Basic2(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic3(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic3(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
@ -226,17 +226,17 @@ func Test_AddMiddleware_Basic3(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic4(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic4(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("1")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
r.Middleware.Next()
|
||||
})
|
||||
@ -256,17 +256,17 @@ func Test_AddMiddleware_Basic4(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic5(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic5(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
r.Middleware.Next()
|
||||
})
|
||||
@ -286,13 +286,13 @@ func Test_AddMiddleware_Basic5(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Status(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Status(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
})
|
||||
s.SetPort(p)
|
||||
@ -333,16 +333,16 @@ func (o *ObjectMiddleware) Info(r *ghttp.Request) {
|
||||
r.Response.Write("Object Info")
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic6(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic6(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindObject("/", new(ObjectMiddleware))
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("4")
|
||||
@ -385,12 +385,12 @@ func Test_Hook_Middleware_Basic1(t *testing.T) {
|
||||
s.BindHookHandler("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Write("d")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("4")
|
||||
|
||||
@ -26,6 +26,9 @@ func Test_Params_Basic(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
if r.GetQuery("array") != nil {
|
||||
r.Response.Write(r.GetQuery("array"))
|
||||
}
|
||||
if r.GetQuery("slice") != nil {
|
||||
r.Response.Write(r.GetQuery("slice"))
|
||||
}
|
||||
@ -47,8 +50,49 @@ func Test_Params_Basic(t *testing.T) {
|
||||
if r.GetQuery("string") != nil {
|
||||
r.Response.Write(r.GetQueryString("string"))
|
||||
}
|
||||
if r.GetQuery("map") != nil {
|
||||
r.Response.Write(r.GetQueryMap()["map"].(map[string]interface{})["b"])
|
||||
}
|
||||
if r.GetQuery("a") != nil {
|
||||
r.Response.Write(r.GetQueryMapStrStr()["a"])
|
||||
}
|
||||
})
|
||||
s.BindHandler("/put", func(r *ghttp.Request) {
|
||||
if r.Get("array") != nil {
|
||||
r.Response.Write(r.Get("array"))
|
||||
}
|
||||
if r.Get("slice") != nil {
|
||||
r.Response.Write(r.Get("slice"))
|
||||
}
|
||||
if r.Get("bool") != nil {
|
||||
r.Response.Write(r.GetBool("bool"))
|
||||
}
|
||||
if r.Get("float32") != nil {
|
||||
r.Response.Write(r.GetFloat32("float32"))
|
||||
}
|
||||
if r.Get("float64") != nil {
|
||||
r.Response.Write(r.GetFloat64("float64"))
|
||||
}
|
||||
if r.Get("int") != nil {
|
||||
r.Response.Write(r.GetInt("int"))
|
||||
}
|
||||
if r.Get("uint") != nil {
|
||||
r.Response.Write(r.GetUint("uint"))
|
||||
}
|
||||
if r.Get("string") != nil {
|
||||
r.Response.Write(r.GetString("string"))
|
||||
}
|
||||
if r.Get("map") != nil {
|
||||
r.Response.Write(r.GetMap()["map"].(map[string]interface{})["b"])
|
||||
}
|
||||
if r.Get("a") != nil {
|
||||
r.Response.Write(r.GetMapStrStr()["a"])
|
||||
}
|
||||
})
|
||||
s.BindHandler("/post", func(r *ghttp.Request) {
|
||||
if r.GetPost("array") != nil {
|
||||
r.Response.Write(r.GetPost("array"))
|
||||
}
|
||||
if r.GetPost("slice") != nil {
|
||||
r.Response.Write(r.GetPost("slice"))
|
||||
}
|
||||
@ -70,6 +114,12 @@ func Test_Params_Basic(t *testing.T) {
|
||||
if r.GetPost("string") != nil {
|
||||
r.Response.Write(r.GetPostString("string"))
|
||||
}
|
||||
if r.GetPost("map") != nil {
|
||||
r.Response.Write(r.GetPostMap()["map"].(map[string]interface{})["b"])
|
||||
}
|
||||
if r.GetPost("a") != nil {
|
||||
r.Response.Write(r.GetPostMapStrStr()["a"])
|
||||
}
|
||||
})
|
||||
s.BindHandler("/map", func(r *ghttp.Request) {
|
||||
if m := r.GetQueryMap(); len(m) > 0 {
|
||||
@ -132,7 +182,8 @@ func Test_Params_Basic(t *testing.T) {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
// GET
|
||||
gtest.Assert(client.GetContent("/get", "slice=1&slice=2"), `["1","2"]`)
|
||||
gtest.Assert(client.GetContent("/get", "array[]=1&array[]=2"), `["1","2"]`)
|
||||
gtest.Assert(client.GetContent("/get", "slice=1&slice=2"), `2`)
|
||||
gtest.Assert(client.GetContent("/get", "bool=1"), `true`)
|
||||
gtest.Assert(client.GetContent("/get", "bool=0"), `false`)
|
||||
gtest.Assert(client.GetContent("/get", "float32=0.11"), `0.11`)
|
||||
@ -142,9 +193,27 @@ func Test_Params_Basic(t *testing.T) {
|
||||
gtest.Assert(client.GetContent("/get", "uint=10000"), `10000`)
|
||||
gtest.Assert(client.GetContent("/get", "uint=9"), `9`)
|
||||
gtest.Assert(client.GetContent("/get", "string=key"), `key`)
|
||||
gtest.Assert(client.GetContent("/get", "map[a]=1&map[b]=2"), `2`)
|
||||
gtest.Assert(client.GetContent("/get", "a=1&b=2"), `1`)
|
||||
|
||||
// PUT
|
||||
gtest.Assert(client.PutContent("/put", "array[]=1&array[]=2"), `["1","2"]`)
|
||||
gtest.Assert(client.PutContent("/put", "slice=1&slice=2"), `2`)
|
||||
gtest.Assert(client.PutContent("/put", "bool=1"), `true`)
|
||||
gtest.Assert(client.PutContent("/put", "bool=0"), `false`)
|
||||
gtest.Assert(client.PutContent("/put", "float32=0.11"), `0.11`)
|
||||
gtest.Assert(client.PutContent("/put", "float64=0.22"), `0.22`)
|
||||
gtest.Assert(client.PutContent("/put", "int=-10000"), `-10000`)
|
||||
gtest.Assert(client.PutContent("/put", "int=10000"), `10000`)
|
||||
gtest.Assert(client.PutContent("/put", "uint=10000"), `10000`)
|
||||
gtest.Assert(client.PutContent("/put", "uint=9"), `9`)
|
||||
gtest.Assert(client.PutContent("/put", "string=key"), `key`)
|
||||
gtest.Assert(client.PutContent("/put", "map[a]=1&map[b]=2"), `2`)
|
||||
gtest.Assert(client.PutContent("/put", "a=1&b=2"), `1`)
|
||||
|
||||
// POST
|
||||
gtest.Assert(client.PostContent("/post", "slice=1&slice=2"), `["1","2"]`)
|
||||
gtest.Assert(client.PostContent("/post", "array[]=1&array[]=2"), `["1","2"]`)
|
||||
gtest.Assert(client.PostContent("/post", "slice=1&slice=2"), `2`)
|
||||
gtest.Assert(client.PostContent("/post", "bool=1"), `true`)
|
||||
gtest.Assert(client.PostContent("/post", "bool=0"), `false`)
|
||||
gtest.Assert(client.PostContent("/post", "float32=0.11"), `0.11`)
|
||||
@ -154,6 +223,8 @@ func Test_Params_Basic(t *testing.T) {
|
||||
gtest.Assert(client.PostContent("/post", "uint=10000"), `10000`)
|
||||
gtest.Assert(client.PostContent("/post", "uint=9"), `9`)
|
||||
gtest.Assert(client.PostContent("/post", "string=key"), `key`)
|
||||
gtest.Assert(client.PostContent("/post", "map[a]=1&map[b]=2"), `2`)
|
||||
gtest.Assert(client.PostContent("/post", "a=1&b=2"), `1`)
|
||||
|
||||
// Map
|
||||
gtest.Assert(client.GetContent("/map", "id=1&name=john"), `john`)
|
||||
|
||||
@ -20,13 +20,13 @@ func Test_Session_Cookie(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set(r.Get("k"), r.Get("v"))
|
||||
r.Session.Set(r.GetString("k"), r.GetString("v"))
|
||||
})
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Session.Get(r.Get("k")))
|
||||
r.Response.Write(r.Session.Get(r.GetString("k")))
|
||||
})
|
||||
s.BindHandler("/remove", func(r *ghttp.Request) {
|
||||
r.Session.Remove(r.Get("k"))
|
||||
r.Session.Remove(r.GetString("k"))
|
||||
})
|
||||
s.BindHandler("/clear", func(r *ghttp.Request) {
|
||||
r.Session.Clear()
|
||||
@ -68,13 +68,13 @@ func Test_Session_Header(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set(r.Get("k"), r.Get("v"))
|
||||
r.Session.Set(r.GetString("k"), r.GetString("v"))
|
||||
})
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Session.Get(r.Get("k")))
|
||||
r.Response.Write(r.Session.Get(r.GetString("k")))
|
||||
})
|
||||
s.BindHandler("/remove", func(r *ghttp.Request) {
|
||||
r.Session.Remove(r.Get("k"))
|
||||
r.Session.Remove(r.GetString("k"))
|
||||
})
|
||||
s.BindHandler("/clear", func(r *ghttp.Request) {
|
||||
r.Session.Clear()
|
||||
@ -121,8 +121,8 @@ func Test_Session_StorageFile(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set(r.Get("k"), r.Get("v"))
|
||||
r.Response.Write(r.Get("k"), "=", r.Get("v"))
|
||||
r.Session.Set(r.GetString("k"), r.GetString("v"))
|
||||
r.Response.Write(r.GetString("k"), "=", r.GetString("v"))
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
@ -146,7 +146,7 @@ func Test_Session_StorageFile(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Session.Get(r.Get("k")))
|
||||
r.Response.Write(r.Session.Get(r.GetString("k")))
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
|
||||
77
net/gsmtp/gsmtp_test.go
Normal file
77
net/gsmtp/gsmtp_test.go
Normal file
@ -0,0 +1,77 @@
|
||||
// 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 gsmtp_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/net/gsmtp"
|
||||
)
|
||||
|
||||
func TestAddress(t *testing.T) {
|
||||
errMessage := "address is either empty or incorrect"
|
||||
|
||||
errValues := []string{
|
||||
"",
|
||||
":",
|
||||
":25",
|
||||
"localhost:",
|
||||
"local.host:25:28",
|
||||
}
|
||||
|
||||
for _, errValue := range errValues {
|
||||
smtpConnection := gsmtp.New(errValue, "smtpUser@smtp.exmail.qq.com", "smtpPassword")
|
||||
res := smtpConnection.SendMail("sender@local.host", "recipient1@domain.com;recipientN@anotherDomain.cn", "This is subject", "Hi! <br><br> This is body")
|
||||
if !strings.Contains(res.Error(), errMessage) {
|
||||
t.Errorf("Test failed on Address: %s", errValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrom(t *testing.T) {
|
||||
errMessage := "from is invalid"
|
||||
|
||||
errValues := []string{
|
||||
"",
|
||||
"qwerty",
|
||||
// "qwe@rty@com",
|
||||
// "@rty",
|
||||
// "qwe@",
|
||||
}
|
||||
|
||||
for _, errValue := range errValues {
|
||||
smtpConnection := gsmtp.New("smtp.exmail.qq.com", "smtpUser@smtp.exmail.qq.com", "smtpPassword")
|
||||
res := smtpConnection.SendMail(errValue, "recipient1@domain.com;recipientN@anotherDomain.cn", "This is subject", "Hi! <br><br> This is body")
|
||||
if !strings.Contains(res.Error(), errMessage) {
|
||||
t.Errorf("Test failed on From: %s", errValue)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTos(t *testing.T) {
|
||||
errMessage := "tos if invalid"
|
||||
|
||||
errValues := []string{
|
||||
"",
|
||||
"qwerty",
|
||||
"qwe;rty",
|
||||
"qwe;rty;com",
|
||||
// "qwe@rty@com",
|
||||
// "@rty",
|
||||
// "qwe@",
|
||||
}
|
||||
|
||||
for _, errValue := range errValues {
|
||||
smtpConnection := gsmtp.New("smtp.exmail.qq.com", "smtpUser@smtp.exmail.qq.com", "smtpPassword")
|
||||
res := smtpConnection.SendMail("from@domain.com", errValue, "This is subject", "Hi! <br><br> This is body")
|
||||
if !strings.Contains(res.Error(), errMessage) {
|
||||
t.Errorf("Test failed on Tos: %s", errValue)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -254,30 +254,30 @@ func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
func (c *Config) GetMapStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapStruct(pattern, pointer, mapping...)
|
||||
return j.GetMapToMap(pattern, pointer, mapping...)
|
||||
}
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
func (c *Config) GetMapStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
func (c *Config) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapStructDeep(pattern, pointer, mapping...)
|
||||
return j.GetMapToMapDeep(pattern, pointer, mapping...)
|
||||
}
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
func (c *Config) GetMapStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
func (c *Config) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapStructs(pattern, pointer, mapping...)
|
||||
return j.GetMapToMaps(pattern, pointer, mapping...)
|
||||
}
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
func (c *Config) GetMapStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapStructsDeep(pattern, pointer, mapping...)
|
||||
return j.GetMapToMapsDeep(pattern, pointer, mapping...)
|
||||
}
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
@ -324,30 +324,30 @@ func (c *Config) ToStructsDeep(pointer interface{}, mapping ...map[string]string
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
func (c *Config) ToMapStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (c *Config) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMapStruct(pointer, mapping...)
|
||||
return j.ToMapToMap(pointer, mapping...)
|
||||
}
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
func (c *Config) ToMapStructDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (c *Config) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMapStructDeep(pointer, mapping...)
|
||||
return j.ToMapToMapDeep(pointer, mapping...)
|
||||
}
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
func (c *Config) ToMapStructs(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (c *Config) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMapStructs(pointer, mapping...)
|
||||
return j.ToMapToMaps(pointer, mapping...)
|
||||
}
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
func (c *Config) ToMapStructsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (c *Config) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMapStructsDeep(pointer, mapping...)
|
||||
return j.ToMapToMapsDeep(pointer, mapping...)
|
||||
}
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright 2019 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,
|
||||
@ -17,8 +17,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
defaultParsedArgs = make([]string, 0)
|
||||
defaultParsedOptions = make(map[string]string)
|
||||
defaultParsedArgs = make([]string, 0)
|
||||
defaultParsedOptions = make(map[string]string)
|
||||
defaultCommandFuncMap = make(map[string]func())
|
||||
)
|
||||
|
||||
// Custom initialization.
|
||||
@ -29,7 +30,7 @@ func doInit() {
|
||||
// Parsing os.Args with default algorithm.
|
||||
// The option should use '=' to separate its name and value in default.
|
||||
for _, arg := range os.Args {
|
||||
array, _ := gregex.MatchString(`^\-{1,2}([\w\?]+)={0,1}(.*)$`, arg)
|
||||
array, _ := gregex.MatchString(`^\-{1,2}([\w\?\.\-]+)={0,1}(.*)$`, arg)
|
||||
if len(array) == 3 {
|
||||
defaultParsedOptions[array[1]] = array[2]
|
||||
} else {
|
||||
|
||||
58
os/gcmd/gcmd_handler.go
Normal file
58
os/gcmd/gcmd_handler.go
Normal file
@ -0,0 +1,58 @@
|
||||
// Copyright 2019 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 gcmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// BindHandle registers callback function <f> with <cmd>.
|
||||
func BindHandle(cmd string, f func()) error {
|
||||
if _, ok := defaultCommandFuncMap[cmd]; ok {
|
||||
return errors.New("duplicated handle for command:" + cmd)
|
||||
} else {
|
||||
defaultCommandFuncMap[cmd] = f
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BindHandle registers callback function with map <m>.
|
||||
func BindHandleMap(m map[string]func()) error {
|
||||
var err error
|
||||
for k, v := range m {
|
||||
if err = BindHandle(k, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// RunHandle executes the callback function registered by <cmd>.
|
||||
func RunHandle(cmd string) error {
|
||||
if handle, ok := defaultCommandFuncMap[cmd]; ok {
|
||||
handle()
|
||||
} else {
|
||||
return errors.New("no handle found for command:" + cmd)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AutoRun automatically recognizes and executes the callback function
|
||||
// by value of index 0 (the first console parameter).
|
||||
func AutoRun() error {
|
||||
if cmd := GetArg(1); cmd != "" {
|
||||
if handle, ok := defaultCommandFuncMap[cmd]; ok {
|
||||
handle()
|
||||
} else {
|
||||
return errors.New("no handle found for command:" + cmd)
|
||||
}
|
||||
} else {
|
||||
return errors.New("no command found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright 2019 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,
|
||||
@ -21,6 +21,17 @@ func (p *Parser) BindHandle(cmd string, f func()) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BindHandle registers callback function with map <m>.
|
||||
func (p *Parser) BindHandleMap(m map[string]func()) error {
|
||||
var err error
|
||||
for k, v := range m {
|
||||
if err = p.BindHandle(k, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// RunHandle executes the callback function registered by <cmd>.
|
||||
func (p *Parser) RunHandle(cmd string) error {
|
||||
if handle, ok := p.commandFuncMap[cmd]; ok {
|
||||
|
||||
@ -44,7 +44,7 @@ const (
|
||||
gDEFAULT_FILE_POOL_FLAGS = os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
||||
gDEFAULT_FPOOL_PERM = os.FileMode(0666)
|
||||
gDEFAULT_FPOOL_EXPIRE = 60000
|
||||
gPATH_FILTER_KEY = "/gf/os/glog/glog"
|
||||
gPATH_FILTER_KEY = "/os/glog/glog"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@ -19,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gPATH_FILTER_KEY = "/gf/test/gtest/gtest"
|
||||
gPATH_FILTER_KEY = "/test/gtest/gtest"
|
||||
)
|
||||
|
||||
// Case creates an unit test case.
|
||||
|
||||
@ -447,7 +447,10 @@ func Split(str, delimiter string) []string {
|
||||
func SplitAndTrim(str, delimiter, cut string) []string {
|
||||
array := strings.Split(str, delimiter)
|
||||
for k, v := range array {
|
||||
array[k] = strings.Trim(v, cut)
|
||||
v = strings.Trim(v, cut)
|
||||
if v != "" {
|
||||
array[k] = strings.Trim(v, cut)
|
||||
}
|
||||
}
|
||||
return array
|
||||
}
|
||||
@ -457,7 +460,10 @@ func SplitAndTrim(str, delimiter, cut string) []string {
|
||||
func SplitAndTrimSpace(str, delimiter string) []string {
|
||||
array := strings.Split(str, delimiter)
|
||||
for k, v := range array {
|
||||
array[k] = strings.TrimSpace(v)
|
||||
v = strings.TrimSpace(v)
|
||||
if v != "" {
|
||||
array[k] = v
|
||||
}
|
||||
}
|
||||
return array
|
||||
}
|
||||
|
||||
@ -90,7 +90,7 @@ func DelimitedScreamingCase(s string, del uint8, screaming bool) string {
|
||||
} else if v >= 'a' && v <= 'z' {
|
||||
n += string(v) + string(del)
|
||||
}
|
||||
} else if v == ' ' || v == '_' || v == '-' {
|
||||
} else if v == ' ' || v == '_' || v == '-' || v == '.' {
|
||||
// replace spaces/underscores with delimiters
|
||||
n += string(del)
|
||||
} else {
|
||||
@ -132,7 +132,7 @@ func toCamelInitCase(s string, initCase bool) string {
|
||||
n += string(v)
|
||||
}
|
||||
}
|
||||
if v == '_' || v == ' ' || v == '-' {
|
||||
if v == '_' || v == ' ' || v == '-' || v == '.' {
|
||||
capNext = true
|
||||
} else {
|
||||
capNext = false
|
||||
|
||||
@ -14,13 +14,13 @@ import (
|
||||
|
||||
// Parse parses the string into map[string]interface{}.
|
||||
//
|
||||
// f1=m&f2=n -> map[f1:m f2:n]
|
||||
// f[a]=m&f[b]=n -> map[f:map[a:m b:n]]
|
||||
// f[a][a]=m&f[a][b]=n -> map[f:map[a:map[a:m b:n]]]
|
||||
// f[]=m&f[]=n -> map[f:[m n]]
|
||||
// f[a][]=m&f[a][]=n -> map[f:map[a:[m n]]]
|
||||
// f[][]=m&f[][]=n -> map[f:[map[]]] // Currently does not support nested slice.
|
||||
// f=m&f[a]=n -> error
|
||||
// v1=m&v2=n -> map[v1:m v2:n]
|
||||
// v[a]=m&v[b]=n -> map[v:map[a:m b:n]]
|
||||
// v[a][a]=m&v[a][b]=n -> map[v:map[a:map[a:m b:n]]]
|
||||
// v[]=m&v[]=n -> map[v:[m n]]
|
||||
// v[a][]=m&v[a][]=n -> map[v:map[a:[m n]]]
|
||||
// v[][]=m&v[][]=n -> map[v:[map[]]] // Currently does not support nested slice.
|
||||
// v=m&v[a]=n -> error
|
||||
// a .[[b=c -> map[a___[b:c]
|
||||
//
|
||||
func Parse(s string) (result map[string]interface{}, err error) {
|
||||
@ -102,7 +102,7 @@ func build(result map[string]interface{}, keys []string, value interface{}) erro
|
||||
|
||||
// The end is slice. like f[], f[a][]
|
||||
if keys[1] == "" && length == 2 {
|
||||
// todo nested slice
|
||||
// TODO nested slice
|
||||
if key == "" {
|
||||
return nil
|
||||
}
|
||||
@ -119,7 +119,7 @@ func build(result map[string]interface{}, keys []string, value interface{}) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// The end is slice + map. like f[][a]
|
||||
// The end is slice + map. like v[][a]
|
||||
if keys[1] == "" && length > 2 && keys[2] != "" {
|
||||
val, ok := result[key]
|
||||
if !ok {
|
||||
@ -144,7 +144,7 @@ func build(result map[string]interface{}, keys []string, value interface{}) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// map. like f[a], f[a][b]
|
||||
// map, like v[a], v[a][b]
|
||||
val, ok := result[key]
|
||||
if !ok {
|
||||
result[key] = map[string]interface{}{}
|
||||
|
||||
@ -20,6 +20,7 @@ func Test_CamelCase(t *testing.T) {
|
||||
{"test", "Test"},
|
||||
{"TestCase", "TestCase"},
|
||||
{" test case ", "TestCase"},
|
||||
{"userLogin_log.bak", "UserLoginLogBak"},
|
||||
{"", ""},
|
||||
{"many_many_words", "ManyManyWords"},
|
||||
{"AnyKind of_string", "AnyKindOfString"},
|
||||
|
||||
@ -18,8 +18,14 @@ import (
|
||||
|
||||
func Test_Parse(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
// name overwrite
|
||||
m, err := gstr.Parse("a=1&a=2")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m, g.Map{
|
||||
"a": 2,
|
||||
})
|
||||
// slice
|
||||
m, err := gstr.Parse("a[]=1&a[]=2")
|
||||
m, err = gstr.Parse("a[]=1&a[]=2")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m, g.Map{
|
||||
"a": g.Slice{"1", "2"},
|
||||
@ -32,6 +38,12 @@ func Test_Parse(t *testing.T) {
|
||||
"b": "2",
|
||||
"c": "3",
|
||||
})
|
||||
m, err = gstr.Parse("a=1&a=2&c=3")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m, g.Map{
|
||||
"a": "2",
|
||||
"c": "3",
|
||||
})
|
||||
// map
|
||||
m, err = gstr.Parse("m[a]=1&m[b]=2&m[c]=3")
|
||||
gtest.Assert(err, nil)
|
||||
@ -42,6 +54,14 @@ func Test_Parse(t *testing.T) {
|
||||
"c": "3",
|
||||
},
|
||||
})
|
||||
m, err = gstr.Parse("m[a]=1&m[a]=2&m[b]=3")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m, g.Map{
|
||||
"m": g.Map{
|
||||
"a": "2",
|
||||
"b": "3",
|
||||
},
|
||||
})
|
||||
// map - slice
|
||||
m, err = gstr.Parse("m[a][]=1&m[a][]=2")
|
||||
gtest.Assert(err, nil)
|
||||
@ -50,6 +70,15 @@ func Test_Parse(t *testing.T) {
|
||||
"a": g.Slice{"1", "2"},
|
||||
},
|
||||
})
|
||||
m, err = gstr.Parse("m[a][b][]=1&m[a][b][]=2")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m, g.Map{
|
||||
"m": g.Map{
|
||||
"a": g.Map{
|
||||
"b": g.Slice{"1", "2"},
|
||||
},
|
||||
},
|
||||
})
|
||||
// map - complicated
|
||||
m, err = gstr.Parse("m[a1][b1][c1][d1]=1&m[a2][b2]=2&m[a3][b3][c3]=3")
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
@ -181,21 +181,21 @@ func MapDeep(value interface{}, tags ...string) map[string]interface{} {
|
||||
return data
|
||||
}
|
||||
|
||||
// MapStruct converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of struct/*struct.
|
||||
func MapStruct(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapStruct(params, pointer, false, mapping...)
|
||||
// MapToMap converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of *map.
|
||||
func MapToMap(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapToMap(params, pointer, false, mapping...)
|
||||
}
|
||||
|
||||
// MapStructDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of struct/*struct.
|
||||
func MapStructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapStruct(params, pointer, true, mapping...)
|
||||
// MapToMapDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of *map.
|
||||
func MapToMapDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapToMap(params, pointer, true, mapping...)
|
||||
}
|
||||
|
||||
// doMapStruct converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of struct/*struct.
|
||||
func doMapStruct(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) error {
|
||||
// The elements of <pointer> should be type of *map.
|
||||
func doMapToMap(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) error {
|
||||
paramsRv := reflect.ValueOf(params)
|
||||
paramsKind := paramsRv.Kind()
|
||||
if paramsKind == reflect.Ptr {
|
||||
@ -240,21 +240,21 @@ func doMapStruct(params interface{}, pointer interface{}, deep bool, mapping ...
|
||||
return nil
|
||||
}
|
||||
|
||||
// MapStructs converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []struct/[]*struct.
|
||||
func MapStructs(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapStructs(params, pointer, false, mapping...)
|
||||
// MapToMaps converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []map/*map.
|
||||
func MapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapToMaps(params, pointer, false, mapping...)
|
||||
}
|
||||
|
||||
// MapStructsDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []struct/[]*struct.
|
||||
func MapStructsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapStructs(params, pointer, true, mapping...)
|
||||
// MapToMapsDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []map/*map.
|
||||
func MapToMapsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error {
|
||||
return doMapToMaps(params, pointer, true, mapping...)
|
||||
}
|
||||
|
||||
// doMapStructs converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []struct/[]*struct.
|
||||
func doMapStructs(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) error {
|
||||
// doMapToMaps converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []map/*map.
|
||||
func doMapToMaps(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) error {
|
||||
paramsRv := reflect.ValueOf(params)
|
||||
paramsKind := paramsRv.Kind()
|
||||
if paramsKind == reflect.Ptr {
|
||||
|
||||
@ -161,7 +161,7 @@ func Test_Map_StructInherit(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MapStruct(t *testing.T) {
|
||||
func Test_MapToMap(t *testing.T) {
|
||||
type User struct {
|
||||
Id int
|
||||
Name string
|
||||
@ -174,7 +174,7 @@ func Test_MapStruct(t *testing.T) {
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[string]User)
|
||||
err := gconv.MapStruct(params, &m)
|
||||
err := gconv.MapToMap(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 1)
|
||||
gtest.Assert(m["key"].Id, 1)
|
||||
@ -182,7 +182,7 @@ func Test_MapStruct(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := (map[string]User)(nil)
|
||||
err := gconv.MapStruct(params, &m)
|
||||
err := gconv.MapToMap(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 1)
|
||||
gtest.Assert(m["key"].Id, 1)
|
||||
@ -190,7 +190,7 @@ func Test_MapStruct(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[string]*User)
|
||||
err := gconv.MapStruct(params, &m)
|
||||
err := gconv.MapToMap(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 1)
|
||||
gtest.Assert(m["key"].Id, 1)
|
||||
@ -198,7 +198,7 @@ func Test_MapStruct(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := (map[string]*User)(nil)
|
||||
err := gconv.MapStruct(params, &m)
|
||||
err := gconv.MapToMap(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 1)
|
||||
gtest.Assert(m["key"].Id, 1)
|
||||
@ -206,7 +206,7 @@ func Test_MapStruct(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MapStructDeep(t *testing.T) {
|
||||
func Test_MapToMapDeep(t *testing.T) {
|
||||
type Ids struct {
|
||||
Id int
|
||||
Uid int
|
||||
@ -227,7 +227,7 @@ func Test_MapStructDeep(t *testing.T) {
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
m := (map[string]*User)(nil)
|
||||
err := gconv.MapStruct(params, &m)
|
||||
err := gconv.MapToMap(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 1)
|
||||
gtest.Assert(m["key"].Id, 0)
|
||||
@ -235,7 +235,7 @@ func Test_MapStructDeep(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := (map[string]*User)(nil)
|
||||
err := gconv.MapStructDeep(params, &m)
|
||||
err := gconv.MapToMapDeep(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 1)
|
||||
gtest.Assert(m["key"].Id, 1)
|
||||
@ -243,7 +243,7 @@ func Test_MapStructDeep(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MapStructs1(t *testing.T) {
|
||||
func Test_MapToMaps1(t *testing.T) {
|
||||
type User struct {
|
||||
Id int
|
||||
Name int
|
||||
@ -260,7 +260,7 @@ func Test_MapStructs1(t *testing.T) {
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[string][]User)
|
||||
err := gconv.MapStructs(params, &m)
|
||||
err := gconv.MapToMaps(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m["key1"][0].Id, 1)
|
||||
@ -270,7 +270,7 @@ func Test_MapStructs1(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := (map[string][]User)(nil)
|
||||
err := gconv.MapStructs(params, &m)
|
||||
err := gconv.MapToMaps(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m["key1"][0].Id, 1)
|
||||
@ -280,7 +280,7 @@ func Test_MapStructs1(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[string][]*User)
|
||||
err := gconv.MapStructs(params, &m)
|
||||
err := gconv.MapToMaps(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m["key1"][0].Id, 1)
|
||||
@ -290,7 +290,7 @@ func Test_MapStructs1(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := (map[string][]*User)(nil)
|
||||
err := gconv.MapStructs(params, &m)
|
||||
err := gconv.MapToMaps(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m["key1"][0].Id, 1)
|
||||
@ -300,7 +300,7 @@ func Test_MapStructs1(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MapStructs2(t *testing.T) {
|
||||
func Test_MapToMaps2(t *testing.T) {
|
||||
type User struct {
|
||||
Id int
|
||||
Name int
|
||||
@ -317,7 +317,7 @@ func Test_MapStructs2(t *testing.T) {
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[int][]User)
|
||||
err := gconv.MapStructs(params, &m)
|
||||
err := gconv.MapToMaps(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m[100][0].Id, 1)
|
||||
@ -327,7 +327,7 @@ func Test_MapStructs2(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[int][]*User)
|
||||
err := gconv.MapStructs(params, &m)
|
||||
err := gconv.MapToMaps(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m[100][0].Id, 1)
|
||||
@ -337,7 +337,7 @@ func Test_MapStructs2(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[string][]*User)
|
||||
err := gconv.MapStructs(params, &m)
|
||||
err := gconv.MapToMaps(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m["100"][0].Id, 1)
|
||||
@ -347,7 +347,7 @@ func Test_MapStructs2(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_MapStructsDeep(t *testing.T) {
|
||||
func Test_MapToMapsDeep(t *testing.T) {
|
||||
type Ids struct {
|
||||
Id int
|
||||
Uid int
|
||||
@ -372,7 +372,7 @@ func Test_MapStructsDeep(t *testing.T) {
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[string][]*User)
|
||||
err := gconv.MapStructs(params, &m)
|
||||
err := gconv.MapToMaps(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m["100"][0].Id, 0)
|
||||
@ -386,7 +386,7 @@ func Test_MapStructsDeep(t *testing.T) {
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
m := make(map[string][]*User)
|
||||
err := gconv.MapStructsDeep(params, &m)
|
||||
err := gconv.MapToMapsDeep(params, &m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(m), 2)
|
||||
gtest.Assert(m["100"][0].Id, 1)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package gf
|
||||
|
||||
const VERSION = "v2.0.0"
|
||||
const VERSION = "v1.9.1"
|
||||
const AUTHORS = "john<john@goframe.org>"
|
||||
|
||||
Reference in New Issue
Block a user