重构: global_params.go/global_params_support.go 合并为 gparams.go

This commit is contained in:
2026-07-22 09:11:00 +08:00
parent 6d0c01c21f
commit a0ec297b61
3 changed files with 285 additions and 286 deletions
-55
View File
@@ -1,55 +0,0 @@
package req
import (
"time"
)
type GlobalParams interface {
User() User
// 获取路径内容
Get(p string) GlobalParams
// 路径包裹
Wrapped(p string) GlobalParams
// 追加到新 json
AppendTo(root, p string) GlobalParams
// 通过路径设置参数
Set(p string, v any) bool
// 通过路径设置原始参数
SetRaw(p string, v string) bool
// 删除路径参数
Delete(p string) bool
// 获取原始JOSN字符串
Raw() string
// 转数组 GlobalParams
Array() []GlobalParams
// 判断内容是否存在
Exists() bool
Value() any
Bool() bool
Time() time.Time
TimeInDefaultLocation(l *time.Location) time.Time
Float64() float64
Float32() float32
Int64() int64
Int32() int32
Int16() int16
Int8() int8
Int() int
Uint() uint
Uint64() uint64
Uint32() uint32
Uint16() uint16
Uint8() uint8
String() string
StringMapString() map[string]string
StringMapStringSlice() map[string][]string
StringMapBool() map[string]bool
StringMapInt() map[string]int
StringMapInt64() map[string]int64
StringMap() map[string]any
Slice() []any
BoolSlice() []bool
StringSlice() []string
IntSlice() []int
}
-231
View File
@@ -1,231 +0,0 @@
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)}
}
Executable
+285
View File
@@ -0,0 +1,285 @@
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
}