2022-06-01 17:55:55 +03:00
|
|
|
package text
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/gojqex"
|
2022-06-01 17:55:55 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-12-21 15:59:54 +03:00
|
|
|
interp.RegisterFunc0("from_urlencode", func(_ *interp.Interp, c string) any {
|
2022-06-01 17:55:55 +03:00
|
|
|
u, err := url.QueryUnescape(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
})
|
2022-12-21 15:59:54 +03:00
|
|
|
interp.RegisterFunc0("to_urlencode", func(_ *interp.Interp, c string) any {
|
2022-06-01 17:55:55 +03:00
|
|
|
return url.QueryEscape(c)
|
|
|
|
})
|
|
|
|
|
2022-12-21 15:59:54 +03:00
|
|
|
interp.RegisterFunc0("from_urlpath", func(_ *interp.Interp, c string) any {
|
2022-06-01 17:55:55 +03:00
|
|
|
u, err := url.PathUnescape(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
})
|
2022-12-21 15:59:54 +03:00
|
|
|
interp.RegisterFunc0("to_urlpath", func(_ *interp.Interp, c string) any {
|
2022-06-01 17:55:55 +03:00
|
|
|
return url.PathEscape(c)
|
|
|
|
})
|
|
|
|
|
|
|
|
fromURLValues := func(q url.Values) any {
|
|
|
|
qm := map[string]any{}
|
|
|
|
for k, v := range q {
|
|
|
|
if len(v) > 1 {
|
|
|
|
vm := []any{}
|
|
|
|
for _, v := range v {
|
|
|
|
vm = append(vm, v)
|
|
|
|
}
|
|
|
|
qm[k] = vm
|
|
|
|
} else {
|
|
|
|
qm[k] = v[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return qm
|
|
|
|
}
|
2022-12-21 15:59:54 +03:00
|
|
|
interp.RegisterFunc0("from_urlquery", func(_ *interp.Interp, c string) any {
|
2022-06-01 17:55:55 +03:00
|
|
|
q, err := url.ParseQuery(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fromURLValues(q)
|
|
|
|
})
|
|
|
|
toURLValues := func(c map[string]any) url.Values {
|
|
|
|
qv := url.Values{}
|
|
|
|
for k, v := range c {
|
2022-08-12 16:27:51 +03:00
|
|
|
if va, ok := gojqex.Cast[[]any](v); ok {
|
2022-06-01 17:55:55 +03:00
|
|
|
var ss []string
|
|
|
|
for _, s := range va {
|
2022-08-12 16:27:51 +03:00
|
|
|
if s, ok := gojqex.Cast[string](s); ok {
|
2022-06-01 17:55:55 +03:00
|
|
|
ss = append(ss, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qv[k] = ss
|
2022-08-12 16:27:51 +03:00
|
|
|
} else if vs, ok := gojqex.Cast[string](v); ok {
|
2022-06-01 17:55:55 +03:00
|
|
|
qv[k] = []string{vs}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return qv
|
|
|
|
}
|
2022-12-21 15:59:54 +03:00
|
|
|
interp.RegisterFunc0("to_urlquery", func(_ *interp.Interp, c map[string]any) any {
|
2022-06-01 17:55:55 +03:00
|
|
|
// TODO: nicer
|
2022-08-12 16:27:51 +03:00
|
|
|
c, ok := gojqex.NormalizeToStrings(c).(map[string]any)
|
2022-06-01 17:55:55 +03:00
|
|
|
if !ok {
|
|
|
|
panic("not map")
|
|
|
|
}
|
|
|
|
return toURLValues(c).Encode()
|
|
|
|
})
|
|
|
|
|
2022-12-21 15:59:54 +03:00
|
|
|
interp.RegisterFunc0("from_url", func(_ *interp.Interp, c string) any {
|
2022-06-01 17:55:55 +03:00
|
|
|
u, err := url.Parse(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]any{}
|
|
|
|
if u.Scheme != "" {
|
|
|
|
m["scheme"] = u.Scheme
|
|
|
|
}
|
|
|
|
if u.User != nil {
|
|
|
|
um := map[string]any{
|
|
|
|
"username": u.User.Username(),
|
|
|
|
}
|
|
|
|
if p, ok := u.User.Password(); ok {
|
|
|
|
um["password"] = p
|
|
|
|
}
|
|
|
|
m["user"] = um
|
|
|
|
}
|
|
|
|
if u.Host != "" {
|
|
|
|
m["host"] = u.Host
|
|
|
|
}
|
|
|
|
if u.Path != "" {
|
|
|
|
m["path"] = u.Path
|
|
|
|
}
|
|
|
|
if u.RawPath != "" {
|
|
|
|
m["rawpath"] = u.RawPath
|
|
|
|
}
|
|
|
|
if u.RawQuery != "" {
|
|
|
|
m["rawquery"] = u.RawQuery
|
|
|
|
m["query"] = fromURLValues(u.Query())
|
|
|
|
}
|
|
|
|
if u.Fragment != "" {
|
|
|
|
m["fragment"] = u.Fragment
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
})
|
2022-12-21 15:59:54 +03:00
|
|
|
interp.RegisterFunc0("to_url", func(_ *interp.Interp, c map[string]any) any {
|
2022-06-01 17:55:55 +03:00
|
|
|
// TODO: nicer
|
2022-08-12 16:27:51 +03:00
|
|
|
c, ok := gojqex.NormalizeToStrings(c).(map[string]any)
|
2022-06-01 17:55:55 +03:00
|
|
|
if !ok {
|
|
|
|
panic("not map")
|
|
|
|
}
|
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
str := func(v any) string { s, _ := gojqex.Cast[string](v); return s }
|
2022-06-01 17:55:55 +03:00
|
|
|
u := url.URL{
|
|
|
|
Scheme: str(c["scheme"]),
|
|
|
|
Host: str(c["host"]),
|
|
|
|
Path: str(c["path"]),
|
|
|
|
Fragment: str(c["fragment"]),
|
|
|
|
}
|
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
if um, ok := gojqex.Cast[map[string]any](c["user"]); ok {
|
2022-06-01 17:55:55 +03:00
|
|
|
username, password := str(um["username"]), str(um["password"])
|
|
|
|
if username != "" {
|
|
|
|
if password == "" {
|
|
|
|
u.User = url.User(username)
|
|
|
|
} else {
|
|
|
|
u.User = url.UserPassword(username, password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 16:27:51 +03:00
|
|
|
if s, ok := gojqex.Cast[string](c["rawquery"]); ok {
|
2022-06-01 17:55:55 +03:00
|
|
|
u.RawQuery = s
|
|
|
|
}
|
2022-08-12 16:27:51 +03:00
|
|
|
if qm, ok := gojqex.Cast[map[string]any](c["query"]); ok {
|
2022-06-01 17:55:55 +03:00
|
|
|
u.RawQuery = toURLValues(qm).Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.String()
|
|
|
|
})
|
|
|
|
}
|