Skip to content

Commit

Permalink
tools: Add linter rule for goconst. (#398)
Browse files Browse the repository at this point in the history
- RELATED ISSUE(S):
Resolves #403

- DESCRIPTION:
Enables the goconst linter and and resolves all it's errors.

This was one of the remaining linters we didn't have enabled from the list of recommended ones that were mentioned in the book 100 go mistakes and how to avoid them.

One side affect of this PR was that schema package was decoupled from using the parser package.
  • Loading branch information
shahzadlone authored May 20, 2022
1 parent fd76c5c commit ec3334a
Show file tree
Hide file tree
Showing 22 changed files with 350 additions and 272 deletions.
2 changes: 1 addition & 1 deletion cli/defradb/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (o BaseLoggerOptions) toLogConfig() logging.Config {
var encoderFormat logging.EncoderFormatOption
if o.EncoderFormat != nil {
switch *o.EncoderFormat {
case "json":
case "json": // nolint:goconst
encoderFormat = logging.NewEncoderFormatOption(logging.JSON)
case "csv":
encoderFormat = logging.NewEncoderFormatOption(logging.CSV)
Expand Down
6 changes: 4 additions & 2 deletions cli/defradb/cmd/serverdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/sourcenetwork/defradb/db"
)

const databaseStoreName string = "badger"

// dumpCmd represents the dump command
var srvDumpCmd = &cobra.Command{
Use: "server-dump",
Expand All @@ -38,7 +40,7 @@ var srvDumpCmd = &cobra.Command{

var rootstore ds.Batching
var err error
if config.Database.Store == "badger" {
if config.Database.Store == databaseStoreName {
log.Info(
ctx,
"opening badger store",
Expand Down Expand Up @@ -69,7 +71,7 @@ func init() {
rootCmd.AddCommand(srvDumpCmd)
srvDumpCmd.Flags().String(
"store",
"badger",
databaseStoreName,
"Specify the data store to use (supported: badger, memory)",
)
}
3 changes: 2 additions & 1 deletion db/collection_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
query "github.com/ipfs/go-datastore/query"
ipld "github.com/ipfs/go-ipld-format"
dag "github.com/ipfs/go-merkledag"
parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/core"
Expand Down Expand Up @@ -227,7 +228,7 @@ func (c *collection) deleteWithFilter(
}

// Extract the dockey in the string format from the document value.
docKey := query.Value()[parser.DocKeyFieldName].(string)
docKey := query.Value()[parserTypes.DocKeyFieldName].(string)

// Convert from string to client.DocKey.
key := core.PrimaryDataStoreKey{
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ require (
golang.org/x/net v0.0.0-20220516155154-20f960328961 // indirect
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3 // indirect
Expand Down
10 changes: 7 additions & 3 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ func (l *logger) ApplyConfig(config Config) {
}

func buildZapLogger(name string, config Config) (*zap.Logger, error) {
const (
encodingTypeConsole string = "console"
encodingTypeJSON string = "json"
)
defaultConfig := zap.NewProductionConfig()
defaultConfig.Encoding = "console"
defaultConfig.Encoding = encodingTypeConsole
defaultConfig.EncoderConfig.ConsoleSeparator = ", "
defaultConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
defaultConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
Expand All @@ -143,10 +147,10 @@ func buildZapLogger(name string, config Config) (*zap.Logger, error) {

if config.EncoderFormat.HasValue {
if config.EncoderFormat.EncoderFormat == JSON {
defaultConfig.Encoding = "json"
defaultConfig.Encoding = encodingTypeJSON
defaultConfig.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
} else if config.EncoderFormat.EncoderFormat == CSV {
defaultConfig.Encoding = "console"
defaultConfig.Encoding = encodingTypeConsole
}
}

Expand Down
17 changes: 9 additions & 8 deletions query/graphql/parser/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"errors"

"github.com/graphql-go/graphql/language/ast"
parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types"
)

type CommitType int
Expand Down Expand Up @@ -44,16 +45,16 @@ type CommitSelect struct {
FieldName string
Cid string

Limit *Limit
OrderBy *OrderBy
Limit *parserTypes.Limit
OrderBy *parserTypes.OrderBy

Fields []Selection

Statement *ast.Field
}

func (c CommitSelect) GetRoot() SelectionType {
return CommitSelection
func (c CommitSelect) GetRoot() parserTypes.SelectionType {
return parserTypes.CommitSelection
}

func (c CommitSelect) GetStatement() ast.Node {
Expand All @@ -80,7 +81,7 @@ func (c CommitSelect) ToSelect() *Select {
OrderBy: c.OrderBy,
Statement: c.Statement,
Fields: c.Fields,
Root: CommitSelection,
Root: parserTypes.CommitSelection,
}
}

Expand All @@ -101,13 +102,13 @@ func parseCommitSelect(field *ast.Field) (*CommitSelect, error) {

for _, argument := range field.Arguments {
prop := argument.Name.Value
if prop == "dockey" {
if prop == parserTypes.DocKey {
raw := argument.Value.(*ast.StringValue)
commit.DocKey = raw.Value
} else if prop == "cid" {
} else if prop == parserTypes.Cid {
raw := argument.Value.(*ast.StringValue)
commit.Cid = raw.Value
} else if prop == "field" {
} else if prop == parserTypes.Field {
raw := argument.Value.(*ast.StringValue)
commit.FieldName = raw.Value
}
Expand Down
17 changes: 10 additions & 7 deletions query/graphql/parser/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import (

"github.com/SierraSoftworks/connor"
"github.com/graphql-go/graphql/language/ast"

gqlp "github.com/graphql-go/graphql/language/parser"
gqls "github.com/graphql-go/graphql/language/source"

parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types"
)

type EvalContext struct {
Expand Down Expand Up @@ -80,20 +83,20 @@ type parseFn func(*ast.ObjectValue) (interface{}, error)
// This function is mostly used by the Sort parser, which needs to parse
// conditions in the same way as the Filter object, however the order
// of the arguments is important.
func ParseConditionsInOrder(stmt *ast.ObjectValue) ([]SortCondition, error) {
func ParseConditionsInOrder(stmt *ast.ObjectValue) ([]parserTypes.SortCondition, error) {
cond, err := parseConditionsInOrder(stmt)
if err != nil {
return nil, err
}

if v, ok := cond.([]SortCondition); ok {
if v, ok := cond.([]parserTypes.SortCondition); ok {
return v, nil
}
return nil, errors.New("Failed to parse statement")
}

func parseConditionsInOrder(stmt *ast.ObjectValue) (interface{}, error) {
conditions := make([]SortCondition, 0)
conditions := make([]parserTypes.SortCondition, 0)
if stmt == nil {
return conditions, nil
}
Expand All @@ -106,16 +109,16 @@ func parseConditionsInOrder(stmt *ast.ObjectValue) (interface{}, error) {

switch v := val.(type) {
case string: // base direction parsed (hopefully, check NameToSortDirection)
dir, ok := NameToSortDirection[v]
dir, ok := parserTypes.NameToSortDirection[v]
if !ok {
return nil, errors.New("Invalid sort direction string")
}
conditions = append(conditions, SortCondition{
conditions = append(conditions, parserTypes.SortCondition{
Field: name,
Direction: dir,
})

case []SortCondition: // flatten and incorporate the parsed slice into our current one
case []parserTypes.SortCondition: // flatten and incorporate the parsed slice into our current one
for _, cond := range v {
// prepend the current field name, to the parsed condition from the slice
// Eg. sort: {author: {name: ASC, birthday: DESC}}
Expand Down Expand Up @@ -171,7 +174,7 @@ func parseConditions(stmt *ast.ObjectValue) (interface{}, error) {
if err != nil {
return nil, err
}
if strings.HasPrefix(name, "_") && name != "_key" {
if strings.HasPrefix(name, "_") && name != parserTypes.DocKeyFieldName {
name = strings.Replace(name, "_", "$", 1)
}
conditions[name] = val
Expand Down
45 changes: 24 additions & 21 deletions query/graphql/parser/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ package parser
import (
"testing"

"github.com/graphql-go/graphql/language/ast"
gqlp "github.com/graphql-go/graphql/language/parser"

parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types"

"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/source"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -688,7 +691,7 @@ func runRunFilterTest(t *testing.T, test testCase) {
assert.True(t, passed == test.shouldPass, "Test Case faild: %s", test.description)
}

func getQuerySortObject(query string) (*OrderBy, error) {
func getQuerySortObject(query string) (*parserTypes.OrderBy, error) {
source := source.NewSource(&source.Source{
Body: []byte(query),
Name: "",
Expand All @@ -704,7 +707,7 @@ func getQuerySortObject(query string) (*OrderBy, error) {
if err != nil {
return nil, err
}
return &OrderBy{
return &parserTypes.OrderBy{
Conditions: conditions,
Statement: sortObj,
}, nil
Expand All @@ -715,7 +718,7 @@ func TestParseConditionsInOrder_Empty(t *testing.T) {
query {
users(order: {})
}`),
[]SortCondition{},
[]parserTypes.SortCondition{},
)
}

Expand All @@ -724,10 +727,10 @@ func TestParseConditionsInOrder_Simple(t *testing.T) {
query {
users(order: {name: ASC})
}`),
[]SortCondition{
[]parserTypes.SortCondition{
{
Field: "name",
Direction: ASC,
Direction: parserTypes.ASC,
},
},
)
Expand All @@ -738,14 +741,14 @@ func TestParseConditionsInOrder_Simple_Multiple(t *testing.T) {
query {
users(order: {name: ASC, date: DESC})
}`),
[]SortCondition{
[]parserTypes.SortCondition{
{
Field: "name",
Direction: ASC,
Direction: parserTypes.ASC,
},
{
Field: "date",
Direction: DESC,
Direction: parserTypes.DESC,
},
},
)
Expand All @@ -756,10 +759,10 @@ func TestParseConditionsInOrder_Embedded(t *testing.T) {
query {
users(order: {author: {name: ASC}})
}`),
[]SortCondition{
[]parserTypes.SortCondition{
{
Field: "author.name",
Direction: ASC,
Direction: parserTypes.ASC,
},
},
)
Expand All @@ -770,14 +773,14 @@ func TestParseConditionsInOrder_Embedded_Multiple(t *testing.T) {
query {
users(order: {author: {name: ASC, birthday: DESC}})
}`),
[]SortCondition{
[]parserTypes.SortCondition{
{
Field: "author.name",
Direction: ASC,
Direction: parserTypes.ASC,
},
{
Field: "author.birthday",
Direction: DESC,
Direction: parserTypes.DESC,
},
},
)
Expand All @@ -788,10 +791,10 @@ func TestParseConditionsInOrder_Embedded_Nested(t *testing.T) {
query {
users(order: {author: {address: {street_name: DESC}}})
}`),
[]SortCondition{
[]parserTypes.SortCondition{
{
Field: "author.address.street_name",
Direction: DESC,
Direction: parserTypes.DESC,
},
},
)
Expand All @@ -802,24 +805,24 @@ func TestParseConditionsInOrder_Complex(t *testing.T) {
query {
users(order: {name: ASC, author: {birthday: ASC, address: {street_name: DESC}}})
}`),
[]SortCondition{
[]parserTypes.SortCondition{
{
Field: "name",
Direction: ASC,
Direction: parserTypes.ASC,
},
{
Field: "author.birthday",
Direction: ASC,
Direction: parserTypes.ASC,
},
{
Field: "author.address.street_name",
Direction: DESC,
Direction: parserTypes.DESC,
},
},
)
}

func runParseConditionInOrderTest(t *testing.T, query string, target []SortCondition) {
func runParseConditionInOrderTest(t *testing.T, query string, target []parserTypes.SortCondition) {
sort, err := getQuerySortObject(query)
assert.NoError(t, err)

Expand Down
Loading

0 comments on commit ec3334a

Please sign in to comment.