Code cleanup; add make lint task

This commit is contained in:
Dan Sosedoff 2022-12-01 16:51:12 -06:00
parent dafda4a977
commit f9e376a117
No known key found for this signature in database
GPG Key ID: 26186197D282B164
3 changed files with 35 additions and 25 deletions

View File

@ -15,6 +15,7 @@ usage:
@echo "make release : Generate binaries for all supported OSes" @echo "make release : Generate binaries for all supported OSes"
@echo "make test : Execute test suite" @echo "make test : Execute test suite"
@echo "make test-all : Execute test suite on multiple PG versions" @echo "make test-all : Execute test suite on multiple PG versions"
@echo "make lint : Execute code linter"
@echo "make clean : Remove all build files and reset assets" @echo "make clean : Remove all build files and reset assets"
@echo "make docker : Build docker image" @echo "make docker : Build docker image"
@echo "make docker-release : Build and tag docker image" @echo "make docker-release : Build and tag docker image"

View File

@ -345,7 +345,7 @@ func GetTable(c *gin.Context) {
var res *client.Result var res *client.Result
var err error var err error
if c.Request.FormValue("type") == "materialized_view" { if c.Request.FormValue("type") == client.ObjTypeMaterializedView {
res, err = DB(c).MaterializedView(c.Params.ByName("table")) res, err = DB(c).MaterializedView(c.Params.ByName("table"))
} else { } else {
res, err = DB(c).Table(c.Params.ByName("table")) res, err = DB(c).Table(c.Params.ByName("table"))

View File

@ -13,27 +13,36 @@ import (
"github.com/sosedoff/pgweb/pkg/command" "github.com/sosedoff/pgweb/pkg/command"
) )
type Row []interface{} const (
ObjTypeTable = "table"
ObjTypeView = "view"
ObjTypeMaterializedView = "materialized_view"
ObjTypeSequence = "sequence"
)
type Pagination struct { type (
Row []interface{}
Pagination struct {
Rows int64 `json:"rows_count"` Rows int64 `json:"rows_count"`
Page int64 `json:"page"` Page int64 `json:"page"`
Pages int64 `json:"pages_count"` Pages int64 `json:"pages_count"`
PerPage int64 `json:"per_page"` PerPage int64 `json:"per_page"`
} }
type Result struct { Result struct {
Pagination *Pagination `json:"pagination,omitempty"` Pagination *Pagination `json:"pagination,omitempty"`
Columns []string `json:"columns"` Columns []string `json:"columns"`
Rows []Row `json:"rows"` Rows []Row `json:"rows"`
} }
type Objects struct { Objects struct {
Tables []string `json:"table"` Tables []string `json:"table"`
Views []string `json:"view"` Views []string `json:"view"`
MaterializedViews []string `json:"materialized_view"` MaterializedViews []string `json:"materialized_view"`
Sequences []string `json:"sequence"` Sequences []string `json:"sequence"`
} }
)
// Due to big int number limitations in javascript, numbers should be encoded // Due to big int number limitations in javascript, numbers should be encoded
// as strings so they could be properly loaded on the frontend. // as strings so they could be properly loaded on the frontend.
@ -139,7 +148,7 @@ func ObjectsFromResult(res *Result) map[string]*Objects {
for _, row := range res.Rows { for _, row := range res.Rows {
schema := row[0].(string) schema := row[0].(string)
name := row[1].(string) name := row[1].(string)
object_type := row[2].(string) objectType := row[2].(string)
if objects[schema] == nil { if objects[schema] == nil {
objects[schema] = &Objects{ objects[schema] = &Objects{
@ -150,14 +159,14 @@ func ObjectsFromResult(res *Result) map[string]*Objects {
} }
} }
switch object_type { switch objectType {
case "table": case ObjTypeTable:
objects[schema].Tables = append(objects[schema].Tables, name) objects[schema].Tables = append(objects[schema].Tables, name)
case "view": case ObjTypeView:
objects[schema].Views = append(objects[schema].Views, name) objects[schema].Views = append(objects[schema].Views, name)
case "materialized_view": case ObjTypeMaterializedView:
objects[schema].MaterializedViews = append(objects[schema].MaterializedViews, name) objects[schema].MaterializedViews = append(objects[schema].MaterializedViews, name)
case "sequence": case ObjTypeSequence:
objects[schema].Sequences = append(objects[schema].Sequences, name) objects[schema].Sequences = append(objects[schema].Sequences, name)
} }
} }