Skip to content

Commit

Permalink
Fixed more test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
neilotoole committed Apr 25, 2023
1 parent edc7ec4 commit 3534eb1
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 20 deletions.
1 change: 1 addition & 0 deletions cli/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ More examples:
cmd.Flags().Bool(flag.SkipVerify, false, flag.SkipVerifyUsage)
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
cmd.Flags().BoolP(flag.AddActive, flag.AddActiveShort, false, flag.AddActiveUsage)

return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion cli/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const (

// SrcOptions is deprecated.
//
// Deprecated FIXME: Use specific options like flag.CSVImportHeader.
// //Deprecated: Use specific options like flag.CSVImportHeader.
SrcOptions = "opts"
SrcOptionsUsage = "Driver-dependent data source options"

Expand Down
24 changes: 20 additions & 4 deletions cli/writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ import (
)

var (
OptPrintHeader = options.NewBool("format.header", true, "Controls whether a header row is printed.")
OptOutputFormat = NewFormatOpt("format", format.Table, "Specify the output format.")
OptPrintHeader = options.NewBool(
"format.header",
true,
"Controls whether a header row is printed.",
"format",
)
OptOutputFormat = NewFormatOpt(
"format",
format.Table,
"Specify the output format.",
"format",
)
)

// writers is a container for the various output writers.
Expand Down Expand Up @@ -238,8 +248,8 @@ func getFormat(cmd *cobra.Command, defaults options.Options) format.Format {
var _ options.Opt = FormatOpt{}

// NewFormatOpt returns an options.FormatOpt instance.
func NewFormatOpt(key string, defaultVal format.Format, comment string) FormatOpt {
opt := FormatOpt{key: key, defaultVal: defaultVal, comment: comment}
func NewFormatOpt(key string, defaultVal format.Format, comment string, tags ...string) FormatOpt {
opt := FormatOpt{key: key, defaultVal: defaultVal, comment: comment, tags: tags}
options.DefaultRegistry.Add(opt)
return opt
}
Expand All @@ -249,6 +259,12 @@ type FormatOpt struct {
key string
comment string
defaultVal format.Format
tags []string
}

// Tags implements options.Opt.
func (o FormatOpt) Tags() []string {
return o.tags
}

// Key implements options.Opt.
Expand Down
15 changes: 13 additions & 2 deletions drivers/csv/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,29 @@ import (
// OptImportHeader allows the user to specify whether the imported CSV
// file has a header or not. If not set, the importer will try to
// detect if the input has a header.
var OptImportHeader = options.NewBool("driver.csv.header", false, "")
var OptImportHeader = options.NewBool(
"driver.csv.header",
false,
"",
"source", "csv",
)

// OptEmptyAsNull determines if an empty CSV field is treated as NULL
// or as the zero value for the kind of that field.
var OptEmptyAsNull = options.NewBool("driver.csv.empty-as-null", true, "")
var OptEmptyAsNull = options.NewBool(
"driver.csv.empty-as-null",
true,
"",
"source", "csv",
)

// OptDelim specifies the CSV delimiter to use.
var OptDelim = options.NewString(
"driver.csv.delim",
"comma",
`Possible values are: comma, space, pipe, tab, colon, semi, period.
Default is comma.`,
"sources", "csv",
)

// importCSV loads the src CSV data into scratchDB.
Expand Down
8 changes: 7 additions & 1 deletion drivers/xlsx/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ import (
// to go away, because it doesn't really work well with the fact that
// an XLSX workbook has multiple sheets, each of which could have a
// header row.
var OptImportHeader = options.NewBool("driver.xlsx.header", false, "")
var OptImportHeader = options.NewBool(
"driver.xlsx.header",
false,
"",
"source",
"xlsx",
)

// xlsxToScratch loads the data in xlFile into scratchDB.
func xlsxToScratch(ctx context.Context, src *source.Source, xlFile *xlsx.File, scratchDB driver.Database) error {
Expand Down
39 changes: 31 additions & 8 deletions libsq/core/options/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ type Opt interface {

// IsSet returns true if this Opt is set in opts.
IsSet(opts Options) bool

// Tags returns any tags on this Opt instance. For example, an Opt might
// have tags [source, csv].
Tags() []string
}

type baseOpt struct {
key string
comment string
tags []string
}

// Key implements options.Opt.
Expand All @@ -53,11 +58,20 @@ func (o baseOpt) String() string {
return o.key
}

// Tags implements options.Opt.
func (o baseOpt) Tags() []string {
return o.tags
}

var _ Opt = String{}

// NewString returns an options.String instance.
func NewString(key, defaultVal, comment string) String {
opt := String{baseOpt: baseOpt{key: key, comment: comment}, defaultVal: defaultVal}
func NewString(key, defaultVal, comment string, tags ...string) String {
opt := String{
baseOpt: baseOpt{key: key, comment: comment, tags: tags},
defaultVal: defaultVal,
}

DefaultRegistry.Add(opt)
return opt
}
Expand Down Expand Up @@ -92,8 +106,11 @@ func (o String) Get(opts Options) string {
var _ Opt = Int{}

// NewInt returns an options.Int instance.
func NewInt(key string, defaultVal int, comment string) Int {
opt := Int{baseOpt: baseOpt{key: key, comment: comment}, defaultVal: defaultVal}
func NewInt(key string, defaultVal int, comment string, tags ...string) Int {
opt := Int{
baseOpt: baseOpt{key: key, comment: comment, tags: tags},
defaultVal: defaultVal,
}
DefaultRegistry.Add(opt)
return opt
}
Expand Down Expand Up @@ -203,8 +220,11 @@ func (o Int) Process(opts Options) (Options, error) {
var _ Opt = Bool{}

// NewBool returns an options.Bool instance.
func NewBool(key string, defaultVal bool, comment string) Bool {
opt := Bool{baseOpt: baseOpt{key: key, comment: comment}, defaultVal: defaultVal}
func NewBool(key string, defaultVal bool, comment string, tags ...string) Bool {
opt := Bool{
baseOpt: baseOpt{key: key, comment: comment, tags: tags},
defaultVal: defaultVal,
}
DefaultRegistry.Add(opt)
return opt
}
Expand Down Expand Up @@ -289,8 +309,11 @@ func (o Bool) Process(opts Options) (Options, error) {
var _ Opt = Duration{}

// NewDuration returns an options.Duration instance.
func NewDuration(key string, defaultVal time.Duration, comment string) Duration {
opt := Duration{baseOpt: baseOpt{key: key, comment: comment}, defaultVal: defaultVal}
func NewDuration(key string, defaultVal time.Duration, comment string, tags ...string) Duration {
opt := Duration{
baseOpt: baseOpt{key: key, comment: comment, tags: tags},
defaultVal: defaultVal,
}
DefaultRegistry.Add(opt)
return opt
}
Expand Down
28 changes: 24 additions & 4 deletions libsq/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,30 @@ func (c *SQLConfig) Apply(db *sql.DB) {
var (
// TODO: merge these options with SQLConfig.

OptConnMaxOpen = options.NewInt("conn.max-open", 0, "")
OptConnMaxIdle = options.NewInt("conn.max-idle", 0, "")
OptConnMaxIdleTime = options.NewDuration("conn.max-idle-time", 0, "")
OptConnMaxLifetime = options.NewDuration("conn.max-lifetime", 0, "")
OptConnMaxOpen = options.NewInt(
"conn.max-open",
0,
"",
"source", "sql",
)
OptConnMaxIdle = options.NewInt(
"conn.max-idle",
0,
"",
"source", "sql",
)
OptConnMaxIdleTime = options.NewDuration(
"conn.max-idle-time",
0,
"",
"source", "sql",
)
OptConnMaxLifetime = options.NewDuration(
"conn.max-lifetime",
0,
"",
"source", "sql",
)
)

// Provider is a factory that returns Driver instances.
Expand Down

0 comments on commit 3534eb1

Please sign in to comment.