mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-13 15:35:28 +03:00
Results struct cleanup (#627)
* Add results format test * Init results slice before formatting data * Add extra test for nil check in CSV function
This commit is contained in:
parent
aeb9dc24ad
commit
1897bef08d
@ -34,15 +34,6 @@ type Client struct {
|
||||
ConnectionString string `json:"connection_string"`
|
||||
}
|
||||
|
||||
// Struct to hold table rows browsing options
|
||||
type RowsOptions struct {
|
||||
Where string // Custom filter
|
||||
Offset int // Number of rows to skip
|
||||
Limit int // Number of rows to fetch
|
||||
SortColumn string // Column to sort by
|
||||
SortOrder string // Sort direction (ASC, DESC)
|
||||
}
|
||||
|
||||
func getSchemaAndTable(str string) (string, string) {
|
||||
chunks := strings.Split(str, ".")
|
||||
if len(chunks) == 1 {
|
||||
|
@ -22,8 +22,18 @@ const (
|
||||
)
|
||||
|
||||
type (
|
||||
// Row represents a single row of data
|
||||
Row []interface{}
|
||||
|
||||
// RowsOptions contains a list of parameters for table browsing requests
|
||||
RowsOptions struct {
|
||||
Where string // Custom filter
|
||||
Offset int // Number of rows to skip
|
||||
Limit int // Number of rows to fetch
|
||||
SortColumn string // Column to sort by
|
||||
SortOrder string // Sort direction (ASC, DESC)
|
||||
}
|
||||
|
||||
Pagination struct {
|
||||
Rows int64 `json:"rows_count"`
|
||||
Page int64 `json:"page"`
|
||||
@ -94,16 +104,15 @@ func (res *Result) PostProcess() {
|
||||
}
|
||||
|
||||
func (res *Result) Format() []map[string]interface{} {
|
||||
var items []map[string]interface{}
|
||||
items := make([]map[string]interface{}, len(res.Rows))
|
||||
|
||||
for _, row := range res.Rows {
|
||||
for rowIdx, row := range res.Rows {
|
||||
item := make(map[string]interface{})
|
||||
|
||||
for i, c := range res.Columns {
|
||||
item[c] = row[i]
|
||||
}
|
||||
|
||||
items = append(items, item)
|
||||
items[rowIdx] = item
|
||||
}
|
||||
|
||||
return items
|
||||
@ -121,20 +130,17 @@ func (res *Result) CSV() []byte {
|
||||
record := make([]string, len(res.Columns))
|
||||
|
||||
for i, item := range row {
|
||||
if item != nil {
|
||||
switch v := item.(type) {
|
||||
case time.Time:
|
||||
record[i] = v.Format("2006-01-02 15:04:05")
|
||||
default:
|
||||
record[i] = fmt.Sprintf("%v", item)
|
||||
}
|
||||
} else {
|
||||
switch v := item.(type) {
|
||||
case time.Time:
|
||||
record[i] = v.Format("2006-01-02 15:04:05")
|
||||
case nil:
|
||||
record[i] = ""
|
||||
default:
|
||||
record[i] = fmt.Sprintf("%v", item)
|
||||
}
|
||||
}
|
||||
|
||||
err := writer.Write(record)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
break
|
||||
|
@ -2,6 +2,7 @@ package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -51,17 +52,20 @@ func TestPostProcess(t *testing.T) {
|
||||
|
||||
func TestCSV(t *testing.T) {
|
||||
result := Result{
|
||||
Columns: []string{"id", "name", "email"},
|
||||
Columns: []string{"id", "name", "email", "extra"},
|
||||
Rows: []Row{
|
||||
{1, "John", "john@example.com"},
|
||||
{2, "Bob", "bob@example.com"},
|
||||
{1, "John", "john@example.com", "data"},
|
||||
{2, "Bob", "bob@example.com", nil},
|
||||
},
|
||||
}
|
||||
|
||||
expected := "id,name,email\n1,John,john@example.com\n2,Bob,bob@example.com\n"
|
||||
output := string(result.CSV())
|
||||
expected := strings.Join([]string{
|
||||
"id,name,email,extra",
|
||||
"1,John,john@example.com,data",
|
||||
"2,Bob,bob@example.com,",
|
||||
}, "\n") + "\n"
|
||||
|
||||
assert.Equal(t, expected, output)
|
||||
assert.Equal(t, expected, string(result.CSV()))
|
||||
}
|
||||
|
||||
func TestJSON(t *testing.T) {
|
||||
@ -116,3 +120,20 @@ func TestJSON(t *testing.T) {
|
||||
assert.Equal(t, `[{"value":"2022-01-01T00:00:00Z"},{"value":"9022-01-01T00:00:00Z"},{"value":"ERR: INVALID_DATE"}]`, string(result.JSON()))
|
||||
})
|
||||
}
|
||||
|
||||
func TestResultFormat(t *testing.T) {
|
||||
result := Result{
|
||||
Columns: []string{"col1", "col2", "col3", "col4"},
|
||||
Rows: []Row{
|
||||
{"1", "2", "3", nil},
|
||||
{"4", "5", "6", nil},
|
||||
},
|
||||
}
|
||||
|
||||
expected := []map[string]interface{}{
|
||||
{"col1": "1", "col2": "2", "col3": "3", "col4": nil},
|
||||
{"col1": "4", "col2": "5", "col3": "6", "col4": nil},
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, result.Format())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user