Skip to content

Commit

Permalink
fix: parse tag list (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack authored Nov 12, 2022
1 parent a142d97 commit 79af7e8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion server/metric_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type MetricCollector struct {
}

const (
segmentMetricWriteKey = "FqYUl1CmssHytFSnnVd0efV4gyGeH0dx"
segmentMetricWriteKey = "fTn5BumOkj352n3TGw9tu0ARH2dOkcoQ"
)

func NewMetricCollector(profile *profile.Profile, store *store.Store) MetricCollector {
Expand Down
35 changes: 20 additions & 15 deletions server/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/labstack/echo/v4"
)

var tagRegexpList = []*regexp.Regexp{regexp.MustCompile(`^#([^\s#]+?) `), regexp.MustCompile(`\s#([^\s#]+?) `)}
var tagRegexpList = []*regexp.Regexp{regexp.MustCompile(`^#([^\s#]+?) `), regexp.MustCompile(`[^\S#]?#([^\s#]+?) `)}

func (s *Server) registerTagRoutes(g *echo.Group) {
g.GET("/tag", func(c echo.Context) error {
Expand Down Expand Up @@ -47,22 +47,10 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
}

tagMapSet := make(map[string]bool)

for _, memo := range memoList {
for _, tagRegexp := range tagRegexpList {
for _, rawTag := range tagRegexp.FindAllString(memo.Content, -1) {
tag := tagRegexp.ReplaceAllString(rawTag, "$1")
tagMapSet[tag] = true
}
}
}

tagList := []string{}
for tag := range tagMapSet {
tagList = append(tagList, tag)
for _, memo := range memoList {
tagList = append(tagList, findTagListFromMemoContent(memo.Content)...)
}

sort.Strings(tagList)

c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
Expand All @@ -72,3 +60,20 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
return nil
})
}

func findTagListFromMemoContent(memoContent string) []string {
tagMapSet := make(map[string]bool)
for _, tagRegexp := range tagRegexpList {
for _, rawTag := range tagRegexp.FindAllString(memoContent, -1) {
tag := tagRegexp.ReplaceAllString(rawTag, "$1")
tagMapSet[tag] = true
}
}

tagList := []string{}
for tag := range tagMapSet {
tagList = append(tagList, tag)
}
sort.Strings(tagList)
return tagList
}
39 changes: 39 additions & 0 deletions server/tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package server

import (
"testing"
)

func TestFindTagListFromMemoContent(t *testing.T) {
tests := []struct {
memoContent string
want []string
}{
{
memoContent: "#tag1 ",
want: []string{"tag1"},
},
{
memoContent: "#tag1 #tag2 ",
want: []string{"tag1", "tag2"},
},
{
memoContent: "#tag1 #tag2 \n#tag3 ",
want: []string{"tag1", "tag2", "tag3"},
},
{
memoContent: "#tag1 #tag2 \n#tag3 #tag4 ",
want: []string{"tag1", "tag2", "tag3", "tag4"},
},
{
memoContent: "#tag1 #tag2 \n#tag3 #tag4 ",
want: []string{"tag1", "tag2", "tag3", "tag4"},
},
}
for _, test := range tests {
result := findTagListFromMemoContent(test.memoContent)
if len(result) != len(test.want) {
t.Errorf("Find tag list %s: got result %v, want %v.", test.memoContent, result, test.want)
}
}
}

0 comments on commit 79af7e8

Please sign in to comment.