From 751a567e8406b6a554d2818ddefae161fd22ad94 Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 14 Dec 2020 18:54:14 +0800 Subject: [PATCH] add Is* functions for package gvar --- container/gvar/gvar.go | 14 +- container/gvar/gvar_is.go | 98 +++++++++++++ container/gvar/gvar_z_unit_is_test.go | 170 ++++++++++++++++++++++ encoding/gjson/gjson_api.go | 69 ++------- encoding/gjson/gjson_deprecated.go | 113 ++++++++++++++ encoding/gjson/gjson_z_unit_basic_test.go | 4 +- 6 files changed, 399 insertions(+), 69 deletions(-) create mode 100644 container/gvar/gvar_is.go create mode 100644 container/gvar/gvar_z_unit_is_test.go create mode 100644 encoding/gjson/gjson_deprecated.go diff --git a/container/gvar/gvar.go b/container/gvar/gvar.go index e9bc40dc0..3ee988702 100644 --- a/container/gvar/gvar.go +++ b/container/gvar/gvar.go @@ -1,4 +1,4 @@ -// Copyright 2018-2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -11,8 +11,6 @@ import ( "github.com/gogf/gf/internal/json" "time" - "github.com/gogf/gf/internal/empty" - "github.com/gogf/gf/container/gtype" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/util/gconv" @@ -88,16 +86,6 @@ func (v *Var) Interface() interface{} { return v.Val() } -// IsNil checks whether is nil. -func (v *Var) IsNil() bool { - return v.Val() == nil -} - -// IsEmpty checks whether is empty. -func (v *Var) IsEmpty() bool { - return empty.IsEmpty(v.Val()) -} - // Bytes converts and returns as []byte. func (v *Var) Bytes() []byte { return gconv.Bytes(v.Val()) diff --git a/container/gvar/gvar_is.go b/container/gvar/gvar_is.go new file mode 100644 index 000000000..396b4e40e --- /dev/null +++ b/container/gvar/gvar_is.go @@ -0,0 +1,98 @@ +// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gvar + +import ( + "github.com/gogf/gf/internal/empty" + "reflect" +) + +// IsNil checks whether is nil. +func (v *Var) IsNil() bool { + return v.Val() == nil +} + +// IsEmpty checks whether is empty. +func (v *Var) IsEmpty() bool { + return empty.IsEmpty(v.Val()) +} + +// IsInt checks whether is type of int. +func (v *Var) IsInt() bool { + switch v.Val().(type) { + case int, *int, int8, *int8, int16, *int16, int32, *int32, int64, *int64: + return true + } + return false +} + +// IsUint checks whether is type of uint. +func (v *Var) IsUint() bool { + switch v.Val().(type) { + case uint, *uint, uint8, *uint8, uint16, *uint16, uint32, *uint32, uint64, *uint64: + return true + } + return false +} + +// IsFloat checks whether is type of float. +func (v *Var) IsFloat() bool { + switch v.Val().(type) { + case float32, *float32, float64, *float64: + return true + } + return false +} + +// IsSlice checks whether is type of slice. +func (v *Var) IsSlice() bool { + var ( + reflectValue = reflect.ValueOf(v.Val()) + reflectKind = reflectValue.Kind() + ) + for reflectKind == reflect.Ptr { + reflectValue = reflectValue.Elem() + } + switch reflectKind { + case reflect.Slice, reflect.Array: + return true + } + return false +} + +// IsMap checks whether is type of map. +func (v *Var) IsMap() bool { + var ( + reflectValue = reflect.ValueOf(v.Val()) + reflectKind = reflectValue.Kind() + ) + for reflectKind == reflect.Ptr { + reflectValue = reflectValue.Elem() + } + switch reflectKind { + case reflect.Map: + return true + } + return false +} + +// IsStruct checks whether is type of struct. +func (v *Var) IsStruct() bool { + var ( + reflectValue = reflect.ValueOf(v.Val()) + reflectKind = reflectValue.Kind() + ) + for reflectKind == reflect.Ptr { + reflectValue = reflectValue.Elem() + reflectKind = reflectValue.Kind() + } + switch reflectKind { + case reflect.Struct: + return true + } + return false +} diff --git a/container/gvar/gvar_z_unit_is_test.go b/container/gvar/gvar_z_unit_is_test.go new file mode 100644 index 000000000..c1656e044 --- /dev/null +++ b/container/gvar/gvar_z_unit_is_test.go @@ -0,0 +1,170 @@ +// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gvar_test + +import ( + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/test/gtest" + "testing" +) + +func TestVar_IsNil(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(0).IsNil(), false) + t.Assert(g.NewVar(nil).IsNil(), true) + t.Assert(g.NewVar(g.Map{}).IsNil(), false) + t.Assert(g.NewVar(g.Slice{}).IsNil(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(1).IsNil(), false) + t.Assert(g.NewVar(0.1).IsNil(), false) + t.Assert(g.NewVar(g.Map{"k": "v"}).IsNil(), false) + t.Assert(g.NewVar(g.Slice{0}).IsNil(), false) + }) +} + +func TestVar_IsEmpty(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(0).IsEmpty(), true) + t.Assert(g.NewVar(nil).IsEmpty(), true) + t.Assert(g.NewVar(g.Map{}).IsEmpty(), true) + t.Assert(g.NewVar(g.Slice{}).IsEmpty(), true) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(1).IsEmpty(), false) + t.Assert(g.NewVar(0.1).IsEmpty(), false) + t.Assert(g.NewVar(g.Map{"k": "v"}).IsEmpty(), false) + t.Assert(g.NewVar(g.Slice{0}).IsEmpty(), false) + }) +} + +func TestVar_IsInt(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(0).IsInt(), true) + t.Assert(g.NewVar(nil).IsInt(), false) + t.Assert(g.NewVar(g.Map{}).IsInt(), false) + t.Assert(g.NewVar(g.Slice{}).IsInt(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(1).IsInt(), true) + t.Assert(g.NewVar(-1).IsInt(), true) + t.Assert(g.NewVar(0.1).IsInt(), false) + t.Assert(g.NewVar(g.Map{"k": "v"}).IsInt(), false) + t.Assert(g.NewVar(g.Slice{0}).IsInt(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(int8(1)).IsInt(), true) + t.Assert(g.NewVar(uint8(1)).IsInt(), false) + }) +} + +func TestVar_IsUint(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(0).IsUint(), false) + t.Assert(g.NewVar(nil).IsUint(), false) + t.Assert(g.NewVar(g.Map{}).IsUint(), false) + t.Assert(g.NewVar(g.Slice{}).IsUint(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(1).IsUint(), false) + t.Assert(g.NewVar(-1).IsUint(), false) + t.Assert(g.NewVar(0.1).IsUint(), false) + t.Assert(g.NewVar(g.Map{"k": "v"}).IsUint(), false) + t.Assert(g.NewVar(g.Slice{0}).IsUint(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(int8(1)).IsUint(), false) + t.Assert(g.NewVar(uint8(1)).IsUint(), true) + }) +} + +func TestVar_IsFloat(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(0).IsFloat(), false) + t.Assert(g.NewVar(nil).IsFloat(), false) + t.Assert(g.NewVar(g.Map{}).IsFloat(), false) + t.Assert(g.NewVar(g.Slice{}).IsFloat(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(1).IsFloat(), false) + t.Assert(g.NewVar(-1).IsFloat(), false) + t.Assert(g.NewVar(0.1).IsFloat(), true) + t.Assert(g.NewVar(float64(1)).IsFloat(), true) + t.Assert(g.NewVar(g.Map{"k": "v"}).IsFloat(), false) + t.Assert(g.NewVar(g.Slice{0}).IsFloat(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(int8(1)).IsFloat(), false) + t.Assert(g.NewVar(uint8(1)).IsFloat(), false) + }) +} + +func TestVar_IsSlice(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(0).IsSlice(), false) + t.Assert(g.NewVar(nil).IsSlice(), false) + t.Assert(g.NewVar(g.Map{}).IsSlice(), false) + t.Assert(g.NewVar(g.Slice{}).IsSlice(), true) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(1).IsSlice(), false) + t.Assert(g.NewVar(-1).IsSlice(), false) + t.Assert(g.NewVar(0.1).IsSlice(), false) + t.Assert(g.NewVar(float64(1)).IsSlice(), false) + t.Assert(g.NewVar(g.Map{"k": "v"}).IsSlice(), false) + t.Assert(g.NewVar(g.Slice{0}).IsSlice(), true) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(int8(1)).IsSlice(), false) + t.Assert(g.NewVar(uint8(1)).IsSlice(), false) + }) +} + +func TestVar_IsMap(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(0).IsMap(), false) + t.Assert(g.NewVar(nil).IsMap(), false) + t.Assert(g.NewVar(g.Map{}).IsMap(), true) + t.Assert(g.NewVar(g.Slice{}).IsMap(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(1).IsMap(), false) + t.Assert(g.NewVar(-1).IsMap(), false) + t.Assert(g.NewVar(0.1).IsMap(), false) + t.Assert(g.NewVar(float64(1)).IsMap(), false) + t.Assert(g.NewVar(g.Map{"k": "v"}).IsMap(), true) + t.Assert(g.NewVar(g.Slice{0}).IsMap(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(int8(1)).IsMap(), false) + t.Assert(g.NewVar(uint8(1)).IsMap(), false) + }) +} + +func TestVar_IsStruct(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(0).IsStruct(), false) + t.Assert(g.NewVar(nil).IsStruct(), false) + t.Assert(g.NewVar(g.Map{}).IsStruct(), false) + t.Assert(g.NewVar(g.Slice{}).IsStruct(), false) + }) + gtest.C(t, func(t *gtest.T) { + t.Assert(g.NewVar(1).IsStruct(), false) + t.Assert(g.NewVar(-1).IsStruct(), false) + t.Assert(g.NewVar(0.1).IsStruct(), false) + t.Assert(g.NewVar(float64(1)).IsStruct(), false) + t.Assert(g.NewVar(g.Map{"k": "v"}).IsStruct(), false) + t.Assert(g.NewVar(g.Slice{0}).IsStruct(), false) + }) + gtest.C(t, func(t *gtest.T) { + a := &struct { + }{} + t.Assert(g.NewVar(a).IsStruct(), true) + t.Assert(g.NewVar(*a).IsStruct(), true) + t.Assert(g.NewVar(&a).IsStruct(), true) + }) +} diff --git a/encoding/gjson/gjson_api.go b/encoding/gjson/gjson_api.go index 3fade66bc..9a76d5992 100644 --- a/encoding/gjson/gjson_api.go +++ b/encoding/gjson/gjson_api.go @@ -1,4 +1,4 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -370,99 +370,60 @@ func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ... return gconv.MapToMapsDeep(j.Get(pattern), pointer, mapping...) } -// ToMap converts current Json object to map[string]interface{}. +// Map converts current Json object to map[string]interface{}. // It returns nil if fails. -func (j *Json) ToMap() map[string]interface{} { +func (j *Json) Map() map[string]interface{} { j.mu.RLock() defer j.mu.RUnlock() return gconv.Map(*(j.p)) } -// ToArray converts current Json object to []interface{}. +// Array converts current Json object to []interface{}. // It returns nil if fails. -func (j *Json) ToArray() []interface{} { +func (j *Json) Array() []interface{} { j.mu.RLock() defer j.mu.RUnlock() return gconv.Interfaces(*(j.p)) } -// ToStruct converts current Json object to specified object. +// Struct converts current Json object to specified object. // The should be a pointer type of *struct. -func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error { +func (j *Json) Struct(pointer interface{}, mapping ...map[string]string) error { j.mu.RLock() defer j.mu.RUnlock() return gconv.Struct(*(j.p), pointer, mapping...) } -// ToStructDeep converts current Json object to specified object recursively. -// The should be a pointer type of *struct. -// Deprecated, use ToStruct instead. -func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error { - j.mu.RLock() - defer j.mu.RUnlock() - return gconv.StructDeep(*(j.p), pointer, mapping...) -} - -// ToStructs converts current Json object to specified object slice. +// Structs converts current Json object to specified object slice. // The should be a pointer type of []struct/*struct. -func (j *Json) ToStructs(pointer interface{}, mapping ...map[string]string) error { +func (j *Json) Structs(pointer interface{}, mapping ...map[string]string) error { j.mu.RLock() defer j.mu.RUnlock() return gconv.Structs(*(j.p), pointer, mapping...) } -// ToStructsDeep converts current Json object to specified object slice recursively. -// The should be a pointer type of []struct/*struct. -func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error { - j.mu.RLock() - defer j.mu.RUnlock() - return gconv.StructsDeep(*(j.p), pointer, mapping...) -} - -// ToScan automatically calls Struct or Structs function according to the type of parameter +// Scan automatically calls Struct or Structs function according to the type of parameter // to implement the converting.. -func (j *Json) ToScan(pointer interface{}, mapping ...map[string]string) error { +func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error { return gconv.Scan(*(j.p), pointer, mapping...) } -// ToScanDeep automatically calls StructDeep or StructsDeep function according to the type of -// parameter to implement the converting.. -func (j *Json) ToScanDeep(pointer interface{}, mapping ...map[string]string) error { - return gconv.ScanDeep(*(j.p), pointer, mapping...) -} - -// ToMapToMap converts current Json object to specified map variable. +// MapToMap converts current Json object to specified map variable. // The parameter of should be type of *map. -func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error { +func (j *Json) MapToMap(pointer interface{}, mapping ...map[string]string) error { j.mu.RLock() defer j.mu.RUnlock() return gconv.MapToMap(*(j.p), pointer, mapping...) } -// ToMapToMapDeep converts current Json object to specified map variable recursively. -// The parameter of should be type of *map. -func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error { - j.mu.RLock() - defer j.mu.RUnlock() - return gconv.MapToMapDeep(*(j.p), pointer, mapping...) -} - -// ToMapToMaps converts current Json object to specified map variable slice. +// MapToMaps converts current Json object to specified map variable slice. // The parameter of should be type of []map/*map. -func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error { +func (j *Json) MapToMaps(pointer interface{}, mapping ...map[string]string) error { j.mu.RLock() defer j.mu.RUnlock() return gconv.MapToMaps(*(j.p), pointer, mapping...) } -// ToMapToMapsDeep converts current Json object to specified map variable slice recursively. -// The parameter of should be type of []map/*map. -func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error { - j.mu.RLock() - defer j.mu.RUnlock() - return gconv.MapToMapsDeep(*(j.p), pointer, mapping...) -} - // Dump prints current Json object with more manually readable. func (j *Json) Dump() { j.mu.RLock() diff --git a/encoding/gjson/gjson_deprecated.go b/encoding/gjson/gjson_deprecated.go new file mode 100644 index 000000000..581f0e653 --- /dev/null +++ b/encoding/gjson/gjson_deprecated.go @@ -0,0 +1,113 @@ +// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gjson + +import "github.com/gogf/gf/util/gconv" + +// ToMap converts current Json object to map[string]interface{}. +// It returns nil if fails. +// Deprecated, use Map instead. +func (j *Json) ToMap() map[string]interface{} { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.Map(*(j.p)) +} + +// ToArray converts current Json object to []interface{}. +// It returns nil if fails. +// Deprecated, use Array instead. +func (j *Json) ToArray() []interface{} { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.Interfaces(*(j.p)) +} + +// ToStruct converts current Json object to specified object. +// The should be a pointer type of *struct. +// Deprecated, use Struct instead. +func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.Struct(*(j.p), pointer, mapping...) +} + +// ToStructDeep converts current Json object to specified object recursively. +// The should be a pointer type of *struct. +// Deprecated, use Struct instead. +func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.StructDeep(*(j.p), pointer, mapping...) +} + +// ToStructs converts current Json object to specified object slice. +// The should be a pointer type of []struct/*struct. +// Deprecated, use Structs instead. +func (j *Json) ToStructs(pointer interface{}, mapping ...map[string]string) error { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.Structs(*(j.p), pointer, mapping...) +} + +// ToStructsDeep converts current Json object to specified object slice recursively. +// The should be a pointer type of []struct/*struct. +// Deprecated, use Structs instead. +func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.StructsDeep(*(j.p), pointer, mapping...) +} + +// ToScan automatically calls Struct or Structs function according to the type of parameter +// to implement the converting.. +// Deprecated, use Scan instead. +func (j *Json) ToScan(pointer interface{}, mapping ...map[string]string) error { + return gconv.Scan(*(j.p), pointer, mapping...) +} + +// ToScanDeep automatically calls StructDeep or StructsDeep function according to the type of +// parameter to implement the converting.. +// Deprecated, use Scan instead. +func (j *Json) ToScanDeep(pointer interface{}, mapping ...map[string]string) error { + return gconv.ScanDeep(*(j.p), pointer, mapping...) +} + +// ToMapToMap converts current Json object to specified map variable. +// The parameter of should be type of *map. +// Deprecated, use MapToMap instead. +func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.MapToMap(*(j.p), pointer, mapping...) +} + +// ToMapToMapDeep converts current Json object to specified map variable recursively. +// The parameter of should be type of *map. +// Deprecated, use MapToMap instead. +func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.MapToMapDeep(*(j.p), pointer, mapping...) +} + +// ToMapToMaps converts current Json object to specified map variable slice. +// The parameter of should be type of []map/*map. +// Deprecated, use MapToMaps instead. +func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.MapToMaps(*(j.p), pointer, mapping...) +} + +// ToMapToMapsDeep converts current Json object to specified map variable slice recursively. +// The parameter of should be type of []map/*map. +// Deprecated, use MapToMaps instead. +func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error { + j.mu.RLock() + defer j.mu.RUnlock() + return gconv.MapToMapsDeep(*(j.p), pointer, mapping...) +} diff --git a/encoding/gjson/gjson_z_unit_basic_test.go b/encoding/gjson/gjson_z_unit_basic_test.go index fddf301c8..6269c94ff 100644 --- a/encoding/gjson/gjson_z_unit_basic_test.go +++ b/encoding/gjson/gjson_z_unit_basic_test.go @@ -159,7 +159,7 @@ func Test_GetArray(t *testing.T) { j, err := gjson.DecodeToJson(data) t.Assert(err, nil) t.Assert(j.GetArray("n"), g.Array{123456789}) - t.Assert(j.GetArray("m"), g.Array{"k", "v"}) + t.Assert(j.GetArray("m"), g.Array{g.Map{"k": "v"}}) t.Assert(j.GetArray("a"), g.Array{1, 2, 3}) }) } @@ -194,7 +194,7 @@ func Test_GetInterfaces(t *testing.T) { j, err := gjson.DecodeToJson(data) t.Assert(err, nil) t.AssertEQ(j.GetInterfaces("n"), g.Array{123456789}) - t.AssertEQ(j.GetInterfaces("m"), g.Array{"k", "v"}) + t.AssertEQ(j.GetInterfaces("m"), g.Array{g.Map{"k": "v"}}) t.AssertEQ(j.GetInterfaces("a"), g.Array{1, 2, 3}) }) }