add default value feature for gjson/gparser; update default value type for ghttp.Request

This commit is contained in:
John
2019-05-09 14:19:27 +08:00
parent 2b865a55ac
commit fdfefbb94d
10 changed files with 280 additions and 351 deletions

View File

@ -15,7 +15,7 @@ import (
)
// Val returns the json value.
func (j *Json) Val() interface{} {
func (j *Json) Value() interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return *(j.p)
@ -27,35 +27,36 @@ func (j *Json) Val() interface{} {
//
// We can also access slice item by its index number in <pattern>,
// eg: "items.name.first", "list.10".
func (j *Json) Get(pattern...string) interface{} {
//
// It returns a default value specified by <def> if value for <pattern> is not found.
func (j *Json) Get(pattern string, def...interface{}) 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(queryPattern)
result = j.getPointerByPattern(pattern)
} else {
result = j.getPointerByPatternWithoutViolenceCheck(queryPattern)
result = j.getPointerByPatternWithoutViolenceCheck(pattern)
}
if result != nil {
return *result
}
if len(def) > 0 {
return def[0]
}
return nil
}
// GetVar returns a *gvar.Var with value by given <pattern>.
func (j *Json) GetVar(pattern...string) *gvar.Var {
return gvar.New(j.Get(pattern...), true)
func (j *Json) GetVar(pattern string, def...interface{}) *gvar.Var {
return gvar.New(j.Get(pattern, def...), true)
}
// GetMap gets the value by specified <pattern>,
// and converts it to map[string]interface{}.
func (j *Json) GetMap(pattern string) map[string]interface{} {
result := j.Get(pattern)
func (j *Json) GetMap(pattern string, def...interface{}) map[string]interface{} {
result := j.Get(pattern, def...)
if result != nil {
return gconv.Map(result)
}
@ -64,8 +65,8 @@ func (j *Json) GetMap(pattern string) map[string]interface{} {
// GetJson gets the value by specified <pattern>,
// and converts it to a Json object.
func (j *Json) GetJson(pattern string) *Json {
result := j.Get(pattern)
func (j *Json) GetJson(pattern string, def...interface{}) *Json {
result := j.Get(pattern, def...)
if result != nil {
return New(result)
}
@ -74,8 +75,8 @@ func (j *Json) GetJson(pattern string) *Json {
// GetJsons gets the value by specified <pattern>,
// and converts it to a slice of Json object.
func (j *Json) GetJsons(pattern string) []*Json {
array := j.GetArray(pattern)
func (j *Json) GetJsons(pattern string, def...interface{}) []*Json {
array := j.GetArray(pattern, def...)
if len(array) > 0 {
jsons := make([]*Json, len(array))
for i := 0; i < len(array); i++ {
@ -88,101 +89,101 @@ func (j *Json) GetJsons(pattern string) []*Json {
// GetArray gets the value by specified <pattern>,
// and converts it to a slice of []interface{}.
func (j *Json) GetArray(pattern string) []interface{} {
return gconv.Interfaces(j.Get(pattern))
func (j *Json) GetArray(pattern string, def...interface{}) []interface{} {
return gconv.Interfaces(j.Get(pattern, def...))
}
// GetString gets the value by specified <pattern>,
// and converts it to string.
func (j *Json) GetString(pattern string) string {
return gconv.String(j.Get(pattern))
func (j *Json) GetString(pattern string, def...interface{}) string {
return gconv.String(j.Get(pattern, def...))
}
// GetBool gets the value by specified <pattern>,
// and converts it to bool.
// It returns false when value is: "", 0, false, off, nil;
// or returns true instead.
func (j *Json) GetBool(pattern string) bool {
return gconv.Bool(j.Get(pattern))
func (j *Json) GetBool(pattern string, def...interface{}) bool {
return gconv.Bool(j.Get(pattern, def...))
}
func (j *Json) GetInt(pattern string) int {
return gconv.Int(j.Get(pattern))
func (j *Json) GetInt(pattern string, def...interface{}) int {
return gconv.Int(j.Get(pattern, def...))
}
func (j *Json) GetInt8(pattern string) int8 {
return gconv.Int8(j.Get(pattern))
func (j *Json) GetInt8(pattern string, def...interface{}) int8 {
return gconv.Int8(j.Get(pattern, def...))
}
func (j *Json) GetInt16(pattern string) int16 {
return gconv.Int16(j.Get(pattern))
func (j *Json) GetInt16(pattern string, def...interface{}) int16 {
return gconv.Int16(j.Get(pattern, def...))
}
func (j *Json) GetInt32(pattern string) int32 {
return gconv.Int32(j.Get(pattern))
func (j *Json) GetInt32(pattern string, def...interface{}) int32 {
return gconv.Int32(j.Get(pattern, def...))
}
func (j *Json) GetInt64(pattern string) int64 {
return gconv.Int64(j.Get(pattern))
func (j *Json) GetInt64(pattern string, def...interface{}) int64 {
return gconv.Int64(j.Get(pattern, def...))
}
func (j *Json) GetInts(pattern string) []int {
return gconv.Ints(j.Get(pattern))
func (j *Json) GetUint(pattern string, def...interface{}) uint {
return gconv.Uint(j.Get(pattern, def...))
}
func (j *Json) GetUint(pattern string) uint {
return gconv.Uint(j.Get(pattern))
func (j *Json) GetUint8(pattern string, def...interface{}) uint8 {
return gconv.Uint8(j.Get(pattern, def...))
}
func (j *Json) GetUint8(pattern string) uint8 {
return gconv.Uint8(j.Get(pattern))
func (j *Json) GetUint16(pattern string, def...interface{}) uint16 {
return gconv.Uint16(j.Get(pattern, def...))
}
func (j *Json) GetUint16(pattern string) uint16 {
return gconv.Uint16(j.Get(pattern))
func (j *Json) GetUint32(pattern string, def...interface{}) uint32 {
return gconv.Uint32(j.Get(pattern, def...))
}
func (j *Json) GetUint32(pattern string) uint32 {
return gconv.Uint32(j.Get(pattern))
func (j *Json) GetUint64(pattern string, def...interface{}) uint64 {
return gconv.Uint64(j.Get(pattern, def...))
}
func (j *Json) GetUint64(pattern string) uint64 {
return gconv.Uint64(j.Get(pattern))
func (j *Json) GetFloat32(pattern string, def...interface{}) float32 {
return gconv.Float32(j.Get(pattern, def...))
}
func (j *Json) GetFloat32(pattern string) float32 {
return gconv.Float32(j.Get(pattern))
func (j *Json) GetFloat64(pattern string, def...interface{}) float64 {
return gconv.Float64(j.Get(pattern, def...))
}
func (j *Json) GetFloat64(pattern string) float64 {
return gconv.Float64(j.Get(pattern))
func (j *Json) GetFloats(pattern string, def...interface{}) []float64 {
return gconv.Floats(j.Get(pattern, def...))
}
func (j *Json) GetFloats(pattern string) []float64 {
return gconv.Floats(j.Get(pattern))
func (j *Json) GetInts(pattern string, def...interface{}) []int {
return gconv.Ints(j.Get(pattern, def...))
}
// GetStrings gets the value by specified <pattern>,
// and converts it to a slice of []string.
func (j *Json) GetStrings(pattern string) []string {
return gconv.Strings(j.Get(pattern))
func (j *Json) GetStrings(pattern string, def...interface{}) []string {
return gconv.Strings(j.Get(pattern, def...))
}
// See GetArray.
func (j *Json) GetInterfaces(pattern string) []interface{} {
return gconv.Interfaces(j.Get(pattern))
func (j *Json) GetInterfaces(pattern string, def...interface{}) []interface{} {
return gconv.Interfaces(j.Get(pattern, def...))
}
func (j *Json) GetTime(pattern string, format ... string) time.Time {
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))
func (j *Json) GetTimeDuration(pattern string, def...interface{}) time.Duration {
return gconv.TimeDuration(j.Get(pattern, def...))
}
func (j *Json) GetGTime(pattern string) *gtime.Time {
return gconv.GTime(j.Get(pattern))
func (j *Json) GetGTime(pattern string, format... string) *gtime.Time {
return gconv.GTime(j.Get(pattern), format...)
}
// Set sets value with specified <pattern>.
@ -236,17 +237,17 @@ func (j *Json) Append(pattern string, value interface{}) error {
// GetToVar gets the value by specified <pattern>,
// and converts it to specified golang variable <v>.
// The <v> should be a pointer type.
func (j *Json) GetToVar(pattern string, v interface{}) error {
// The <pointer> should be a pointer type.
func (j *Json) GetToVar(pattern string, pointer interface{}) error {
r := j.Get(pattern)
if r != nil {
if t, err := Encode(r); err == nil {
return DecodeTo(t, v)
return DecodeTo(t, pointer)
} else {
return err
}
} else {
v = nil
pointer = nil
}
return nil
}
@ -254,8 +255,8 @@ func (j *Json) GetToVar(pattern string, v interface{}) error {
// GetToStruct gets the value by specified <pattern>,
// and converts it to specified object <objPointer>.
// The <objPointer> should be the pointer to an object.
func (j *Json) GetToStruct(pattern string, objPointer interface{}) error {
return gconv.Struct(j.Get(pattern), objPointer)
func (j *Json) GetToStruct(pattern string, pointer interface{}) error {
return gconv.Struct(j.Get(pattern), pointer)
}
// ToMap converts current Json object to map[string]interface{}.