2020-08-06 20:58:47 +03:00
|
|
|
// Package xmlw implements output writers for XML.
|
|
|
|
package xmlw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/output"
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlz"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// recordWriter implements output.RecordWriter.
|
|
|
|
type recordWriter struct {
|
|
|
|
out io.Writer
|
|
|
|
fm *output.Formatting
|
|
|
|
|
|
|
|
recMeta sqlz.RecordMeta
|
|
|
|
|
|
|
|
// outBuf holds output prior to flushing.
|
|
|
|
outBuf *bytes.Buffer
|
|
|
|
|
|
|
|
// recWritten indicates that at least one record has been written
|
|
|
|
// to outBuf.
|
|
|
|
recsWritten bool
|
|
|
|
|
|
|
|
elemColor *color.Color
|
|
|
|
|
|
|
|
tplRecStart string
|
|
|
|
tplRecEnd string
|
|
|
|
tplFieldStart []string
|
|
|
|
tplFieldEnd []string
|
|
|
|
|
2022-12-17 02:34:33 +03:00
|
|
|
fieldPrintFns []func(w io.Writer, a ...any)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
decl = "<?xml version=\"1.0\"?>"
|
|
|
|
recsElementName = "records"
|
|
|
|
recElemName = "record"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewRecordWriter returns an output.RecordWriter instance for XML.
|
|
|
|
func NewRecordWriter(out io.Writer, fm *output.Formatting) output.RecordWriter {
|
|
|
|
return &recordWriter{out: out, fm: fm, elemColor: fm.Key.Add(color.Faint)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open implements output.RecordWriter.
|
|
|
|
func (w *recordWriter) Open(recMeta sqlz.RecordMeta) error {
|
|
|
|
w.recMeta = recMeta
|
|
|
|
|
|
|
|
var indent, newline string
|
|
|
|
if w.fm.Pretty {
|
|
|
|
indent = w.fm.Indent
|
|
|
|
newline = "\n"
|
|
|
|
}
|
|
|
|
|
2022-12-17 02:34:33 +03:00
|
|
|
w.fieldPrintFns = make([]func(w io.Writer, a ...any), len(recMeta))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
w.tplFieldStart = make([]string, len(recMeta))
|
|
|
|
w.tplFieldEnd = make([]string, len(recMeta))
|
|
|
|
|
|
|
|
w.tplRecStart = "\n" + indent + w.elemColor.Sprintf("<%s>", recElemName)
|
|
|
|
w.tplRecEnd = newline + indent + w.elemColor.Sprintf("</%s>", recElemName)
|
|
|
|
|
|
|
|
for i, name := range recMeta.Names() {
|
|
|
|
elementName := stringz.SanitizeAlphaNumeric(name, '_')
|
|
|
|
w.tplFieldStart[i] = newline + indent + indent + w.elemColor.Sprintf("<%s>", elementName)
|
|
|
|
w.tplFieldEnd[i] = w.elemColor.Sprintf("</%s>", elementName)
|
|
|
|
|
|
|
|
if w.fm.IsMonochrome() {
|
|
|
|
w.fieldPrintFns[i] = monoPrint
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-18 03:51:33 +03:00
|
|
|
switch w.recMeta[i].Kind() { //nolint:exhaustive // ignore kind.Unknown and kind.Null
|
2020-08-06 20:58:47 +03:00
|
|
|
default:
|
|
|
|
w.fieldPrintFns[i] = monoPrint
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Datetime, kind.Date, kind.Time:
|
2020-08-06 20:58:47 +03:00
|
|
|
w.fieldPrintFns[i] = w.fm.Datetime.FprintFunc()
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Int, kind.Decimal, kind.Float:
|
2020-08-06 20:58:47 +03:00
|
|
|
w.fieldPrintFns[i] = w.fm.Number.FprintFunc()
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Bool:
|
2020-08-06 20:58:47 +03:00
|
|
|
w.fieldPrintFns[i] = w.fm.Bool.FprintFunc()
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Bytes:
|
2020-08-06 20:58:47 +03:00
|
|
|
w.fieldPrintFns[i] = w.fm.Bytes.FprintFunc()
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Text:
|
2020-08-06 20:58:47 +03:00
|
|
|
w.fieldPrintFns[i] = w.fm.String.FprintFunc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.outBuf = &bytes.Buffer{}
|
|
|
|
w.outBuf.WriteString(w.fm.Faint.Sprint(decl))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// monoPrint delegates to fmt.Fprint, for
|
|
|
|
// monochrome (non-color) printing.
|
2022-12-17 02:34:33 +03:00
|
|
|
func monoPrint(w io.Writer, a ...any) {
|
2020-08-06 20:58:47 +03:00
|
|
|
_, _ = fmt.Fprint(w, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush implements output.RecordWriter.
|
|
|
|
func (w *recordWriter) Flush() error {
|
|
|
|
_, err := w.outBuf.WriteTo(w.out) // resets buf
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements output.RecordWriter.
|
|
|
|
func (w *recordWriter) Close() error {
|
|
|
|
w.outBuf.WriteByte('\n')
|
|
|
|
|
|
|
|
if w.recsWritten {
|
|
|
|
w.outBuf.WriteString(w.elemColor.Sprintf("</%s>", recsElementName))
|
|
|
|
} else {
|
|
|
|
// empty element: <records />
|
|
|
|
w.outBuf.WriteString(w.elemColor.Sprintf("<%s />", recsElementName))
|
|
|
|
}
|
|
|
|
|
|
|
|
w.outBuf.WriteByte('\n')
|
|
|
|
|
|
|
|
return w.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteRecords implements output.RecordWriter.
|
|
|
|
// Note that (by design) the XML element is omitted for any nil value
|
|
|
|
// in a record.
|
|
|
|
func (w *recordWriter) WriteRecords(recs []sqlz.Record) error {
|
|
|
|
if len(recs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !w.recsWritten {
|
|
|
|
w.outBuf.WriteByte('\n')
|
|
|
|
w.outBuf.WriteString(w.elemColor.Sprintf("<%s>", recsElementName))
|
|
|
|
w.recsWritten = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, rec := range recs {
|
|
|
|
err = w.writeRecord(rec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *recordWriter) writeRecord(rec sqlz.Record) error {
|
|
|
|
var err error
|
|
|
|
tmpBuf := &bytes.Buffer{}
|
|
|
|
|
|
|
|
w.outBuf.WriteString(w.tplRecStart)
|
|
|
|
|
|
|
|
for i, val := range rec {
|
|
|
|
if val == nil {
|
|
|
|
continue // omit the element if val is nil
|
|
|
|
}
|
|
|
|
|
|
|
|
w.outBuf.WriteString(w.tplFieldStart[i])
|
|
|
|
|
|
|
|
switch val := val.(type) {
|
|
|
|
default:
|
|
|
|
// should never happen
|
|
|
|
err = xml.EscapeText(tmpBuf, []byte(fmt.Sprintf("%v", val)))
|
|
|
|
if err != nil {
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
w.fieldPrintFns[i](w.outBuf, tmpBuf.String())
|
|
|
|
tmpBuf.Reset()
|
|
|
|
case *string:
|
|
|
|
err = xml.EscapeText(tmpBuf, []byte(*val))
|
|
|
|
if err != nil {
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
w.fieldPrintFns[i](w.outBuf, tmpBuf.String())
|
|
|
|
tmpBuf.Reset()
|
|
|
|
case *[]byte:
|
|
|
|
w.fieldPrintFns[i](w.outBuf, base64.StdEncoding.EncodeToString(*val))
|
|
|
|
case *bool:
|
|
|
|
w.fieldPrintFns[i](w.outBuf, strconv.FormatBool(*val))
|
|
|
|
case *int64:
|
|
|
|
w.fieldPrintFns[i](w.outBuf, strconv.FormatInt(*val, 10))
|
|
|
|
case *float64:
|
|
|
|
w.fieldPrintFns[i](w.outBuf, stringz.FormatFloat(*val))
|
|
|
|
case *time.Time:
|
|
|
|
switch w.recMeta[i].Kind() {
|
|
|
|
default:
|
|
|
|
w.fieldPrintFns[i](w.outBuf, val.Format(stringz.DatetimeFormat))
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Time:
|
2020-08-06 20:58:47 +03:00
|
|
|
w.fieldPrintFns[i](w.outBuf, val.Format(stringz.TimeFormat))
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Date:
|
2020-08-06 20:58:47 +03:00
|
|
|
w.fieldPrintFns[i](w.outBuf, val.Format(stringz.DateFormat))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.outBuf.WriteString(w.tplFieldEnd[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
w.outBuf.WriteString(w.tplRecEnd)
|
|
|
|
|
|
|
|
if w.outBuf.Len() > output.FlushThreshold {
|
|
|
|
return w.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|