add alias g.Meta for gmeta.Meta

This commit is contained in:
John Guo
2021-10-06 20:11:54 +08:00
parent 892d3abf38
commit 5d804987b9
4 changed files with 50 additions and 25 deletions

View File

@ -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 (

View File

@ -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)
}

View File

@ -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"}`)
})
}

View File

@ -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"}`)
})
}