diff --git a/os/gstructs/gstructs_field_tag.go b/os/gstructs/gstructs_field_tag.go index fa7211515..08342b2cc 100644 --- a/os/gstructs/gstructs_field_tag.go +++ b/os/gstructs/gstructs_field_tag.go @@ -101,8 +101,12 @@ func (f *Field) TagPriorityName() string { name := f.Name() for _, tagName := range gtag.StructTagPriority { if tagValue := f.Tag(tagName); tagValue != "" { - name = tagValue - break + // Strip tag options after comma, e.g., json:"name,omitempty" -> "name". + tagValue = strings.Split(tagValue, ",")[0] + if tagValue != "" { + name = tagValue + break + } } } return name diff --git a/os/gstructs/gstructs_z_unit_test.go b/os/gstructs/gstructs_z_unit_test.go index 15b1befd5..fd68f7322 100644 --- a/os/gstructs/gstructs_z_unit_test.go +++ b/os/gstructs/gstructs_z_unit_test.go @@ -546,4 +546,32 @@ func Test_Fields_TagPriorityName(t *testing.T) { t.Assert(fields[2].TagPriorityName(), "pass_json") t.Assert(fields[3].TagPriorityName(), "IsMen") }) + // Tag with omitempty option should return name only, not "name,omitempty". + gtest.C(t, func(t *gtest.T) { + type User struct { + Name string `json:"user_name,omitempty"` + Age uint `gconv:"age,string" json:"age_json"` + } + var user *User + fields, _ := gstructs.Fields(gstructs.FieldsInput{ + Pointer: user, + RecursiveOption: 0, + }) + t.Assert(fields[0].TagPriorityName(), "user_name") + t.Assert(fields[1].TagPriorityName(), "age") + }) + // Empty tag name with options (e.g., gconv:",omitempty") should fallthrough to next priority tag. + gtest.C(t, func(t *gtest.T) { + type User struct { + Name string `gconv:",omitempty" json:"name_json"` + Age uint `gconv:",string" param:",omitempty" json:"age_json"` + } + var user *User + fields, _ := gstructs.Fields(gstructs.FieldsInput{ + Pointer: user, + RecursiveOption: 0, + }) + t.Assert(fields[0].TagPriorityName(), "name_json") + t.Assert(fields[1].TagPriorityName(), "age_json") + }) }