Enforce new linting rules: errname, errorlint, lll, stylecheck (#32)
This commit is contained in:
parent
ec08a5a918
commit
482dd095a6
16 changed files with 891 additions and 336 deletions
internal/generator
|
@ -1,12 +1,13 @@
|
|||
package generator_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/sunboyy/repogen/internal/code"
|
||||
"github.com/sunboyy/repogen/internal/generator"
|
||||
"github.com/sunboyy/repogen/internal/spec"
|
||||
"github.com/sunboyy/repogen/internal/testutils"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -76,8 +77,16 @@ func TestGenerateMongoRepository(t *testing.T) {
|
|||
Query: spec.QuerySpec{
|
||||
Operator: spec.OperatorAnd,
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{genderField}, Comparator: spec.ComparatorNot, ParamIndex: 1},
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorLessThan, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
Comparator: spec.ComparatorNot,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorLessThan,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -96,7 +105,11 @@ func TestGenerateMongoRepository(t *testing.T) {
|
|||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorLessThanEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorLessThanEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
Sorts: []spec.Sort{
|
||||
|
@ -118,7 +131,11 @@ func TestGenerateMongoRepository(t *testing.T) {
|
|||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorGreaterThan, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorGreaterThan,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
Sorts: []spec.Sort{
|
||||
|
@ -140,7 +157,11 @@ func TestGenerateMongoRepository(t *testing.T) {
|
|||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorGreaterThanEqual, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorGreaterThanEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
Sorts: []spec.Sort{
|
||||
|
@ -163,7 +184,11 @@ func TestGenerateMongoRepository(t *testing.T) {
|
|||
Mode: spec.QueryModeMany,
|
||||
Query: spec.QuerySpec{
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorBetween, ParamIndex: 1},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorBetween,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -184,153 +209,33 @@ func TestGenerateMongoRepository(t *testing.T) {
|
|||
Query: spec.QuerySpec{
|
||||
Operator: spec.OperatorOr,
|
||||
Predicates: []spec.Predicate{
|
||||
{FieldReference: spec.FieldReference{genderField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
|
||||
{FieldReference: spec.FieldReference{ageField}, Comparator: spec.ComparatorEqual, ParamIndex: 2},
|
||||
{
|
||||
FieldReference: spec.FieldReference{genderField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 1,
|
||||
},
|
||||
{
|
||||
FieldReference: spec.FieldReference{ageField},
|
||||
Comparator: spec.ComparatorEqual,
|
||||
ParamIndex: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
expectedBytes, err := os.ReadFile("../../test/generator_test_expected.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expectedCode := string(expectedBytes)
|
||||
|
||||
code, err := generator.GenerateRepository("user", userModel, "UserRepository", methods)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := testutils.ExpectMultiLineString(expectedCode, code); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
expectedCode := `// Code generated by repogen. DO NOT EDIT.
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func NewUserRepository(collection *mongo.Collection) UserRepository {
|
||||
return &UserRepositoryMongo{
|
||||
collection: collection,
|
||||
}
|
||||
}
|
||||
|
||||
type UserRepositoryMongo struct {
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
func (r *UserRepositoryMongo) FindByID(arg0 context.Context, arg1 primitive.ObjectID) (*UserModel, error) {
|
||||
var entity UserModel
|
||||
if err := r.collection.FindOne(arg0, bson.M{
|
||||
"_id": arg1,
|
||||
}, options.FindOne().SetSort(bson.M{})).Decode(&entity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &entity, nil
|
||||
}
|
||||
|
||||
func (r *UserRepositoryMongo) FindByGenderNotAndAgeLessThan(arg0 context.Context, arg1 Gender, arg2 int) (*UserModel, error) {
|
||||
cursor, err := r.collection.Find(arg0, bson.M{
|
||||
"$and": []bson.M{
|
||||
{"gender": bson.M{"$ne": arg1}},
|
||||
{"age": bson.M{"$lt": arg2}},
|
||||
},
|
||||
}, options.Find().SetSort(bson.M{}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var entities []*UserModel
|
||||
if err := cursor.All(arg0, &entities); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entities, nil
|
||||
}
|
||||
|
||||
func (r *UserRepositoryMongo) FindByAgeLessThanEqualOrderByAge(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||
cursor, err := r.collection.Find(arg0, bson.M{
|
||||
"age": bson.M{"$lte": arg1},
|
||||
}, options.Find().SetSort(bson.M{
|
||||
"age": 1,
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var entities []*UserModel
|
||||
if err := cursor.All(arg0, &entities); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entities, nil
|
||||
}
|
||||
|
||||
func (r *UserRepositoryMongo) FindByAgeGreaterThanOrderByAgeAsc(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||
cursor, err := r.collection.Find(arg0, bson.M{
|
||||
"age": bson.M{"$gt": arg1},
|
||||
}, options.Find().SetSort(bson.M{
|
||||
"age": 1,
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var entities []*UserModel
|
||||
if err := cursor.All(arg0, &entities); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entities, nil
|
||||
}
|
||||
|
||||
func (r *UserRepositoryMongo) FindByAgeGreaterThanEqualOrderByAgeDesc(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
||||
cursor, err := r.collection.Find(arg0, bson.M{
|
||||
"age": bson.M{"$gte": arg1},
|
||||
}, options.Find().SetSort(bson.M{
|
||||
"age": -1,
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var entities []*UserModel
|
||||
if err := cursor.All(arg0, &entities); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entities, nil
|
||||
}
|
||||
|
||||
func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, arg2 int) ([]*UserModel, error) {
|
||||
cursor, err := r.collection.Find(arg0, bson.M{
|
||||
"age": bson.M{"$gte": arg1, "$lte": arg2},
|
||||
}, options.Find().SetSort(bson.M{}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var entities []*UserModel
|
||||
if err := cursor.All(arg0, &entities); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entities, nil
|
||||
}
|
||||
|
||||
func (r *UserRepositoryMongo) FindByGenderOrAge(arg0 context.Context, arg1 Gender, arg2 int) ([]*UserModel, error) {
|
||||
cursor, err := r.collection.Find(arg0, bson.M{
|
||||
"$or": []bson.M{
|
||||
{"gender": arg1},
|
||||
{"age": arg2},
|
||||
},
|
||||
}, options.Find().SetSort(bson.M{}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var entities []*UserModel
|
||||
if err := cursor.All(arg0, &entities); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entities, nil
|
||||
}
|
||||
`
|
||||
expectedCodeLines := strings.Split(expectedCode, "\n")
|
||||
actualCodeLines := strings.Split(code, "\n")
|
||||
|
||||
for i, line := range expectedCodeLines {
|
||||
if line != actualCodeLines[i] {
|
||||
t.Errorf("On line %d\nExpected = %v\nActual = %v", i, line, actualCodeLines[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue