Skip to content

Commit

Permalink
Revert go-sumtype deletion (#941)
Browse files Browse the repository at this point in the history
* Revert "Replace go-sumtype with exhaustive linter (#935)"

This reverts commit 97540c0.

* Keep refactorings and linter settings

* Fix test

* Fix tests

* Tune exhaustive linter
  • Loading branch information
artemgavrilov authored Jun 16, 2022
1 parent 5457696 commit e3484c6
Show file tree
Hide file tree
Showing 30 changed files with 111 additions and 10 deletions.
5 changes: 4 additions & 1 deletion .golangci-required.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ linters-settings:
- default # Contains all imports that could not be matched to another section type.
- prefix(github.com/percona/pmm) # Groups all local imports.

exhaustive:
default-signifies-exhaustive: true

linters:
disable-all: true
enable:
# keep sorted
- depguard
- exhaustive
- gci
- goimports
- govet
- ineffassign
- nolintlint
- staticcheck
- exhaustive

run:
skip-dirs:
Expand Down
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ linters-settings:
unparam:
check-exported: true

exhaustive:
default-signifies-exhaustive: true

linters:
enable-all: true
disable:
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ gen: clean ## Generate files.
make -C agent gen
make format
make format ## TODO: One formatting run is not enough, figure out why.
bin/go-sumtype ./...
go install -v ./...

gen-alertmanager: # Generate Alertmanager client.
Expand Down
4 changes: 4 additions & 0 deletions agent/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/percona/pmm/api/agentpb"
)

// go-sumtype:decl Action

