mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
@@ -49,6 +49,9 @@ func Parse(tag string) (*Tags, error) {
|
||||
|
||||
// Get gets tag value by specified key
|
||||
func (t *Tags) Get(key string) (*Tag, error) {
|
||||
if t == nil {
|
||||
return nil, errTagNotExist
|
||||
}
|
||||
for _, tag := range t.tags {
|
||||
if tag.Key == key {
|
||||
return tag, nil
|
||||
@@ -60,6 +63,9 @@ func (t *Tags) Get(key string) (*Tag, error) {
|
||||
|
||||
// Keys returns all keys in Tags
|
||||
func (t *Tags) Keys() []string {
|
||||
if t == nil {
|
||||
return []string{}
|
||||
}
|
||||
var keys []string
|
||||
for _, tag := range t.tags {
|
||||
keys = append(keys, tag.Key)
|
||||
@@ -69,5 +75,8 @@ func (t *Tags) Keys() []string {
|
||||
|
||||
// Tags returns all tags in Tags
|
||||
func (t *Tags) Tags() []*Tag {
|
||||
if t == nil {
|
||||
return []*Tag{}
|
||||
}
|
||||
return t.tags
|
||||
}
|
||||
|
||||
68
tools/goctl/api/spec/tags_test.go
Normal file
68
tools/goctl/api/spec/tags_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package spec
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTags_Get(t *testing.T) {
|
||||
tags := &Tags{
|
||||
tags: []*Tag{
|
||||
{Key: "json", Name: "foo", Options: []string{"omitempty"}},
|
||||
{Key: "xml", Name: "bar", Options: nil},
|
||||
},
|
||||
}
|
||||
|
||||
tag, err := tags.Get("json")
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, tag)
|
||||
assert.Equal(t, "json", tag.Key)
|
||||
assert.Equal(t, "foo", tag.Name)
|
||||
|
||||
_, err = tags.Get("yaml")
|
||||
assert.Error(t, err)
|
||||
|
||||
var nilTags *Tags
|
||||
_, err = nilTags.Get("json")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestTags_Keys(t *testing.T) {
|
||||
tags := &Tags{
|
||||
tags: []*Tag{
|
||||
{Key: "json", Name: "foo", Options: []string{"omitempty"}},
|
||||
{Key: "xml", Name: "bar", Options: nil},
|
||||
},
|
||||
}
|
||||
|
||||
keys := tags.Keys()
|
||||
expected := []string{"json", "xml"}
|
||||
assert.Equal(t, expected, keys)
|
||||
|
||||
var nilTags *Tags
|
||||
nilKeys := nilTags.Keys()
|
||||
assert.Empty(t, nilKeys)
|
||||
}
|
||||
|
||||
func TestTags_Tags(t *testing.T) {
|
||||
tags := &Tags{
|
||||
tags: []*Tag{
|
||||
{Key: "json", Name: "foo", Options: []string{"omitempty"}},
|
||||
{Key: "xml", Name: "bar", Options: nil},
|
||||
},
|
||||
}
|
||||
|
||||
result := tags.Tags()
|
||||
assert.Len(t, result, 2)
|
||||
assert.Equal(t, "json", result[0].Key)
|
||||
assert.Equal(t, "foo", result[0].Name)
|
||||
assert.Equal(t, []string{"omitempty"}, result[0].Options)
|
||||
assert.Equal(t, "xml", result[1].Key)
|
||||
assert.Equal(t, "bar", result[1].Name)
|
||||
assert.Nil(t, result[1].Options)
|
||||
|
||||
var nilTags *Tags
|
||||
nilResult := nilTags.Tags()
|
||||
assert.Empty(t, nilResult)
|
||||
}
|
||||
Reference in New Issue
Block a user