xgoprev/httpx/url.go

76 lines
1.1 KiB
Go

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" :
return &Values{
Values: u.Query(),
}, nil
}
// Nothing found.
return nil, nil
}