fix: implement various deepsource suggestions

This commit is contained in:
Vikram Rangnekar
2020-06-15 10:16:47 -04:00
parent 06214a3850
commit dd4accfdd2
26 changed files with 76 additions and 86 deletions

View File

@ -146,7 +146,7 @@ func (al *List) Load() ([]Item, error) {
return parse(string(b), al.filepath)
}
func parse(b string, filename string) ([]Item, error) {
func parse(b, filename string) ([]Item, error) {
var items []Item
var s scanner.Scanner

View File

@ -14,7 +14,7 @@ func TestGQLName1(t *testing.T) {
name := QueryName(q)
if len(name) != 0 {
if name != "" {
t.Fatal("Name should be empty, not ", name)
}
}

View File

@ -774,7 +774,7 @@ func (c *compilerContext) renderBaseSelect(sel *qcode.Select, ti *DBTableInfo, r
case ti.IsSingular:
io.WriteString(c.w, ` LIMIT ('1') :: integer`)
case len(sel.Paging.Limit) != 0:
case sel.Paging.Limit != "":
//fmt.Fprintf(w, ` LIMIT ('%s') :: integer`, c.sel.Paging.Limit)
io.WriteString(c.w, ` LIMIT ('`)
io.WriteString(c.w, sel.Paging.Limit)
@ -787,7 +787,7 @@ func (c *compilerContext) renderBaseSelect(sel *qcode.Select, ti *DBTableInfo, r
io.WriteString(c.w, ` LIMIT ('20') :: integer`)
}
if len(sel.Paging.Offset) != 0 {
if sel.Paging.Offset != "" {
//fmt.Fprintf(w, ` OFFSET ('%s') :: integer`, c.sel.Paging.Offset)
io.WriteString(c.w, ` OFFSET ('`)
io.WriteString(c.w, sel.Paging.Offset)
@ -1011,11 +1011,8 @@ func (c *compilerContext) renderExp(ex *qcode.Exp, ti *DBTableInfo, skipNested b
return err
}
} else {
//fmt.Fprintf(w, `(("%s"."%s") `, c.sel.Name, val.Col)
if err := c.renderOp(val, ti); err != nil {
return err
}
} else if err := c.renderOp(val, ti); err != nil {
return err
}
}
//qcode.FreeExp(val)
@ -1077,7 +1074,7 @@ func (c *compilerContext) renderOp(ex *qcode.Exp, ti *DBTableInfo) error {
return nil
}
if len(ex.Col) != 0 {
if ex.Col != "" {
if col, ok = ti.ColMap[ex.Col]; !ok {
return fmt.Errorf("no column '%s' found ", ex.Col)
}
@ -1317,7 +1314,7 @@ func funcPrefixLen(fm map[string]*DBFunction, fn string) int {
return 0
}
func hasBit(n uint32, pos uint32) bool {
func hasBit(n, pos uint32) bool {
val := n & (1 << pos)
return (val > 0)
}

View File

@ -381,25 +381,25 @@ func withFragment3(t *testing.T) {
compileGQLToPSQL(t, gql, nil, "anon")
}
func withInlineFragment(t *testing.T) {
gql := `
query {
users {
... on users {
id
email
}
created_at
... on user {
first_name
last_name
}
}
}
`
// func withInlineFragment(t *testing.T) {
// gql := `
// query {
// users {
// ... on users {
// id
// email
// }
// created_at
// ... on user {
// first_name
// last_name
// }
// }
// }
// `
compileGQLToPSQL(t, gql, nil, "anon")
}
// compileGQLToPSQL(t, gql, nil, "anon")
// }
func withCursor(t *testing.T) {
gql := `query {

View File

@ -328,7 +328,7 @@ func (s *DBSchema) secondDegreeRels(t DBTable, cols []DBColumn) error {
for i := range cols {
c := cols[i]
if len(c.FKeyTable) == 0 {
if c.FKeyTable == "" {
continue
}

View File

@ -121,12 +121,10 @@ func (c *compilerContext) renderUpdateStmt(w io.Writer, qc *qcode.QCode, item re
}
io.WriteString(w, `)`)
} else {
if qc.Selects[0].Where != nil {
io.WriteString(w, `WHERE `)
if err := c.renderWhere(&qc.Selects[0], ti); err != nil {
return err
}
} else if qc.Selects[0].Where != nil {
io.WriteString(w, `WHERE `)
if err := c.renderWhere(&qc.Selects[0], ti); err != nil {
return err
}
}

View File

@ -141,8 +141,7 @@ func (l *lexer) current() (Pos, Pos) {
func (l *lexer) emit(t itemType) {
l.items = append(l.items, item{t, l.start, l.pos, l.line})
// Some items contain text internally. If so, count their newlines.
switch t {
case itemStringVal:
if t == itemStringVal {
for i := l.start; i < l.pos; i++ {
if l.input[i] == '\n' {
l.line++
@ -404,15 +403,15 @@ func isAlphaNumeric(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
}
func equals(b []byte, s Pos, e Pos, val []byte) bool {
func equals(b []byte, s, e Pos, val []byte) bool {
return bytes.EqualFold(b[s:e], val)
}
func contains(b []byte, s Pos, e Pos, chars string) bool {
func contains(b []byte, s, e Pos, chars string) bool {
return bytes.ContainsAny(b[s:e], chars)
}
func lowercase(b []byte, s Pos, e Pos) {
func lowercase(b []byte, s, e Pos) {
for i := s; i < e; i++ {
if b[i] >= 'A' && b[i] <= 'Z' {
b[i] = ('a' + (b[i] - 'A'))

View File

@ -574,10 +574,8 @@ func (p *Parser) parseList() (*Node, error) {
}
if ty == 0 {
ty = node.Type
} else {
if ty != node.Type {
return nil, errors.New("All values in a list must be of the same type")
}
} else if ty != node.Type {
return nil, errors.New("All values in a list must be of the same type")
}
node.Parent = parent
nodes = append(nodes, node)

View File

@ -386,7 +386,7 @@ func (com *Compiler) compileQuery(qc *QCode, op *Operation, role string) error {
s.Type = STUnion
}
if len(field.Alias) != 0 {
if field.Alias != "" {
s.FieldName = field.Alias
} else {
s.FieldName = s.Name
@ -628,14 +628,12 @@ func (com *Compiler) compileArgNode(st *util.Stack, node *Node, usePool bool) (*
}
// Objects inside a list
if len(node.Name) == 0 {
if node.Name == "" {
pushChildren(st, node.exp, node)
continue
} else {
if _, ok := com.bl[node.Name]; ok {
continue
}
} else if _, ok := com.bl[node.Name]; ok {
continue
}
ex, err := newExp(st, node, usePool)
@ -1049,7 +1047,7 @@ func setWhereColName(ex *Exp, node *Node) {
if n.Type != NodeObj {
continue
}
if len(n.Name) != 0 {
if n.Name != "" {
k := n.Name
if k == "and" || k == "or" || k == "not" ||
k == "_and" || k == "_or" || k == "_not" {