improve package gmeta

This commit is contained in:
John Guo
2021-10-12 17:52:31 +08:00
parent 18ceebeffd
commit f887c9f44b
3 changed files with 41 additions and 5 deletions

View File

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

View File

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

View File

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