diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c4443f6..3c18416 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -29,4 +29,3 @@ jobs: with: flags: unittests token: ${{ secrets.CODECOV_TOKEN }} - exclude: examples diff --git a/codecov.yml b/codecov.yml index affd1a7..cb4361b 100644 --- a/codecov.yml +++ b/codecov.yml @@ -6,3 +6,8 @@ coverage: patch: default: target: 80% + +ignore: + - "examples" + - "internal/teststub" + - "internal/testutils" diff --git a/internal/generator/errors.go b/internal/generator/errors.go new file mode 100644 index 0000000..5d12987 --- /dev/null +++ b/internal/generator/errors.go @@ -0,0 +1,10 @@ +package generator + +import "errors" + +var ( + ErrStructNotFound = errors.New("struct not found") + ErrNotNamedStruct = errors.New("not a named struct") + ErrInterfaceNotFound = errors.New("interface not found") + ErrNotInterface = errors.New("not an interface") +) diff --git a/internal/generator/generator.go b/internal/generator/generator.go index 5644ced..2ab98ae 100644 --- a/internal/generator/generator.go +++ b/internal/generator/generator.go @@ -2,16 +2,80 @@ package generator import ( "go/types" + "log" "github.com/sunboyy/repogen/internal/codegen" "github.com/sunboyy/repogen/internal/mongo" "github.com/sunboyy/repogen/internal/spec" ) -// GenerateRepository generates repository implementation code from repository -// interface specification. -func GenerateRepository(pkg *types.Package, namedStruct *types.Named, - interfaceName string, methodSpecs []spec.MethodSpec) (string, error) { +func GenerateRepositoryImpl(pkg *types.Package, structModelName, + repoInterfaceName string) (string, error) { + + namedStruct, intf, err := deriveSourceTypes(pkg, structModelName, + repoInterfaceName) + if err != nil { + return "", err + } + + methodSpecs, err := constructRepositorySpec(pkg, namedStruct, intf) + if err != nil { + return "", err + } + + codeBuilder, err := constructCodeBuilder(pkg, namedStruct, + repoInterfaceName, methodSpecs) + if err != nil { + return "", err + } + + return codeBuilder.Build() +} + +func deriveSourceTypes(pkg *types.Package, structModelName string, + repositoryInterfaceName string) (*types.Named, *types.Interface, error) { + + structModelObj := pkg.Scope().Lookup(structModelName) + if structModelObj == nil { + return nil, nil, ErrStructNotFound + } + namedStruct := structModelObj.Type().(*types.Named) + if _, ok := namedStruct.Underlying().(*types.Struct); !ok { + return nil, nil, ErrNotNamedStruct + } + + intfObj := pkg.Scope().Lookup(repositoryInterfaceName) + if intfObj == nil { + return nil, nil, ErrInterfaceNotFound + } + intf, ok := intfObj.Type().Underlying().(*types.Interface) + if !ok { + return nil, nil, ErrNotInterface + } + + return namedStruct, intf, nil +} + +func constructRepositorySpec(pkg *types.Package, namedStruct *types.Named, + intf *types.Interface) ([]spec.MethodSpec, error) { + + var methodSpecs []spec.MethodSpec + for i := 0; i < intf.NumMethods(); i++ { + method := intf.Method(i) + log.Println("Generating method:", method.Name()) + + methodSpec, err := spec.ParseInterfaceMethod(pkg, namedStruct, method) + if err != nil { + return nil, err + } + methodSpecs = append(methodSpecs, methodSpec) + } + + return methodSpecs, nil +} + +func constructCodeBuilder(pkg *types.Package, namedStruct *types.Named, + interfaceName string, methodSpecs []spec.MethodSpec) (*codegen.Builder, error) { generator := mongo.NewGenerator(pkg, namedStruct, interfaceName) @@ -23,7 +87,7 @@ func GenerateRepository(pkg *types.Package, namedStruct *types.Named, constructorBuilder, err := generator.GenerateConstructor() if err != nil { - return "", err + return nil, err } codeBuilder.AddImplementer(constructorBuilder) @@ -32,10 +96,10 @@ func GenerateRepository(pkg *types.Package, namedStruct *types.Named, for _, method := range methodSpecs { methodBuilder, err := generator.GenerateMethod(method) if err != nil { - return "", err + return nil, err } codeBuilder.AddImplementer(methodBuilder) } - return codeBuilder.Build() + return codeBuilder, nil } diff --git a/internal/generator/generator_test.go b/internal/generator/generator_test.go index 7bfa8fa..d32adb8 100644 --- a/internal/generator/generator_test.go +++ b/internal/generator/generator_test.go @@ -1,305 +1,27 @@ package generator_test import ( - "go/token" - "go/types" + "errors" "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" ) -func createSignature(params []*types.Var, results []*types.Var) *types.Signature { - return types.NewSignatureType(nil, nil, nil, types.NewTuple(params...), types.NewTuple(results...), false) -} - -func createTypeVar(t types.Type) *types.Var { - return types.NewVar(token.NoPos, nil, "", t) -} +const ( + validStructModelName = "User" + validRepoInterfaceName = "UserRepositoryIntegration" +) -func TestGenerateMongoRepository(t *testing.T) { - methods := []spec.MethodSpec{ - // test find: One mode - { - Name: "FindByID", - Signature: createSignature( - []*types.Var{ - createTypeVar(testutils.TypeContextNamed), - createTypeVar(testutils.TypeObjectIDNamed), - }, - []*types.Var{ - createTypeVar(types.NewPointer(testutils.TypeUserNamed)), - createTypeVar(code.TypeError), - }, - ), - Operation: spec.FindOperation{ - Mode: spec.QueryModeOne, - Query: spec.QuerySpec{ - Predicates: []spec.Predicate{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, - }, - Comparator: spec.ComparatorEqual, - ParamIndex: 1, - }, - }, - }, - }, - }, - // test find: Many mode, And operator, NOT and LessThan comparator - { - Name: "FindByGenderNotAndAgeLessThan", - Signature: createSignature( - []*types.Var{ - createTypeVar(testutils.TypeContextNamed), - createTypeVar(testutils.TypeGenderNamed), - createTypeVar(code.TypeInt), - }, - []*types.Var{ - createTypeVar(types.NewPointer(testutils.TypeUserNamed)), - createTypeVar(code.TypeError), - }, - ), - Operation: spec.FindOperation{ - Mode: spec.QueryModeMany, - Query: spec.QuerySpec{ - Operator: spec.OperatorAnd, - Predicates: []spec.Predicate{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, - }, - Comparator: spec.ComparatorNot, - ParamIndex: 1, - }, - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Comparator: spec.ComparatorLessThan, - ParamIndex: 2, - }, - }, - }, - }, - }, - { - Name: "FindByAgeLessThanEqualOrderByAge", - Signature: createSignature( - []*types.Var{ - createTypeVar(testutils.TypeContextNamed), - createTypeVar(code.TypeInt), - }, - []*types.Var{ - createTypeVar(types.NewSlice(types.NewPointer(testutils.TypeUserNamed))), - createTypeVar(code.TypeError), - }, - ), - Operation: spec.FindOperation{ - Mode: spec.QueryModeMany, - Query: spec.QuerySpec{ - Predicates: []spec.Predicate{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Comparator: spec.ComparatorLessThanEqual, - ParamIndex: 1, - }, - }, - }, - Sorts: []spec.Sort{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Ordering: spec.OrderingAscending, - }, - }, - }, - }, - { - Name: "FindByAgeGreaterThanOrderByAgeAsc", - Signature: createSignature( - []*types.Var{ - createTypeVar(testutils.TypeContextNamed), - createTypeVar(code.TypeInt), - }, - []*types.Var{ - createTypeVar(types.NewSlice(types.NewPointer(testutils.TypeUserNamed))), - createTypeVar(code.TypeError), - }, - ), - Operation: spec.FindOperation{ - Mode: spec.QueryModeMany, - Query: spec.QuerySpec{ - Predicates: []spec.Predicate{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Comparator: spec.ComparatorGreaterThan, - ParamIndex: 1, - }, - }, - }, - Sorts: []spec.Sort{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Ordering: spec.OrderingAscending, - }, - }, - }, - }, - { - Name: "FindByAgeGreaterThanEqualOrderByAgeDesc", - Signature: createSignature( - []*types.Var{ - createTypeVar(testutils.TypeContextNamed), - createTypeVar(code.TypeInt), - }, - []*types.Var{ - createTypeVar(types.NewSlice(types.NewPointer(testutils.TypeUserNamed))), - createTypeVar(code.TypeError), - }, - ), - Operation: spec.FindOperation{ - Mode: spec.QueryModeMany, - Query: spec.QuerySpec{ - Predicates: []spec.Predicate{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Comparator: spec.ComparatorGreaterThanEqual, - ParamIndex: 1, - }, - }, - }, - Sorts: []spec.Sort{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Ordering: spec.OrderingDescending, - }, - }, - }, - }, - { - Name: "FindByAgeBetween", - Signature: createSignature( - []*types.Var{ - createTypeVar(testutils.TypeContextNamed), - createTypeVar(code.TypeInt), - createTypeVar(code.TypeInt), - }, - []*types.Var{ - createTypeVar(types.NewSlice(types.NewPointer(testutils.TypeUserNamed))), - createTypeVar(code.TypeError), - }, - ), - Operation: spec.FindOperation{ - Mode: spec.QueryModeMany, - Query: spec.QuerySpec{ - Predicates: []spec.Predicate{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Comparator: spec.ComparatorBetween, - ParamIndex: 1, - }, - }, - }, - }, - }, - { - Name: "FindByGenderOrAge", - Signature: createSignature( - []*types.Var{ - createTypeVar(testutils.TypeContextNamed), - createTypeVar(testutils.TypeGenderNamed), - createTypeVar(code.TypeInt), - }, - []*types.Var{ - createTypeVar(types.NewSlice(types.NewPointer(testutils.TypeUserNamed))), - createTypeVar(code.TypeError), - }, - ), - Operation: spec.FindOperation{ - Mode: spec.QueryModeMany, - Query: spec.QuerySpec{ - Operator: spec.OperatorOr, - Predicates: []spec.Predicate{ - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, - }, - Comparator: spec.ComparatorEqual, - ParamIndex: 1, - }, - { - FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, - }, - Comparator: spec.ComparatorEqual, - ParamIndex: 2, - }, - }, - }, - }, - }, - } +func TestGenerateRepository_Success(t *testing.T) { expectedBytes, err := os.ReadFile("../../test/generator_test_expected.txt") if err != nil { t.Fatal(err) } expectedCode := string(expectedBytes) - code, err := generator.GenerateRepository(testutils.Pkg, testutils.TypeUserNamed, "UserRepository", methods) + code, err := generator.GenerateRepositoryImpl(testutils.Pkg, validStructModelName, validRepoInterfaceName) if err != nil { t.Fatal(err) @@ -308,3 +30,39 @@ func TestGenerateMongoRepository(t *testing.T) { t.Error(err) } } + +func TestGenerateRepositoryImpl_StructNotFound(t *testing.T) { + _, err := generator.GenerateRepositoryImpl(testutils.Pkg, "UnknownModel", validRepoInterfaceName) + + expectedError := generator.ErrStructNotFound + if !errors.Is(err, expectedError) { + t.Errorf("\nExpected = %+v\nReceived = %+v", expectedError, err) + } +} + +func TestGenerateRepositoryImpl_ModelNameNotStruct(t *testing.T) { + _, err := generator.GenerateRepositoryImpl(testutils.Pkg, "UserRepositoryFind", validRepoInterfaceName) + + expectedError := generator.ErrNotNamedStruct + if !errors.Is(err, expectedError) { + t.Errorf("\nExpected = %+v\nReceived = %+v", expectedError, err) + } +} + +func TestGenerateRepositoryImpl_InterfaceNotFound(t *testing.T) { + _, err := generator.GenerateRepositoryImpl(testutils.Pkg, validStructModelName, "UnknownRepository") + + expectedError := generator.ErrInterfaceNotFound + if !errors.Is(err, expectedError) { + t.Errorf("\nExpected = %+v\nReceived = %+v", expectedError, err) + } +} + +func TestGenerateRepositoryImpl_RepoNameNotInterface(t *testing.T) { + _, err := generator.GenerateRepositoryImpl(testutils.Pkg, validStructModelName, "User") + + expectedError := generator.ErrNotInterface + if !errors.Is(err, expectedError) { + t.Errorf("\nExpected = %+v\nReceived = %+v", expectedError, err) + } +} diff --git a/internal/mongo/count_test.go b/internal/mongo/count_test.go index 82dbd29..7ee79fb 100644 --- a/internal/mongo/count_test.go +++ b/internal/mongo/count_test.go @@ -35,10 +35,7 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -76,20 +73,14 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, }, { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -134,20 +125,14 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, }, { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -190,10 +175,7 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorNot, ParamIndex: 1, @@ -231,10 +213,7 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorLessThan, ParamIndex: 1, @@ -272,10 +251,7 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorLessThanEqual, ParamIndex: 1, @@ -313,10 +289,7 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorGreaterThan, ParamIndex: 1, @@ -354,10 +327,7 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorGreaterThanEqual, ParamIndex: 1, @@ -396,10 +366,7 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorBetween, ParamIndex: 1, @@ -438,10 +405,7 @@ func TestGenerateMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorIn, ParamIndex: 1, diff --git a/internal/mongo/delete_test.go b/internal/mongo/delete_test.go index 3b49277..a495d15 100644 --- a/internal/mongo/delete_test.go +++ b/internal/mongo/delete_test.go @@ -37,10 +37,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, ParamIndex: 1, }, @@ -77,10 +74,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, @@ -119,20 +113,14 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 2, }, @@ -178,20 +166,14 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 2, }, @@ -235,10 +217,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorNot, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, @@ -277,10 +256,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorLessThan, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -319,10 +295,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorLessThanEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -361,10 +334,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorGreaterThan, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -403,10 +373,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorGreaterThanEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -446,10 +413,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorBetween, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -489,10 +453,7 @@ func TestGenerateMethod_Delete(t *testing.T) { { Comparator: spec.ComparatorIn, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, diff --git a/internal/mongo/find_test.go b/internal/mongo/find_test.go index 2d37f89..2dafc57 100644 --- a/internal/mongo/find_test.go +++ b/internal/mongo/find_test.go @@ -37,10 +37,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, ParamIndex: 1, }, @@ -78,10 +75,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, @@ -124,14 +118,8 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), - Tag: `bson:"name"`, - }, - { - Var: testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), - Tag: `bson:"first"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), + testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), }, ParamIndex: 1, }, @@ -171,20 +159,14 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 2, }, @@ -236,20 +218,14 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, { Comparator: spec.ComparatorEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 2, }, @@ -299,10 +275,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorNot, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, @@ -347,10 +320,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorLessThan, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -395,10 +365,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorLessThanEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -443,10 +410,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorGreaterThan, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -491,10 +455,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorGreaterThanEqual, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -540,10 +501,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorBetween, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, }, @@ -589,10 +547,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorIn, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, @@ -637,10 +592,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorNotIn, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, }, @@ -684,10 +636,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorTrue, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), - Tag: `bson:"enabled"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), }, ParamIndex: 1, }, @@ -729,10 +678,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorFalse, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), - Tag: `bson:"enabled"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), }, ParamIndex: 1, }, @@ -774,10 +720,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorExists, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer"), - Tag: `bson:"referrer"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer"), }, ParamIndex: 1, }, @@ -821,10 +764,7 @@ func TestGenerateMethod_Find(t *testing.T) { { Comparator: spec.ComparatorNotExists, FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer"), - Tag: `bson:"referrer"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer"), }, ParamIndex: 1, }, @@ -866,10 +806,7 @@ func TestGenerateMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingAscending, }, @@ -908,10 +845,7 @@ func TestGenerateMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingDescending, }, @@ -950,14 +884,8 @@ func TestGenerateMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), - Tag: `bson:"name"`, - }, - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "First"), - Tag: `bson:"first"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), + testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), }, Ordering: spec.OrderingAscending, }, @@ -996,19 +924,13 @@ func TestGenerateMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Ordering: spec.OrderingAscending, }, { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingDescending, }, @@ -1048,10 +970,7 @@ func TestGenerateMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingDescending, }, diff --git a/internal/mongo/generator_test.go b/internal/mongo/generator_test.go index 46623f3..b1812c3 100644 --- a/internal/mongo/generator_test.go +++ b/internal/mongo/generator_test.go @@ -192,9 +192,7 @@ func TestGenerateMethod_Invalid(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "AccessToken"), - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "AccessToken"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -223,9 +221,7 @@ func TestGenerateMethod_Invalid(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "AccessToken"), - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "AccessToken"), }, Ordering: spec.OrderingAscending, }, @@ -253,9 +249,7 @@ func TestGenerateMethod_Invalid(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "AccessToken"), - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "AccessToken"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, @@ -266,10 +260,7 @@ func TestGenerateMethod_Invalid(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -302,10 +293,7 @@ func TestGenerateMethod_Invalid(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -335,10 +323,7 @@ func TestGenerateMethod_Invalid(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), - Tag: `bson:"consent_history"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), }, ParamIndex: 1, Operator: "APPEND", @@ -349,10 +334,7 @@ func TestGenerateMethod_Invalid(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, diff --git a/internal/mongo/update_test.go b/internal/mongo/update_test.go index 802428a..86d94fe 100644 --- a/internal/mongo/update_test.go +++ b/internal/mongo/update_test.go @@ -38,10 +38,7 @@ func TestGenerateMethod_Update(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -79,10 +76,7 @@ func TestGenerateMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, @@ -93,10 +87,7 @@ func TestGenerateMethod_Update(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -136,10 +127,7 @@ func TestGenerateMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, @@ -150,10 +138,7 @@ func TestGenerateMethod_Update(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - Tag: `bson:"gender"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -193,10 +178,7 @@ func TestGenerateMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), - Tag: `bson:"consent_history"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), }, ParamIndex: 1, Operator: spec.UpdateOperatorPush, @@ -207,10 +189,7 @@ func TestGenerateMethod_Update(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -250,10 +229,7 @@ func TestGenerateMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), - Tag: `bson:"age"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, Operator: spec.UpdateOperatorInc, @@ -264,10 +240,7 @@ func TestGenerateMethod_Update(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -308,20 +281,14 @@ func TestGenerateMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), - Tag: `bson:"enabled"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, }, spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), - Tag: `bson:"consent_history"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), }, ParamIndex: 2, Operator: spec.UpdateOperatorPush, @@ -332,10 +299,7 @@ func TestGenerateMethod_Update(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 3, @@ -378,14 +342,8 @@ func TestGenerateMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), - Tag: `bson:"name"`, - }, - { - Var: testutils.FindStructFieldByName(testutils.TypeNameStruct, "FirstName"), - Tag: `bson:"first"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), + testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, @@ -396,10 +354,7 @@ func TestGenerateMethod_Update(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - { - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), - Tag: `bson:"_id,omitempty"`, - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, diff --git a/internal/spec/parser_test.go b/internal/spec/parser_test.go index 7716a1c..2e4cb2b 100644 --- a/internal/spec/parser_test.go +++ b/internal/spec/parser_test.go @@ -61,7 +61,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorBetween, ParamIndex: 1, @@ -74,7 +74,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorGreaterThan, ParamIndex: 1, @@ -87,7 +87,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorGreaterThanEqual, ParamIndex: 1, @@ -100,7 +100,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorLessThan, ParamIndex: 1, @@ -113,7 +113,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorLessThanEqual, ParamIndex: 1, @@ -126,7 +126,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -141,14 +141,14 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, }, { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -162,7 +162,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorIn, ParamIndex: 1, @@ -175,7 +175,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorNot, ParamIndex: 1, @@ -188,7 +188,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorNotIn, ParamIndex: 1, @@ -203,14 +203,14 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, }, { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -224,7 +224,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -233,7 +233,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingAscending, }, @@ -245,7 +245,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -254,7 +254,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingAscending, }, @@ -266,7 +266,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -275,7 +275,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingDescending, }, @@ -287,7 +287,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -296,13 +296,13 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Ordering: spec.OrderingAscending, }, { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingDescending, }, @@ -314,7 +314,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -323,8 +323,8 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name")}, - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeNameStruct, "First")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), + testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), }, Ordering: spec.OrderingAscending, }, @@ -336,7 +336,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), }, Comparator: spec.ComparatorFalse, ParamIndex: 1, @@ -349,7 +349,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), }, Comparator: spec.ComparatorTrue, ParamIndex: 1, @@ -362,7 +362,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -375,8 +375,8 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name")}, - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeNameStruct, "First")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), + testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -389,9 +389,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{ - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "PhoneNumber"), - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "PhoneNumber"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -404,9 +402,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{ - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer"), - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer"), }, Comparator: spec.ComparatorExists, ParamIndex: 1, @@ -419,8 +415,8 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer")}, - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer"), + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -433,7 +429,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Referrer"), }, Comparator: spec.ComparatorNotExists, ParamIndex: 1, @@ -446,7 +442,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -455,7 +451,7 @@ func TestParseInterfaceMethod_Find(t *testing.T) { Sorts: []spec.Sort{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Ordering: spec.OrderingDescending, }, @@ -495,7 +491,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, ParamIndex: 1, Operator: spec.UpdateOperatorInc, @@ -505,7 +501,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -519,7 +515,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -531,9 +527,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{ - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), }, ParamIndex: 1, Operator: spec.UpdateOperatorPush, @@ -543,7 +537,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -555,16 +549,14 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Enabled"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, }, spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{ - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ConsentHistory"), }, ParamIndex: 2, Operator: spec.UpdateOperatorPush, @@ -574,7 +566,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 3, @@ -586,14 +578,14 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, }, spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, ParamIndex: 2, Operator: spec.UpdateOperatorSet, @@ -603,7 +595,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 3, @@ -615,7 +607,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, @@ -625,7 +617,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -637,7 +629,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, @@ -647,7 +639,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -659,8 +651,8 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Update: spec.UpdateFields{ spec.UpdateField{ FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name")}, - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeNameStruct, "First")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), + testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), }, ParamIndex: 1, Operator: spec.UpdateOperatorSet, @@ -670,7 +662,7 @@ func TestParseInterfaceMethod_Update(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -715,7 +707,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorBetween, ParamIndex: 1, @@ -728,7 +720,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorGreaterThan, ParamIndex: 1, @@ -741,7 +733,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorGreaterThanEqual, ParamIndex: 1, @@ -754,7 +746,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorLessThan, ParamIndex: 1, @@ -767,7 +759,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Age"), }, Comparator: spec.ComparatorLessThanEqual, ParamIndex: 1, @@ -780,7 +772,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -795,14 +787,14 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, }, { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -816,7 +808,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorIn, ParamIndex: 1, @@ -829,7 +821,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorNot, ParamIndex: 1, @@ -844,14 +836,14 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, }, { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 2, @@ -865,7 +857,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "ID"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -878,8 +870,8 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name")}, - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeNameStruct, "First")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), + testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -892,9 +884,7 @@ func TestParseInterfaceMethod_Delete(t *testing.T) { Query: spec.QuerySpec{Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{ - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "PhoneNumber"), - }, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "PhoneNumber"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -939,7 +929,7 @@ func TestParseInterfaceMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -953,8 +943,8 @@ func TestParseInterfaceMethod_Count(t *testing.T) { Predicates: []spec.Predicate{ { FieldReference: spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name")}, - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeNameStruct, "First")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Name"), + testutils.FindStructFieldByName(testutils.TypeNameStruct, "First"), }, Comparator: spec.ComparatorEqual, ParamIndex: 1, @@ -1066,13 +1056,11 @@ func TestParseInterfaceMethod_Find_Invalid(t *testing.T) { // FindByGenderAndCityOrAge spec.NewInvalidQueryError([]string{"Gender", "And", "City", "Or", "Age"}), // FindByGenderFalse - spec.NewIncompatibleComparatorError(spec.ComparatorFalse, code.StructField{ - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - }), + spec.NewIncompatibleComparatorError(spec.ComparatorFalse, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")), // FindByGenderTrue - spec.NewIncompatibleComparatorError(spec.ComparatorTrue, code.StructField{ - Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), - }), + spec.NewIncompatibleComparatorError(spec.ComparatorTrue, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")), // FindByID spec.NewUnsupportedReturnError(testutils.TypeUserNamed, 0), // FindByNameMiddle @@ -1122,7 +1110,7 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) { spec.NewUnsupportedReturnError(code.TypeFloat64, 0), // UpdateCityIncByID spec.NewIncompatibleUpdateOperatorError(spec.UpdateOperatorInc, spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "City")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "City"), }), // UpdateConsentHistoryPushByID spec.NewArgumentTypeNotMatchedError("ConsentHistory", @@ -1139,7 +1127,7 @@ func TestParseInterfaceMethod_Update_Invalid(t *testing.T) { spec.NewUnsupportedReturnError(code.TypeBool, 1), // UpdateGenderPushByID spec.NewIncompatibleUpdateOperatorError(spec.UpdateOperatorPush, spec.FieldReference{ - code.StructField{Var: testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender")}, + testutils.FindStructFieldByName(testutils.TypeUserStruct, "Gender"), }), } diff --git a/internal/teststub/user.go b/internal/teststub/user.go index a736c29..48adec2 100644 --- a/internal/teststub/user.go +++ b/internal/teststub/user.go @@ -9,22 +9,22 @@ import ( type Gender string type User struct { - ID primitive.ObjectID - PhoneNumber string - Gender Gender - City string - Age int - Name Name - Contact Contact - Referrer *User - Enabled bool - ConsentHistory []ConsentHistory + ID primitive.ObjectID `bson:"_id,omitempty"` + PhoneNumber string `bson:"phone_number"` + Gender Gender `bson:"gender"` + City string `bson:"city"` + Age int `bson:"age"` + Name Name `bson:"name"` + Contact Contact `bson:"contact"` + Referrer *User `bson:"referrer"` + Enabled bool `bson:"enabled"` + ConsentHistory []ConsentHistory `bson:"consent_history"` AccessToken string } type Name struct { - First string - Last string + First string `bson:"first"` + Last string `bson:"last"` } type Contact struct { diff --git a/internal/teststub/user_repo_integration.go b/internal/teststub/user_repo_integration.go new file mode 100644 index 0000000..b20ae70 --- /dev/null +++ b/internal/teststub/user_repo_integration.go @@ -0,0 +1,20 @@ +package teststub + +import ( + "context" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +type UserRepositoryIntegration interface { + FindAll(ctx context.Context) ([]*User, error) + FindByAgeLessThanEqualOrderByAge(ctx context.Context, age int) ([]*User, error) + FindByAgeGreaterThanEqualOrderByAgeDesc(ctx context.Context, age int) ([]*User, error) + FindByAgeGreaterThanOrderByAgeAsc(ctx context.Context, age int) ([]*User, error) + FindByAgeBetween(ctx context.Context, ageFrom int, ageTo int) ([]*User, error) + FindByGenderNotAndAgeLessThan(ctx context.Context, gender Gender, age int) ([]*User, error) + FindByGenderOrAge(ctx context.Context, gender Gender, age int) ([]*User, error) + FindByID(ctx context.Context, id primitive.ObjectID) (*User, error) + InsertMany(ctx context.Context, users []*User) ([]interface{}, error) + InsertOne(ctx context.Context, user *User) (interface{}, error) +} diff --git a/internal/testutils/stub_provider.go b/internal/testutils/stub_provider.go index f1e03b6..7feeae9 100644 --- a/internal/testutils/stub_provider.go +++ b/internal/testutils/stub_provider.go @@ -2,7 +2,9 @@ package testutils import ( "go/types" + "reflect" + "github.com/sunboyy/repogen/internal/code" "golang.org/x/tools/go/packages" ) @@ -52,11 +54,17 @@ func init() { TypeConsentHistoryNamed = Pkg.Scope().Lookup("ConsentHistory").Type().(*types.Named) } -func FindStructFieldByName(s *types.Struct, name string) *types.Var { +func FindStructFieldByName(s *types.Struct, name string) code.StructField { + fieldIndex := -1 for i := 0; i < s.NumFields(); i++ { if s.Field(i).Name() == name { - return s.Field(i) + fieldIndex = i + break } } - return nil + + return code.StructField{ + Var: s.Field(fieldIndex), + Tag: reflect.StructTag(s.Tag(fieldIndex)), + } } diff --git a/main.go b/main.go index 10b4371..54c99d0 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,11 @@ import ( "errors" "flag" "fmt" - "go/types" "log" "os" "path/filepath" "github.com/sunboyy/repogen/internal/generator" - "github.com/sunboyy/repogen/internal/spec" "golang.org/x/tools/go/packages" ) @@ -27,7 +25,7 @@ func main() { flag.Usage = printUsage versionPtr := flag.Bool("version", false, "print version of repogen") - pkgDirPtr := flag.String("pkg", ".", "package directory to scan for model struct and repository interface") + pkgPtr := flag.String("pkg", ".", "package directory to scan for model struct and repository interface") destPtr := flag.String("dest", "", "destination file") modelPtr := flag.String("model", "", "model struct name") repoPtr := flag.String("repo", "", "repository interface name") @@ -48,7 +46,13 @@ func main() { log.Fatal("-repo flag required") } - code, err := generateFromRequest(*pkgDirPtr, *modelPtr, *repoPtr) + request := GenerationRequest{ + Pkg: *pkgPtr, + ModelName: *modelPtr, + RepoName: *repoPtr, + Dest: *destPtr, + } + code, err := generateFromRequest(request) if err != nil { panic(err) } @@ -71,6 +75,13 @@ func main() { } } +type GenerationRequest struct { + Pkg string + ModelName string + RepoName string + Dest string +} + func printUsage() { fmt.Println(usageText) flag.PrintDefaults() @@ -80,11 +91,15 @@ func printVersion() { fmt.Println(version) } -func generateFromRequest(pkgDir, structModelName, repositoryInterfaceName string) (string, error) { +var ( + errNoPackageFound = errors.New("no package found") +) + +func generateFromRequest(request GenerationRequest) (string, error) { cfg := packages.Config{ Mode: packages.NeedName | packages.NeedTypes, } - pkgs, err := packages.Load(&cfg, pkgDir) + pkgs, err := packages.Load(&cfg, request.Pkg) if err != nil { return "", err } @@ -94,47 +109,5 @@ func generateFromRequest(pkgDir, structModelName, repositoryInterfaceName string pkg := pkgs[0] - return generateRepository(pkg.Types, structModelName, repositoryInterfaceName) -} - -var ( - errNoPackageFound = errors.New("no package found") - errStructNotFound = errors.New("struct not found") - errNotNamedStruct = errors.New("not a named struct") - errInterfaceNotFound = errors.New("interface not found") - errNotInterface = errors.New("not an interface") -) - -func generateRepository(pkg *types.Package, structModelName, repositoryInterfaceName string) (string, error) { - structModelObj := pkg.Scope().Lookup(structModelName) - if structModelObj == nil { - return "", errStructNotFound - } - namedStruct, ok := structModelObj.Type().(*types.Named) - if !ok { - return "", errNotNamedStruct - } - - intfObj := pkg.Scope().Lookup(repositoryInterfaceName) - if intfObj == nil { - return "", errInterfaceNotFound - } - intf, ok := intfObj.Type().Underlying().(*types.Interface) - if !ok { - return "", errNotInterface - } - - var methodSpecs []spec.MethodSpec - for i := 0; i < intf.NumMethods(); i++ { - method := intf.Method(i) - log.Println("Generating method:", method.Name()) - - methodSpec, err := spec.ParseInterfaceMethod(pkg, namedStruct, method) - if err != nil { - return "", err - } - methodSpecs = append(methodSpecs, methodSpec) - } - - return generator.GenerateRepository(pkg, namedStruct, repositoryInterfaceName, methodSpecs) + return generator.GenerateRepositoryImpl(pkg.Types, request.ModelName, request.RepoName) } diff --git a/test/generator_test_expected.txt b/test/generator_test_expected.txt index 22a270e..2accd30 100644 --- a/test/generator_test_expected.txt +++ b/test/generator_test_expected.txt @@ -10,39 +10,33 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -func NewUserRepository(collection *mongo.Collection) *UserRepositoryMongo { - return &UserRepositoryMongo{ +func NewUserRepositoryIntegration(collection *mongo.Collection) *UserRepositoryIntegrationMongo { + return &UserRepositoryIntegrationMongo{ collection: collection, } } -type UserRepositoryMongo struct { +type UserRepositoryIntegrationMongo struct { collection *mongo.Collection } -func (r *UserRepositoryMongo) FindByID(arg0 context.Context, arg1 primitive.ObjectID) (*User, error) { - var entity User - if err := r.collection.FindOne(arg0, bson.M{ - "_id": arg1, - }, options.FindOne().SetSort(bson.M{})).Decode(&entity); err != nil { +func (r *UserRepositoryIntegrationMongo) FindAll(arg0 context.Context) ([]*User, error) { + cursor, err := r.collection.Find(arg0, bson.M{}, options.Find().SetSort(bson.M{})) + if err != nil { return nil, err } - return &entity, nil + entities := []*User{} + if err := cursor.All(arg0, &entities); err != nil { + return nil, err + } + return entities, nil } -func (r *UserRepositoryMongo) FindByGenderNotAndAgeLessThan(arg0 context.Context, arg1 Gender, arg2 int) (*User, error) { +func (r *UserRepositoryIntegrationMongo) FindByAgeBetween(arg0 context.Context, arg1 int, arg2 int) ([]*User, error) { cursor, err := r.collection.Find(arg0, bson.M{ - "$and": []bson.M{ - { - "gender": bson.M{ - "$ne": arg1, - }, - }, - { - "age": bson.M{ - "$lt": arg2, - }, - }, + "age": bson.M{ + "$gte": arg1, + "$lte": arg2, }, }, options.Find().SetSort(bson.M{})) if err != nil { @@ -55,13 +49,13 @@ func (r *UserRepositoryMongo) FindByGenderNotAndAgeLessThan(arg0 context.Context return entities, nil } -func (r *UserRepositoryMongo) FindByAgeLessThanEqualOrderByAge(arg0 context.Context, arg1 int) ([]*User, error) { +func (r *UserRepositoryIntegrationMongo) FindByAgeGreaterThanEqualOrderByAgeDesc(arg0 context.Context, arg1 int) ([]*User, error) { cursor, err := r.collection.Find(arg0, bson.M{ "age": bson.M{ - "$lte": arg1, + "$gte": arg1, }, }, options.Find().SetSort(bson.M{ - "age": 1, + "age": -1, })) if err != nil { return nil, err @@ -73,7 +67,7 @@ func (r *UserRepositoryMongo) FindByAgeLessThanEqualOrderByAge(arg0 context.Cont return entities, nil } -func (r *UserRepositoryMongo) FindByAgeGreaterThanOrderByAgeAsc(arg0 context.Context, arg1 int) ([]*User, error) { +func (r *UserRepositoryIntegrationMongo) FindByAgeGreaterThanOrderByAgeAsc(arg0 context.Context, arg1 int) ([]*User, error) { cursor, err := r.collection.Find(arg0, bson.M{ "age": bson.M{ "$gt": arg1, @@ -91,13 +85,13 @@ func (r *UserRepositoryMongo) FindByAgeGreaterThanOrderByAgeAsc(arg0 context.Con return entities, nil } -func (r *UserRepositoryMongo) FindByAgeGreaterThanEqualOrderByAgeDesc(arg0 context.Context, arg1 int) ([]*User, error) { +func (r *UserRepositoryIntegrationMongo) FindByAgeLessThanEqualOrderByAge(arg0 context.Context, arg1 int) ([]*User, error) { cursor, err := r.collection.Find(arg0, bson.M{ "age": bson.M{ - "$gte": arg1, + "$lte": arg1, }, }, options.Find().SetSort(bson.M{ - "age": -1, + "age": 1, })) if err != nil { return nil, err @@ -109,11 +103,19 @@ func (r *UserRepositoryMongo) FindByAgeGreaterThanEqualOrderByAgeDesc(arg0 conte return entities, nil } -func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, arg2 int) ([]*User, error) { +func (r *UserRepositoryIntegrationMongo) FindByGenderNotAndAgeLessThan(arg0 context.Context, arg1 Gender, arg2 int) ([]*User, error) { cursor, err := r.collection.Find(arg0, bson.M{ - "age": bson.M{ - "$gte": arg1, - "$lte": arg2, + "$and": []bson.M{ + { + "gender": bson.M{ + "$ne": arg1, + }, + }, + { + "age": bson.M{ + "$lt": arg2, + }, + }, }, }, options.Find().SetSort(bson.M{})) if err != nil { @@ -126,7 +128,7 @@ func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, a return entities, nil } -func (r *UserRepositoryMongo) FindByGenderOrAge(arg0 context.Context, arg1 Gender, arg2 int) ([]*User, error) { +func (r *UserRepositoryIntegrationMongo) FindByGenderOrAge(arg0 context.Context, arg1 Gender, arg2 int) ([]*User, error) { cursor, err := r.collection.Find(arg0, bson.M{ "$or": []bson.M{ { @@ -146,3 +148,33 @@ func (r *UserRepositoryMongo) FindByGenderOrAge(arg0 context.Context, arg1 Gende } return entities, nil } + +func (r *UserRepositoryIntegrationMongo) FindByID(arg0 context.Context, arg1 primitive.ObjectID) (*User, error) { + var entity User + 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 *UserRepositoryIntegrationMongo) InsertMany(arg0 context.Context, arg1 []*User) ([]interface{}, error) { + var entities []interface{} + for _, model := range arg1 { + entities = append(entities, model) + } + result, err := r.collection.InsertMany(arg0, entities) + if err != nil { + return nil, err + } + return result.InsertedIDs, nil +} + +func (r *UserRepositoryIntegrationMongo) InsertOne(arg0 context.Context, arg1 *User) (interface{}, error) { + result, err := r.collection.InsertOne(arg0, arg1) + if err != nil { + return nil, err + } + return result.InsertedID, nil +}