gormchecker

package module
v0.0.0-...-b0d27a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2020 License: MIT Imports: 5 Imported by: 0

README

gormchecker

gormchecker is a static analysis tool which can find wrong gorm using logic. Current state is alphe version. Only check pipeed function( make pipe is originally not wrong but my working team stop to use to make review easier).

Install

You can get gormchecker by go get command.

$ go get -u github.com/gosagawa/gormchecker/cmd/gormchecker

How to use

gormchecker run with go vet as below when Go is 1.12 and higher.

$ go vet -vettool=$(which gormchecker) your_check_path

When Go is lower than 1.12, just run gormchecker command with the package name (import path).

$ gormchecker your_check_path

Example

This is expected function.

func f1() (*User, error) {

	db, err := getConnection()
	if err != nil {
		return nil, err
	}

	u := User{}
	db = db.Where("column_a = xxx")
	db = db.Where("column_b = xxx")
	db = db.Find(&u)

	if db.RecordNotFound() {
		return nil, nil
	}
	return &u, db.Error
}

This code is piped. It is grammatically correct, but want stop to use to make review easier.

// using pipe
func f2() (*User, error) {

	db, err := getConnection()
	if err != nil {
		return nil, err
	}

	u := User{}
	db = db.Where("column_a = xxx").Where("column_b = xxx") // want "do not use pipe"
	db = db.Find(&u)

	if db.RecordNotFound() {
		return nil, nil
	}
	return &u, db.Error
}

Allow pipe function except db. Now only check paramater name is "db" or not...

// not db pipe
// TODO:(sagawa) strictly check gorm.DB or not
func f3() (*User, error) {

	xdb, err := getConnection()
	if err != nil {
		return nil, err
	}

	u := User{}
	xdb = xdb.Where("column_a = xxx").Where("column_b = xxx") // ok
	xdb = xdb.Find(&u)

	if xdb.RecordNotFound() {
		return nil, nil
	}
	return &u, xdb.Error
}

TODO

  • check db.Find(&u) only ( db = db.Find(&u) is correct )
  • check First or Find not exist function
  • check Not using function paramter

Documentation

Index

Constants

View Source
const Doc = "gormchecker is analyzer for gorm using funtion"

Doc is explain for gormchecker

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "gormchecker",
	Doc:  Doc,
	Run:  run,
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Analyzer is analizer for gormchecker

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL