diff --git a/g/container/garray/garray_interface.go b/g/container/garray/garray_interface.go index 5699740f0..a74be51f4 100644 --- a/g/container/garray/garray_interface.go +++ b/g/container/garray/garray_interface.go @@ -30,32 +30,32 @@ func NewArray(size int, cap ... int) *Array { // 获取指定索引的数据项, 调用方注意判断数组边界 func (a *Array) Get(index int) interface{} { a.mu.RLock() + defer a.mu.RUnlock() value := a.array[index] - a.mu.RUnlock() return value } // 设置指定索引的数据项, 调用方注意判断数组边界 func (a *Array) Set(index int, value interface{}) { a.mu.Lock() + defer a.mu.Unlock() a.array[index] = value - a.mu.Unlock() } // 在当前索引位置前插入一个数据项, 调用方注意判断数组边界 func (a *Array) Insert(index int, value interface{}) { a.mu.Lock() + defer a.mu.Unlock() rear := append([]interface{}{}, a.array[index : ]...) a.array = append(a.array[0 : index], value) a.array = append(a.array, rear...) - a.mu.Unlock() } // 删除指定索引的数据项, 调用方注意判断数组边界 func (a *Array) Remove(index int) { a.mu.Lock() + defer a.mu.Unlock() a.array = append(a.array[ : index], a.array[index + 1 : ]...) - a.mu.Unlock() } // 追加数据项 diff --git a/g/container/garray/garray_sorted_int.go b/g/container/garray/garray_sorted_int.go index 1651b47cb..bdeca5460 100644 --- a/g/container/garray/garray_sorted_int.go +++ b/g/container/garray/garray_sorted_int.go @@ -70,16 +70,16 @@ func (a *SortedIntArray) Add(value int) { // 获取指定索引的数据项, 调用方注意判断数组边界 func (a *SortedIntArray) Get(index int) int { a.mu.RLock() + defer a.mu.RUnlock() value := a.array[index] - a.mu.RUnlock() return value } // 删除指定索引的数据项, 调用方注意判断数组边界 func (a *SortedIntArray) Remove(index int) { a.mu.Lock() + defer a.mu.Unlock() a.array = append(a.array[ : index], a.array[index + 1 : ]...) - a.mu.Unlock() } // 数组长度 diff --git a/g/container/garray/garray_sorted_interface.go b/g/container/garray/garray_sorted_interface.go index 35eeaa259..3e8704bea 100644 --- a/g/container/garray/garray_sorted_interface.go +++ b/g/container/garray/garray_sorted_interface.go @@ -55,16 +55,16 @@ func (a *SortedArray) Add(value interface{}) { // 获取指定索引的数据项, 调用方注意判断数组边界 func (a *SortedArray) Get(index int) interface{} { a.mu.RLock() + defer a.mu.RUnlock() value := a.array[index] - a.mu.RUnlock() return value } // 删除指定索引的数据项, 调用方注意判断数组边界 func (a *SortedArray) Remove(index int) { a.mu.Lock() + defer a.mu.Unlock() a.array = append(a.array[ : index], a.array[index + 1 : ]...) - a.mu.Unlock() } // 数组长度 diff --git a/g/container/garray/garray_sorted_string.go b/g/container/garray/garray_sorted_string.go index a3fc76e47..020866515 100644 --- a/g/container/garray/garray_sorted_string.go +++ b/g/container/garray/garray_sorted_string.go @@ -65,16 +65,16 @@ func (a *SortedStringArray) Add(value string) { // 获取指定索引的数据项, 调用方注意判断数组边界 func (a *SortedStringArray) Get(index int) string { a.mu.RLock() + defer a.mu.RUnlock() value := a.array[index] - a.mu.RUnlock() return value } // 删除指定索引的数据项, 调用方注意判断数组边界 func (a *SortedStringArray) Remove(index int) { a.mu.Lock() + defer a.mu.Unlock() a.array = append(a.array[ : index], a.array[index + 1 : ]...) - a.mu.Unlock() } // 数组长度 diff --git a/g/container/gmap/int_int_map.go b/g/container/gmap/int_int_map.go index dea27f0e4..a1f87951e 100644 --- a/g/container/gmap/int_int_map.go +++ b/g/container/gmap/int_int_map.go @@ -9,6 +9,8 @@ package gmap import ( "sync" + "gitee.com/johng/gf/g/util/gconv" + "time" ) type IntIntMap struct { @@ -68,6 +70,70 @@ func (this *IntIntMap) Get(key int) (int) { return val } +func (this *IntIntMap) GetBool(key int) bool { + return gconv.Bool(this.Get(key)) +} + +func (this *IntIntMap) GetInt(key int) int { + return gconv.Int(this.Get(key)) +} + +func (this *IntIntMap) GetInt8(key int) int8 { + return gconv.Int8(this.Get(key)) +} + +func (this *IntIntMap) GetInt16(key int) int16 { + return gconv.Int16(this.Get(key)) +} + +func (this *IntIntMap) GetInt32(key int) int32 { + return gconv.Int32(this.Get(key)) +} + +func (this *IntIntMap) GetInt64(key int) int64 { + return gconv.Int64(this.Get(key)) +} + +func (this *IntIntMap) GetUint (key int) uint { + return gconv.Uint(this.Get(key)) +} + +func (this *IntIntMap) GetUint8 (key int) uint8 { + return gconv.Uint8(this.Get(key)) +} + +func (this *IntIntMap) GetUint16 (key int) uint16 { + return gconv.Uint16(this.Get(key)) +} + +func (this *IntIntMap) GetUint32 (key int) uint32 { + return gconv.Uint32(this.Get(key)) +} + +func (this *IntIntMap) GetUint64 (key int) uint64 { + return gconv.Uint64(this.Get(key)) +} + +func (this *IntIntMap) GetFloat32 (key int) float32 { + return gconv.Float32(this.Get(key)) +} + +func (this *IntIntMap) GetFloat64 (key int) float64 { + return gconv.Float64(this.Get(key)) +} + +func (this *IntIntMap) GetString (key int) string { + return gconv.String(this.Get(key)) +} + +func (this *IntIntMap) GetTime (key int, format...string) time.Time { + return gconv.Time(this.Get(key), format...) +} + +func (this *IntIntMap) GetTimeDuration (key int) time.Duration { + return gconv.TimeDuration(this.Get(key)) +} + // 获取键值,如果键值不存在则写入默认值 func (this *IntIntMap) GetWithDefault(key int, value int) int { this.mu.Lock() @@ -148,7 +214,7 @@ func (this *IntIntMap) Size() int { // 哈希表是否为空 func (this *IntIntMap) IsEmpty() bool { this.mu.RLock() - empty := (len(this.m) == 0) + empty := len(this.m) == 0 this.mu.RUnlock() return empty } diff --git a/g/container/gmap/int_interface_map.go b/g/container/gmap/int_interface_map.go index 708598771..dbac2bd67 100644 --- a/g/container/gmap/int_interface_map.go +++ b/g/container/gmap/int_interface_map.go @@ -10,6 +10,7 @@ package gmap import ( "sync" "gitee.com/johng/gf/g/util/gconv" + "time" ) type IntInterfaceMap struct { @@ -141,6 +142,14 @@ func (this *IntInterfaceMap) GetStrings (key int) []string { return gconv.Strings(this.Get(key)) } +func (this *IntInterfaceMap) GetTime (key int, format...string) time.Time { + return gconv.Time(this.Get(key), format...) +} + +func (this *IntInterfaceMap) GetTimeDuration (key int) time.Duration { + return gconv.TimeDuration(this.Get(key)) +} + // 删除键值对 func (this *IntInterfaceMap) Remove(key int) { this.mu.Lock() diff --git a/g/container/gmap/int_string_map.go b/g/container/gmap/int_string_map.go index 10b685691..6cde82dc5 100644 --- a/g/container/gmap/int_string_map.go +++ b/g/container/gmap/int_string_map.go @@ -9,6 +9,8 @@ package gmap import ( "sync" + "gitee.com/johng/gf/g/util/gconv" + "time" ) type IntStringMap struct { @@ -68,6 +70,70 @@ func (this *IntStringMap) Get(key int) string { return val } +func (this *IntStringMap) GetBool(key int) bool { + return gconv.Bool(this.Get(key)) +} + +func (this *IntStringMap) GetInt(key int) int { + return gconv.Int(this.Get(key)) +} + +func (this *IntStringMap) GetInt8(key int) int8 { + return gconv.Int8(this.Get(key)) +} + +func (this *IntStringMap) GetInt16(key int) int16 { + return gconv.Int16(this.Get(key)) +} + +func (this *IntStringMap) GetInt32(key int) int32 { + return gconv.Int32(this.Get(key)) +} + +func (this *IntStringMap) GetInt64(key int) int64 { + return gconv.Int64(this.Get(key)) +} + +func (this *IntStringMap) GetUint (key int) uint { + return gconv.Uint(this.Get(key)) +} + +func (this *IntStringMap) GetUint8 (key int) uint8 { + return gconv.Uint8(this.Get(key)) +} + +func (this *IntStringMap) GetUint16 (key int) uint16 { + return gconv.Uint16(this.Get(key)) +} + +func (this *IntStringMap) GetUint32 (key int) uint32 { + return gconv.Uint32(this.Get(key)) +} + +func (this *IntStringMap) GetUint64 (key int) uint64 { + return gconv.Uint64(this.Get(key)) +} + +func (this *IntStringMap) GetFloat32 (key int) float32 { + return gconv.Float32(this.Get(key)) +} + +func (this *IntStringMap) GetFloat64 (key int) float64 { + return gconv.Float64(this.Get(key)) +} + +func (this *IntStringMap) GetString (key int) string { + return gconv.String(this.Get(key)) +} + +func (this *IntStringMap) GetTime (key int, format...string) time.Time { + return gconv.Time(this.Get(key), format...) +} + +func (this *IntStringMap) GetTimeDuration (key int) time.Duration { + return gconv.TimeDuration(this.Get(key)) +} + // 获取键值,如果键值不存在则写入默认值 func (this *IntStringMap) GetWithDefault(key int, value string) string { this.mu.Lock() diff --git a/g/container/gmap/interface_interface_map.go b/g/container/gmap/interface_interface_map.go index bdea3c174..437fcee49 100644 --- a/g/container/gmap/interface_interface_map.go +++ b/g/container/gmap/interface_interface_map.go @@ -10,6 +10,7 @@ package gmap import ( "sync" "gitee.com/johng/gf/g/util/gconv" + "time" ) type InterfaceInterfaceMap struct { @@ -89,10 +90,42 @@ func (this *InterfaceInterfaceMap) GetInt(key interface{}) int { return gconv.Int(this.Get(key)) } +func (this *InterfaceInterfaceMap) GetInt8(key interface{}) int8 { + return gconv.Int8(this.Get(key)) +} + +func (this *InterfaceInterfaceMap) GetInt16(key interface{}) int16 { + return gconv.Int16(this.Get(key)) +} + +func (this *InterfaceInterfaceMap) GetInt32(key interface{}) int32 { + return gconv.Int32(this.Get(key)) +} + +func (this *InterfaceInterfaceMap) GetInt64(key interface{}) int64 { + return gconv.Int64(this.Get(key)) +} + func (this *InterfaceInterfaceMap) GetUint (key interface{}) uint { return gconv.Uint(this.Get(key)) } +func (this *InterfaceInterfaceMap) GetUint8 (key interface{}) uint8 { + return gconv.Uint8(this.Get(key)) +} + +func (this *InterfaceInterfaceMap) GetUint16 (key interface{}) uint16 { + return gconv.Uint16(this.Get(key)) +} + +func (this *InterfaceInterfaceMap) GetUint32 (key interface{}) uint32 { + return gconv.Uint32(this.Get(key)) +} + +func (this *InterfaceInterfaceMap) GetUint64 (key interface{}) uint64 { + return gconv.Uint64(this.Get(key)) +} + func (this *InterfaceInterfaceMap) GetFloat32 (key interface{}) float32 { return gconv.Float32(this.Get(key)) } @@ -105,6 +138,18 @@ func (this *InterfaceInterfaceMap) GetString (key interface{}) string { return gconv.String(this.Get(key)) } +func (this *InterfaceInterfaceMap) GetStrings (key interface{}) []string { + return gconv.Strings(this.Get(key)) +} + +func (this *InterfaceInterfaceMap) GetTime (key interface{}, format...string) time.Time { + return gconv.Time(this.Get(key), format...) +} + +func (this *InterfaceInterfaceMap) GetTimeDuration (key interface{}) time.Duration { + return gconv.TimeDuration(this.Get(key)) +} + // 删除键值对 func (this *InterfaceInterfaceMap) Remove(key interface{}) { this.mu.Lock() diff --git a/g/container/gmap/string_int_map.go b/g/container/gmap/string_int_map.go index 17487dfc2..ce6dd035d 100644 --- a/g/container/gmap/string_int_map.go +++ b/g/container/gmap/string_int_map.go @@ -9,6 +9,8 @@ package gmap import ( "sync" + "gitee.com/johng/gf/g/util/gconv" + "time" ) type StringIntMap struct { @@ -68,6 +70,74 @@ func (this *StringIntMap) Get(key string) int { return val } +func (this *StringIntMap) GetBool(key string) bool { + return gconv.Bool(this.Get(key)) +} + +func (this *StringIntMap) GetInt(key string) int { + return gconv.Int(this.Get(key)) +} + +func (this *StringIntMap) GetInt8(key string) int8 { + return gconv.Int8(this.Get(key)) +} + +func (this *StringIntMap) GetInt16(key string) int16 { + return gconv.Int16(this.Get(key)) +} + +func (this *StringIntMap) GetInt32(key string) int32 { + return gconv.Int32(this.Get(key)) +} + +func (this *StringIntMap) GetInt64(key string) int64 { + return gconv.Int64(this.Get(key)) +} + +func (this *StringIntMap) GetUint (key string) uint { + return gconv.Uint(this.Get(key)) +} + +func (this *StringIntMap) GetUint8 (key string) uint8 { + return gconv.Uint8(this.Get(key)) +} + +func (this *StringIntMap) GetUint16 (key string) uint16 { + return gconv.Uint16(this.Get(key)) +} + +func (this *StringIntMap) GetUint32 (key string) uint32 { + return gconv.Uint32(this.Get(key)) +} + +func (this *StringIntMap) GetUint64 (key string) uint64 { + return gconv.Uint64(this.Get(key)) +} + +func (this *StringIntMap) GetFloat32 (key string) float32 { + return gconv.Float32(this.Get(key)) +} + +func (this *StringIntMap) GetFloat64 (key string) float64 { + return gconv.Float64(this.Get(key)) +} + +func (this *StringIntMap) GetString (key string) string { + return gconv.String(this.Get(key)) +} + +func (this *StringIntMap) GetStrings (key string) []string { + return gconv.Strings(this.Get(key)) +} + +func (this *StringIntMap) GetTime (key string, format...string) time.Time { + return gconv.Time(this.Get(key), format...) +} + +func (this *StringIntMap) GetTimeDuration (key string) time.Duration { + return gconv.TimeDuration(this.Get(key)) +} + // 获取键值,如果键值不存在则写入默认值 func (this *StringIntMap) GetWithDefault(key string, value int) int { this.mu.Lock() diff --git a/g/container/gmap/string_interface_map.go b/g/container/gmap/string_interface_map.go index 4dbb6c4ab..5e470f160 100644 --- a/g/container/gmap/string_interface_map.go +++ b/g/container/gmap/string_interface_map.go @@ -10,6 +10,7 @@ package gmap import ( "sync" "gitee.com/johng/gf/g/util/gconv" + "time" ) type StringInterfaceMap struct { @@ -82,27 +83,71 @@ func (this *StringInterfaceMap) GetWithDefault(key string, value interface{}) in } func (this *StringInterfaceMap) GetBool(key string) bool { - return gconv.Bool(this.Get(key)) + return gconv.Bool(this.Get(key)) } func (this *StringInterfaceMap) GetInt(key string) int { - return gconv.Int(this.Get(key)) + return gconv.Int(this.Get(key)) +} + +func (this *StringInterfaceMap) GetInt8(key string) int8 { + return gconv.Int8(this.Get(key)) +} + +func (this *StringInterfaceMap) GetInt16(key string) int16 { + return gconv.Int16(this.Get(key)) +} + +func (this *StringInterfaceMap) GetInt32(key string) int32 { + return gconv.Int32(this.Get(key)) +} + +func (this *StringInterfaceMap) GetInt64(key string) int64 { + return gconv.Int64(this.Get(key)) } func (this *StringInterfaceMap) GetUint (key string) uint { - return gconv.Uint(this.Get(key)) + return gconv.Uint(this.Get(key)) +} + +func (this *StringInterfaceMap) GetUint8 (key string) uint8 { + return gconv.Uint8(this.Get(key)) +} + +func (this *StringInterfaceMap) GetUint16 (key string) uint16 { + return gconv.Uint16(this.Get(key)) +} + +func (this *StringInterfaceMap) GetUint32 (key string) uint32 { + return gconv.Uint32(this.Get(key)) +} + +func (this *StringInterfaceMap) GetUint64 (key string) uint64 { + return gconv.Uint64(this.Get(key)) } func (this *StringInterfaceMap) GetFloat32 (key string) float32 { - return gconv.Float32(this.Get(key)) + return gconv.Float32(this.Get(key)) } func (this *StringInterfaceMap) GetFloat64 (key string) float64 { - return gconv.Float64(this.Get(key)) + return gconv.Float64(this.Get(key)) } func (this *StringInterfaceMap) GetString (key string) string { - return gconv.String(this.Get(key)) + return gconv.String(this.Get(key)) +} + +func (this *StringInterfaceMap) GetStrings (key string) []string { + return gconv.Strings(this.Get(key)) +} + +func (this *StringInterfaceMap) GetTime (key string, format...string) time.Time { + return gconv.Time(this.Get(key), format...) +} + +func (this *StringInterfaceMap) GetTimeDuration (key string) time.Duration { + return gconv.TimeDuration(this.Get(key)) } // 删除键值对 diff --git a/g/container/gmap/string_string_map.go b/g/container/gmap/string_string_map.go index 33bea9847..4c8d4262c 100644 --- a/g/container/gmap/string_string_map.go +++ b/g/container/gmap/string_string_map.go @@ -9,6 +9,8 @@ package gmap import ( "sync" + "gitee.com/johng/gf/g/util/gconv" + "time" ) type StringStringMap struct { @@ -68,6 +70,74 @@ func (this *StringStringMap) Get(key string) string { return val } +func (this *StringStringMap) GetBool(key string) bool { + return gconv.Bool(this.Get(key)) +} + +func (this *StringStringMap) GetInt(key string) int { + return gconv.Int(this.Get(key)) +} + +func (this *StringStringMap) GetInt8(key string) int8 { + return gconv.Int8(this.Get(key)) +} + +func (this *StringStringMap) GetInt16(key string) int16 { + return gconv.Int16(this.Get(key)) +} + +func (this *StringStringMap) GetInt32(key string) int32 { + return gconv.Int32(this.Get(key)) +} + +func (this *StringStringMap) GetInt64(key string) int64 { + return gconv.Int64(this.Get(key)) +} + +func (this *StringStringMap) GetUint (key string) uint { + return gconv.Uint(this.Get(key)) +} + +func (this *StringStringMap) GetUint8 (key string) uint8 { + return gconv.Uint8(this.Get(key)) +} + +func (this *StringStringMap) GetUint16 (key string) uint16 { + return gconv.Uint16(this.Get(key)) +} + +func (this *StringStringMap) GetUint32 (key string) uint32 { + return gconv.Uint32(this.Get(key)) +} + +func (this *StringStringMap) GetUint64 (key string) uint64 { + return gconv.Uint64(this.Get(key)) +} + +func (this *StringStringMap) GetFloat32 (key string) float32 { + return gconv.Float32(this.Get(key)) +} + +func (this *StringStringMap) GetFloat64 (key string) float64 { + return gconv.Float64(this.Get(key)) +} + +func (this *StringStringMap) GetString (key string) string { + return gconv.String(this.Get(key)) +} + +func (this *StringStringMap) GetStrings (key string) []string { + return gconv.Strings(this.Get(key)) +} + +func (this *StringStringMap) GetTime (key string, format...string) time.Time { + return gconv.Time(this.Get(key), format...) +} + +func (this *StringStringMap) GetTimeDuration (key string) time.Duration { + return gconv.TimeDuration(this.Get(key)) +} + // 获取键值,如果键值不存在则写入默认值 func (this *StringStringMap) GetWithDefault(key string, value string) string { this.mu.Lock() diff --git a/g/container/gmap/uint_interface_map.go b/g/container/gmap/uint_interface_map.go index d3d7fa6a5..e6e622bea 100644 --- a/g/container/gmap/uint_interface_map.go +++ b/g/container/gmap/uint_interface_map.go @@ -10,6 +10,7 @@ package gmap import ( "sync" "gitee.com/johng/gf/g/util/gconv" + "time" ) type UintInterfaceMap struct { @@ -79,7 +80,6 @@ func (this *UintInterfaceMap) GetWithDefault(key uint, value interface{}) interf this.mu.Unlock() return val } - func (this *UintInterfaceMap) GetBool(key uint) bool { return gconv.Bool(this.Get(key)) } @@ -88,10 +88,42 @@ func (this *UintInterfaceMap) GetInt(key uint) int { return gconv.Int(this.Get(key)) } +func (this *UintInterfaceMap) GetInt8(key uint) int8 { + return gconv.Int8(this.Get(key)) +} + +func (this *UintInterfaceMap) GetInt16(key uint) int16 { + return gconv.Int16(this.Get(key)) +} + +func (this *UintInterfaceMap) GetInt32(key uint) int32 { + return gconv.Int32(this.Get(key)) +} + +func (this *UintInterfaceMap) GetInt64(key uint) int64 { + return gconv.Int64(this.Get(key)) +} + func (this *UintInterfaceMap) GetUint (key uint) uint { return gconv.Uint(this.Get(key)) } +func (this *UintInterfaceMap) GetUint8 (key uint) uint8 { + return gconv.Uint8(this.Get(key)) +} + +func (this *UintInterfaceMap) GetUint16 (key uint) uint16 { + return gconv.Uint16(this.Get(key)) +} + +func (this *UintInterfaceMap) GetUint32 (key uint) uint32 { + return gconv.Uint32(this.Get(key)) +} + +func (this *UintInterfaceMap) GetUint64 (key uint) uint64 { + return gconv.Uint64(this.Get(key)) +} + func (this *UintInterfaceMap) GetFloat32 (key uint) float32 { return gconv.Float32(this.Get(key)) } @@ -104,6 +136,18 @@ func (this *UintInterfaceMap) GetString (key uint) string { return gconv.String(this.Get(key)) } +func (this *UintInterfaceMap) GetStrings (key uint) []string { + return gconv.Strings(this.Get(key)) +} + +func (this *UintInterfaceMap) GetTime (key uint, format...string) time.Time { + return gconv.Time(this.Get(key), format...) +} + +func (this *UintInterfaceMap) GetTimeDuration (key uint) time.Duration { + return gconv.TimeDuration(this.Get(key)) +} + // 删除键值对 func (this *UintInterfaceMap) Remove(key uint) { this.mu.Lock() diff --git a/g/encoding/gjson/gjson.go b/g/encoding/gjson/gjson.go index 24dd51d50..1dad364db 100644 --- a/g/encoding/gjson/gjson.go +++ b/g/encoding/gjson/gjson.go @@ -21,6 +21,7 @@ import ( "gitee.com/johng/gf/g/encoding/gyaml" "gitee.com/johng/gf/g/encoding/gtoml" "gitee.com/johng/gf/g/util/gstr" + "time" ) const ( @@ -32,7 +33,7 @@ type Json struct { mu sync.RWMutex p *interface{} // 注意这是一个指针 c byte // 层级分隔符,默认为"." - vc bool // 是否执行分隔符冲突检测(默认为true,检测会比较影响检索效率) + vc bool // 是否执行分隔符冲突检测(默认为false,检测会比较影响检索效率) } // 将变量转换为Json对象进行处理,该变量至少应当是一个map或者array,否者转换没有意义 @@ -42,13 +43,13 @@ func New(value interface{}) *Json { return &Json{ p : &value, c : byte(gDEFAULT_SPLIT_CHAR), - vc : true , + vc : false , } case []interface{}: return &Json{ p : &value, c : byte(gDEFAULT_SPLIT_CHAR), - vc : true , + vc : false , } default: // 这里效率会比较低 @@ -57,7 +58,7 @@ func New(value interface{}) *Json { return &Json{ p : &v, c : byte(gDEFAULT_SPLIT_CHAR), - vc : true, + vc : false, } } } @@ -144,7 +145,8 @@ func (j *Json) SetSplitChar(char byte) { j.mu.Unlock() } -// 设置自定义的层级分隔符号 +// 设置是否执行层级冲突检查,当键名中存在层级符号时需要开启该特性,默认为关闭。 +// 开启比较耗性能,也不建议允许键名中存在分隔符,最好在应用端避免这种情况。 func (j *Json) SetViolenceCheck(check bool) { j.mu.Lock() j.vc = check @@ -205,6 +207,19 @@ func (j *Json) GetString(pattern string) string { return gconv.String(j.Get(pattern)) } +// 返回指定json中的strings(转换为[]string数组) +func (j *Json) GetStrings(pattern string) []string { + return gconv.Strings(j.Get(pattern)) +} + +func (j *Json) GetTime(pattern string, format ... string) time.Time { + return gconv.Time(j.Get(pattern), format...) +} + +func (j *Json) GetTimeDuration(pattern string) time.Duration { + return gconv.TimeDuration(j.Get(pattern)) +} + // 返回指定json中的bool(false:"", 0, false, off) func (j *Json) GetBool(pattern string) bool { return gconv.Bool(j.Get(pattern)) @@ -460,18 +475,22 @@ func (j *Json) setPointerWithValue(pointer *interface{}, key string, value inter return pointer } -// 根据约定字符串方式访问json解析数据,参数形如: "items.name.first", "list.0" -// 返回的结果类型的interface{},因此需要自己做类型转换 -// 如果找不到对应节点的数据,返回nil -func (j *Json) Get(pattern string) interface{} { +// 根据约定字符串方式访问json解析数据,参数形如: "items.name.first", "list.0"; 当pattern为空时,表示获取所有数据; +// 返回的结果类型的interface{},因此需要自己做类型转换; +// 如果找不到对应节点的数据,返回nil; +func (j *Json) Get(pattern...string) interface{} { j.mu.RLock() defer j.mu.RUnlock() + queryPattern := "" + if len(pattern) > 0 { + queryPattern = pattern[0] + } var result *interface{} if j.vc { - result = j.getPointerByPattern(pattern) + result = j.getPointerByPattern(queryPattern) } else { - result = j.getPointerByPatternWithoutSplitCharViolenceCheck(pattern) + result = j.getPointerByPatternWithoutSplitCharViolenceCheck(queryPattern) } if result != nil { return *result diff --git a/g/encoding/gparser/gparser.go b/g/encoding/gparser/gparser.go index a711ef466..ad73dd507 100644 --- a/g/encoding/gparser/gparser.go +++ b/g/encoding/gparser/gparser.go @@ -9,6 +9,7 @@ package gparser import ( "gitee.com/johng/gf/g/encoding/gjson" + "time" ) type Parser struct { @@ -74,6 +75,18 @@ func (p *Parser) GetString(pattern string) string { return p.json.GetString(pattern) } +func (p *Parser) GetStrings(pattern string) []string { + return p.json.GetStrings(pattern) +} + +func (p *Parser) GetTime(pattern string, format ... string) time.Time { + return p.json.GetTime(pattern, format...) +} + +func (p *Parser) GetTimeDuration(pattern string) time.Duration { + return p.json.GetTimeDuration(pattern) +} + // 返回指定json中的bool(false:"", 0, false, off) func (p *Parser) GetBool(pattern string) bool { return p.json.GetBool(pattern) @@ -138,11 +151,11 @@ func (p *Parser) Remove(pattern string) error { return p.json.Remove(pattern) } -// 根据约定字符串方式访问json解析数据,参数形如: "items.name.first", "list.0" -// 返回的结果类型的interface{},因此需要自己做类型转换 -// 如果找不到对应节点的数据,返回nil -func (p *Parser) Get(pattern string) interface{} { - return p.json.Get(pattern) +// 根据约定字符串方式访问json解析数据,参数形如: "items.name.first", "list.0"; 当pattern为空时,表示获取所有数据 +// 返回的结果类型的interface{},因此需要自己做类型转换; +// 如果找不到对应节点的数据,返回nil; +func (p *Parser) Get(pattern...string) interface{} { + return p.json.Get(pattern...) } // 转换为map[string]interface{}类型,如果转换失败,返回nil diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 10652ac86..e5429fce9 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -132,6 +132,13 @@ func (c *Config) GetString(pattern string, file...string) string { return "" } +func (c *Config) GetStrings(pattern string, file...string) []string { + if j := c.getJson(file...); j != nil { + return j.GetStrings(pattern) + } + return nil +} + // 返回指定json中的bool func (c *Config) GetBool(pattern string, file...string) bool { if j := c.getJson(file...); j != nil {