Skip to content

Commit

Permalink
type cast values
Browse files Browse the repository at this point in the history
  • Loading branch information
dudo committed Dec 5, 2023
1 parent fdf3838 commit 55644ec
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,44 @@ func NewConfig() *Config {

// Satisfies the TableRecordHandler interface.
type tablePrinter struct {
columnNames []string // A slice of strings to hold column names
headerValues []string // A slice of strings to hold column names
}

func (t *tablePrinter) HandleInit(ctx context.Context, metadata types.TableMetadata) error {
// Store column names in order
for _, col := range metadata.ColInfo {
t.columnNames = append(t.columnNames, col.Name)
t.headerValues = append(t.headerValues, col.Name)
}
return nil
}

func (t *tablePrinter) HandleRecord(ctx context.Context, r *types.Record) error {
if len(r.Data) != len(t.headerValues) {
return fmt.Errorf("%w: mismatch in header and data sizes", errdefs.ErrInvalidArgument)
}

recordMap := make(map[string]interface{})

for i, d := range r.Data {
recordMap[t.columnNames[i]] = d.String()
var value interface{}

switch v := d.(type) {
case *types.BooleanValue:
value = v.Value()
case *types.Int64Value:
value = v.Value()
case types.Float64Value:
value = v.Value()
case *types.Time64NSValue:
value = v.Value()
case *types.UInt128Value:
value = v.Value()
default:
// Fallback to string representation if type is unknown
value = d.String()
}

recordMap[t.headerValues[i]] = value
}

jsonRecord, err := json.Marshal(recordMap)
Expand Down

0 comments on commit 55644ec

Please sign in to comment.