first commit
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
package req
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
type iGlobalParams struct {
|
||||
user *User
|
||||
gjson gjson.Result
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
func (this iGlobalParams) User() User {
|
||||
return *this.user
|
||||
}
|
||||
|
||||
// 获取指定路径的 value
|
||||
func (this iGlobalParams) Get(key string) GlobalParams {
|
||||
return &iGlobalParams{
|
||||
user: this.user,
|
||||
gjson: this.gjson.Get(key),
|
||||
}
|
||||
}
|
||||
|
||||
func (this iGlobalParams) Array() []GlobalParams {
|
||||
return lo.Map(this.gjson.Array(), func(item gjson.Result, _ int) GlobalParams {
|
||||
return &iGlobalParams{user: this.user, gjson: gjson.Parse(item.Raw)}
|
||||
})
|
||||
}
|
||||
|
||||
// 获取golang原始类型
|
||||
func (this iGlobalParams) Value() any {
|
||||
return this.gjson.Value()
|
||||
}
|
||||
|
||||
func (this iGlobalParams) Raw() string {
|
||||
return this.gjson.Raw
|
||||
}
|
||||
|
||||
// 设置json值
|
||||
func (this *iGlobalParams) Set(p string, v any) bool {
|
||||
if s, err := sjson.Set(this.Raw(), p, v); err != nil {
|
||||
return false
|
||||
} else {
|
||||
this.gjson = gjson.Parse(s)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 设置json原始值
|
||||
func (this *iGlobalParams) SetRaw(p, v string) bool {
|
||||
if s, err := sjson.SetRaw(this.Raw(), p, v); err != nil {
|
||||
return false
|
||||
} else {
|
||||
this.gjson = gjson.Parse(s)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 删除路径内容
|
||||
func (this *iGlobalParams) Delete(p string) bool {
|
||||
if s, err := sjson.Delete(this.Raw(), p); err != nil {
|
||||
return false
|
||||
} else {
|
||||
this.gjson = gjson.Parse(s)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 路径包裹
|
||||
func (this *iGlobalParams) Wrapped(p string) GlobalParams {
|
||||
return this.AppendTo("", p)
|
||||
}
|
||||
|
||||
// 包裹内容
|
||||
func (this *iGlobalParams) AppendTo(root, p string) GlobalParams {
|
||||
if root != "" && !gjson.Valid(root) {
|
||||
root = ""
|
||||
}
|
||||
|
||||
json := lo.Must(sjson.SetRaw(root, p, this.Raw()))
|
||||
|
||||
return &iGlobalParams{
|
||||
user: this.user,
|
||||
gjson: gjson.Parse(json),
|
||||
}
|
||||
}
|
||||
|
||||
// 判断值是否存在
|
||||
func (this iGlobalParams) Exists() bool {
|
||||
return this.gjson.Exists()
|
||||
}
|
||||
|
||||
// to Bool
|
||||
func (this iGlobalParams) Bool() bool {
|
||||
return cast.ToBool(this.Value())
|
||||
}
|
||||
|
||||
// to Time
|
||||
func (this iGlobalParams) Time() time.Time {
|
||||
return cast.ToTime(this.Value())
|
||||
}
|
||||
|
||||
// to Location Time
|
||||
func (this iGlobalParams) TimeInDefaultLocation(location *time.Location) time.Time {
|
||||
return cast.ToTimeInDefaultLocation(this, location)
|
||||
}
|
||||
|
||||
// to float64
|
||||
func (this iGlobalParams) Float64() float64 {
|
||||
return cast.ToFloat64(this.Value())
|
||||
}
|
||||
|
||||
// to float32
|
||||
func (this iGlobalParams) Float32() float32 {
|
||||
return cast.ToFloat32(this.Value())
|
||||
}
|
||||
|
||||
// to int64
|
||||
func (this iGlobalParams) Int64() int64 {
|
||||
return cast.ToInt64(this.Value())
|
||||
}
|
||||
|
||||
// to int32
|
||||
func (this iGlobalParams) Int32() int32 {
|
||||
return cast.ToInt32(this.Value())
|
||||
}
|
||||
|
||||
// to int16
|
||||
func (this iGlobalParams) Int16() int16 {
|
||||
return cast.ToInt16(this.Value())
|
||||
}
|
||||
|
||||
// to int8
|
||||
func (this iGlobalParams) Int8() int8 {
|
||||
return cast.ToInt8(this.Value())
|
||||
}
|
||||
|
||||
// to int
|
||||
func (this iGlobalParams) Int() int {
|
||||
return cast.ToInt(this.Value())
|
||||
}
|
||||
|
||||
// to uint
|
||||
func (this iGlobalParams) Uint() uint {
|
||||
return cast.ToUint(this.Value())
|
||||
}
|
||||
|
||||
// to uint64
|
||||
func (this iGlobalParams) Uint64() uint64 {
|
||||
return cast.ToUint64(this.Value())
|
||||
}
|
||||
|
||||
// to uint32
|
||||
func (this iGlobalParams) Uint32() uint32 {
|
||||
return cast.ToUint32(this.Value())
|
||||
}
|
||||
|
||||
// to uint16
|
||||
func (this iGlobalParams) Uint16() uint16 {
|
||||
return cast.ToUint16(this.Value())
|
||||
}
|
||||
|
||||
// to uint8
|
||||
func (this iGlobalParams) Uint8() uint8 {
|
||||
return cast.ToUint8(this.Value())
|
||||
}
|
||||
|
||||
// to string
|
||||
func (this iGlobalParams) String() string {
|
||||
return cast.ToString(this.Value())
|
||||
}
|
||||
|
||||
// to map[string]string
|
||||
func (this iGlobalParams) StringMapString() map[string]string {
|
||||
return cast.ToStringMapString(this.Value())
|
||||
}
|
||||
|
||||
// to map[string][]string
|
||||
func (this iGlobalParams) StringMapStringSlice() map[string][]string {
|
||||
return cast.ToStringMapStringSlice(this.Value())
|
||||
}
|
||||
|
||||
// to map[string]bool
|
||||
func (this iGlobalParams) StringMapBool() map[string]bool {
|
||||
return cast.ToStringMapBool(this.Value())
|
||||
}
|
||||
|
||||
// to map[string]int
|
||||
func (this iGlobalParams) StringMapInt() map[string]int {
|
||||
return cast.ToStringMapInt(this.Value())
|
||||
}
|
||||
|
||||
// to map[string]int64
|
||||
func (this iGlobalParams) StringMapInt64() map[string]int64 {
|
||||
return cast.ToStringMapInt64(this.Value())
|
||||
}
|
||||
|
||||
// to map[string]any
|
||||
func (this iGlobalParams) StringMap() map[string]any {
|
||||
return cast.ToStringMap(this.Value())
|
||||
}
|
||||
|
||||
// to []any
|
||||
func (this iGlobalParams) Slice() []any {
|
||||
return cast.ToSlice(this.Value())
|
||||
}
|
||||
|
||||
// to []bool
|
||||
func (this iGlobalParams) BoolSlice() []bool {
|
||||
return cast.ToBoolSlice(this.Value())
|
||||
}
|
||||
|
||||
// to []string
|
||||
func (this iGlobalParams) StringSlice() []string {
|
||||
return cast.ToStringSlice(this.Value())
|
||||
}
|
||||
|
||||
// to []int
|
||||
func (this iGlobalParams) IntSlice() []int {
|
||||
return cast.ToIntSlice(this.Value())
|
||||
}
|
||||
|
||||
func NewGlobalParam(data string, user User) GlobalParams {
|
||||
return &iGlobalParams{user: &user, gjson: gjson.Parse(data)}
|
||||
}
|
||||
Reference in New Issue
Block a user