-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
346eda2
commit 0410bce
Showing
96 changed files
with
3,289 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: Open string enums | ||
--- | ||
|
||
Some people like to use tables and foreign keys to encode enums. It'll typically take this form: | ||
|
||
```sql | ||
create table title (code text primary key); | ||
insert into title (code) values ('mr'), ('ms'), ('dr'), ('phd'); | ||
|
||
create table titledperson | ||
( | ||
title text not null references title, | ||
name text not null | ||
); | ||
|
||
``` | ||
|
||
You can configure typo to generate so-called "open enums" for you. | ||
|
||
```scala | ||
val options = Options( | ||
// ... | ||
openEnums = Selector.relationNames("title") | ||
) | ||
``` | ||
|
||
|
||
And typo will output the [Primary key type](./id-types.md) as something like this: | ||
|
||
```scala | ||
/** Type for the primary key of table `public.title`. It has some known values: | ||
* - dr | ||
* - mr | ||
* - ms | ||
* - phd | ||
*/ | ||
sealed abstract class TitleId(val value: String) | ||
|
||
object TitleId { | ||
def apply(underlying: String): TitleId = | ||
ByName.getOrElse(underlying, Unknown(underlying)) | ||
case object dr extends TitleId("dr") | ||
case object mr extends TitleId("mr") | ||
case object ms extends TitleId("ms") | ||
case object phd extends TitleId("phd") | ||
case class Unknown(override val value: String) extends TitleId(value) | ||
val All: List[TitleId] = List(dr, mr, ms, phd) | ||
val ByName: Map[String, TitleId] = All.map(x => (x.value, x)).toMap | ||
|
||
// type class instances | ||
} | ||
``` | ||
|
||
## Supported data types | ||
|
||
Currently, typo supports the following data types for open enums: | ||
- `text` | ||
- a domain which has `text` as its base type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
typo-tester-anorm/generated-and-checked-in/adventureworks/public/title/TitleFields.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* File has been automatically generated by `typo`. | ||
* | ||
* IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN. | ||
*/ | ||
package adventureworks | ||
package public | ||
package title | ||
|
||
import typo.dsl.Path | ||
import typo.dsl.SqlExpr.FieldLikeNoHkt | ||
import typo.dsl.SqlExpr.IdField | ||
import typo.dsl.Structure.Relation | ||
|
||
trait TitleFields { | ||
def code: IdField[TitleId, TitleRow] | ||
} | ||
|
||
object TitleFields { | ||
lazy val structure: Relation[TitleFields, TitleRow] = | ||
new Impl(Nil) | ||
|
||
private final class Impl(val _path: List[Path]) | ||
extends Relation[TitleFields, TitleRow] { | ||
|
||
override lazy val fields: TitleFields = new TitleFields { | ||
override def code = IdField[TitleId, TitleRow](_path, "code", None, None, x => x.code, (row, value) => row.copy(code = value)) | ||
} | ||
|
||
override lazy val columns: List[FieldLikeNoHkt[?, TitleRow]] = | ||
List[FieldLikeNoHkt[?, TitleRow]](fields.code) | ||
|
||
override def copy(path: List[Path]): Impl = | ||
new Impl(path) | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
typo-tester-anorm/generated-and-checked-in/adventureworks/public/title/TitleId.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* File has been automatically generated by `typo`. | ||
* | ||
* IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN. | ||
*/ | ||
package adventureworks | ||
package public | ||
package title | ||
|
||
import anorm.Column | ||
import anorm.ParameterMetaData | ||
import anorm.ToStatement | ||
import java.sql.Types | ||
import play.api.libs.json.JsValue | ||
import play.api.libs.json.Reads | ||
import play.api.libs.json.Writes | ||
|
||
/** Type for the primary key of table `public.title`. It has some known values: | ||
* - dr | ||
* - mr | ||
* - ms | ||
* - phd | ||
*/ | ||
sealed abstract class TitleId(val value: String) | ||
|
||
object TitleId { | ||
def apply(underlying: String): TitleId = | ||
ByName.getOrElse(underlying, Unknown(underlying)) | ||
case object dr extends TitleId("dr") | ||
case object mr extends TitleId("mr") | ||
case object ms extends TitleId("ms") | ||
case object phd extends TitleId("phd") | ||
case class Unknown(override val value: String) extends TitleId(value) | ||
val All: List[TitleId] = List(dr, mr, ms, phd) | ||
val ByName: Map[String, TitleId] = All.map(x => (x.value, x)).toMap | ||
|
||
implicit lazy val arrayColumn: Column[Array[TitleId]] = Column.columnToArray[String](Column.columnToString, implicitly).map(_.map(TitleId.apply)) | ||
implicit lazy val arrayToStatement: ToStatement[Array[TitleId]] = ToStatement.arrayToParameter(ParameterMetaData.StringParameterMetaData).contramap(_.map(_.value)) | ||
implicit lazy val column: Column[TitleId] = Column.columnToString.map(TitleId.apply) | ||
implicit lazy val ordering: Ordering[TitleId] = Ordering.by(_.value) | ||
implicit lazy val parameterMetadata: ParameterMetaData[TitleId] = new ParameterMetaData[TitleId] { | ||
override def sqlType: String = "text" | ||
override def jdbcType: Int = Types.OTHER | ||
} | ||
implicit lazy val reads: Reads[TitleId] = Reads[TitleId]{(value: JsValue) => value.validate(Reads.StringReads).map(TitleId.apply)} | ||
implicit lazy val text: Text[TitleId] = new Text[TitleId] { | ||
override def unsafeEncode(v: TitleId, sb: StringBuilder) = Text.stringInstance.unsafeEncode(v.value, sb) | ||
override def unsafeArrayEncode(v: TitleId, sb: StringBuilder) = Text.stringInstance.unsafeArrayEncode(v.value, sb) | ||
} | ||
implicit lazy val toStatement: ToStatement[TitleId] = ToStatement.stringToStatement.contramap(_.value) | ||
implicit lazy val writes: Writes[TitleId] = Writes[TitleId](value => Writes.StringWrites.writes(value.value)) | ||
} |
Oops, something went wrong.