代码拉取完成,页面将自动刷新
package gbase
import (
"encoding/xml"
"github.com/cloudwego/hertz/pkg/app/server/binding"
jsoniter "github.com/json-iterator/go"
"strings"
)
// 全局基类
type Base struct { /*这里不能写字段名*/
}
func (this *Base) ToString(child any) string {
str, _ := jsoniter.MarshalToString(child)
return str
}
func (this *Base) ToBytes(child any) []byte {
bytes, _ := jsoniter.Marshal(child)
return bytes
}
func (this *Base) ToMap(child any) (cm Map) {
cm = Map{}
_ = cm.FromX(child)
return
}
func (this *Base) ToMapExclude(child any, keys ...string) (cm Map) {
return this.ToMap(child).RemoveKeys(keys...)
}
func (this *Base) ToMapInclude(child any, keys ...string) (cm Map) {
all := this.ToMap(child)
cm = Map{}
for _, v := range keys {
if all.Exist(v) {
cm[v] = all[v]
}
}
return
}
func (this *Base) FromForm(child any, formStr string) (err error) {
list := strings.Split(formStr, "&")
m := make(map[any]any)
if len(list) > 0 {
for _, v := range list {
cur := strings.Split(v, "=")
if len(cur) == 2 {
m[cur[0]] = cur[1]
}
}
}
if len(m) > 0 {
if bytes, err := jsoniter.Marshal(m); err != nil {
return err
} else {
if err = jsoniter.Unmarshal(bytes, child); err != nil {
return err
}
}
}
return
}
// from string,[]byte,map,struct
func (this *Base) FromX(child any, params any) error {
var (
jsonBytes []byte
err error
//jsonStr string
)
switch params.(type) {
case string:
jsonBytes = String(params.(string)).ToBytes()
case []byte:
//do nothing
jsonBytes = params.([]byte)
default:
if jsonBytes, err = jsoniter.Marshal(params); err != nil {
return err
}
}
if err = jsoniter.Unmarshal(jsonBytes, child); err != nil {
return err
}
return nil
}
func (this *Base) FromXml(child any, xmlStr string) error {
return xml.Unmarshal([]byte(xmlStr), child)
}
func (this *Base) Vd(child any) error {
return binding.Validate(child)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。