286 lines
5.6 KiB
Go
Executable File
286 lines
5.6 KiB
Go
Executable File
package req
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"git.fsdpf.net/go/reflux/valuex"
|
|
"github.com/samber/lo"
|
|
"github.com/spf13/cast"
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/sjson"
|
|
)
|
|
|
|
type GParams = GlobalParams
|
|
|
|
// GlobalParams 为全局参数访问与操作接口
|
|
type GlobalParams interface {
|
|
UserAccessor
|
|
|
|
Session() string
|
|
|
|
// 通过路径获取值
|
|
Get(path string) GlobalParams
|
|
// 路径包裹
|
|
Wrapped(p string) GlobalParams
|
|
// 追加到新 json
|
|
AppendTo(root, p string) GlobalParams
|
|
// 通过路径设置参数
|
|
Set(p string, v any) bool
|
|
// 通过路径设置原始参数
|
|
SetValue(p string, v string) bool
|
|
// 删除路径参数
|
|
Delete(p string) bool
|
|
// 转数组 GlobalParams
|
|
Array() []GlobalParams
|
|
// 判断内容是否存在
|
|
Exists() bool
|
|
}
|
|
|
|
type gparams struct {
|
|
session string
|
|
user *User
|
|
gjson gjson.Result
|
|
}
|
|
|
|
// 获取用户信息
|
|
func (this gparams) Session() string {
|
|
return this.session
|
|
}
|
|
|
|
// 获取用户信息
|
|
func (this gparams) User() User {
|
|
return *this.user
|
|
}
|
|
|
|
// 获取指定路径的 value
|
|
func (this gparams) Get(path string) GlobalParams {
|
|
return &gparams{
|
|
user: this.user,
|
|
session: this.session,
|
|
gjson: this.gjson.Get(path),
|
|
}
|
|
}
|
|
|
|
func (this gparams) Array() []GlobalParams {
|
|
return lo.Map(this.gjson.Array(), func(item gjson.Result, _ int) GlobalParams {
|
|
return &gparams{user: this.user, gjson: item, session: this.session}
|
|
})
|
|
}
|
|
|
|
func (this gparams) Lookup(path string) (valuex.Accessor, bool) {
|
|
v := this.gjson.Get(path)
|
|
if v.Exists() {
|
|
return this.Get(path), true
|
|
}
|
|
return valuex.Nil, false
|
|
}
|
|
|
|
func (this gparams) MustLookup(path string) valuex.Accessor {
|
|
if accessor, ok := this.Lookup(path); ok {
|
|
return accessor
|
|
}
|
|
return valuex.Nil
|
|
}
|
|
|
|
func (this gparams) Any() any {
|
|
return this.gjson.Value()
|
|
}
|
|
|
|
func (this gparams) Ptr() any {
|
|
return nil
|
|
}
|
|
|
|
func (this gparams) Raw() reflect.Value {
|
|
return reflect.ValueOf(this.gjson)
|
|
}
|
|
|
|
func (this gparams) Value() string {
|
|
return this.gjson.Raw
|
|
}
|
|
|
|
// 设置json值
|
|
func (this *gparams) Set(p string, v any) bool {
|
|
if s, err := sjson.Set(this.Value(), p, v); err != nil {
|
|
return false
|
|
} else {
|
|
this.gjson = gjson.Parse(s)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 设置json原始值
|
|
func (this *gparams) SetValue(p, v string) bool {
|
|
if s, err := sjson.SetRaw(this.Value(), p, v); err != nil {
|
|
return false
|
|
} else {
|
|
this.gjson = gjson.Parse(s)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 删除路径内容
|
|
func (this *gparams) Delete(p string) bool {
|
|
if s, err := sjson.Delete(this.Value(), p); err != nil {
|
|
return false
|
|
} else {
|
|
this.gjson = gjson.Parse(s)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 路径包裹
|
|
func (this *gparams) Wrapped(p string) GlobalParams {
|
|
return this.AppendTo("", p)
|
|
}
|
|
|
|
// 包裹内容
|
|
func (this *gparams) AppendTo(root, p string) GlobalParams {
|
|
if root != "" && !gjson.Valid(root) {
|
|
root = ""
|
|
}
|
|
|
|
json := lo.Must(sjson.SetRaw(root, p, this.Value()))
|
|
|
|
return &gparams{
|
|
user: this.user,
|
|
session: this.session,
|
|
gjson: gjson.Parse(json),
|
|
}
|
|
}
|
|
|
|
// 判断值是否存在
|
|
func (this gparams) Exists() bool {
|
|
return this.gjson.Exists()
|
|
}
|
|
|
|
// to Bool
|
|
func (this gparams) Bool() bool {
|
|
return cast.ToBool(this.Any())
|
|
}
|
|
|
|
// to float64
|
|
func (this gparams) Float64() float64 {
|
|
return cast.ToFloat64(this.Any())
|
|
}
|
|
|
|
// to float32
|
|
func (this gparams) Float32() float32 {
|
|
return cast.ToFloat32(this.Any())
|
|
}
|
|
|
|
// to int64
|
|
func (this gparams) Int64() int64 {
|
|
return cast.ToInt64(this.Any())
|
|
}
|
|
|
|
// to int32
|
|
func (this gparams) Int32() int32 {
|
|
return cast.ToInt32(this.Any())
|
|
}
|
|
|
|
// to int16
|
|
func (this gparams) Int16() int16 {
|
|
return cast.ToInt16(this.Any())
|
|
}
|
|
|
|
// to int8
|
|
func (this gparams) Int8() int8 {
|
|
return cast.ToInt8(this.Any())
|
|
}
|
|
|
|
// to int
|
|
func (this gparams) Int() int {
|
|
return cast.ToInt(this.Any())
|
|
}
|
|
|
|
// to uint
|
|
func (this gparams) Uint() uint {
|
|
return cast.ToUint(this.Any())
|
|
}
|
|
|
|
// to uint64
|
|
func (this gparams) Uint64() uint64 {
|
|
return cast.ToUint64(this.Any())
|
|
}
|
|
|
|
// to uint32
|
|
func (this gparams) Uint32() uint32 {
|
|
return cast.ToUint32(this.Any())
|
|
}
|
|
|
|
// to uint16
|
|
func (this gparams) Uint16() uint16 {
|
|
return cast.ToUint16(this.Any())
|
|
}
|
|
|
|
// to uint8
|
|
func (this gparams) Uint8() uint8 {
|
|
return cast.ToUint8(this.Any())
|
|
}
|
|
|
|
// to string
|
|
func (this gparams) String() string {
|
|
return cast.ToString(this.Any())
|
|
}
|
|
|
|
// to map[string]string
|
|
func (this gparams) StringMapString() map[string]string {
|
|
return cast.ToStringMapString(this.Any())
|
|
}
|
|
|
|
// to map[string][]string
|
|
func (this gparams) StringMapStringSlice() map[string][]string {
|
|
return cast.ToStringMapStringSlice(this.Any())
|
|
}
|
|
|
|
// to map[string]bool
|
|
func (this gparams) StringMapBool() map[string]bool {
|
|
return cast.ToStringMapBool(this.Any())
|
|
}
|
|
|
|
// to map[string]int
|
|
func (this gparams) StringMapInt() map[string]int {
|
|
return cast.ToStringMapInt(this.Any())
|
|
}
|
|
|
|
// to map[string]int64
|
|
func (this gparams) StringMapInt64() map[string]int64 {
|
|
return cast.ToStringMapInt64(this.Any())
|
|
}
|
|
|
|
// to map[string]any
|
|
func (this gparams) StringMap() map[string]any {
|
|
return cast.ToStringMap(this.Any())
|
|
}
|
|
|
|
// to []any
|
|
func (this gparams) Slice() []any {
|
|
return cast.ToSlice(this.Any())
|
|
}
|
|
|
|
// to []bool
|
|
func (this gparams) BoolSlice() []bool {
|
|
return cast.ToBoolSlice(this.Any())
|
|
}
|
|
|
|
// to []string
|
|
func (this gparams) StringSlice() []string {
|
|
return cast.ToStringSlice(this.Any())
|
|
}
|
|
|
|
// to []int
|
|
func (this gparams) IntSlice() []int {
|
|
return cast.ToIntSlice(this.Any())
|
|
}
|
|
|
|
// NewGlobalParams 创建一个新的 GlobalParams 实例
|
|
// data 为 JSON 字符串, user 为当前用户
|
|
// 可选的 opts 用于设置会话等附加信息
|
|
func NewGlobalParams(data string, user User, opts ...gParamsOpt) GlobalParams {
|
|
g := &gparams{user: &user, gjson: gjson.Parse(data)}
|
|
for _, opt := range opts {
|
|
opt(g)
|
|
}
|
|
return g
|
|
}
|