Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore][pkg/stanza] Minor cleanup in split package #27075

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions pkg/stanza/split/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,38 @@ type Config struct {
}

// Func will return a bufio.SplitFunc based on the config
func (c Config) Func(enc encoding.Encoding, flushAtEOF bool, maxLogSize int) (splitFunc bufio.SplitFunc, err error) {
switch {
case c.LineEndPattern != "" && c.LineStartPattern != "":
return nil, fmt.Errorf("only one of line_start_pattern or line_end_pattern can be set")
case enc == encoding.Nop && (c.LineEndPattern != "" || c.LineStartPattern != ""):
return nil, fmt.Errorf("line_start_pattern or line_end_pattern should not be set when using nop encoding")
case enc == encoding.Nop:
return NoSplitFunc(maxLogSize), nil
case c.LineEndPattern == "" && c.LineStartPattern == "":
splitFunc, err = NewlineSplitFunc(enc, flushAtEOF)
if err != nil {
return nil, err
func (c Config) Func(enc encoding.Encoding, flushAtEOF bool, maxLogSize int) (bufio.SplitFunc, error) {
if enc == encoding.Nop {
if c.LineEndPattern != "" {
return nil, fmt.Errorf("line_end_pattern should not be set when using nop encoding")
}
if c.LineStartPattern != "" {
return nil, fmt.Errorf("line_start_pattern should not be set when using nop encoding")
}
case c.LineEndPattern != "":
return NoSplitFunc(maxLogSize), nil
}

if c.LineEndPattern == "" && c.LineStartPattern == "" {
return NewlineSplitFunc(enc, flushAtEOF)
}

if c.LineEndPattern != "" && c.LineStartPattern == "" {
re, err := regexp.Compile("(?m)" + c.LineEndPattern)
if err != nil {
return nil, fmt.Errorf("compile line end regex: %w", err)
}
splitFunc = LineEndSplitFunc(re, c.OmitPattern, flushAtEOF)
case c.LineStartPattern != "":
return LineEndSplitFunc(re, c.OmitPattern, flushAtEOF), nil
}

if c.LineEndPattern == "" && c.LineStartPattern != "" {
re, err := regexp.Compile("(?m)" + c.LineStartPattern)
if err != nil {
return nil, fmt.Errorf("compile line start regex: %w", err)
}
splitFunc = LineStartSplitFunc(re, c.OmitPattern, flushAtEOF)
return LineStartSplitFunc(re, c.OmitPattern, flushAtEOF), nil
}
return splitFunc, nil

return nil, fmt.Errorf("only one of line_start_pattern or line_end_pattern can be set")
}

// LineStartSplitFunc creates a bufio.SplitFunc that splits an incoming stream into
Expand All @@ -61,8 +66,7 @@ func LineStartSplitFunc(re *regexp.Regexp, omitPattern bool, flushAtEOF bool) bu
}
return 0, nil, nil // read more data and try again.
}
firstMatchStart := firstLoc[0]
firstMatchEnd := firstLoc[1]
firstMatchStart, firstMatchEnd := firstLoc[0], firstLoc[1]

if firstMatchStart != 0 {
// the beginning of the file does not match the start pattern, so return a token up to the first match so we don't lose data
Expand Down
20 changes: 10 additions & 10 deletions pkg/stanza/split/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ func TestConfigFunc(t *testing.T) {
assert.Equal(t, raw[:maxLogSize], token)
})

t.Run("NopEncodingError", func(t *testing.T) {
endCfg := Config{LineEndPattern: "\n"}
_, err := endCfg.Func(encoding.Nop, false, 0)
require.Equal(t, err, fmt.Errorf("line_end_pattern should not be set when using nop encoding"))

startCfg := Config{LineStartPattern: "\n"}
_, err = startCfg.Func(encoding.Nop, false, 0)
require.Equal(t, err, fmt.Errorf("line_start_pattern should not be set when using nop encoding"))
})

t.Run("Newline", func(t *testing.T) {
cfg := Config{}
f, err := cfg.Func(unicode.UTF8, false, maxLogSize)
Expand Down Expand Up @@ -778,16 +788,6 @@ func TestNoSplitFunc(t *testing.T) {
}
}

func TestNoopEncodingError(t *testing.T) {
endCfg := Config{LineEndPattern: "\n"}
_, err := endCfg.Func(encoding.Nop, false, 0)
require.Equal(t, err, fmt.Errorf("line_start_pattern or line_end_pattern should not be set when using nop encoding"))

startCfg := Config{LineStartPattern: "\n"}
_, err = startCfg.Func(encoding.Nop, false, 0)
require.Equal(t, err, fmt.Errorf("line_start_pattern or line_end_pattern should not be set when using nop encoding"))
}

func TestNewlineSplitFunc_Encodings(t *testing.T) {
cases := []struct {
name string
Expand Down