Skip to content

Commit

Permalink
Parentheses and keeping it as part of the field
Browse files Browse the repository at this point in the history
  • Loading branch information
kbence committed Dec 20, 2016
1 parent 056eb0e commit 07c62ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 7 additions & 4 deletions parser/columnparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ func isWhitespace(ch byte) bool {

type separator struct {
start, end byte
keep bool
}

var separators = []separator{
separator{'"', '"'},
separator{'[', ']'},
separator{'"', '"', false},
separator{'\'', '\'', false},
separator{'[', ']', true},
separator{'(', ')', true},
}

func isQuoteStart(char byte) bool {
Expand Down Expand Up @@ -71,7 +74,7 @@ func ParseColumns(output types.LogLineChannel, input types.LogLineChannel) {

if ws {
if !isws {
if !isQuoteStart(char) {
if !isQuoteStart(char) || separators[getQuoteType(char)].keep {
columnBuffer.WriteByte(char)
}
ws = false
Expand All @@ -83,7 +86,7 @@ func ParseColumns(output types.LogLineChannel, input types.LogLineChannel) {
columnBuffer = bytes.Buffer{}
ws = true
} else {
if !isQuoteEnd(char) {
if !isQuoteEnd(char) || separators[quoteType].keep {
columnBuffer.WriteByte(char)
}
}
Expand Down
16 changes: 8 additions & 8 deletions parser/columnparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestSimpleColumns(t *testing.T) {
}

func TestQuotedColumns(t *testing.T) {
input := makeChan("first \"second column\" \"third\"")
input := makeChan("first \"second column\" 'third'")
output := make(types.LogLineChannel)

go ParseColumns(output, input)
Expand All @@ -55,14 +55,14 @@ func TestQuotedColumns(t *testing.T) {
if len(result.Columns) != 3 {
t.Error("Result must have 3 columns")
} else if result.Columns[2] != "second column" {
t.Errorf("Column[1] \"%s\" != \"second column\"", result.Columns[1])
t.Errorf("Column[2] \"%s\" != \"second column\"", result.Columns[2])
} else if result.Columns[3] != "third" {
t.Errorf("Column[2] \"%s\" != \"third\"", result.Columns[2])
t.Errorf("Column[3] \"%s\" != \"third\"", result.Columns[3])
}
}

func TestBracketedColumns(t *testing.T) {
input := makeChan("first [second column] [third]")
input := makeChan("first [second column] (third column)")
output := make(types.LogLineChannel)

go ParseColumns(output, input)
Expand All @@ -71,9 +71,9 @@ func TestBracketedColumns(t *testing.T) {

if len(result.Columns) != 3 {
t.Error("Result must have 3 columns")
} else if result.Columns[2] != "second column" {
t.Errorf("Column[1] \"%s\" != \"second column\"", result.Columns[1])
} else if result.Columns[3] != "third" {
t.Errorf("Column[2] \"%s\" != \"third\"", result.Columns[2])
} else if result.Columns[2] != "[second column]" {
t.Errorf("Column[2] \"%s\" != \"[second column]\"", result.Columns[2])
} else if result.Columns[3] != "(third column)" {
t.Errorf("Column[3] \"%s\" != \"third column\"", result.Columns[3])
}
}

0 comments on commit 07c62ce

Please sign in to comment.