sq/cli/diff/render.go
Neil O'Toole c810d17eec
#229: sq diff ready for beta release (#239)
* sq config edit: fixed glaring bug that prevented editing a source

* Refine sq diff
2023-05-25 21:58:43 -06:00

120 lines
3.7 KiB
Go

package diff
import (
"github.com/neilotoole/sq/libsq/core/ioz"
"github.com/neilotoole/sq/libsq/source"
)
// renderSourceMeta2YAML returns a YAML rendering of source.Metadata.
// The returned YAML is subtly different from that
// returned by yamlw.NewSourceWriter. For example, it
// adds a "table_count" field.
func renderSourceMeta2YAML(sm *source.Metadata) (string, error) {
if sm == nil {
return "", nil
}
// sourceMeta holds values of source.Metadata in the structure
// that diff wants.
type sourceMeta struct {
Handle string `json:"handle" yaml:"handle"`
Location string `json:"location" yaml:"location"`
Name string `json:"name" yaml:"name"`
FQName string `json:"name_fq" yaml:"name_fq"`
Schema string `json:"schema,omitempty" yaml:"schema,omitempty"`
Driver source.DriverType `json:"driver" yaml:"driver"`
DBDriver source.DriverType `json:"db_driver" yaml:"db_driver"`
DBProduct string `json:"db_product" yaml:"db_product"`
DBVersion string `json:"db_version" yaml:"db_version"`
User string `json:"user,omitempty" yaml:"user,omitempty"`
Size int64 `json:"size" yaml:"size"`
TableCount int64 `json:"table_count" yaml:"table_count"`
}
smr := &sourceMeta{
Handle: sm.Handle,
Location: source.RedactLocation(sm.Location),
Name: sm.Name,
FQName: sm.FQName,
Schema: sm.Schema,
Driver: sm.Driver,
DBDriver: sm.DBDriver,
DBProduct: sm.DBProduct,
DBVersion: sm.DBVersion,
User: sm.User,
Size: sm.Size,
TableCount: sm.TableCount,
}
b, err := ioz.MarshalYAML(smr)
if err != nil {
return "", err
}
return string(b), nil
}
// renderTableMeta2YAML returns a YAML rendering of source.TableMetadata.
// The returned YAML is subtly different from that
// returned by yamlw.NewSourceWriter. For example, it
// adds a "column_count" field.
func renderTableMeta2YAML(showRowCounts bool, tm *source.TableMetadata) (string, error) {
if tm == nil {
return "", nil
}
// tableMeta hosts values of source.TableMetadata in the
// structure that diff wants.
type tableMeta struct {
Name string `json:"name" yaml:"name"`
FQName string `json:"name_fq,omitempty" yaml:"name_fq,omitempty"`
TableType string `json:"table_type,omitempty" yaml:"table_type,omitempty"`
DBTableType string `json:"table_type_db,omitempty" yaml:"table_type_db,omitempty"`
// RowCount is a pointer, because its display is controlled
// by a variable.
RowCount *int64 `json:"row_count,omitempty" yaml:"row_count,omitempty"`
Size *int64 `json:"size,omitempty" yaml:"size,omitempty"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
ColumnCount int64 `json:"column_count" yaml:"column_count"`
Columns []*source.ColMetadata `json:"columns" yaml:"columns"`
}
tmr := &tableMeta{
Name: tm.Name,
FQName: tm.FQName,
TableType: tm.TableType,
DBTableType: tm.DBTableType,
// TODO: Printing of Size should be controlled by a param,
// e.g. "show-volatile-fields". Until then, we omit it.
Size: nil,
Comment: tm.Comment,
ColumnCount: int64(len(tm.Columns)),
Columns: tm.Columns,
}
if showRowCounts {
tmr.RowCount = &tm.RowCount
}
b, err := ioz.MarshalYAML(tmr)
if err != nil {
return "", err
}
return string(b), nil
}
// renderDBProperties2YAML returns a YAML rendering of db properties.
func renderDBProperties2YAML(props map[string]any) (string, error) {
if len(props) == 0 {
return "", nil
}
b, err := ioz.MarshalYAML(props)
if err != nil {
return "", err
}
return string(b), nil
}