From 5d804987b9946b246826e7bce1a7805b03d101ca Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 6 Oct 2021 20:11:54 +0800 Subject: [PATCH] add alias g.Meta for gmeta.Meta --- frame/g/g.go | 6 ++- util/gmeta/gmeta.go | 12 ++++-- .../{gmeta_test.go => gmeta_bench_test.go} | 19 ---------- util/gmeta/gmeta_unit_test.go | 38 +++++++++++++++++++ 4 files changed, 50 insertions(+), 25 deletions(-) rename util/gmeta/{gmeta_test.go => gmeta_bench_test.go} (71%) create mode 100644 util/gmeta/gmeta_unit_test.go diff --git a/frame/g/g.go b/frame/g/g.go index c8d4163e7..be1cbef2d 100644 --- a/frame/g/g.go +++ b/frame/g/g.go @@ -9,11 +9,13 @@ package g import ( "context" "github.com/gogf/gf/container/gvar" + "github.com/gogf/gf/util/gmeta" ) type ( - Var = gvar.Var // Var is a universal variable interface, like generics. - Ctx = context.Context // Ctx is alias of frequently-used context.Context. + Var = gvar.Var // Var is a universal variable interface, like generics. + Ctx = context.Context // Ctx is alias of frequently-used type context.Context. + Meta = gmeta.Meta // Meta is alias of frequently-used type gmeta.Meta. ) type ( diff --git a/util/gmeta/gmeta.go b/util/gmeta/gmeta.go index c522fcd7b..d81e13ed2 100644 --- a/util/gmeta/gmeta.go +++ b/util/gmeta/gmeta.go @@ -12,16 +12,16 @@ import ( "github.com/gogf/gf/internal/structs" ) -// Meta is used as an embedded attribute for struct to enabled meta data feature. +// Meta is used as an embedded attribute for struct to enabled metadata feature. type Meta struct{} const ( - // metaAttributeName is the attribute name of meta data in struct. + // metaAttributeName is the attribute name of metadata in struct. metaAttributeName = "Meta" ) // Data retrieves and returns all metadata from `object`. -// It automatically parses and caches the tag string from "Mata" attribute as its meta data. +// It automatically parses and caches the tag string from "Mata" attribute as its metadata. func Data(object interface{}) map[string]interface{} { reflectType, err := structs.StructType(object) if err != nil { @@ -42,5 +42,9 @@ func Data(object interface{}) map[string]interface{} { // Get retrieves and returns specified metadata by `key` from `object`. func Get(object interface{}, key string) *gvar.Var { - return gvar.New(Data(object)[key]) + v := Data(object)[key] + if v == nil { + return nil + } + return gvar.New(v) } diff --git a/util/gmeta/gmeta_test.go b/util/gmeta/gmeta_bench_test.go similarity index 71% rename from util/gmeta/gmeta_test.go rename to util/gmeta/gmeta_bench_test.go index 2186d5414..6858ba534 100644 --- a/util/gmeta/gmeta_test.go +++ b/util/gmeta/gmeta_bench_test.go @@ -7,11 +7,8 @@ package gmeta_test import ( - "github.com/gogf/gf/internal/json" "github.com/gogf/gf/util/gmeta" "testing" - - "github.com/gogf/gf/test/gtest" ) type A struct { @@ -60,19 +57,3 @@ func Benchmark_Data_Get_Pointer2(b *testing.B) { gmeta.Get(&a2, "tag") } } - -func TestMeta_Basic(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - a := &A{ - Id: 100, - Name: "john", - } - t.Assert(len(gmeta.Data(a)), 2) - t.Assert(gmeta.Get(a, "tag").String(), "123") - t.Assert(gmeta.Get(a, "orm").String(), "456") - - b, err := json.Marshal(a) - t.AssertNil(err) - t.Assert(b, `{"Id":100,"Name":"john"}`) - }) -} diff --git a/util/gmeta/gmeta_unit_test.go b/util/gmeta/gmeta_unit_test.go new file mode 100644 index 000000000..71e310529 --- /dev/null +++ b/util/gmeta/gmeta_unit_test.go @@ -0,0 +1,38 @@ +// Copyright GoFrame Author(https://goframe.org). 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 gmeta_test + +import ( + "github.com/gogf/gf/internal/json" + "github.com/gogf/gf/util/gmeta" + "testing" + + "github.com/gogf/gf/test/gtest" +) + +func TestMeta_Basic(t *testing.T) { + type A struct { + gmeta.Meta `tag:"123" orm:"456"` + Id int + Name string + } + + gtest.C(t, func(t *gtest.T) { + a := &A{ + Id: 100, + Name: "john", + } + t.Assert(len(gmeta.Data(a)), 2) + t.AssertEQ(gmeta.Get(a, "tag").String(), "123") + t.AssertEQ(gmeta.Get(a, "orm").String(), "456") + t.AssertEQ(gmeta.Get(a, "none"), nil) + + b, err := json.Marshal(a) + t.AssertNil(err) + t.Assert(b, `{"Id":100,"Name":"john"}`) + }) +}