1
0
mirror of https://github.com/fumiama/tienyik.git synced 2026-06-04 23:10:26 +08:00
Files
tienyik/internal/horm/req.go
2025-11-24 17:35:20 +08:00

42 lines
927 B
Go

package horm
import (
"net/url"
"reflect"
"strconv"
"github.com/fumiama/tienyik"
"github.com/fumiama/tienyik/internal/textio"
)
func Marshal(tya *tienyik.AES, x any) []byte {
q := url.Values{}
v := reflect.ValueOf(x).Elem()
t := v.Type()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldType := t.Field(i)
formTag := fieldType.Tag.Get("form")
if formTag == "" || formTag == "-" {
continue
}
switch field.Kind() {
case reflect.String:
q.Set(formTag, field.String())
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint, reflect.Uintptr:
q.Set(formTag, strconv.FormatUint(field.Uint(), 10))
case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
q.Set(formTag, strconv.FormatInt(field.Int(), 10))
}
}
s := q.Encode()
if tya != nil {
return tya.Encrypt(textio.StringToBytes(s))
}
return textio.StringToBytes(s)
}