Skip to content

Commit

Permalink
mir: support set custom tag name to mir
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Jan 15, 2019
1 parent 7569578 commit 760bab7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
9 changes: 9 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (b *blog) GetArticles() string {
return "GetArticles"
}

type comment struct {
group Group `urban:"v1"`
index Get `urban:"/index/"`
}

func (c *comment) Index() string {
return "Index"
}

func pingChain() string {
return "simpleChain"
}
Expand Down
12 changes: 9 additions & 3 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ import (
"unicode/utf8"
)


const (
// TagName mir's struct tag string name
TagName = "mir"
// DefaultTag indicate default mir's struct tag name
DefaultTag = "mir"
)

var (
// tagName indicate mir's struct tag string name
tagName = DefaultTag
)

var (
Expand Down Expand Up @@ -186,7 +192,7 @@ func tagInfoFrom(field reflect.StructField) (*tagInfo, error) {
info := &tagInfo{}

// lookup mir tag info from struct field
tag, exist := field.Tag.Lookup(TagName)
tag, exist := field.Tag.Lookup(tagName)
if !exist {
return nil, tagNotExist
}
Expand Down
7 changes: 7 additions & 0 deletions mir.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ var (
engine Engine
)

// SetTag set custom mir's struct tag name(eg: mir)
func SetTag(name string) {
if name != "" {
tagName = name
}
}

// SetDefault set default engine for register handler.
func SetDefault(e Engine) {
if engine != nil {
Expand Down
23 changes: 23 additions & 0 deletions mir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ func TestTagMirFrom(t *testing.T) {
assertTagMir(t, tagMirs)
}

func TestSetTag(t *testing.T) {
// set custom tag name
SetTag("urban")

tagMirs, err := TagMirFrom(&comment{})
if err != nil {
t.Error(err)
}
if len(tagMirs) != 1 {
t.Errorf("want one TagMir but have %d", len(tagMirs))
}
tagMir := tagMirs[0]
if len(tagMir.Fields) != 1 {
t.Errorf("want one TagFields but hava %d", len(tagMir.Fields))
}
if tagMir.Group != "v1" {
t.Errorf("want group v1 but is %s", tagMir.Group)
}

// Reset tag name to default
SetTag(DefaultTag)
}

func assertTagMir(t *testing.T, tagMirs []*TagMir) {
isCheckedGroupV2 := false
for _, mir := range tagMirs {
Expand Down

0 comments on commit 760bab7

Please sign in to comment.