// Action describes an abstract thing that can be run by a client and return some output.
type Action interface {
// ID returns an Action ID.
Expand All @@ -36,6 +38,8 @@ type Action interface {
Type() string
// Run runs an Action and returns output and error.
Run(ctx context.Context) ([]byte, error)

sealed()
}

// readRows reads and closes given *sql.Rows, returning columns, data rows, and first encountered error.
Expand Down
2 changes: 2 additions & 0 deletions agent/actions/mongodb_explain_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ func (a *mongodbExplainAction) Run(ctx context.Context) ([]byte, error) {
// We need it because result
return []byte(result.String()), nil
}

func (a *mongodbExplainAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/mongodb_query_admincommand_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ func (a *mongodbQueryAdmincommandAction) Run(ctx context.Context) ([]byte, error
data := []map[string]interface{}{doc}
return agentpb.MarshalActionQueryDocsResult(data)
}

func (a *mongodbQueryAdmincommandAction) sealed() {}
4 changes: 2 additions & 2 deletions agent/actions/mysql_explain_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ func (a *mysqlExplainAction) Run(ctx context.Context) ([]byte, error) {
response.ExplainResult, err = a.explainJSON(ctx, tx)
case agentpb.MysqlExplainOutputFormat_MYSQL_EXPLAIN_OUTPUT_FORMAT_TRADITIONAL_JSON:
response.ExplainResult, err = a.explainTraditionalJSON(ctx, tx)
case agentpb.MysqlExplainOutputFormat_MYSQL_EXPLAIN_OUTPUT_FORMAT_INVALID:
return nil, errors.Errorf("invalid output format")
default:
return nil, errors.Errorf("unsupported output format %s", a.params.OutputFormat)
}
Expand All @@ -121,6 +119,8 @@ func (a *mysqlExplainAction) Run(ctx context.Context) ([]byte, error) {
return b, nil
}

func (a *mysqlExplainAction) sealed() {}

func (a *mysqlExplainAction) explainDefault(ctx context.Context, tx *sql.Tx) ([]byte, error) {
rows, err := tx.QueryContext(ctx, fmt.Sprintf("EXPLAIN /* pmm-agent */ %s", a.query))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions agent/actions/mysql_query_select_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ func (a *mysqlQuerySelectAction) Run(ctx context.Context) ([]byte, error) {
}
return agentpb.MarshalActionQuerySQLResult(columns, dataRows)
}

func (a *mysqlQuerySelectAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/mysql_query_show_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ func (a *mysqlQueryShowAction) Run(ctx context.Context) ([]byte, error) {
}
return agentpb.MarshalActionQuerySQLResult(columns, dataRows)
}

func (a *mysqlQueryShowAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/mysql_show_create_table_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ func (a *mysqlShowCreateTableAction) Run(ctx context.Context) ([]byte, error) {
}
return []byte(tableDef), nil
}

func (a *mysqlShowCreateTableAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/mysql_show_index_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ func (a *mysqlShowIndexAction) Run(ctx context.Context) ([]byte, error) {
}
return jsonRows(columns, dataRows)
}

func (a *mysqlShowIndexAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/mysql_show_table_status_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ func (a *mysqlShowTableStatusAction) Run(ctx context.Context) ([]byte, error) {
}
return jsonRows(columns, dataRows)
}

func (a *mysqlShowTableStatusAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/postgresql_query_select_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ func (a *postgresqlQuerySelectAction) Run(ctx context.Context) ([]byte, error) {
}
return agentpb.MarshalActionQuerySQLResult(columns, dataRows)
}

func (a *postgresqlQuerySelectAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/postgresql_query_show_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ func (a *postgresqlQueryShowAction) Run(ctx context.Context) ([]byte, error) {
}
return agentpb.MarshalActionQuerySQLResult(columns, dataRows)
}

func (a *postgresqlQueryShowAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/postgresql_show_create_table_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func (a *postgresqlShowCreateTableAction) printTableInit(ctx context.Context, w
return tableID, nil
}

func (a *postgresqlShowCreateTableAction) sealed() {}

func (a *postgresqlShowCreateTableAction) printColumnsInfo(ctx context.Context, w io.Writer, db *sql.DB, tableID string) error {
rows, err := db.QueryContext(ctx, `SELECT /* pmm-agent */ a.attname,
pg_catalog.format_type(a.atttypid, a.atttypmod),
Expand Down
2 changes: 2 additions & 0 deletions agent/actions/postgresql_show_index_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ func (a *postgresqlShowIndexAction) Run(ctx context.Context) ([]byte, error) {
}
return jsonRows(columns, dataRows)
}

func (a *postgresqlShowIndexAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/process_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ func (p *processAction) Run(ctx context.Context) ([]byte, error) {

return cmd.CombinedOutput()
}

func (*processAction) sealed() {}
2 changes: 2 additions & 0 deletions agent/actions/pt_mysql_summary_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ func (p *ptMySQLSummaryAction) ListFromMySQLParams() []string {

return args
}

func (*ptMySQLSummaryAction) sealed() {}
2 changes: 1 addition & 1 deletion agent/agents/postgres/pgstatmonitor/stat_monitor_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (ssc *statMonitorCache) getStatMonitorExtended(ctx context.Context, q *refo
totalN++

var c pgStatMonitorExtended
switch pgMonitorVersion { // nolint:exhaustive
switch pgMonitorVersion {
case pgStatMonitorVersion06:
c.pgStatMonitor = *row
c.Database = databases[row.DBID]
Expand Down
4 changes: 2 additions & 2 deletions agent/agents/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func (s *Supervisor) startBuiltin(agentID string, builtinAgent *agentpb.SetState
dsn = builtinAgent.Dsn
}

switch builtinAgent.Type { //nolint:exhaustive
switch builtinAgent.Type {
case inventorypb.AgentType_QAN_MYSQL_PERFSCHEMA_AGENT:
params := &perfschema.Params{
DSN: dsn,
Expand Down Expand Up @@ -501,7 +501,7 @@ func (s *Supervisor) processParams(agentID string, agentProcess *agentpb.SetStat
templateParams := map[string]interface{}{
"listen_port": port,
}
switch agentProcess.Type { //nolint:exhaustive
switch agentProcess.Type {
case inventorypb.AgentType_NODE_EXPORTER:
templateParams["paths_base"] = s.paths.PathsBase
processParams.Path = s.paths.NodeExporter
Expand Down
2 changes: 0 additions & 2 deletions agent/connectionchecker/connection_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ func (cc *ConnectionChecker) Check(ctx context.Context, msg *agentpb.CheckConnec
return cc.checkProxySQLConnection(ctx, msg.Dsn)
case inventorypb.ServiceType_EXTERNAL_SERVICE, inventorypb.ServiceType_HAPROXY_SERVICE:
return cc.checkExternalConnection(ctx, msg.Dsn)
case inventorypb.ServiceType_SERVICE_TYPE_INVALID:
panic("invalid service type")
default:
panic(fmt.Sprintf("unknown service type: %v", msg.Type))
}
Expand Down
2 changes: 1 addition & 1 deletion agent/connectionchecker/connection_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestConnectionChecker(t *testing.T) {
Type: inventorypb.ServiceType_SERVICE_TYPE_INVALID,
Timeout: durationpb.New(time.Nanosecond),
},
expectedErr: `invalid service type`,
expectedErr: `unknown service type: SERVICE_TYPE_INVALID`,
panic: true,
},
{
Expand Down
52 changes: 52 additions & 0 deletions api/agentpb/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,40 @@ package agentpb

import "google.golang.org/protobuf/proto"

//go-sumtype:decl isAgentMessage_Payload
//go-sumtype:decl isServerMessage_Payload

//go-sumtype:decl AgentRequestPayload
//go-sumtype:decl AgentResponsePayload
//go-sumtype:decl ServerResponsePayload
//go-sumtype:decl ServerRequestPayload

//go-sumtype:decl isStartActionRequest_Params

// code below uses the same order as payload types at AgentMessage / ServerMessage

// AgentRequestPayload represents agent's request payload.
type AgentRequestPayload interface {
AgentMessageRequestPayload() isAgentMessage_Payload
sealed()
}

// AgentResponsePayload represents agent's response payload.
type AgentResponsePayload interface {
AgentMessageResponsePayload() isAgentMessage_Payload
sealed()
}

// ServerResponsePayload represents server's response payload.
type ServerResponsePayload interface {
ServerMessageResponsePayload() isServerMessage_Payload
sealed()
}

// ServerRequestPayload represents server's request payload.
type ServerRequestPayload interface {
ServerMessageRequestPayload() isServerMessage_Payload
sealed()
}

// A list of AgentMessage request payloads.
Expand Down Expand Up @@ -169,6 +183,38 @@ func (m *ParseDefaultsFileRequest) ServerMessageRequestPayload() isServerMessage
return &ServerMessage_ParseDefaultsFile{ParseDefaultsFile: m}
}

// in alphabetical order
func (*ActionResultRequest) sealed() {}
func (*ActionResultResponse) sealed() {}
func (*CheckConnectionRequest) sealed() {}
func (*CheckConnectionResponse) sealed() {}
func (*JobProgress) sealed() {}
func (*JobResult) sealed() {}
func (*JobStatusRequest) sealed() {}
func (*JobStatusResponse) sealed() {}
func (*ParseDefaultsFileRequest) sealed() {}
func (*ParseDefaultsFileResponse) sealed() {}
func (*Ping) sealed() {}
func (*Pong) sealed() {}
func (*QANCollectRequest) sealed() {}
func (*QANCollectResponse) sealed() {}
func (*SetStateRequest) sealed() {}
func (*SetStateResponse) sealed() {}
func (*StartActionRequest) sealed() {}
func (*StartActionResponse) sealed() {}
func (*StartJobRequest) sealed() {}
func (*StartJobResponse) sealed() {}
func (*StateChangedRequest) sealed() {}
func (*StateChangedResponse) sealed() {}
func (*StopActionRequest) sealed() {}
func (*StopActionResponse) sealed() {}
func (*StopJobRequest) sealed() {}
func (*StopJobResponse) sealed() {}
func (*GetVersionsRequest) sealed() {}
func (*GetVersionsResponse) sealed() {}
func (*PBMSwitchPITRRequest) sealed() {}
func (*PBMSwitchPITRResponse) sealed() {}

// check interfaces
var (
// A list of AgentMessage request payloads.
Expand Down Expand Up @@ -211,7 +257,13 @@ var (
_ ServerRequestPayload = (*ParseDefaultsFileRequest)(nil)
)

//go-sumtype:decl AgentParams

// AgentParams is a common interface for AgentProcess and BuiltinAgent parameters.
type AgentParams interface {
proto.Message
sealedAgentParams()
}

func (*SetStateRequest_AgentProcess) sealedAgentParams() {}
func (*SetStateRequest_BuiltinAgent) sealedAgentParams() {}
4 changes: 3 additions & 1 deletion api/agentpb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
)

//go-sumtype:decl isQueryActionValue_Kind

func makeValue(value interface{}) (*QueryActionValue, error) {
// In the future, we may decide to:
// * dereference pointers;
Expand Down Expand Up @@ -95,7 +97,7 @@ func makeValue(value interface{}) (*QueryActionValue, error) {

// use reflection for slices (except []byte) and maps
v := reflect.ValueOf(value)
switch v.Kind() { //nolint:exhaustive
switch v.Kind() {
case reflect.Slice:
size := v.Len()
s := make([]*QueryActionValue, size)
Expand Down
2 changes: 2 additions & 0 deletions api/inventorypb/agents.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package inventorypb

//go-sumtype:decl Agent

// Agent is a common interface for all types of Agents.
type Agent interface {
sealedAgent()
Expand Down
2 changes: 2 additions & 0 deletions api/inventorypb/nodes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package inventorypb

//go-sumtype:decl Node

// Node is a common interface for all types of Nodes.
type Node interface {
sealedNode()
Expand Down
2 changes: 2 additions & 0 deletions api/inventorypb/services.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package inventorypb

//go-sumtype:decl Service

// Service is a common interface for all types of Services.
type Service interface {
sealedService()
Expand Down
1 change: 1 addition & 0 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
replace github.com/go-openapi/spec => github.com/Percona-Lab/spec v0.20.5-percona

require (
github.com/BurntSushi/go-sumtype v0.0.0-20190304192233-fcb4a6205bdc
github.com/Percona-Lab/swagger-order v0.0.0-20191002141859-166b3973d026
github.com/bufbuild/buf v1.5.0
github.com/daixiang0/gci v0.3.4
Expand Down
3 changes: 3 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ github.com/Antonboom/errname v0.1.6 h1:LzIJZlyLOCSu51o3/t2n9Ck7PcoP9wdbrdaW6J8fX
github.com/Antonboom/errname v0.1.6/go.mod h1:7lz79JAnuoMNDAWE9MeeIr1/c/VpSUWatBv2FH9NYpI=
github.com/Antonboom/nilnil v0.1.1 h1:PHhrh5ANKFWRBh7TdYmyyq2gyT2lotnvFvvFbylF81Q=
github.com/Antonboom/nilnil v0.1.1/go.mod h1:L1jBqoWM7AOeTD+tSquifKSesRHs4ZdaxvZR+xdJEaI=
github.com/BurntSushi/go-sumtype v0.0.0-20190304192233-fcb4a6205bdc h1:nvTP+jmloR0+J4YQur/rLRdLcGVEU4SquDgH+Bo7gBY=
github.com/BurntSushi/go-sumtype v0.0.0-20190304192233-fcb4a6205bdc/go.mod h1:7yTWMMG2vOm4ABVciEt4EgNVP7fxwtcKIb/EuiLiKqY=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
Expand Down Expand Up @@ -1393,6 +1395,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190110163146-51295c7ec13a/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190221204921-83362c3779f5/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190307163923-6a08e3108db3/go.mod h1:25r3+/G6/xytQM8iWZKq3Hn0kr0rgFKPUNVEL/dr3z4=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
Expand Down
2 changes: 2 additions & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package tools

import (
_ "github.com/BurntSushi/go-sumtype"
_ "github.com/Percona-Lab/swagger-order"
_ "github.com/bufbuild/buf/cmd/buf"
_ "github.com/daixiang0/gci"
Expand All @@ -29,6 +30,7 @@ import (
//go:generate go build -o ../bin/buf github.com/bufbuild/buf/cmd/buf
//go:generate go build -o ../bin/gci github.com/daixiang0/gci
//go:generate go build -o ../bin/go-consistent github.com/quasilyte/go-consistent
//go:generate go build -o ../bin/go-sumtype github.com/BurntSushi/go-sumtype
//go:generate go build -o ../bin/gofumpt mvdan.cc/gofumpt
//go:generate go build -o ../bin/goimports golang.org/x/tools/cmd/goimports
//go:generate go build -o ../bin/golangci-lint github.com/golangci/golangci-lint/cmd/golangci-lint
Expand Down

0 comments on commit e3484c6

Please sign in to comment.