-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
101 additions
and
56 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
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,44 @@ | ||
package output | ||
|
||
import "errors" | ||
|
||
// Format describes a printable data representation. | ||
type Format string | ||
|
||
const ( | ||
FormatJSON = "json" | ||
FormatCSV = "csv" | ||
FormatTab = "tab" | ||
FormatText = "text" | ||
) | ||
|
||
func (f *Format) Validate() error { | ||
switch *f { | ||
case FormatJSON, FormatTab, FormatCSV, FormatText: | ||
return nil | ||
default: | ||
return errors.New("output format is expected to be 'json', 'tab', 'text' or 'csv'") | ||
} | ||
} | ||
|
||
func formatsFor(data any) []Format { | ||
var formats []Format | ||
switch data.(type) { | ||
case Serializable, SerializableIterator: | ||
formats = append(formats, FormatJSON) | ||
case Table, TableIterator: | ||
formats = append(formats, FormatTab, FormatCSV) | ||
case Text: | ||
formats = append(formats, FormatText) | ||
} | ||
return formats | ||
} | ||
|
||
func supportsFormat(data any, f Format) bool { | ||
for _, format := range formatsFor(data) { | ||
if format == f { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,42 @@ | ||
package output | ||
|
||
// A Table represents a tabular data that can also be printed as CSV. | ||
// Suitable for small tables that fit into memory. | ||
type Table interface { | ||
Header() []string | ||
Rows() []TableRow | ||
} | ||
|
||
// A TableIterator that can efficiently be printed as large table or CSV. | ||
// Suitable for larger tables that do not fit into memory. | ||
type TableIterator interface { | ||
// Header returns the header of the table | ||
Header() []any | ||
// NextRow returns a new TableRow until the error is io.EOF | ||
NextRow() (TableRow, error) | ||
} | ||
|
||
// A TableRow represents all data that belongs to a table row. | ||
type TableRow interface { | ||
// Cells returns all table cells for this row. This is used to | ||
// print tabular formats such csv. The returned slice has the same | ||
// length as the header slice returned by the parent TableIterator. | ||
Cells() []any | ||
} | ||
|
||
// Serializable represents data that can be converted to JSON or YAML. | ||
type Serializable interface { | ||
// SerializableData returns arbitrary data that can be converted to formats like JSON or YAML. | ||
SerializableData() any | ||
} | ||
|
||
// SerializableIterator represents a stream of data that can be converted to JSON or YAML. | ||
type SerializableIterator interface { | ||
NextSerializable() (any, error) | ||
} | ||
|
||
// Text represents a multi line text that can be printed but is not a table or another | ||
// structured format such as JSON or YAML. | ||
type Text interface { | ||
Text() (string, 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