diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index 49dadbc41d9..1e5c34a7726 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -355,6 +355,9 @@ func newArgumentsFromInput(object interface{}) (args []Argument, err error) { arg.Short, reflect.TypeOf(object).String(), field.Name(), ) } + if arg.Brief == "" { + arg.Brief = field.TagDescription() + } if v, ok := metaData[gtag.Arg]; ok { arg.IsArg = gconv.Bool(v) } diff --git a/os/gstructs/gstructs_field.go b/os/gstructs/gstructs_field.go index 74d8ee0d1e1..a99a2f37655 100644 --- a/os/gstructs/gstructs_field.go +++ b/os/gstructs/gstructs_field.go @@ -8,16 +8,11 @@ package gstructs import ( "reflect" - "strings" "github.com/gogf/gf/v2/internal/utils" "github.com/gogf/gf/v2/util/gtag" ) -const ( - jsonTagName = `json` -) - // Tag returns the value associated with key in the tag string. If there is no // such key in the tag, Tag returns the empty string. func (f *Field) Tag(key string) string { @@ -28,14 +23,6 @@ func (f *Field) Tag(key string) string { return s } -// TagJsonName returns the `json` tag name string of the field. -func (f *Field) TagJsonName() string { - if jsonTag := f.Tag(jsonTagName); jsonTag != "" { - return strings.Split(jsonTag, ",")[0] - } - return "" -} - // TagLookup returns the value associated with key in the tag string. // If the key is present in the tag the value (which may be empty) // is returned. Otherwise, the returned value will be the empty string. diff --git a/os/gstructs/gstructs_field_tag.go b/os/gstructs/gstructs_field_tag.go new file mode 100644 index 00000000000..2fd039781b8 --- /dev/null +++ b/os/gstructs/gstructs_field_tag.go @@ -0,0 +1,90 @@ +// 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 gstructs + +import ( + "strings" + + "github.com/gogf/gf/v2/util/gtag" +) + +// TagJsonName returns the `json` tag name string of the field. +func (f *Field) TagJsonName() string { + if jsonTag := f.Tag(gtag.Json); jsonTag != "" { + return strings.Split(jsonTag, ",")[0] + } + return "" +} + +// TagDefault returns the most commonly used tag `default/d` value of the field. +func (f *Field) TagDefault() string { + v := f.Tag(gtag.Default) + if v == "" { + v = f.Tag(gtag.DefaultShort) + } + return v +} + +// TagParam returns the most commonly used tag `param/p` value of the field. +func (f *Field) TagParam() string { + v := f.Tag(gtag.Param) + if v == "" { + v = f.Tag(gtag.ParamShort) + } + return v +} + +// TagValid returns the most commonly used tag `valid/v` value of the field. +func (f *Field) TagValid() string { + v := f.Tag(gtag.Valid) + if v == "" { + v = f.Tag(gtag.ValidShort) + } + return v +} + +// TagDescription returns the most commonly used tag `description/des/dc` value of the field. +func (f *Field) TagDescription() string { + v := f.Tag(gtag.Description) + if v == "" { + v = f.Tag(gtag.DescriptionShort) + } + if v == "" { + v = f.Tag(gtag.DescriptionShort2) + } + return v +} + +// TagSummary returns the most commonly used tag `summary/sum/sm` value of the field. +func (f *Field) TagSummary() string { + v := f.Tag(gtag.Summary) + if v == "" { + v = f.Tag(gtag.SummaryShort) + } + if v == "" { + v = f.Tag(gtag.SummaryShort2) + } + return v +} + +// TagAdditional returns the most commonly used tag `additional/ad` value of the field. +func (f *Field) TagAdditional() string { + v := f.Tag(gtag.Additional) + if v == "" { + v = f.Tag(gtag.AdditionalShort) + } + return v +} + +// TagExample returns the most commonly used tag `example/eg` value of the field. +func (f *Field) TagExample() string { + v := f.Tag(gtag.Example) + if v == "" { + v = f.Tag(gtag.ExampleShort) + } + return v +} diff --git a/os/gstructs/gstructs_z_unit_test.go b/os/gstructs/gstructs_z_unit_test.go index 1b814895bf8..264bf257743 100644 --- a/os/gstructs/gstructs_z_unit_test.go +++ b/os/gstructs/gstructs_z_unit_test.go @@ -381,3 +381,149 @@ func TestType_TagMap(t *testing.T) { t.Assert(r[1].TagMap()["description"], `应用Id`) }) } + +func TestType_TagJsonName(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Name string `json:"name,omitempty"` + } + r, err := gstructs.Fields(gstructs.FieldsInput{ + Pointer: new(A), + RecursiveOption: 0, + }) + t.AssertNil(err) + + t.Assert(len(r), 1) + t.Assert(r[0].TagJsonName(), `name`) + }) +} + +func TestType_TagDefault(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Name string `default:"john"` + Name2 string `d:"john"` + } + r, err := gstructs.Fields(gstructs.FieldsInput{ + Pointer: new(A), + RecursiveOption: 0, + }) + t.AssertNil(err) + + t.Assert(len(r), 2) + t.Assert(r[0].TagDefault(), `john`) + t.Assert(r[1].TagDefault(), `john`) + }) +} + +func TestType_TagParam(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Name string `param:"name"` + Name2 string `p:"name"` + } + r, err := gstructs.Fields(gstructs.FieldsInput{ + Pointer: new(A), + RecursiveOption: 0, + }) + t.AssertNil(err) + + t.Assert(len(r), 2) + t.Assert(r[0].TagParam(), `name`) + t.Assert(r[1].TagParam(), `name`) + }) +} + +func TestType_TagValid(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Name string `valid:"required"` + Name2 string `v:"required"` + } + r, err := gstructs.Fields(gstructs.FieldsInput{ + Pointer: new(A), + RecursiveOption: 0, + }) + t.AssertNil(err) + + t.Assert(len(r), 2) + t.Assert(r[0].TagValid(), `required`) + t.Assert(r[1].TagValid(), `required`) + }) +} + +func TestType_TagDescription(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Name string `description:"my name"` + Name2 string `des:"my name"` + Name3 string `dc:"my name"` + } + r, err := gstructs.Fields(gstructs.FieldsInput{ + Pointer: new(A), + RecursiveOption: 0, + }) + t.AssertNil(err) + + t.Assert(len(r), 3) + t.Assert(r[0].TagDescription(), `my name`) + t.Assert(r[1].TagDescription(), `my name`) + t.Assert(r[2].TagDescription(), `my name`) + }) +} + +func TestType_TagSummary(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Name string `summary:"my name"` + Name2 string `sum:"my name"` + Name3 string `sm:"my name"` + } + r, err := gstructs.Fields(gstructs.FieldsInput{ + Pointer: new(A), + RecursiveOption: 0, + }) + t.AssertNil(err) + + t.Assert(len(r), 3) + t.Assert(r[0].TagSummary(), `my name`) + t.Assert(r[1].TagSummary(), `my name`) + t.Assert(r[2].TagSummary(), `my name`) + }) +} + +func TestType_TagAdditional(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Name string `additional:"my name"` + Name2 string `ad:"my name"` + } + r, err := gstructs.Fields(gstructs.FieldsInput{ + Pointer: new(A), + RecursiveOption: 0, + }) + t.AssertNil(err) + + t.Assert(len(r), 2) + t.Assert(r[0].TagAdditional(), `my name`) + t.Assert(r[1].TagAdditional(), `my name`) + }) +} + +func TestType_TagExample(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type A struct { + Name string `example:"john"` + Name2 string `eg:"john"` + } + r, err := gstructs.Fields(gstructs.FieldsInput{ + Pointer: new(A), + RecursiveOption: 0, + }) + t.AssertNil(err) + + t.Assert(len(r), 2) + t.Assert(r[0].TagExample(), `john`) + t.Assert(r[1].TagExample(), `john`) + }) +}