Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Tag* functions to retreive most commonly used tag value from struct field for package gstructs; use description tag as default value if brief is empty for gcmd.Argument #2299

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions os/gcmd/gcmd_command_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 0 additions & 13 deletions os/gstructs/gstructs_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
90 changes: 90 additions & 0 deletions os/gstructs/gstructs_field_tag.go
Original file line number Diff line number Diff line change
@@ -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
}
146 changes: 146 additions & 0 deletions os/gstructs/gstructs_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
})
}