Skip to content

Commit

Permalink
Improvement/tutorial (#12)
Browse files Browse the repository at this point in the history
* update mockery version

* fix typos in Authentication, resource and constructor

* fix route for auth unit tests

* remove error that is always nil

* add TODO to the changes to be done in the session service

* add error management to middlewareJSON

* add CommandInitializer to init configuration keys

* init database configuration keys

* init session configuration keys

* init initialization configuration keys

* init server configuration keys

* init logger configuration keys

* remove unused r.md file in commands

* move time to utils

* create super user when adding the auth controller

* update info controller and routes creation

* change the way badaas server is created

* move test e2e to test_e2e/ and docker to docker/

* do not run auto migration on test e2e execution + automigration refactor

* update docs and developers utils

* remove unnecesary init.sh for cockroach and add health check

* move db connection to orm module and make automigration possible by fx

* info and auth modules now also provide the corresponding services and middlewares

* use gotestsum to run tests + create tests report + tool to install dependencies

* add base models to orm

* add crud services and repositories in orm module

* adapt existing services and controllers to use orm module

* add integration tests

* update documentation

* update changelog

* add support for operators in conditions

* add noteq

* add lt

* add ltoreq

* add gt

* add gtoreq

* add isnull

* add isnotnull

* add is true

* add is not true

* add is false

* add is not false

* add is unknown

* add is not unknown

* add is distict

* add is not distict

* add array in

* add array not in

* add like

* add between

* add not between

* add and

* add or

* add not

* add unsafe conditions

* use all coverage files in sonar

* ignore all tests results

* update changelog

* create mocks for new types

* replace inverse join generation by inverse reference in models

* table class in place of strings for table name

* update to conditions generation that fix embedded names

* refactor: add field identifier

* add possibility to do preload

* support nested preloads

* add a way to know if a relation was loaded or not

* list and nested attributes preload

* update mocks

* update changelog

* refactor: replace gorm.db by query object

* move table to the same file of query

* move unsafe condition to unsafe package

* add details to errors

* add dynamic operators

* add unsafe operators

* update chagelog

* update mocks

* add new linting configuration

* fix new lint errors

* add test e2e to linting

* fix new linting errors in test e2e

* badaas-orm module reorganization

* add readthedocs files

* add golangci-lint to dependencies

* move validators to utils

* move badaas docs to readthedocs

* move badaas-orm docs to readthedocs

* add code of conduct

* add readme badges

* move contributing docs to readthedocs

* add new default logger and update gormzap

* add transaction trace

* add logger config from config file

* move gormdatabase to database and config new logger

* update mocks

* update docs

* add support for other sql databases

* allow runing int tests over multiple databases

* add database specific operators

* update docs

* update changelog

* rename query to gormquery

* change crud repos to orm query api

* new conditions api

* adapt docs to new api

* move gormfx to a independient package

* add badaas integration tests

* add badaas-orm quickstart docs

* add badaas-orm tutorial docs

* cli: tutorial
  • Loading branch information
FrancoLiberali authored Dec 18, 2023
1 parent f459c19 commit 8da267e
Show file tree
Hide file tree
Showing 126 changed files with 5,794 additions and 3,896 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test_integration_postgresql: postgresql
DB=postgresql gotestsum --format testname ./testintegration

test_integration_cockroachdb: cockroachdb
DB=postgresql gotestsum --format testname ./testintegration
DB=postgresql gotestsum --format testname ./testintegration -tags=cockroachdb

test_integration_mysql: mysql
DB=mysql gotestsum --format testname ./testintegration -tags=mysql
Expand Down
201 changes: 99 additions & 102 deletions cli/cmd/gen/conditions/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ import (
)

const (
// badaas/orm
badaasORMFieldIs = "FieldIs"
badaasORMBoolFieldIs = "BoolFieldIs"
badaasORMStringFieldIs = "StringFieldIs"
// badaas/orm/condition
conditionPath = badaasORMPath + "/condition"
badaasORMCondition = "Condition"
badaasORMWhereCondition = "WhereCondition"
badaasORMJoinCondition = "JoinCondition"
badaasORMNewFieldCondition = "NewFieldCondition"
badaasORMNewJoinCondition = "NewJoinCondition"
badaasORMNewCollectionPreload = "NewCollectionPreloadCondition"
badaasORMNewPreloadCondition = "NewPreloadCondition"
// badaas/orm/query
queryPath = badaasORMPath + "/query"
badaasORMFieldIdentifier = "FieldIdentifier"
// badaas/orm/operator
operatorPath = badaasORMPath + "/operator"
badaasORMOperator = "Operator"
// badaas/orm/model
modelPath = badaasORMPath + "/model"
uIntID = "UIntID"
Expand All @@ -33,18 +32,26 @@ const (
uIntModel = "UIntModel"
)

const preloadMethod = "Preload"

type Condition struct {
codes []jen.Code
param *JenParam
destPkg string
fieldIdentifier string
preloadName string
FieldName string
FieldType *jen.Statement
FieldDefinition *jen.Statement
ConditionMethod *jen.Statement
PreloadRelationName string
PreloadRelationMethod *jen.Statement
param *JenParam
destPkg string
modelType string
}

func NewCondition(destPkg string, objectType Type, field Field) *Condition {
condition := &Condition{
param: NewJenParam(),
destPkg: destPkg,
FieldName: field.CompleteName(),
param: NewJenParam(),
destPkg: destPkg,
modelType: getConditionsModelType(objectType.Name()),
}
condition.generate(objectType, field)

Expand Down Expand Up @@ -171,46 +178,56 @@ func (condition *Condition) generateWhere(objectType Type, field Field) {
objectType.Name(),
)

fieldCondition := jen.Qual(
conditionPath, badaasORMNewFieldCondition,
).Types(
objectTypeQual,
condition.param.GenericType(),
conditionName := getConditionName(field) + "Is"
log.Logger.Debugf("Generated %q", conditionName)

condition.createFieldIdentifier(
objectType.Name(), field,
)

whereCondition := jen.Qual(
conditionPath, badaasORMWhereCondition,
fieldIs := jen.Qual(
badaasORMPath, badaasORMFieldIs,
).Types(
objectTypeQual,
condition.param.GenericType(),
)

conditionName := getConditionName(objectType, field)
log.Logger.Debugf("Generated %q", conditionName)
fieldIsCreationValues := jen.Dict{
jen.Id("FieldID"): jen.Id(condition.modelType).Dot(field.CompleteName()),
}

condition.codes = append(
condition.codes,
jen.Func().Id(
conditionName,
).Params(
condition.param.Statement(),
).Add(
whereCondition,
).Block(
jen.Return(
fieldCondition.Call(
condition.createFieldIdentifier(
objectType.Name(), field, conditionName,
),
jen.Id("operator"),
),
),
),
if condition.param.isString {
fieldIsCreationValues = jen.Dict{
jen.Id("FieldIs"): fieldIs.Clone().Values(fieldIsCreationValues),
}
fieldIs = jen.Qual(
badaasORMPath, badaasORMStringFieldIs,
).Types(objectTypeQual)
} else if condition.param.isBool {
fieldIsCreationValues = jen.Dict{
jen.Id("FieldIs"): fieldIs.Clone().Values(fieldIsCreationValues),
}
fieldIs = jen.Qual(
badaasORMPath, badaasORMBoolFieldIs,
).Types(objectTypeQual)
}

condition.ConditionMethod = createMethod(condition.modelType, conditionName).Params().Add(
fieldIs,
).Block(
jen.Return(fieldIs.Clone().Values(fieldIsCreationValues)),
)
}

func createMethod(typeName, methodName string) *jen.Statement {
return jen.Func().Params(
jen.Id(typeName).Id(typeName),
).Id(methodName)
}

// create a variable containing the definition of the field identifier
// to use it in the where condition and in the preload condition
func (condition *Condition) createFieldIdentifier(objectName string, field Field, conditionName string) *jen.Statement {
func (condition *Condition) createFieldIdentifier(objectName string, field Field) {
fieldIdentifierValues := jen.Dict{
jen.Id("ModelType"): jen.Id(getObjectTypeName(objectName)),
jen.Id("Field"): jen.Lit(field.Name),
Expand All @@ -227,24 +244,17 @@ func (condition *Condition) createFieldIdentifier(objectName string, field Field
fieldIdentifierValues[jen.Id("ColumnPrefix")] = jen.Lit(columnPrefix)
}

fieldIdentifierVar := jen.Qual(
condition.FieldType = jen.Id(condition.FieldName).Qual(
queryPath, badaasORMFieldIdentifier,
).Types(
condition.param.GenericType(),
).Values(fieldIdentifierValues)

fieldIdentifierName := conditionName + "Field"

condition.codes = append(
condition.codes,
jen.Var().Id(
fieldIdentifierName,
).Op("=").Add(fieldIdentifierVar),
)

condition.fieldIdentifier = fieldIdentifierName

return jen.Qual("", fieldIdentifierName)
condition.FieldDefinition = jen.Qual(
queryPath, badaasORMFieldIdentifier,
).Types(
condition.param.GenericType(),
).Values(fieldIdentifierValues)
}

// Generate a JoinCondition between the object and field's object
Expand Down Expand Up @@ -282,7 +292,7 @@ func (condition *Condition) generateJoin(objectType Type, field Field, t1Field,
field.TypeName(),
)

conditionName := getConditionName(objectType, field)
conditionName := getConditionName(field)
log.Logger.Debugf("Generated %q", conditionName)

ormT1IJoinCondition := jen.Qual(
Expand All @@ -297,42 +307,36 @@ func (condition *Condition) generateJoin(objectType Type, field Field, t1Field,
t1, t2,
)

condition.codes = append(
condition.codes,
jen.Func().Id(
conditionName,
).Params(
jen.Id("conditions").Op("...").Add(ormT2Condition),
).Add(
ormT1IJoinCondition,
).Block(
jen.Return(
ormJoinCondition.Call(
jen.Id("conditions"),
jen.Lit(field.Name),
jen.Lit(t1Field),
jen.Id(getPreloadAttributesName(objectType.Name())),
jen.Lit(t2Field),
),
condition.ConditionMethod = createMethod(condition.modelType, conditionName).Params(
jen.Id("conditions").Op("...").Add(ormT2Condition),
).Add(
ormT1IJoinCondition,
).Block(
jen.Return(
ormJoinCondition.Call(
jen.Id("conditions"),
jen.Lit(field.Name),
jen.Lit(t1Field),
jen.Id(condition.modelType).Dot(preloadMethod).Call(),
jen.Lit(t2Field),
),
),
)

// preload for the relation
preloadName := getPreloadRelationName(objectType, field)
condition.codes = append(
condition.codes,
jen.Var().Id(
preloadName,
).Op("=").Add(jen.Id(conditionName)).Call(
jen.Id(getPreloadAttributesName(field.TypeName())),
),
condition.setPreloadRelationName(field)

condition.PreloadRelationMethod = createMethod(condition.modelType, condition.PreloadRelationName).Params().Add(
ormT1IJoinCondition,
).Block(
jen.Return(jen.Id(condition.modelType).Dot(conditionName).Call(
jen.Id(field.TypeName()).Dot(preloadMethod).Call(),
)),
)
condition.preloadName = preloadName
}

func getPreloadRelationName(objectType Type, field Field) string {
return objectType.Name() + "Preload" + field.Name
func (condition *Condition) setPreloadRelationName(field Field) {
condition.PreloadRelationName = "Preload" + field.Name
}

func (condition *Condition) generateCollectionPreload(objectType Type, field Field) {
Expand All @@ -358,32 +362,25 @@ func (condition *Condition) generateCollectionPreload(objectType Type, field Fie
t1, t2,
)

preloadName := getPreloadRelationName(objectType, field)

condition.codes = append(
condition.codes,
jen.Func().Id(
preloadName,
).Params(
jen.Id("nestedPreloads").Op("...").Add(ormT2IJoinCondition),
).Add(
ormT1Condition,
).Block(
jen.Return(
ormNewCollectionPreload.Call(
jen.Lit(field.Name),
jen.Id("nestedPreloads"),
),
condition.setPreloadRelationName(field)

condition.PreloadRelationMethod = createMethod(condition.modelType, condition.PreloadRelationName).Params(
jen.Id("nestedPreloads").Op("...").Add(ormT2IJoinCondition),
).Add(
ormT1Condition,
).Block(
jen.Return(
ormNewCollectionPreload.Call(
jen.Lit(field.Name),
jen.Id("nestedPreloads"),
),
),
)

condition.preloadName = preloadName + "()"
}

// Generate condition names
func getConditionName(typeV Type, field Field) string {
return typeV.Name() + strcase.ToPascal(field.NamePrefix) + strcase.ToPascal(field.Name)
func getConditionName(field Field) string {
return strcase.ToPascal(field.NamePrefix) + strcase.ToPascal(field.Name)
}

// Avoid importing the same package as the destination one
Expand Down
Loading

0 comments on commit 8da267e

Please sign in to comment.