From f9e376a117a1a93b57e96e1a6ecb465d517061d9 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Thu, 1 Dec 2022 16:51:12 -0600 Subject: [PATCH] Code cleanup; add make lint task --- Makefile | 1 + pkg/api/api.go | 2 +- pkg/client/result.go | 57 +++++++++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 722ecc1..57d2672 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ usage: @echo "make release : Generate binaries for all supported OSes" @echo "make test : Execute test suite" @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 docker : Build docker image" @echo "make docker-release : Build and tag docker image" diff --git a/pkg/api/api.go b/pkg/api/api.go index ebb88d6..291097a 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -345,7 +345,7 @@ func GetTable(c *gin.Context) { var res *client.Result 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")) } else { res, err = DB(c).Table(c.Params.ByName("table")) diff --git a/pkg/client/result.go b/pkg/client/result.go index 88d7da0..994b2a9 100644 --- a/pkg/client/result.go +++ b/pkg/client/result.go @@ -13,27 +13,36 @@ import ( "github.com/sosedoff/pgweb/pkg/command" ) -type Row []interface{} +const ( + ObjTypeTable = "table" + ObjTypeView = "view" + ObjTypeMaterializedView = "materialized_view" + ObjTypeSequence = "sequence" +) -type Pagination struct { - Rows int64 `json:"rows_count"` - Page int64 `json:"page"` - Pages int64 `json:"pages_count"` - PerPage int64 `json:"per_page"` -} +type ( + Row []interface{} -type Result struct { - Pagination *Pagination `json:"pagination,omitempty"` - Columns []string `json:"columns"` - Rows []Row `json:"rows"` -} + Pagination struct { + Rows int64 `json:"rows_count"` + Page int64 `json:"page"` + Pages int64 `json:"pages_count"` + PerPage int64 `json:"per_page"` + } -type Objects struct { - Tables []string `json:"table"` - Views []string `json:"view"` - MaterializedViews []string `json:"materialized_view"` - Sequences []string `json:"sequence"` -} + Result struct { + Pagination *Pagination `json:"pagination,omitempty"` + Columns []string `json:"columns"` + Rows []Row `json:"rows"` + } + + Objects struct { + Tables []string `json:"table"` + Views []string `json:"view"` + MaterializedViews []string `json:"materialized_view"` + Sequences []string `json:"sequence"` + } +) // Due to big int number limitations in javascript, numbers should be encoded // 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 { schema := row[0].(string) name := row[1].(string) - object_type := row[2].(string) + objectType := row[2].(string) if objects[schema] == nil { objects[schema] = &Objects{ @@ -150,14 +159,14 @@ func ObjectsFromResult(res *Result) map[string]*Objects { } } - switch object_type { - case "table": + switch objectType { + case ObjTypeTable: objects[schema].Tables = append(objects[schema].Tables, name) - case "view": + case ObjTypeView: objects[schema].Views = append(objects[schema].Views, name) - case "materialized_view": + case ObjTypeMaterializedView: objects[schema].MaterializedViews = append(objects[schema].MaterializedViews, name) - case "sequence": + case ObjTypeSequence: objects[schema].Sequences = append(objects[schema].Sequences, name) } }