mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve package gmeta
This commit is contained in:
@ -277,9 +277,5 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
|
||||
}
|
||||
|
||||
func (oai *OpenApiV3) doesStructHasNoFields(s interface{}) bool {
|
||||
structFields, _ := structs.Fields(structs.FieldsInput{
|
||||
Pointer: s,
|
||||
RecursiveOption: structs.RecursiveOptionEmbeddedNoTag,
|
||||
})
|
||||
return len(structFields) == 0
|
||||
return reflect.TypeOf(s).NumField() == 0
|
||||
}
|
||||
|
||||
@ -282,6 +282,11 @@ func doMapConvertForMapOrStructValue(isRoot bool, value interface{}, recursive b
|
||||
}
|
||||
switch rvAttrKind {
|
||||
case reflect.Struct:
|
||||
// Embedded struct and has no fields, just ignores it.
|
||||
// Eg: gmeta.Meta
|
||||
if rvAttrField.Type().NumField() == 0 {
|
||||
continue
|
||||
}
|
||||
var (
|
||||
hasNoTag = mapKey == fieldName
|
||||
rvAttrInterface = rvAttrField.Interface()
|
||||
|
||||
@ -8,6 +8,7 @@ package gmeta_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/gmeta"
|
||||
"testing"
|
||||
|
||||
@ -36,3 +37,37 @@ func TestMeta_Basic(t *testing.T) {
|
||||
t.Assert(b, `{"Id":100,"Name":"john"}`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMeta_Convert_Map(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",
|
||||
}
|
||||
m := gconv.Map(a)
|
||||
t.Assert(len(m), 2)
|
||||
t.Assert(m[`Meta`], nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMeta_Json(t *testing.T) {
|
||||
type A struct {
|
||||
gmeta.Meta `tag:"123" orm:"456"`
|
||||
Id int
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
a := &A{
|
||||
Id: 100,
|
||||
}
|
||||
b, err := json.Marshal(a)
|
||||
t.AssertNil(err)
|
||||
t.Assert(string(b), `{"Id":100}`)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user