2024-05-06 21:08:56 +03:00
|
|
|
package httpx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/d5/tengo/v2"
|
|
|
|
"net/url"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = tengo.Object(&Values{})
|
|
|
|
type Values struct {
|
|
|
|
tengo.ObjectImpl
|
|
|
|
url.Values
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *Values) TypeName() string {
|
|
|
|
return "*url.Values"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *Values) String() string {
|
|
|
|
return fmt.Sprintf("%v", vs.Values)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *Values) IndexGet(
|
|
|
|
index tengo.Object,
|
|
|
|
) (tengo.Object, error) {
|
|
|
|
key, ok := tengo.ToString(index)
|
|
|
|
if !ok {
|
|
|
|
return nil, tengo.ErrInvalidIndexValueType
|
|
|
|
}
|
|
|
|
|
|
|
|
val, ok := vs.Values[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := make([]tengo.Object, len(val))
|
|
|
|
for i, v := range val {
|
|
|
|
arr[i], _ = tengo.FromInterface(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tengo.Array{Value: arr}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type URL struct {
|
|
|
|
tengo.ObjectImpl
|
|
|
|
*url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URL) TypeName() string {
|
|
|
|
return "<URL>"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URL) String() string {
|
|
|
|
return u.URL.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URL) IndexGet(
|
|
|
|
index tengo.Object,
|
|
|
|
) (tengo.Object, error) {
|
|
|
|
key, ok := tengo.ToString(index)
|
|
|
|
if !ok {
|
|
|
|
return nil, tengo.ErrInvalidIndexValueType
|
|
|
|
}
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "path" :
|
|
|
|
return tengo.FromInterface(u.Path)
|
|
|
|
case "query" :
|
2024-05-17 23:55:02 +03:00
|
|
|
return &Values{
|
2024-05-06 21:08:56 +03:00
|
|
|
Values: u.Query(),
|
2024-05-17 23:55:02 +03:00
|
|
|
}, nil
|
2024-05-06 21:08:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing found.
|
|
|
|
return nil, nil
|
|
|
|
}
|