From 79af7e8abf2641fe4c4d806e2908592e82f40244 Mon Sep 17 00:00:00 2001 From: boojack Date: Sat, 12 Nov 2022 09:02:44 +0800 Subject: [PATCH] fix: parse tag list (#446) --- server/metric_collector.go | 2 +- server/tag.go | 35 +++++++++++++++++++--------------- server/tag_test.go | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 server/tag_test.go diff --git a/server/metric_collector.go b/server/metric_collector.go index 0be3b368..550db88c 100644 --- a/server/metric_collector.go +++ b/server/metric_collector.go @@ -19,7 +19,7 @@ type MetricCollector struct { } const ( - segmentMetricWriteKey = "FqYUl1CmssHytFSnnVd0efV4gyGeH0dx" + segmentMetricWriteKey = "fTn5BumOkj352n3TGw9tu0ARH2dOkcoQ" ) func NewMetricCollector(profile *profile.Profile, store *store.Store) MetricCollector { diff --git a/server/tag.go b/server/tag.go index 493b8057..33f4a680 100644 --- a/server/tag.go +++ b/server/tag.go @@ -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 { @@ -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) @@ -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 +} diff --git a/server/tag_test.go b/server/tag_test.go new file mode 100644 index 00000000..0d0fa982 --- /dev/null +++ b/server/tag_test.go @@ -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) + } + } +}