Skip to content

Commit

Permalink
Move out ltsv parse func
Browse files Browse the repository at this point in the history
  • Loading branch information
u5surf committed Jul 5, 2023
1 parent 4115923 commit 5e6df6d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
36 changes: 17 additions & 19 deletions src/core/tailer/tailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ type PatternTailer struct {
}

type LTSVTailer struct {
handle *tail.Tail
ltsvParseString func(line string) map[string]string
handle *tail.Tail
}

func NewTailer(file string) (*Tailer, error) {
Expand Down Expand Up @@ -102,25 +101,11 @@ func NewPatternTailer(file string, patterns map[string]string) (*PatternTailer,
}

func NewLTSVTailer(file string) (*LTSVTailer, error) {
ltsvParseString := func(line string) map[string]string {
columns := strings.Split(line, "\t")
lvs := make(map[string]string)
for _, column := range columns {
lv := strings.SplitN(column, ":", 2)
if len(lv) < 2 {
continue
}
label, value := strings.TrimSpace(lv[0]), strings.TrimSpace(lv[1])
lvs[label] = value
}
return lvs
}
t, err := tail.TailFile(file, tailConfig)
if err != nil {
return nil, err
}

return &LTSVTailer{t, ltsvParseString}, nil
return &LTSVTailer{t}, nil
}

func (t *Tailer) Tail(ctx context.Context, data chan<- string) {
Expand Down Expand Up @@ -189,8 +174,7 @@ func (t *LTSVTailer) Tail(ctx context.Context, data chan<- map[string]string) {
if line.Err != nil {
continue
}

l := t.ltsvParseString(line.Text)
l := t.parse(line.Text)
if l != nil {
data <- l
}
Expand All @@ -207,3 +191,17 @@ func (t *LTSVTailer) Tail(ctx context.Context, data chan<- map[string]string) {
}
}
}

func (t *LTSVTailer) parse(line string) map[string]string {
columns := strings.Split(line, "\t")
lineMap := make(map[string]string)
for _, column := range columns {
labelValue := strings.SplitN(column, ":", 2)
if len(labelValue) < 2 {
continue
}
label, value := strings.TrimSpace(labelValue[0]), strings.TrimSpace(labelValue[1])
lineMap[label] = value
}
return lineMap
}
4 changes: 2 additions & 2 deletions src/core/tailer/tailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ func TestLTSVTailer(t *testing.T) {
accessLogFile.Close()

var count int
var res map[string]string
var res map[string]string
T:
for {
select {
case r := <-data:
res = r
res = r
count++
case <-time.After(timeoutDuration):
break T
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e6df6d

Please sign in to comment.