commit
fea7a5fc82
3 changed files with 137 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/yaoapp/yao/utils/datetime"
|
"github.com/yaoapp/yao/utils/datetime"
|
||||||
"github.com/yaoapp/yao/utils/str"
|
"github.com/yaoapp/yao/utils/str"
|
||||||
"github.com/yaoapp/yao/utils/tree"
|
"github.com/yaoapp/yao/utils/tree"
|
||||||
|
"github.com/yaoapp/yao/utils/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Init the utils
|
// Init the utils
|
||||||
|
|
@ -83,4 +84,9 @@ func Init() {
|
||||||
process.Register("utils.now.DateTime", datetime.ProcessDateTime)
|
process.Register("utils.now.DateTime", datetime.ProcessDateTime)
|
||||||
process.Register("utils.now.Timestamp", datetime.ProcessTimestamp)
|
process.Register("utils.now.Timestamp", datetime.ProcessTimestamp)
|
||||||
process.Register("utils.now.Timestampms", datetime.ProcessTimestampms)
|
process.Register("utils.now.Timestampms", datetime.ProcessTimestampms)
|
||||||
|
|
||||||
|
// URL
|
||||||
|
process.Register("utils.url.ParseQuery", url.ProcessParseQuery)
|
||||||
|
process.Register("utils.url.QueryParam", url.ProcessQueryParam)
|
||||||
|
process.Register("utils.url.ParseURL", url.ProcessParseURL)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
76
utils/url/url.go
Normal file
76
utils/url/url.go
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
package url
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/yaoapp/gou/process"
|
||||||
|
"github.com/yaoapp/gou/types"
|
||||||
|
"github.com/yaoapp/kun/exception"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProcessParseQuery utils.url.ParseQuery
|
||||||
|
func ProcessParseQuery(process *process.Process) interface{} {
|
||||||
|
process.ValidateArgNums(1)
|
||||||
|
queryString := process.ArgsString(0)
|
||||||
|
params, err := url.ParseQuery(queryString)
|
||||||
|
if err != nil {
|
||||||
|
exception.New("make audio captcha error: %s", 500, err).Throw()
|
||||||
|
}
|
||||||
|
return params
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProcessParseURL utils.url.ParseURL
|
||||||
|
func ProcessParseURL(process *process.Process) interface{} {
|
||||||
|
process.ValidateArgNums(1)
|
||||||
|
rawurl := process.ArgsString(0)
|
||||||
|
u, err := url.Parse(rawurl)
|
||||||
|
if err != nil {
|
||||||
|
exception.New("parse url error: %s", 500, err).Throw()
|
||||||
|
}
|
||||||
|
return map[string]interface{}{
|
||||||
|
"scheme": u.Scheme,
|
||||||
|
"host": u.Host,
|
||||||
|
"domain": u.Hostname(),
|
||||||
|
"path": u.Path,
|
||||||
|
"port": u.Port(),
|
||||||
|
"query": u.Query(),
|
||||||
|
"url": u.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProcessQueryParam handle the get Template request
|
||||||
|
func ProcessQueryParam(process *process.Process) interface{} {
|
||||||
|
process.ValidateArgNums(1)
|
||||||
|
switch v := process.Args[0].(type) {
|
||||||
|
|
||||||
|
case url.Values:
|
||||||
|
return types.URLToQueryParam(v)
|
||||||
|
|
||||||
|
case map[string][]string:
|
||||||
|
return types.URLToQueryParam(v)
|
||||||
|
|
||||||
|
case map[string]interface{}:
|
||||||
|
values := url.Values{}
|
||||||
|
for key, value := range v {
|
||||||
|
switch val := value.(type) {
|
||||||
|
case []string:
|
||||||
|
for _, v := range val {
|
||||||
|
values.Add(key, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
case []interface{}:
|
||||||
|
for _, v := range val {
|
||||||
|
values.Add(key, fmt.Sprintf("%v", v))
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
values.Set(key, fmt.Sprintf("%v", value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return types.URLToQueryParam(values)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, _ := types.AnyToQueryParam(process.Args[0])
|
||||||
|
return v
|
||||||
|
}
|
||||||
55
utils/url_test.go
Normal file
55
utils/url_test.go
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/yaoapp/gou/process"
|
||||||
|
"github.com/yaoapp/gou/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProcessParseQuery(t *testing.T) {
|
||||||
|
Init()
|
||||||
|
args := []interface{}{"a=1&b=2&c=3&c=4"}
|
||||||
|
result, err := process.New("utils.url.ParseQuery", args...).Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("ProcessParseQuery error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, result.(url.Values).Get("a"), "1")
|
||||||
|
assert.Equal(t, result.(url.Values).Get("b"), "2")
|
||||||
|
assert.Equal(t, result.(url.Values)["c"], []string{"3", "4"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessParseURL(t *testing.T) {
|
||||||
|
Init()
|
||||||
|
args := []interface{}{"http://www.google.com:8080/search?q=dotnet"}
|
||||||
|
result, err := process.New("utils.url.ParseURL", args...).Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("ProcessParseURL error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, result.(map[string]interface{})["scheme"], "http")
|
||||||
|
assert.Equal(t, result.(map[string]interface{})["host"], "www.google.com:8080")
|
||||||
|
assert.Equal(t, result.(map[string]interface{})["domain"], "www.google.com")
|
||||||
|
assert.Equal(t, result.(map[string]interface{})["path"], "/search")
|
||||||
|
assert.Equal(t, result.(map[string]interface{})["port"], "8080")
|
||||||
|
assert.Equal(t, result.(map[string]interface{})["query"].(url.Values).Get("q"), "dotnet")
|
||||||
|
assert.Equal(t, result.(map[string]interface{})["url"], "http://www.google.com:8080/search?q=dotnet")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessQueryParam(t *testing.T) {
|
||||||
|
Init()
|
||||||
|
args := []interface{}{
|
||||||
|
map[string]interface{}{
|
||||||
|
"where.name.eq": "yao",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
result, err := process.New("utils.url.QueryParam", args...).Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("ProcessQueryParam error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Len(t, result.(types.QueryParam).Wheres, 1)
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue