diff --git a/_example/slice/main.go b/_example/slice/main.go index 286d95c..c5e5bd7 100644 --- a/_example/slice/main.go +++ b/_example/slice/main.go @@ -8,7 +8,7 @@ import ( "github.com/noborus/trdsql" ) -func sliceQuery(slice interface{}, tableName string, query string) ([][]interface{}, error) { +func sliceQuery(slice any, tableName string, query string) ([][]any, error) { // trdsql.EnableDebug() importer := trdsql.NewSliceImporter(tableName, slice) writer := trdsql.NewSliceWriter() diff --git a/_example/writer/main.go b/_example/writer/main.go index 9b0b8ba..3cebdd4 100644 --- a/_example/writer/main.go +++ b/_example/writer/main.go @@ -23,7 +23,7 @@ func (w *Write) PreWrite(columns []string, types []string) error { return nil } -func (w *Write) WriteRow(values []interface{}, columns []string) error { +func (w *Write) WriteRow(values []any, columns []string) error { for i, v := range values { if i > 0 { if _, err := w.writer.Write([]byte(",")); err != nil { diff --git a/database.go b/database.go index 39bdd02..a9deeb6 100644 --- a/database.go +++ b/database.go @@ -100,7 +100,7 @@ func (db *DB) queryCreateTable(tableName string, columnNames []string, columnTyp type importTable struct { tableName string columns []string - row []interface{} + row []any maxCap int lastCount int count int @@ -125,7 +125,7 @@ func (db *DB) ImportContext(ctx context.Context, tableName string, columnNames [ columns[i] = db.QuotedName(columnNames[i]) } - row := make([]interface{}, len(columnNames)) + row := make([]any, len(columnNames)) table := &importTable{ tableName: tableName, columns: columns, @@ -209,7 +209,7 @@ func (db *DB) insertImport(ctx context.Context, table *importTable, reader Reade } else { table.maxCap = (db.maxBulk / len(table.row)) * len(table.row) } - bulk := make([]interface{}, 0, table.maxCap) + bulk := make([]any, 0, table.maxCap) preRows := reader.PreReadRow() preRowNum := len(preRows) @@ -262,7 +262,7 @@ func (db *DB) stmtClose(stmt *sql.Stmt) { } } -func bulkPush(ctx context.Context, table *importTable, input Reader, bulk []interface{}) ([]interface{}, error) { +func bulkPush(ctx context.Context, table *importTable, input Reader, bulk []any) ([]any, error) { for (table.count * len(table.row)) < table.maxCap { row, err := input.ReadRow(table.row) if err != nil { diff --git a/debug.go b/debug.go index e1b1f52..ed85056 100644 --- a/debug.go +++ b/debug.go @@ -13,7 +13,7 @@ func EnableDebug() { debug = true } -func (d debugT) Printf(format string, args ...interface{}) { +func (d debugT) Printf(format string, args ...any) { if d { log.Printf(format, args...) } diff --git a/exporter.go b/exporter.go index 2b6777f..21e3e67 100644 --- a/exporter.go +++ b/exporter.go @@ -107,8 +107,8 @@ func (e *WriteFormat) exportContext(ctx context.Context, db *DB, query string) e } func (e *WriteFormat) write(ctx context.Context, rows *sql.Rows) error { - values := make([]interface{}, len(e.columns)) - scanArgs := make([]interface{}, len(e.columns)) + values := make([]any, len(e.columns)) + scanArgs := make([]any, len(e.columns)) for i := range values { scanArgs[i] = &values[i] } diff --git a/go.mod b/go.mod index 6af01db..46dce68 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/klauspost/compress v1.17.4 github.com/lib/pq v1.10.9 github.com/mattn/go-runewidth v0.0.15 - github.com/mattn/go-sqlite3 v1.14.18 + github.com/mattn/go-sqlite3 v1.14.19 github.com/multiprocessio/go-sqlite3-stdlib v0.0.0-20220822170115-9f6825a1cd25 github.com/noborus/guesswidth v0.3.4 github.com/noborus/sqlss v0.1.0 @@ -18,7 +18,7 @@ require ( github.com/olekukonko/tablewriter v0.0.5 github.com/pierrec/lz4 v2.6.1+incompatible github.com/ulikunitz/xz v0.5.11 - golang.org/x/term v0.15.0 + golang.org/x/term v0.16.0 modernc.org/sqlite v1.28.0 ) @@ -35,16 +35,16 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rivo/uniseg v0.4.4 // indirect - golang.org/x/crypto v0.17.0 // indirect + golang.org/x/crypto v0.18.0 // indirect golang.org/x/mod v0.14.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/sys v0.16.0 // indirect golang.org/x/tools v0.16.1 // indirect golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect gonum.org/v1/gonum v0.14.0 // indirect lukechampine.com/uint128 v1.3.0 // indirect modernc.org/cc/v3 v3.41.0 // indirect modernc.org/ccgo/v3 v3.16.15 // indirect - modernc.org/libc v1.37.5 // indirect + modernc.org/libc v1.40.1 // indirect modernc.org/mathutil v1.6.0 // indirect modernc.org/memory v1.7.2 // indirect modernc.org/opt v0.1.3 // indirect diff --git a/go.sum b/go.sum index 7c7e0ea..69c7116 100644 --- a/go.sum +++ b/go.sum @@ -57,8 +57,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI= -github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= +github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/multiprocessio/go-sqlite3-stdlib v0.0.0-20220822170115-9f6825a1cd25 h1:bnhGk2UFFPqylhxTEffs1ehDRn4bEZsEoDH53Z4HqA8= github.com/multiprocessio/go-sqlite3-stdlib v0.0.0-20220822170115-9f6825a1cd25/go.mod h1:RrGEZqqiyEcLyTVLDSgtNZVLqJykj0F4vwuuqvMdT60= github.com/noborus/guesswidth v0.3.4 h1:+iKmbm0iFTS3pksIOKQQvLVZVOKNZHavqJoFK2mPoTQ= @@ -87,8 +87,8 @@ github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4A github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8= github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= @@ -101,13 +101,13 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= -golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= +golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= +golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= @@ -127,8 +127,8 @@ modernc.org/ccgo/v3 v3.16.15 h1:KbDR3ZAVU+wiLyMESPtbtE/Add4elztFyfsWoNTgxS0= modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI= modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk= modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM= -modernc.org/libc v1.37.5 h1:IDpTjKpyMYFenZ7DkOr6Jswhn79yxsXeWHi/4DaFDBA= -modernc.org/libc v1.37.5/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE= +modernc.org/libc v1.40.1 h1:ZhRylEBcj3GyQbPVC8JxIg7SdrT4JOxIDJoUon0NfF8= +modernc.org/libc v1.40.1/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE= modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E= diff --git a/importer_slice.go b/importer_slice.go index 55e181c..304a8b9 100644 --- a/importer_slice.go +++ b/importer_slice.go @@ -11,7 +11,7 @@ type SliceImporter struct { } // NewSliceImporter returns trdsql SliceImporter. -func NewSliceImporter(tableName string, data interface{}) *SliceImporter { +func NewSliceImporter(tableName string, data any) *SliceImporter { return &SliceImporter{ SliceReader: NewSliceReader(tableName, data), } diff --git a/importer_slice_test.go b/importer_slice_test.go index c08c56d..3aa3899 100644 --- a/importer_slice_test.go +++ b/importer_slice_test.go @@ -8,7 +8,7 @@ import ( func TestNewSliceImporter(t *testing.T) { type args struct { tableName string - data interface{} + data any } tests := []struct { name string @@ -19,7 +19,7 @@ func TestNewSliceImporter(t *testing.T) { name: "test1", args: args{ tableName: "test", - data: [][]interface{}{ + data: [][]any{ {1, "one"}, {2, "two"}, {3, "three"}, @@ -40,7 +40,7 @@ func TestNewSliceImporter(t *testing.T) { func TestSliceImporter_Import(t *testing.T) { type fields struct { tableName string - data interface{} + data any } tests := []struct { name string @@ -53,7 +53,7 @@ func TestSliceImporter_Import(t *testing.T) { name: "testErr", fields: fields{ tableName: "", - data: [][]interface{}{ + data: [][]any{ {1, "one"}, {2, "two"}, {3, "three"}, @@ -67,7 +67,7 @@ func TestSliceImporter_Import(t *testing.T) { name: "test1", fields: fields{ tableName: "test", - data: [][]interface{}{ + data: [][]any{ {1, "one"}, {2, "two"}, {3, "three"}, diff --git a/input_csv.go b/input_csv.go index d339ff6..ec8a2f3 100644 --- a/input_csv.go +++ b/input_csv.go @@ -140,11 +140,11 @@ func (r *CSVReader) Types() ([]string, error) { } // PreReadRow is returns only columns that store preread rows. -func (r *CSVReader) PreReadRow() [][]interface{} { +func (r *CSVReader) PreReadRow() [][]any { rowNum := len(r.preRead) - rows := make([][]interface{}, rowNum) + rows := make([][]any, rowNum) for n := 0; n < rowNum; n++ { - rows[n] = make([]interface{}, len(r.names)) + rows[n] = make([]any, len(r.names)) for i, f := range r.preRead[n] { rows[n][i] = f if r.needNULL { @@ -156,7 +156,7 @@ func (r *CSVReader) PreReadRow() [][]interface{} { } // ReadRow is read the rest of the row. -func (r *CSVReader) ReadRow(row []interface{}) ([]interface{}, error) { +func (r *CSVReader) ReadRow(row []any) ([]any, error) { if r.limitRead { return nil, io.EOF } diff --git a/input_csv_test.go b/input_csv_test.go index 2fdb982..f705b5a 100644 --- a/input_csv_test.go +++ b/input_csv_test.go @@ -297,13 +297,13 @@ func TestCSVReader_PreReadRow(t *testing.T) { name string fileName string opts *ReadOpts - want [][]interface{} + want [][]any }{ { name: "testBlank", fileName: "testnull.csv", opts: NewReadOpts(), - want: [][]interface{}{ + want: [][]any{ {"1", ""}, }, }, @@ -314,7 +314,7 @@ func TestCSVReader_PreReadRow(t *testing.T) { InNeedNULL(true), InNULL(""), ), - want: [][]interface{}{ + want: [][]any{ {"1", nil}, }, }, @@ -325,7 +325,7 @@ func TestCSVReader_PreReadRow(t *testing.T) { InNeedNULL(true), InNULL("1"), ), - want: [][]interface{}{ + want: [][]any{ {nil, ""}, }, }, @@ -349,14 +349,14 @@ func TestCSVReader_PreReadRow(t *testing.T) { func TestCSVReader_ReadRow(t *testing.T) { type args struct { - row []interface{} + row []any } tests := []struct { name string fileName string opts *ReadOpts args args - want []interface{} + want []any wantErr bool }{ { @@ -364,11 +364,11 @@ func TestCSVReader_ReadRow(t *testing.T) { fileName: "test.csv", opts: NewReadOpts(), args: args{ - []interface{}{ + []any{ "", "", }, }, - want: []interface{}{ + want: []any{ "2", "Melon", }, wantErr: false, @@ -378,11 +378,11 @@ func TestCSVReader_ReadRow(t *testing.T) { fileName: "testnull2.csv", opts: NewReadOpts(), args: args{ - []interface{}{ + []any{ "", "", "", }, }, - want: []interface{}{ + want: []any{ "2", "", "g3", }, wantErr: false, @@ -395,11 +395,11 @@ func TestCSVReader_ReadRow(t *testing.T) { InNULL(""), ), args: args{ - []interface{}{ + []any{ "", "", "", }, }, - want: []interface{}{ + want: []any{ "2", nil, "g3", }, wantErr: false, @@ -409,11 +409,11 @@ func TestCSVReader_ReadRow(t *testing.T) { fileName: "test_indefinite2.csv", opts: NewReadOpts(), args: args{ - []interface{}{ + []any{ "", "", "", }, }, - want: []interface{}{ + want: []any{ "2", "Melon", nil, }, wantErr: false, diff --git a/input_gw.go b/input_gw.go index 64ede71..9929697 100644 --- a/input_gw.go +++ b/input_gw.go @@ -75,14 +75,14 @@ func (r *GWReader) Types() ([]string, error) { } // PreReadRow is returns only columns that store preread rows. -func (r *GWReader) PreReadRow() [][]interface{} { - rows := make([][]interface{}, r.preRead) +func (r *GWReader) PreReadRow() [][]any { + rows := make([][]any, r.preRead) for n := 0; n < r.preRead; n++ { record, err := r.reader.Read() if err != nil { return rows } - rows[n] = make([]interface{}, len(r.names)) + rows[n] = make([]any, len(r.names)) for i := 0; i < len(r.names); i++ { rows[n][i] = record[i] if r.needNULL { @@ -94,7 +94,7 @@ func (r *GWReader) PreReadRow() [][]interface{} { } // ReadRow is read the rest of the row. -func (r *GWReader) ReadRow(row []interface{}) ([]interface{}, error) { +func (r *GWReader) ReadRow(row []any) ([]any, error) { if r.limitRead { return nil, io.EOF } diff --git a/input_gw_test.go b/input_gw_test.go index e5d5fad..8e98769 100644 --- a/input_gw_test.go +++ b/input_gw_test.go @@ -66,14 +66,14 @@ func TestNewGWReader(t *testing.T) { func TestGWReader_ReadRow(t *testing.T) { type args struct { - row []interface{} + row []any } tests := []struct { name string fileName string opts *ReadOpts args args - want []interface{} + want []any wantErr bool }{ { @@ -81,11 +81,11 @@ func TestGWReader_ReadRow(t *testing.T) { fileName: "ps.txt", opts: NewReadOpts(), args: args{ - []interface{}{ + []any{ "", "", "", "", "", "", "", "", "", "", "", }, }, - want: []interface{}{ + want: []any{ "root", "1", "0.0", "0.0", "168720", "13812", "?", "Ss", "Mar11", "1:11", "/sbin/init splash", }, wantErr: false, @@ -97,11 +97,11 @@ func TestGWReader_ReadRow(t *testing.T) { InSkip(3), ), args: args{ - []interface{}{ + []any{ "", "", "", "", "", }, }, - want: []interface{}{ + want: []any{ "ii", "accountsservice", "22.07.5-2ubuntu1.3", "amd64", "query and manipulate user account information", }, wantErr: false, diff --git a/input_json.go b/input_json.go index a7fe1a4..8e6add6 100644 --- a/input_json.go +++ b/input_json.go @@ -23,7 +23,7 @@ type JSONReader struct { query *gojq.Query already map[string]bool inNULL string - preRead []map[string]interface{} + preRead []map[string]any names []string types []string limitRead bool @@ -36,7 +36,7 @@ func NewJSONReader(reader io.Reader, opts *ReadOpts) (*JSONReader, error) { r.reader = json.NewDecoder(reader) r.reader.UseNumber() r.already = make(map[string]bool) - var top interface{} + var top any if opts.InJQuery != "" { str := trimQuoteAll(opts.InJQuery) @@ -91,11 +91,11 @@ func (r *JSONReader) Types() ([]string, error) { } // readAhead parses the top level of the JSON and stores it in preRead. -func (r *JSONReader) readAhead(top interface{}) error { +func (r *JSONReader) readAhead(top any) error { switch m := top.(type) { - case []interface{}: + case []any: // [] - r.preRead = make([]map[string]interface{}, 0, len(m)) + r.preRead = make([]map[string]any, 0, len(m)) if r.reader.More() { pre, names, err := r.etcRow(m) if err != nil { @@ -136,9 +136,9 @@ func (r *JSONReader) appendNames(names []string) { } } -func (r *JSONReader) topLevel(top interface{}) (map[string]interface{}, []string, error) { +func (r *JSONReader) topLevel(top any) (map[string]any, []string, error) { switch obj := top.(type) { - case map[string]interface{}: + case map[string]any: return r.objectRow(obj) default: return r.etcRow(obj) @@ -147,10 +147,10 @@ func (r *JSONReader) topLevel(top interface{}) (map[string]interface{}, []string // PreReadRow is returns only columns that store preRead rows. // One json (not jsonl) returns all rows with preRead. -func (r *JSONReader) PreReadRow() [][]interface{} { - rows := make([][]interface{}, len(r.preRead)) +func (r *JSONReader) PreReadRow() [][]any { + rows := make([][]any, len(r.preRead)) for n, v := range r.preRead { - rows[n] = make([]interface{}, len(r.names)) + rows[n] = make([]any, len(r.names)) for i := range r.names { rows[n][i] = v[r.names[i]] if r.needNULL { @@ -163,12 +163,12 @@ func (r *JSONReader) PreReadRow() [][]interface{} { // ReadRow is read the rest of the row. // Only jsonl requires ReadRow in json. -func (r *JSONReader) ReadRow(row []interface{}) ([]interface{}, error) { +func (r *JSONReader) ReadRow(row []any) ([]any, error) { if r.limitRead { return nil, io.EOF } - var data interface{} + var data any if err := r.reader.Decode(&data); err != nil { return nil, err } @@ -179,9 +179,9 @@ func (r *JSONReader) ReadRow(row []interface{}) ([]interface{}, error) { return r.rowParse(row, data), nil } -func (r *JSONReader) rowParse(row []interface{}, jsonRow interface{}) []interface{} { +func (r *JSONReader) rowParse(row []any, jsonRow any) []any { switch m := jsonRow.(type) { - case map[string]interface{}: + case map[string]any: for i := range r.names { row[i] = r.jsonString(m[r.names[i]]) } @@ -194,10 +194,10 @@ func (r *JSONReader) rowParse(row []interface{}, jsonRow interface{}) []interfac return row } -func (r *JSONReader) objectRow(obj map[string]interface{}) (map[string]interface{}, []string, error) { +func (r *JSONReader) objectRow(obj map[string]any) (map[string]any, []string, error) { // {"a":"b"} object names := make([]string, 0, len(obj)) - row := make(map[string]interface{}) + row := make(map[string]any) for k, v := range obj { names = append(names, k) if v == nil { @@ -209,20 +209,20 @@ func (r *JSONReader) objectRow(obj map[string]interface{}) (map[string]interface return row, names, nil } -func (r *JSONReader) etcRow(val interface{}) (map[string]interface{}, []string, error) { +func (r *JSONReader) etcRow(val any) (map[string]any, []string, error) { // ex. array array // [["a"], // ["b"]] var names []string k := "c1" names = append(names, k) - row := make(map[string]interface{}) + row := make(map[string]any) row[k] = r.jsonString(val) return row, names, nil } // jqueryRun is a gojq.Run for json. -func (r *JSONReader) jqueryRun(top interface{}) error { +func (r *JSONReader) jqueryRun(top any) error { iter := r.query.Run(top) for { v, ok := iter.Next() @@ -240,7 +240,7 @@ func (r *JSONReader) jqueryRun(top interface{}) error { } // jqueryRunJsonl gojq.Run for rows of jsonl. -func (r *JSONReader) jqueryRunJsonl(row []interface{}, jsonRow interface{}) ([]interface{}, error) { +func (r *JSONReader) jqueryRunJsonl(row []any, jsonRow any) ([]any, error) { iter := r.query.Run(jsonRow) for { v, ok := iter.Next() @@ -257,12 +257,12 @@ func (r *JSONReader) jqueryRunJsonl(row []interface{}, jsonRow interface{}) ([]i } // jsonString returns the string of the argument. -func (r *JSONReader) jsonString(val interface{}) interface{} { +func (r *JSONReader) jsonString(val any) any { var str string switch val.(type) { case nil: return nil - case map[string]interface{}, []interface{}: + case map[string]any, []any: b, err := json.Marshal(val) if err != nil { log.Printf("ERROR: jsonString:%s", err) diff --git a/input_json_test.go b/input_json_test.go index 6d64f0d..18d520c 100644 --- a/input_json_test.go +++ b/input_json_test.go @@ -69,7 +69,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: nil, - preRead: []map[string]interface{}{{}}, + preRead: []map[string]any{{}}, }, wantErr: false, }, @@ -81,7 +81,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"c1", "c2"}, - preRead: []map[string]interface{}{{"c1": "1", "c2": "Orange"}, {"c1": "2", "c2": "Melon"}, {"c1": "3", "c2": "Apple"}}, + preRead: []map[string]any{{"c1": "1", "c2": "Orange"}, {"c1": "2", "c2": "Melon"}, {"c1": "3", "c2": "Apple"}}, }, wantErr: false, }, @@ -96,7 +96,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"c1", "c2"}, - preRead: []map[string]interface{}{{"c1": "1", "c2": "Orange"}}, + preRead: []map[string]any{{"c1": "1", "c2": "Orange"}}, }, wantErr: false, }, @@ -108,7 +108,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"c1"}, - preRead: []map[string]interface{}{{"c1": "[\"a\"]"}, {"c1": "[\"b\"]"}}, + preRead: []map[string]any{{"c1": "[\"a\"]"}, {"c1": "[\"b\"]"}}, }, wantErr: false, }, @@ -120,7 +120,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"c1"}, - preRead: []map[string]interface{}{{"c1": "[\"a\",\"b\"]"}, {"c1": "[\"c\",\"d\"]"}}, + preRead: []map[string]any{{"c1": "[\"a\",\"b\"]"}, {"c1": "[\"c\",\"d\"]"}}, }, wantErr: false, }, @@ -132,7 +132,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"c1"}, - preRead: []map[string]interface{}{{"c1": "a"}, {"c1": "b"}}, + preRead: []map[string]any{{"c1": "a"}, {"c1": "b"}}, }, wantErr: false, }, @@ -144,7 +144,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"a"}, - preRead: []map[string]interface{}{{"a": "b"}}, + preRead: []map[string]any{{"a": "b"}}, }, wantErr: false, }, @@ -160,7 +160,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"id", "name"}, - preRead: []map[string]interface{}{{"id": "1", "name": "Orange"}}, + preRead: []map[string]any{{"id": "1", "name": "Orange"}}, }, wantErr: false, }, @@ -176,7 +176,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"id", "name", "color"}, - preRead: []map[string]interface{}{ + preRead: []map[string]any{ {"id": "1", "name": "Orange"}, {"id": "2", "name": "Melon"}, {"id": "3", "name": "Apple"}, @@ -193,7 +193,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"c1", "c2"}, - preRead: []map[string]interface{}{{"c1": "1", "c2": "Orange"}}, + preRead: []map[string]any{{"c1": "1", "c2": "Orange"}}, }, wantErr: false, }, @@ -209,7 +209,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"name", "email"}, - preRead: []map[string]interface{}{ + preRead: []map[string]any{ {"name": "Shyam", "email": "shyamjaiswal@gmail.com"}, {"name": "Bob", "email": "bob32@gmail.com"}, {"name": "Jai", "email": "jai87@gmail.com"}, @@ -235,7 +235,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"value", "onclick"}, - preRead: []map[string]interface{}{ + preRead: []map[string]any{ {"value": "New", "onclick": "CreateDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Save", "onclick": "SaveDoc()"}, @@ -252,7 +252,7 @@ func TestNewJSONReader(t *testing.T) { }, want: &JSONReader{ names: []string{"id"}, - preRead: []map[string]interface{}{ + preRead: []map[string]any{ {"id": "1"}, {"id": "2"}, {"id": "3"}, @@ -297,7 +297,7 @@ func TestNewJSONReaderWithNULL(t *testing.T) { }, want: &JSONReader{ names: []string{"id"}, - preRead: []map[string]interface{}{ + preRead: []map[string]any{ {"id": "1"}, {"id": nil}, {"id": "3"}, @@ -316,7 +316,7 @@ func TestNewJSONReaderWithNULL(t *testing.T) { }, want: &JSONReader{ names: []string{"id"}, - preRead: []map[string]interface{}{ + preRead: []map[string]any{ {"id": "1"}, {"id": nil}, {"id": "3"}, @@ -335,7 +335,7 @@ func TestNewJSONReaderWithNULL(t *testing.T) { }, want: &JSONReader{ names: []string{"c1", "c2"}, - preRead: []map[string]interface{}{{"c1": "1", "c2": nil}, {"c1": "2", "c2": nil}, {"c1": "3", "c2": "Apple"}}, + preRead: []map[string]any{{"c1": "1", "c2": nil}, {"c1": "2", "c2": nil}, {"c1": "3", "c2": "Apple"}}, }, wantErr: false, }, diff --git a/input_ltsv.go b/input_ltsv.go index ee0cddc..90f20e8 100644 --- a/input_ltsv.go +++ b/input_ltsv.go @@ -81,11 +81,11 @@ func (r *LTSVReader) Types() ([]string, error) { } // PreReadRow is returns only columns that store preread rows. -func (r *LTSVReader) PreReadRow() [][]interface{} { +func (r *LTSVReader) PreReadRow() [][]any { rowNum := len(r.preRead) - rows := make([][]interface{}, rowNum) + rows := make([][]any, rowNum) for n := 0; n < rowNum; n++ { - rows[n] = make([]interface{}, len(r.names)) + rows[n] = make([]any, len(r.names)) for i := range r.names { rows[n][i] = r.preRead[n][r.names[i]] if r.needNULL { @@ -97,7 +97,7 @@ func (r *LTSVReader) PreReadRow() [][]interface{} { } // ReadRow is read the rest of the row. -func (r *LTSVReader) ReadRow(row []interface{}) ([]interface{}, error) { +func (r *LTSVReader) ReadRow(row []any) ([]any, error) { if r.limitRead { return nil, io.EOF } diff --git a/input_ltsv_test.go b/input_ltsv_test.go index c2874a4..5e45ace 100644 --- a/input_ltsv_test.go +++ b/input_ltsv_test.go @@ -205,7 +205,7 @@ func TestLTSVReader_PreReadRow(t *testing.T) { name string fileName string opts *ReadOpts - want [][]interface{} + want [][]any }{ { name: "test1", @@ -213,7 +213,7 @@ func TestLTSVReader_PreReadRow(t *testing.T) { opts: NewReadOpts( InPreRead(100), ), - want: [][]interface{}{ + want: [][]any{ {"1", "Orange", "50", "", ""}, {"2", "Melon", "500", "ibaraki", ""}, {"3", "Apple", "100", "aomori", "red"}, @@ -227,7 +227,7 @@ func TestLTSVReader_PreReadRow(t *testing.T) { InNeedNULL(true), InNULL(""), ), - want: [][]interface{}{ + want: [][]any{ {"1", "Orange", "50", nil, nil}, {"2", "Melon", "500", "ibaraki", nil}, {"3", "Apple", "100", "aomori", "red"}, @@ -253,14 +253,14 @@ func TestLTSVReader_PreReadRow(t *testing.T) { func TestLTSVReader_ReadRow(t *testing.T) { type args struct { - row []interface{} + row []any } tests := []struct { name string fileName string opts *ReadOpts args args - want []interface{} + want []any wantErr bool }{ { @@ -268,11 +268,11 @@ func TestLTSVReader_ReadRow(t *testing.T) { fileName: "test_indefinite.ltsv", opts: NewReadOpts(), args: args{ - []interface{}{ + []any{ "", "", "", }, }, - want: []interface{}{ + want: []any{ "2", "Melon", "500", }, wantErr: false, @@ -285,11 +285,11 @@ func TestLTSVReader_ReadRow(t *testing.T) { InNULL(""), ), args: args{ - []interface{}{ + []any{ "", "", "", }, }, - want: []interface{}{ + want: []any{ "2", nil, "500", }, wantErr: false, diff --git a/input_slice.go b/input_slice.go index 9842395..d889759 100644 --- a/input_slice.go +++ b/input_slice.go @@ -12,7 +12,7 @@ type SliceReader struct { tableName string names []string types []string - data [][]interface{} + data [][]any } // NewSliceReader takes a tableName and tabular data in memory @@ -22,7 +22,7 @@ type SliceReader struct { // a two-dimensional array, // a map, // and an array of structures. -func NewSliceReader(tableName string, args interface{}) *SliceReader { +func NewSliceReader(tableName string, args any) *SliceReader { val := reflect.ValueOf(args) if val.Kind() == reflect.Ptr { val = reflect.Indirect(val) @@ -38,7 +38,7 @@ func NewSliceReader(tableName string, args interface{}) *SliceReader { return sliceReader(tableName, val) default: single := val.Interface() - data := [][]interface{}{ + data := [][]any{ {single}, } names := []string{"c1"} @@ -58,9 +58,9 @@ func mapReader(tableName string, val reflect.Value) *SliceReader { keyType := val.MapKeys()[0].Kind() valType := val.MapIndex(val.MapKeys()[0]).Kind() types := []string{typeToDBType(keyType), typeToDBType(valType)} - data := make([][]interface{}, 0) + data := make([][]any, 0) for _, e := range val.MapKeys() { - data = append(data, []interface{}{e.Interface(), val.MapIndex(e).Interface()}) + data = append(data, []any{e.Interface(), val.MapIndex(e).Interface()}) } return &SliceReader{ tableName: tableName, @@ -80,11 +80,11 @@ func structReader(tableName string, val reflect.Value) *SliceReader { names[i] = f.Name types[i] = typeToDBType(f.Type.Kind()) } - single := make([]interface{}, t.NumField()) + single := make([]any, t.NumField()) for j := 0; j < t.NumField(); j++ { single[j] = fmt.Sprintf("%v", val.Field(j)) } - data := [][]interface{}{ + data := [][]any{ single, } @@ -129,10 +129,10 @@ func structSliceReader(tableName string, val reflect.Value) *SliceReader { names[i] = f.Name types[i] = typeToDBType(f.Type.Kind()) } - data := make([][]interface{}, 0) + data := make([][]any, 0) for i := 0; i < length; i++ { rows := val.Index(i) - r := make([]interface{}, rows.NumField()) + r := make([]any, rows.NumField()) for j := 0; j < rows.NumField(); j++ { r[j] = fmt.Sprintf("%v", rows.Field(j)) } @@ -158,9 +158,9 @@ func sliceSliceReader(tableName string, val reflect.Value) *SliceReader { types[i] = typeToDBType(colType) } - data := make([][]interface{}, 0) + data := make([][]any, 0) for i := 0; i < length; i++ { - data = append(data, val.Index(i).Interface().([]interface{})) + data = append(data, val.Index(i).Interface().([]any)) } return &SliceReader{ tableName: tableName, @@ -176,9 +176,9 @@ func interfaceSliceReader(tableName string, val reflect.Value) *SliceReader { t := reflect.ValueOf(v) names := []string{"c1"} types := []string{typeToDBType(t.Kind())} - data := make([][]interface{}, length) + data := make([][]any, length) for i := 0; i < length; i++ { - data[i] = []interface{}{val.Index(i).Interface()} + data[i] = []any{val.Index(i).Interface()} } return &SliceReader{ tableName: tableName, @@ -214,11 +214,11 @@ func (r *SliceReader) Types() ([]string, error) { } // PreReadRow is returns entity of the data. -func (r *SliceReader) PreReadRow() [][]interface{} { +func (r *SliceReader) PreReadRow() [][]any { return r.data } // ReadRow only returns EOF. -func (r *SliceReader) ReadRow(row []interface{}) ([]interface{}, error) { +func (r *SliceReader) ReadRow(row []any) ([]any, error) { return nil, io.EOF } diff --git a/input_slice_test.go b/input_slice_test.go index ee98192..80fdf41 100644 --- a/input_slice_test.go +++ b/input_slice_test.go @@ -8,7 +8,7 @@ import ( func TestNewSliceReader(t *testing.T) { type args struct { tableName string - args interface{} + args any } tests := []struct { name string @@ -25,7 +25,7 @@ func TestNewSliceReader(t *testing.T) { tableName: "one", names: []string{"c1"}, types: []string{"text"}, - data: [][]interface{}{{"one"}}, + data: [][]any{{"one"}}, }, }, { @@ -38,40 +38,40 @@ func TestNewSliceReader(t *testing.T) { tableName: "one", names: []string{"c1"}, types: []string{"int"}, - data: [][]interface{}{{1}}, + data: [][]any{{1}}, }, }, { name: "single", args: args{ tableName: "single", - args: []interface{}{"a", "b"}, + args: []any{"a", "b"}, }, want: &SliceReader{ tableName: "single", names: []string{"c1"}, types: []string{"text"}, - data: [][]interface{}{{"a"}, {"b"}}, + data: [][]any{{"a"}, {"b"}}, }, }, { name: "singleInt", args: args{ tableName: "singleInt", - args: []interface{}{1, 2, 3}, + args: []any{1, 2, 3}, }, want: &SliceReader{ tableName: "singleInt", names: []string{"c1"}, types: []string{"int"}, - data: [][]interface{}{{1}, {2}, {3}}, + data: [][]any{{1}, {2}, {3}}, }, }, { name: "slice", args: args{ tableName: "slice", - args: [][]interface{}{ + args: [][]any{ {1, "one"}, {2, "two"}, {3, "three"}, @@ -81,7 +81,7 @@ func TestNewSliceReader(t *testing.T) { tableName: "slice", names: []string{"c1", "c2"}, types: []string{"int", "text"}, - data: [][]interface{}{ + data: [][]any{ {1, "one"}, {2, "two"}, {3, "three"}, @@ -103,7 +103,7 @@ func TestNewSliceReader(t *testing.T) { tableName: "struct", names: []string{"id", "name"}, types: []string{"int", "text"}, - data: [][]interface{}{ + data: [][]any{ {"1", "one"}, }, }, @@ -125,7 +125,7 @@ func TestNewSliceReader(t *testing.T) { tableName: "struct", names: []string{"id", "name"}, types: []string{"int", "text"}, - data: [][]interface{}{ + data: [][]any{ {"1", "one"}, {"2", "two"}, {"3", "three"}, @@ -145,7 +145,7 @@ func TestNewSliceReader(t *testing.T) { func TestNewMapSliceReader(t *testing.T) { type args struct { tableName string - args interface{} + args any } tests := []struct { name string @@ -166,7 +166,7 @@ func TestNewMapSliceReader(t *testing.T) { tableName: "map", names: []string{"c1", "c2"}, types: []string{"text", "text"}, - data: [][]interface{}{ + data: [][]any{ {"1", "one"}, {"2", "two"}, {"3", "three"}, @@ -188,12 +188,12 @@ func TestSliceReader_PreReadRow(t *testing.T) { tableName string names []string types []string - data [][]interface{} + data [][]any } tests := []struct { name string fields fields - want [][]interface{} + want [][]any }{ { name: "struct", @@ -201,13 +201,13 @@ func TestSliceReader_PreReadRow(t *testing.T) { tableName: "struct", names: []string{"id", "name"}, types: []string{"text", "text"}, - data: [][]interface{}{ + data: [][]any{ {"1", "one"}, {"2", "two"}, {"3", "three"}, }, }, - want: [][]interface{}{ + want: [][]any{ {"1", "one"}, {"2", "two"}, {"3", "three"}, diff --git a/input_tbln.go b/input_tbln.go index 99186f5..7c6028f 100644 --- a/input_tbln.go +++ b/input_tbln.go @@ -12,7 +12,7 @@ import ( type TBLNRead struct { reader tbln.Reader inNULL string - preRead [][]interface{} + preRead [][]any limitRead bool needNULL bool } @@ -61,7 +61,7 @@ func NewTBLNReader(reader io.Reader, opts *ReadOpts) (*TBLNRead, error) { } } - r.preRead = make([][]interface{}, 0, opts.InPreRead) + r.preRead = make([][]any, 0, opts.InPreRead) r.preRead = append(r.preRead, r.recToRow(rec)) for n := 1; n < opts.InPreRead; n++ { rec, err := r.reader.ReadRow() @@ -98,12 +98,12 @@ func (r *TBLNRead) Types() ([]string, error) { } // PreReadRow is returns only columns that store preread rows. -func (r *TBLNRead) PreReadRow() [][]interface{} { +func (r *TBLNRead) PreReadRow() [][]any { return r.preRead } // ReadRow is read the rest of the row. -func (r *TBLNRead) ReadRow(row []interface{}) ([]interface{}, error) { +func (r *TBLNRead) ReadRow(row []any) ([]any, error) { if r.limitRead { return nil, io.EOF } @@ -116,8 +116,8 @@ func (r *TBLNRead) ReadRow(row []interface{}) ([]interface{}, error) { return row, nil } -func (r *TBLNRead) recToRow(rec []string) []interface{} { - row := make([]interface{}, len(rec)) +func (r *TBLNRead) recToRow(rec []string) []any { + row := make([]any, len(rec)) for i, c := range rec { if c != "" { row[i] = c diff --git a/input_tbln_test.go b/input_tbln_test.go index 8bd7ac5..70812ad 100644 --- a/input_tbln_test.go +++ b/input_tbln_test.go @@ -38,7 +38,7 @@ func TestNewTBLNReader(t *testing.T) { reader: strings.NewReader("| 1 | test |"), }, want: &TBLNRead{ - preRead: [][]interface{}{{"1", "test"}}, + preRead: [][]any{{"1", "test"}}, }, wantNames: []string{"c1", "c2"}, wantTypes: []string{"text", "text"}, @@ -50,7 +50,7 @@ func TestNewTBLNReader(t *testing.T) { reader: strings.NewReader("; type: | int | text |\n| 1 | test |"), }, want: &TBLNRead{ - preRead: [][]interface{}{{"1", "test"}}, + preRead: [][]any{{"1", "test"}}, }, wantNames: []string{"c1", "c2"}, wantTypes: []string{"int", "text"}, @@ -62,7 +62,7 @@ func TestNewTBLNReader(t *testing.T) { reader: strings.NewReader("; name: | id | name |\n| 1 | test |"), }, want: &TBLNRead{ - preRead: [][]interface{}{{"1", "test"}}, + preRead: [][]any{{"1", "test"}}, }, wantNames: []string{"id", "name"}, wantTypes: []string{"text", "text"}, @@ -86,7 +86,7 @@ func TestNewTBLNReader(t *testing.T) { reader: strings.NewReader("; name: | id | name |\n; type: | int | text |\n| 1 | test |"), }, want: &TBLNRead{ - preRead: [][]interface{}{{"1", "test"}}, + preRead: [][]any{{"1", "test"}}, }, wantNames: []string{"id", "name"}, wantTypes: []string{"int", "text"}, @@ -141,7 +141,7 @@ func TestNewTBLNReaderFile(t *testing.T) { fileName: "test.tbln", opts: NewReadOpts(), want: &TBLNRead{ - preRead: [][]interface{}{ + preRead: [][]any{ {"1", "Bob"}, }, }, @@ -154,7 +154,7 @@ func TestNewTBLNReaderFile(t *testing.T) { InPreRead(2), ), want: &TBLNRead{ - preRead: [][]interface{}{ + preRead: [][]any{ {"1", "Bob"}, {"2", "Alice"}, }, @@ -168,7 +168,7 @@ func TestNewTBLNReaderFile(t *testing.T) { InPreRead(100), ), want: &TBLNRead{ - preRead: [][]interface{}{ + preRead: [][]any{ {"1", "Bob"}, {"2", "Alice"}, }, @@ -182,7 +182,7 @@ func TestNewTBLNReaderFile(t *testing.T) { InPreRead(3), ), want: &TBLNRead{ - preRead: [][]interface{}{ + preRead: [][]any{ {"1", "Bob"}, {"2", "Alice"}, {"3", "NULL"}, @@ -199,7 +199,7 @@ func TestNewTBLNReaderFile(t *testing.T) { InNULL("NULL"), ), want: &TBLNRead{ - preRead: [][]interface{}{ + preRead: [][]any{ {"1", "Bob"}, {"2", "Alice"}, {"3", nil}, diff --git a/input_yaml.go b/input_yaml.go index fad5840..ac93bf1 100644 --- a/input_yaml.go +++ b/input_yaml.go @@ -18,7 +18,7 @@ type YAMLReader struct { query *gojq.Query already map[string]bool inNULL string - preRead []map[string]interface{} + preRead []map[string]any names []string types []string limitRead bool @@ -58,7 +58,7 @@ func jqParse(q string) (*gojq.Query, error) { return query, nil } -func (r *YAMLReader) wrapDecode(v interface{}) (err error) { +func (r *YAMLReader) wrapDecode(v any) (err error) { defer func() { if rec := recover(); rec != nil { err = fmt.Errorf("%s", rec) @@ -75,7 +75,7 @@ func (r *YAMLReader) yamlParse(opts *ReadOpts) error { r.needNULL = opts.InNeedNULL r.inNULL = opts.InNULL - var top interface{} + var top any for i := 0; i < opts.InPreRead; i++ { if err := r.wrapDecode(&top); err != nil { if !errors.Is(err, io.EOF) { @@ -100,7 +100,7 @@ func (r *YAMLReader) yamlParse(opts *ReadOpts) error { } // jquery parses the top level of the YAML and stores it in preRead. -func (r *YAMLReader) jquery(top interface{}) error { +func (r *YAMLReader) jquery(top any) error { iter := r.query.Run(top) for { v, ok := iter.Next() @@ -134,9 +134,9 @@ func (r *YAMLReader) Types() ([]string, error) { } // readAhead parses the top level of the YAML and stores it in preRead. -func (r *YAMLReader) readAhead(top interface{}) error { +func (r *YAMLReader) readAhead(top any) error { switch m := top.(type) { - case []interface{}: // YAML array (-). + case []any: for _, v := range m { pre, names, err := r.topLevel(v) if err != nil { @@ -145,7 +145,7 @@ func (r *YAMLReader) readAhead(top interface{}) error { r.appendNames(names) r.preRead = append(r.preRead, pre) } - case map[string]interface{}: // YAML object (key: value). + case map[string]any: pre, names, err := r.topLevel(m) if err != nil { return err @@ -180,9 +180,9 @@ func (r *YAMLReader) appendNames(names []string) { } } -func (r *YAMLReader) topLevel(top interface{}) (map[string]interface{}, []string, error) { +func (r *YAMLReader) topLevel(top any) (map[string]any, []string, error) { switch obj := top.(type) { - case map[string]interface{}: + case map[string]any: return r.objectRow(obj) case yaml.MapSlice: return r.objectMapSlice(obj) @@ -193,10 +193,10 @@ func (r *YAMLReader) topLevel(top interface{}) (map[string]interface{}, []string // PreReadRow is returns only columns that store preRead rows. // One YAML (not YAMLl) returns all rows with preRead. -func (r *YAMLReader) PreReadRow() [][]interface{} { - rows := make([][]interface{}, len(r.preRead)) +func (r *YAMLReader) PreReadRow() [][]any { + rows := make([][]any, len(r.preRead)) for n, v := range r.preRead { - rows[n] = make([]interface{}, len(r.names)) + rows[n] = make([]any, len(r.names)) for i := range r.names { rows[n][i] = v[r.names[i]] } @@ -207,12 +207,12 @@ func (r *YAMLReader) PreReadRow() [][]interface{} { // ReadRow is read the rest of the row. // Only YAMLl requires ReadRow in YAML. -func (r *YAMLReader) ReadRow(row []interface{}) ([]interface{}, error) { +func (r *YAMLReader) ReadRow(row []any) ([]any, error) { if r.limitRead { return nil, io.EOF } - var data interface{} + var data any if err := r.reader.Decode(&data); err != nil { return nil, err } @@ -220,9 +220,9 @@ func (r *YAMLReader) ReadRow(row []interface{}) ([]interface{}, error) { return v, nil } -func (r *YAMLReader) rowParse(row []interface{}, yamlRow interface{}) []interface{} { +func (r *YAMLReader) rowParse(row []any, yamlRow any) []any { switch m := yamlRow.(type) { - case map[string]interface{}: + case map[string]any: for i := range r.names { row[i] = r.toString(m[r.names[i]]) } @@ -236,9 +236,9 @@ func (r *YAMLReader) rowParse(row []interface{}, yamlRow interface{}) []interfac } // objectRow returns a map of the YAML object and the column names. -func (r *YAMLReader) objectRow(obj map[string]interface{}) (map[string]interface{}, []string, error) { +func (r *YAMLReader) objectRow(obj map[string]any) (map[string]any, []string, error) { names := make([]string, 0, len(obj)) - row := make(map[string]interface{}) + row := make(map[string]any) for k, v := range obj { names = append(names, k) if v == nil { @@ -251,9 +251,9 @@ func (r *YAMLReader) objectRow(obj map[string]interface{}) (map[string]interface } // objectMapSlice returns a yaml.MapSlice of the YAML object and the column names. -func (r *YAMLReader) objectMapSlice(obj yaml.MapSlice) (map[string]interface{}, []string, error) { +func (r *YAMLReader) objectMapSlice(obj yaml.MapSlice) (map[string]any, []string, error) { names := make([]string, 0, len(obj)) - row := make(map[string]interface{}) + row := make(map[string]any) for _, item := range obj { key := item.Key.(string) names = append(names, key) @@ -267,23 +267,23 @@ func (r *YAMLReader) objectMapSlice(obj yaml.MapSlice) (map[string]interface{}, } // etcRow returns 1 element with column name c1. -func (r *YAMLReader) etcRow(val interface{}) (map[string]interface{}, []string, error) { +func (r *YAMLReader) etcRow(val any) (map[string]any, []string, error) { var names []string k := "c1" names = append(names, k) - row := make(map[string]interface{}) + row := make(map[string]any) row[k] = r.toString(val) return row, names, nil } // toString returns a string representation of val. // It will be YAML if val is a struct or map, otherwise it will be a string representation of val. -func (r *YAMLReader) toString(val interface{}) interface{} { +func (r *YAMLReader) toString(val any) any { var str string switch t := val.(type) { case nil: return nil - case map[string]interface{}, []yaml.MapSlice, []interface{}: + case map[string]any, []yaml.MapSlice, []any: b, err := yaml.Marshal(val) if err != nil { log.Printf("ERROR: YAMLString:%s", err) diff --git a/input_yaml_test.go b/input_yaml_test.go index 4a33f69..b0ebf78 100644 --- a/input_yaml_test.go +++ b/input_yaml_test.go @@ -38,7 +38,7 @@ func TestNewYAMLReader(t *testing.T) { }, want: &YAMLReader{ names: []string{"a", "b"}, - preRead: []map[string]interface{}{{"a": "1", "b": "test"}}, + preRead: []map[string]any{{"a": "1", "b": "test"}}, }, wantErr: false, }, @@ -57,7 +57,7 @@ func TestNewYAMLReader(t *testing.T) { }, want: &YAMLReader{ names: []string{"c1", "c2"}, - preRead: []map[string]interface{}{{"c1": "1", "c2": "Orange"}, {"c1": "2", "c2": "Melon"}, {"c1": "3", "c2": "Apple"}}, + preRead: []map[string]any{{"c1": "1", "c2": "Orange"}, {"c1": "2", "c2": "Melon"}, {"c1": "3", "c2": "Apple"}}, }, wantErr: false, }, @@ -73,7 +73,7 @@ func TestNewYAMLReader(t *testing.T) { }, want: &YAMLReader{ names: []string{"c1"}, - preRead: []map[string]interface{}{{"c1": "a"}, {"c1": "b"}, {"c1": "c"}}, + preRead: []map[string]any{{"c1": "a"}, {"c1": "b"}, {"c1": "c"}}, }, wantErr: false, }, @@ -92,7 +92,7 @@ func TestNewYAMLReader(t *testing.T) { }, want: &YAMLReader{ names: []string{"c1", "c2"}, - preRead: []map[string]interface{}{{"c1": "1", "c2": "Orange"}}, + preRead: []map[string]any{{"c1": "1", "c2": "Orange"}}, }, wantErr: false, }, diff --git a/output_csv.go b/output_csv.go index 16737c2..da47de8 100644 --- a/output_csv.go +++ b/output_csv.go @@ -71,7 +71,7 @@ func (w *CSVWriter) PreWrite(columns []string, types []string) error { } // WriteRow is row write. -func (w *CSVWriter) WriteRow(values []interface{}, _ []string) error { +func (w *CSVWriter) WriteRow(values []any, _ []string) error { for n, column := range values { if n > 0 { if _, err := w.writer.WriteRune(w.outDelimiter); err != nil { @@ -86,7 +86,7 @@ func (w *CSVWriter) WriteRow(values []interface{}, _ []string) error { return err } -func (w *CSVWriter) writeColumn(column interface{}) error { +func (w *CSVWriter) writeColumn(column any) error { if column == nil { var err error if w.needNULL { diff --git a/output_json.go b/output_json.go index 5eb9790..6462df0 100644 --- a/output_json.go +++ b/output_json.go @@ -33,7 +33,7 @@ func (w *JSONWriter) PreWrite(columns []string, types []string) error { } // WriteRow is Addition to array. -func (w *JSONWriter) WriteRow(values []interface{}, columns []string) error { +func (w *JSONWriter) WriteRow(values []any, columns []string) error { m := orderedmap.New() for i, col := range values { m.Set(columns[i], compatibleJSON(col, w.needNULL, w.outNULL)) @@ -43,7 +43,7 @@ func (w *JSONWriter) WriteRow(values []interface{}, columns []string) error { } // CompatibleJSON converts the value to a JSON-compatible value. -func compatibleJSON(v interface{}, needNULL bool, outNULL string) interface{} { +func compatibleJSON(v any, needNULL bool, outNULL string) any { switch t := v.(type) { case []byte: if isJSON(t) { @@ -77,7 +77,7 @@ func isJSON(s []byte) bool { return false } - var js interface{} + var js any err := json.Unmarshal(s, &js) return err == nil } diff --git a/output_json_test.go b/output_json_test.go index 912a422..b3cc0f4 100644 --- a/output_json_test.go +++ b/output_json_test.go @@ -7,14 +7,14 @@ import ( func Test_compatibleJSON(t *testing.T) { type args struct { - v interface{} + v any needNULL bool outNULL string } tests := []struct { name string args args - want interface{} + want any }{ { name: "testText", diff --git a/output_jsonl.go b/output_jsonl.go index 3b298bf..dc67667 100644 --- a/output_jsonl.go +++ b/output_jsonl.go @@ -28,7 +28,7 @@ func (w *JSONLWriter) PreWrite(columns []string, types []string) error { } // WriteRow is write one JSONL. -func (w *JSONLWriter) WriteRow(values []interface{}, columns []string) error { +func (w *JSONLWriter) WriteRow(values []any, columns []string) error { m := orderedmap.New() for i, col := range values { m.Set(columns[i], compatibleJSON(col, w.needNULL, w.outNULL)) diff --git a/output_ltsv.go b/output_ltsv.go index 402a9cc..5e6f5dd 100644 --- a/output_ltsv.go +++ b/output_ltsv.go @@ -30,7 +30,7 @@ func (w *LTSVWriter) PreWrite(columns []string, types []string) error { } // WriteRow is row write. -func (w *LTSVWriter) WriteRow(values []interface{}, labels []string) error { +func (w *LTSVWriter) WriteRow(values []any, labels []string) error { for n, col := range values { if n > 0 { if _, err := w.writer.WriteRune(w.delimiter); err != nil { diff --git a/output_raw.go b/output_raw.go index 9fc9f51..f812f98 100644 --- a/output_raw.go +++ b/output_raw.go @@ -56,7 +56,7 @@ func (w *RAWWriter) PreWrite(columns []string, types []string) error { } // WriteRow is row write. -func (w *RAWWriter) WriteRow(values []interface{}, _ []string) error { +func (w *RAWWriter) WriteRow(values []any, _ []string) error { for n, col := range values { if n > 0 { if _, err := w.writer.WriteString(w.delimiter); err != nil { diff --git a/output_slice.go b/output_slice.go index df08a80..54c5dd8 100644 --- a/output_slice.go +++ b/output_slice.go @@ -2,7 +2,7 @@ package trdsql // SliceWriter is a structure to receive the result in slice. type SliceWriter struct { - Table [][]interface{} + Table [][]any } // NewSliceWriter return SliceWriter. @@ -12,13 +12,13 @@ func NewSliceWriter() *SliceWriter { // PreWrite prepares the area. func (w *SliceWriter) PreWrite(columns []string, types []string) error { - w.Table = make([][]interface{}, 0) + w.Table = make([][]any, 0) return nil } // WriteRow stores the result in Table. -func (w *SliceWriter) WriteRow(values []interface{}, columns []string) error { - row := make([]interface{}, len(values)) +func (w *SliceWriter) WriteRow(values []any, columns []string) error { + row := make([]any, len(values)) copy(row, values) w.Table = append(w.Table, row) return nil diff --git a/output_slice_test.go b/output_slice_test.go index ef92a4a..ce93cfe 100644 --- a/output_slice_test.go +++ b/output_slice_test.go @@ -26,7 +26,7 @@ func TestNewSliceWriter(t *testing.T) { func TestSliceWriter_PreWrite(t *testing.T) { type fields struct { - Table [][]interface{} + Table [][]any } type args struct { columns []string @@ -41,7 +41,7 @@ func TestSliceWriter_PreWrite(t *testing.T) { { name: "test1", fields: fields{ - Table: [][]interface{}{ + Table: [][]any{ {"1", "one"}, {"2", "two"}, {"3", "three"}, @@ -65,10 +65,10 @@ func TestSliceWriter_PreWrite(t *testing.T) { func TestSliceWriter_WriteRow(t *testing.T) { type fields struct { - Table [][]interface{} + Table [][]any } type args struct { - values []interface{} + values []any columns []string } tests := []struct { @@ -80,14 +80,14 @@ func TestSliceWriter_WriteRow(t *testing.T) { { name: "test1", fields: fields{ - Table: [][]interface{}{ + Table: [][]any{ {"1", "one"}, {"2", "two"}, {"3", "three"}, }, }, args: args{ - values: []interface{}{ + values: []any{ "", "", }, columns: []string{"id", "name"}, diff --git a/output_tablewriter.go b/output_tablewriter.go index 1c71c41..989ebb1 100644 --- a/output_tablewriter.go +++ b/output_tablewriter.go @@ -42,7 +42,7 @@ func (w *TWWriter) PreWrite(columns []string, types []string) error { } // WriteRow is Addition to array. -func (w *TWWriter) WriteRow(values []interface{}, columns []string) error { +func (w *TWWriter) WriteRow(values []any, columns []string) error { for i, col := range values { str := "" if col == nil && w.needNULL { diff --git a/output_tbln.go b/output_tbln.go index 835f713..27cfe6b 100644 --- a/output_tbln.go +++ b/output_tbln.go @@ -41,7 +41,7 @@ func (w *TBLNWriter) PreWrite(columns []string, types []string) error { } // WriteRow is row write. -func (w *TBLNWriter) WriteRow(values []interface{}, columns []string) error { +func (w *TBLNWriter) WriteRow(values []any, columns []string) error { for i, col := range values { str := "" if col == nil && w.needNULL { diff --git a/output_vertical.go b/output_vertical.go index 25c2307..6e1385b 100644 --- a/output_vertical.go +++ b/output_vertical.go @@ -49,7 +49,7 @@ func (w *VFWriter) PreWrite(columns []string, types []string) error { } // WriteRow is Actual output. -func (w *VFWriter) WriteRow(values []interface{}, columns []string) error { +func (w *VFWriter) WriteRow(values []any, columns []string) error { w.count++ _, err := fmt.Fprintf(w.writer, "---[ %d]%s\n", w.count, strings.Repeat("-", (w.termWidth-16))) diff --git a/output_yaml.go b/output_yaml.go index b01f913..85285e1 100644 --- a/output_yaml.go +++ b/output_yaml.go @@ -31,7 +31,7 @@ func (w *YAMLWriter) PreWrite(columns []string, types []string) error { } // WriteRow is Addition to array. -func (w *YAMLWriter) WriteRow(values []interface{}, columns []string) error { +func (w *YAMLWriter) WriteRow(values []any, columns []string) error { m := make(yaml.MapSlice, len(values)) for i, col := range values { m[i].Key = columns[i] @@ -42,8 +42,8 @@ func (w *YAMLWriter) WriteRow(values []interface{}, columns []string) error { } // CompatibleYAML converts the value to a YAML-compatible value. -func compatibleYAML(v interface{}, needNULL bool, outNULL string) interface{} { - var yl interface{} +func compatibleYAML(v any, needNULL bool, outNULL string) any { + var yl any switch t := v.(type) { case []byte: if err := yaml.Unmarshal(t, &yl); err == nil { diff --git a/reader.go b/reader.go index 01d0d9f..10e3c1f 100644 --- a/reader.go +++ b/reader.go @@ -74,9 +74,9 @@ type Reader interface { // Types returns column types. Types() ([]string, error) // PreReadRow is returns only columns that store preRead rows. - PreReadRow() [][]interface{} + PreReadRow() [][]any // ReadRow is read the rest of the row. - ReadRow(row []interface{}) ([]interface{}, error) + ReadRow(row []any) ([]any, error) } // ReadOpts represents options that determine the behavior of the reader. @@ -227,7 +227,7 @@ func NewReader(reader io.Reader, readOpts *ReadOpts) (Reader, error) { } func skipRead(r Reader, skipNum int) { - skip := make([]interface{}, 1) + skip := make([]any, 1) for i := 0; i < skipNum; i++ { row, err := r.ReadRow(skip) if err != nil { diff --git a/writer.go b/writer.go index bedf541..d547a92 100644 --- a/writer.go +++ b/writer.go @@ -27,7 +27,7 @@ type Writer interface { // The arguments are a list of column names and a list of type names. PreWrite(columns []string, types []string) error // WriteRow is row write. - WriteRow(row []interface{}, columns []string) error + WriteRow(row []any, columns []string) error // PostWrite is called last in the write. PostWrite() error } diff --git a/writer_test.go b/writer_test.go index 87a2206..5b6f01d 100644 --- a/writer_test.go +++ b/writer_test.go @@ -11,7 +11,7 @@ func (e errorWriter) PreWrite([]string, []string) error { return nil } -func (e errorWriter) WriteRow([]interface{}, []string) error { +func (e errorWriter) WriteRow([]any, []string) error { return ErrTest }