-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from nanic/feat/json-produce-support
Feat/json produce support
- Loading branch information
Showing
13 changed files
with
246 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package input | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/deviceinsight/kafkactl/output" | ||
"github.com/deviceinsight/kafkactl/util" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const defaultKeyColumnIdx = 0 | ||
const defaultValueColumnIdx = 1 | ||
const defaultColumnCount = 2 | ||
|
||
type csvParser struct { | ||
key string | ||
separator string | ||
keyColumnIdx int | ||
valueColumnIdx int | ||
columnCount int | ||
} | ||
|
||
func NewCsvParser(key string, separator string) Parser { | ||
return &csvParser{ | ||
key: key, | ||
separator: separator, | ||
keyColumnIdx: defaultKeyColumnIdx, | ||
valueColumnIdx: defaultValueColumnIdx, | ||
columnCount: defaultColumnCount, | ||
} | ||
} | ||
|
||
func (p *csvParser) ParseLine(line string) (Message, error) { | ||
|
||
if p.separator == "" { | ||
return Message{Key: p.key, Value: line}, nil | ||
} | ||
|
||
input := strings.Split(line, util.ConvertControlChars(p.separator)) | ||
if len(input) < 2 { | ||
return Message{}, fmt.Errorf("the provided input does not contain the separator %s", p.separator) | ||
} else if len(input) == 3 && p.columnCount == defaultColumnCount { | ||
// lazy resolving of column indices | ||
var err error | ||
p.keyColumnIdx, p.valueColumnIdx, p.columnCount, err = resolveColumns(input) | ||
if err != nil { | ||
return Message{}, err | ||
} | ||
} else if len(input) != p.columnCount { | ||
return Message{}, fmt.Errorf("line contains unexpected amount of separators:\n%s", line) | ||
} | ||
|
||
return Message{input[p.keyColumnIdx], input[p.valueColumnIdx]}, nil | ||
} | ||
|
||
func resolveColumns(line []string) (keyColumnIdx, valueColumnIdx, columnCount int, err error) { | ||
if isTimestamp(line[0]) { | ||
output.Warnf("assuming column 0 to be message timestamp. Column will be ignored") | ||
return 1, 2, 3, nil | ||
} else if isTimestamp(line[1]) { | ||
output.Warnf("assuming column 1 to be message timestamp. Column will be ignored") | ||
return 0, 2, 3, nil | ||
} | ||
return -1, -1, -1, errors.Errorf("line contains unexpected amount of separators:\n%s", line) | ||
} | ||
|
||
func isTimestamp(value string) bool { | ||
_, e := time.Parse(time.RFC3339, value) | ||
return e == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package input | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type jsonParser struct { | ||
} | ||
|
||
func NewJSONParser() Parser { | ||
return &jsonParser{} | ||
} | ||
|
||
func (p *jsonParser) ParseLine(line string) (Message, error) { | ||
|
||
var message Message | ||
|
||
if err := json.Unmarshal([]byte(line), &message); err != nil { | ||
return message, fmt.Errorf("can't unmarshal line: %w", err) | ||
} | ||
|
||
return message, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package input | ||
|
||
type Message struct { | ||
Key string `json:"key"` | ||
Value string `json:"value"` | ||
} | ||
|
||
type Parser interface { | ||
ParseLine(line string) (Message, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2006-01-02T15:04:05.000Z,1,a | ||
2006-01-02T16:04:05.000Z,2,b | ||
2006-01-02T17:04:05.000Z,3,c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1,2006-01-02T15:04:05.000Z,a | ||
2,2006-01-02T16:04:05.000Z,b | ||
3,2006-01-02T17:04:05.000Z,c |
Oops, something went wrong.