From fe20e4ab694cdc00f9062f328580b6ef4271ba60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Raddum=20Berg?= Date: Mon, 1 Jan 2024 20:34:08 +0100 Subject: [PATCH] Fix `generateMockRepos` (closes #81) (#82) - add !selector combinator --- .../customize-selected-relations.md | 3 ++ .../scripts/GeneratedAdventureWorks.scala | 1 + .../PurchaseorderdetailRepoMock.scala | 25 ----------------- .../PurchaseorderdetailRepoMock.scala | 27 ------------------ .../PurchaseorderdetailRepoMock.scala | 28 ------------------- typo/src/scala/typo/Selector.scala | 2 ++ .../typo/internal/codegen/FilesTable.scala | 12 ++++---- 7 files changed, 13 insertions(+), 85 deletions(-) delete mode 100644 typo-tester-anorm/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala delete mode 100644 typo-tester-doobie/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala delete mode 100644 typo-tester-zio-jdbc/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala diff --git a/site-in/customization/customize-selected-relations.md b/site-in/customization/customize-selected-relations.md index e97922feaf..2488755af6 100644 --- a/site-in/customization/customize-selected-relations.md +++ b/site-in/customization/customize-selected-relations.md @@ -28,6 +28,9 @@ val mySchema = Selector.schemas("myschema") // picks all relations in schema // heaviest syntax, but most flexible val custom: Selector = relName => relName.schema.exists(_.contains("foo")) && relName.name.contains("bar") + +// can also invert the selector +!Selector.schemas("myschema") // matches everything except schema "myschema" ``` Selectors are also composable: diff --git a/typo-scripts/src/scala/scripts/GeneratedAdventureWorks.scala b/typo-scripts/src/scala/scripts/GeneratedAdventureWorks.scala index 718f5e3169..04797f0136 100644 --- a/typo-scripts/src/scala/scripts/GeneratedAdventureWorks.scala +++ b/typo-scripts/src/scala/scripts/GeneratedAdventureWorks.scala @@ -48,6 +48,7 @@ object GeneratedAdventureWorks { }, enableDsl = true, enableTestInserts = Selector.All, + generateMockRepos = !Selector.relationNames("purchaseorderdetail"), readonlyRepo = Selector.relationNames("purchaseorderdetail") ) val targetSources = buildDir.resolve(s"$projectPath/generated-and-checked-in") diff --git a/typo-tester-anorm/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala b/typo-tester-anorm/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala deleted file mode 100644 index 0e0cee857c..0000000000 --- a/typo-tester-anorm/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala +++ /dev/null @@ -1,25 +0,0 @@ -/** - * File has been automatically generated by `typo`. - * - * IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN. - */ -package adventureworks -package purchasing -package purchaseorderdetail - -import java.sql.Connection -import typo.dsl.SelectBuilder -import typo.dsl.SelectBuilderMock -import typo.dsl.SelectParams - -class PurchaseorderdetailRepoMock(map: scala.collection.mutable.Map[PurchaseorderdetailId, PurchaseorderdetailRow] = scala.collection.mutable.Map.empty) extends PurchaseorderdetailRepo { - override def select: SelectBuilder[PurchaseorderdetailFields, PurchaseorderdetailRow] = { - SelectBuilderMock(PurchaseorderdetailFields, () => map.values.toList, SelectParams.empty) - } - override def selectAll(implicit c: Connection): List[PurchaseorderdetailRow] = { - map.values.toList - } - override def selectById(compositeId: PurchaseorderdetailId)(implicit c: Connection): Option[PurchaseorderdetailRow] = { - map.get(compositeId) - } -} diff --git a/typo-tester-doobie/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala b/typo-tester-doobie/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala deleted file mode 100644 index 554425c870..0000000000 --- a/typo-tester-doobie/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala +++ /dev/null @@ -1,27 +0,0 @@ -/** - * File has been automatically generated by `typo`. - * - * IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN. - */ -package adventureworks -package purchasing -package purchaseorderdetail - -import doobie.free.connection.ConnectionIO -import doobie.free.connection.delay -import fs2.Stream -import typo.dsl.SelectBuilder -import typo.dsl.SelectBuilderMock -import typo.dsl.SelectParams - -class PurchaseorderdetailRepoMock(map: scala.collection.mutable.Map[PurchaseorderdetailId, PurchaseorderdetailRow] = scala.collection.mutable.Map.empty) extends PurchaseorderdetailRepo { - override def select: SelectBuilder[PurchaseorderdetailFields, PurchaseorderdetailRow] = { - SelectBuilderMock(PurchaseorderdetailFields, delay(map.values.toList), SelectParams.empty) - } - override def selectAll: Stream[ConnectionIO, PurchaseorderdetailRow] = { - Stream.emits(map.values.toList) - } - override def selectById(compositeId: PurchaseorderdetailId): ConnectionIO[Option[PurchaseorderdetailRow]] = { - delay(map.get(compositeId)) - } -} diff --git a/typo-tester-zio-jdbc/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala b/typo-tester-zio-jdbc/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala deleted file mode 100644 index 063b12d220..0000000000 --- a/typo-tester-zio-jdbc/generated-and-checked-in/adventureworks/purchasing/purchaseorderdetail/PurchaseorderdetailRepoMock.scala +++ /dev/null @@ -1,28 +0,0 @@ -/** - * File has been automatically generated by `typo`. - * - * IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN. - */ -package adventureworks -package purchasing -package purchaseorderdetail - -import typo.dsl.SelectBuilder -import typo.dsl.SelectBuilderMock -import typo.dsl.SelectParams -import zio.Chunk -import zio.ZIO -import zio.jdbc.ZConnection -import zio.stream.ZStream - -class PurchaseorderdetailRepoMock(map: scala.collection.mutable.Map[PurchaseorderdetailId, PurchaseorderdetailRow] = scala.collection.mutable.Map.empty) extends PurchaseorderdetailRepo { - override def select: SelectBuilder[PurchaseorderdetailFields, PurchaseorderdetailRow] = { - SelectBuilderMock(PurchaseorderdetailFields, ZIO.succeed(Chunk.fromIterable(map.values)), SelectParams.empty) - } - override def selectAll: ZStream[ZConnection, Throwable, PurchaseorderdetailRow] = { - ZStream.fromIterable(map.values) - } - override def selectById(compositeId: PurchaseorderdetailId): ZIO[ZConnection, Throwable, Option[PurchaseorderdetailRow]] = { - ZIO.succeed(map.get(compositeId)) - } -} diff --git a/typo/src/scala/typo/Selector.scala b/typo/src/scala/typo/Selector.scala index 08d9ab2f8e..46420c9478 100644 --- a/typo/src/scala/typo/Selector.scala +++ b/typo/src/scala/typo/Selector.scala @@ -7,6 +7,8 @@ trait Selector { final def or(other: Selector): Selector = rel => include(rel) || other.include(rel) final def and(other: Selector): Selector = rel => include(rel) && other.include(rel) + + final def unary_! : Selector = rel => !include(rel) } object Selector { diff --git a/typo/src/scala/typo/internal/codegen/FilesTable.scala b/typo/src/scala/typo/internal/codegen/FilesTable.scala index b86037656d..08f869515d 100644 --- a/typo/src/scala/typo/internal/codegen/FilesTable.scala +++ b/typo/src/scala/typo/internal/codegen/FilesTable.scala @@ -156,11 +156,13 @@ case class FilesTable(table: ComputedTable, options: InternalOptions, genOrderin } private val maybeMockRepo: Option[sc.File] = - for { - id <- table.maybeId - repoMethods <- table.repoMethods - dbLib <- options.dbLib - } yield relation.RepoMockFile(dbLib, id, repoMethods) + if (options.generateMockRepos.include(table.dbTable.name)) + for { + id <- table.maybeId + repoMethods <- table.repoMethods + dbLib <- options.dbLib + } yield relation.RepoMockFile(dbLib, id, repoMethods) + else None val all: List[sc.File] = List( RowFile,