Skip to content

Commit

Permalink
For #615 - format jobs, too
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars T Hansen committed Nov 12, 2024
1 parent 639d0d3 commit b8718ec
Show file tree
Hide file tree
Showing 16 changed files with 540 additions and 395 deletions.
3 changes: 1 addition & 2 deletions code/sonalyze/clusters/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (cc *ClusterCommand) Clusters(_ io.Reader, stdout, stderr io.Writer) error
clustersFormatters,
cc.PrintOpts,
printable,
ComputePrintMods(cc.PrintOpts),
)

return nil
Expand Down Expand Up @@ -142,7 +141,7 @@ var clustersAliases = map[string][]string{
type SFS = SimpleFormatSpec

// MT: Constant after initialization; immutable
var clustersFormatters map[string]Formatter[any, PrintMods] = ReflectFormattersFromMap(
var clustersFormatters map[string]Formatter = ReflectFormattersFromMap(
// TODO: Go 1.22, reflect.TypeFor[db.ClusterEntry]
reflect.TypeOf((*db.ClusterEntry)(nil)).Elem(),
map[string]any{
Expand Down
4 changes: 2 additions & 2 deletions code/sonalyze/command/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,10 @@ func (fa *FormatArgs) ReifyForRemote(x *Reifier) error {
return nil
}

func ValidateFormatArgs[Data, Ctx any](
func ValidateFormatArgs(
fa *FormatArgs,
defaultFields string,
formatters map[string]Formatter[Data, Ctx],
formatters map[string]Formatter,
aliases map[string][]string,
def DefaultFormat,
) error {
Expand Down
68 changes: 59 additions & 9 deletions code/sonalyze/command/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,55 @@ import (
umaps "go-utils/maps"
)

////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Print modifiers.
//
// See comment block below.

type PrintMods = int

const (
// These apply per-field according to modifiers
// TODO: These are currently unimplemented.
PrintModSec = (1 << iota) // timestamps are printed as seconds since epoch
PrintModIso // timestamps are printed as Iso timestamps

// These are for the output format and are applied to all fields
PrintModFixed // fixed format
PrintModJson // JSON format
PrintModCsv // CSV format
PrintModCsvNamed // CSVNamed format
PrintModAwk // AWK format
PrintModNoDefaults // Set (for any format) if option is set
)

func ComputePrintMods(opts *FormatOptions) PrintMods {
var x PrintMods
switch {
case opts.Csv:
if opts.Named {
x = PrintModCsvNamed
} else {
x = PrintModCsv
}
case opts.Json:
x = PrintModJson
case opts.Awk:
x = PrintModAwk
case opts.Fixed:
x = PrintModFixed
}
if opts.NoDefaults && (opts.Csv || opts.Json) {
x |= PrintModNoDefaults
}
return x
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Formatting specs

type FormatOptions struct {
Tag string // if not ""
Json bool // json explicitly requested
Expand Down Expand Up @@ -126,14 +175,15 @@ func StandardFormatOptions(others map[string]bool, def DefaultFormat) *FormatOpt

// FormatData defaults to fixed formatting.

func FormatData[Datum, Ctx any](
func FormatData(
out io.Writer,
fields []string,
formatters map[string]Formatter[Datum, Ctx],
formatters map[string]Formatter,
opts *FormatOptions,
data []Datum,
ctx Ctx,
data []any,
) {
ctx := ComputePrintMods(opts)

// TODO: OPTIMIZEME: Instead of creating this huge matrix up-front and serializing field
// formatting and output formatting, it might be better to set up some kind of
// generator-formatter pipeline. Allocation volume would be the same but we'd lower peak memory
Expand All @@ -147,7 +197,7 @@ func FormatData[Datum, Ctx any](
}

// Produce the formatted field values for all fields.
fmt := make([]func(Datum, Ctx) string, len(fields))
fmt := make([]func(any, PrintMods) string, len(fields))
for c, kwd := range fields {
fmt[c] = formatters[kwd].Fmt
}
Expand Down Expand Up @@ -442,15 +492,15 @@ type FormatHelp struct {
Defaults string
}

type Formatter[Data, Ctx any] struct {
Fmt func(data Data, ctx Ctx) string
type Formatter struct {
Fmt func(data any, ctx PrintMods) string
Help string
}

func StandardFormatHelp[Data, Ctx any](
func StandardFormatHelp(
fmt string,
helpText string,
formatters map[string]Formatter[Data, Ctx],
formatters map[string]Formatter,
aliases map[string][]string,
defaultFields string,
) *FormatHelp {
Expand Down
Loading

0 comments on commit b8718ec

Please sign in to comment.