mirror of
https://github.com/fumiama/tienyik.git
synced 2026-06-27 06:10:29 +08:00
feat: add log & supporting packages
This commit is contained in:
70
internal/textio/api.go
Normal file
70
internal/textio/api.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package textio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/fumiama/tienyik"
|
||||
)
|
||||
|
||||
func API() string {
|
||||
pc, f, _, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
panic("cannot get api of caller")
|
||||
}
|
||||
if strings.Contains(f, "\\") {
|
||||
f = strings.ReplaceAll(f, "\\", "/")
|
||||
}
|
||||
_, p, ok := strings.Cut(f, "/tienyik/")
|
||||
if !ok {
|
||||
panic("cannot cut api " + f + " of caller")
|
||||
}
|
||||
f = strings.TrimSuffix(p, ".go")
|
||||
fn := runtime.FuncForPC(pc)
|
||||
if fn == nil {
|
||||
panic("cannot get func name of caller, api: " + f)
|
||||
}
|
||||
p = fn.Name()
|
||||
i := strings.LastIndex(p, ".")
|
||||
if i < 0 {
|
||||
panic("func name of caller '" + p + " has no '.', api: " + f)
|
||||
}
|
||||
p = p[i+1:]
|
||||
if len(p) <= 1 {
|
||||
panic("func name of caller '" + p + " too short', api: " + f)
|
||||
}
|
||||
sb := strings.Builder{}
|
||||
sb.WriteString(f)
|
||||
sb.WriteByte('/')
|
||||
sb.WriteString(strings.ToLower(p[:1]))
|
||||
sb.WriteString(p[1:])
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func EUrlParams(tya *tienyik.AES, params url.Values) string {
|
||||
return url.Values{
|
||||
FuncName(1, true): {BytesToString(tya.Encrypt(
|
||||
StringToBytes(params.Encode()),
|
||||
))},
|
||||
}.Encode()
|
||||
}
|
||||
|
||||
func ParseQuery(tya *tienyik.AES, eparams string) (url.Values, error) {
|
||||
q, err := url.ParseQuery(eparams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(q) != 1 {
|
||||
return nil, errors.New("len(q) must be 1")
|
||||
}
|
||||
for _, v := range q {
|
||||
dec, err := tya.Decrypt(StringToBytes(v[0]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return url.ParseQuery(BytesToString(dec))
|
||||
}
|
||||
panic("unexpected")
|
||||
}
|
||||
Reference in New Issue
Block a user