Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update page update dsl #33

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object QueryDatabase extends ZIOAppDefault {
)

def example: ZIO[Notion, NotionError, Unit] = {
val filter = number("Col1") >= 10 and date("Col2") <= LocalDate.of(2022, 2, 2)
val filter = $"Col1".asNumber >= 10 and $"Col2".asDate <= LocalDate.of(2022, 2, 2)
val sorts = $"Col1".descending andThen createdTime

for {
Expand Down
6 changes: 3 additions & 3 deletions examples/update-page/src/main/scala/example/UpdatePage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend

import zio._
import zio.notion._
import zio.notion.dsl._
import zio.notion.model.page.Page
import zio.notion.model.page.patch.PatchedProperty._

import java.time.LocalDate

Expand All @@ -20,8 +20,8 @@ object UpdatePage extends ZIOAppDefault {

for {
patch0 <- Right(page.patch)
patch1 <- patch0.updateProperty(PatchedNumber.ceil.on("Col1"))
patch2 <- patch1.updateProperty(PatchedDate.between(date, date.plusDays(14)).on("Col2"))
patch1 <- patch0.updateProperty($"col1".asNumber.patch.ceil)
patch2 <- patch1.updateProperty($"col2".asDate.patch.between(date, date.plusDays(14)))
} yield patch2.archive
}

Expand Down
2 changes: 1 addition & 1 deletion zio-notion-core/src/main/scala/zio/notion/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object Main extends ZIOAppDefault {
val configuration: NotionConfiguration = NotionConfiguration(bearer = "secret_dnjrnOCfZBOiKsITF8AFDNL5QwYYHF5t7Rysbl0Mfzd")

val sorts: Sorts = $"Name".ascending
val filter: Filter = title("Name").startsWith("a")
val filter: Filter = $"Name".asTitle.startsWith("a")

def app: ZIO[Notion, NotionError, Unit] =
for {
Expand Down
50 changes: 11 additions & 39 deletions zio-notion-core/src/main/scala/zio/notion/PropertyUpdater.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,20 @@ object PropertyUpdater {
final case class One(key: String) extends ColumnMatcher
}

final case class FieldSetter[T <: PatchedProperty](matcher: ColumnMatcher, value: T) extends PropertyUpdater[Nothing, T]
final case class FieldUpdater[+E, T <: PatchedProperty](matcher: ColumnMatcher, transform: T => Either[E, T])
extends PropertyUpdater[E, T]

type UTransformation[P <: PatchedProperty] = Transformation[Nothing, P]

trait Patch[E, P <: PatchedProperty]

trait Transformation[E, P <: PatchedProperty] extends Patch[E, P] { self =>
def transform(property: P): Either[E, P]

def map(f: P => P): Transformation[E, P] = Transformation(p => transform(p).map(f))

def andThen(next: Transformation[E, P]): Transformation[E, P] = date => transform(date).flatMap(curr => next.transform(curr))

def on(fieldName: String): FieldUpdater[E, P] = FieldUpdater(ColumnMatcher.One(fieldName), transform)

def onAll: FieldUpdater[E, P] = FieldUpdater(ColumnMatcher.Predicate(_ => true), transform)

def onAllMatching(predicate: String => Boolean): FieldUpdater[E, P] = FieldUpdater(ColumnMatcher.Predicate(predicate), transform)
final case class FieldSetter[P <: PatchedProperty](matcher: ColumnMatcher, value: P) extends PropertyUpdater[Nothing, P] {
def map(f: P => P): FieldSetter[P] = FieldSetter(matcher, f(value))
}

object Transformation {
def apply[E, P <: PatchedProperty](f: P => Either[E, P]): Transformation[E, P] = (property: P) => f(property)

def succeed[P <: PatchedProperty](f: P => P): UTransformation[P] = Transformation(p => Right(f(p)))
final case class FieldUpdater[+E, P <: PatchedProperty](
matcher: ColumnMatcher,
f: P => Either[E, P]
) extends PropertyUpdater[E, P] {
def map(g: P => P): FieldUpdater[E, P] = FieldUpdater(matcher, property => f(property).map(g))
}

trait Setter[P <: PatchedProperty] extends Patch[Nothing, P] {
protected def build(): P
final def value: P = build()

def map(f: P => P): Setter[P] = Setter(f(value))

final def on(fieldName: String): FieldSetter[P] = FieldSetter(ColumnMatcher.One(fieldName), value)

def onAll: FieldSetter[P] = FieldSetter(ColumnMatcher.Predicate(_ => true), value)

def onAllMatching(predicate: String => Boolean): FieldSetter[P] = FieldSetter(ColumnMatcher.Predicate(predicate), value)
}
type UFieldUpdater[P <: PatchedProperty] = FieldUpdater[Nothing, P]

object Setter {
def apply[P <: PatchedProperty](p: P): Setter[P] = () => p
object FieldUpdater {
def succeed[E, P <: PatchedProperty](matcher: ColumnMatcher, f: P => P): FieldUpdater[E, P] =
FieldUpdater(matcher, property => Right(f(property)))
}
}
273 changes: 251 additions & 22 deletions zio-notion-core/src/main/scala/zio/notion/dsl/Column.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
package zio.notion.dsl

import zio.notion.PropertyUpdater.ColumnMatcher
import zio.notion.model.database.patch.PatchPlan
import zio.notion.model.database.patch.PatchPlan.PropertyType

trait Definition {
def patchPlan: PatchPlan
def matcher: ColumnMatcher
}

final case class ColumnDefinition(colName: String, patchPlan: PatchPlan) extends Definition {
def rename(name: String): ColumnDefinition = copy(patchPlan = patchPlan.copy(name = Some(name)))

def as(propertyType: PropertyType): ColumnDefinition = copy(patchPlan = patchPlan.copy(propertyType = Some(propertyType)))

override def matcher: ColumnMatcher = ColumnMatcher.One(colName)
}

final case class ColumnDefinitions(predicate: String => Boolean, patchPlan: PatchPlan) extends Definition {
def as(propertyType: PropertyType): ColumnDefinitions = copy(patchPlan = patchPlan.copy(propertyType = Some(propertyType)))

override def matcher: ColumnMatcher = ColumnMatcher.Predicate(predicate)
final case class ColumnDefinition(colName: String) {
def patch: PatchedColumnDefinition = PatchedColumnDefinition(colName, PatchPlan.unit)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package zio.notion.dsl

import zio.notion.model.database.patch.PatchPlan

final case class ColumnDefinitions(predicate: String => Boolean) {
def patch: PatchedColumnDefinitions = PatchedColumnDefinitions(predicate, PatchPlan.unit)
}
83 changes: 83 additions & 0 deletions zio-notion-core/src/main/scala/zio/notion/dsl/Columns.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package zio.notion.dsl

import zio.notion.PropertyUpdater.ColumnMatcher.Predicate
import zio.notion.dsl.Columns._
import zio.notion.dsl.PatchedColumn._

final case class Columns(predicate: String => Boolean) {
def definition: ColumnDefinitions = columnDefinitionsMatching(predicate)

def asNumber: NumberDSLConstructor = NumberDSLConstructor(predicate)

def asTitle: TitleDSLConstructor = TitleDSLConstructor(predicate)

def asRichText: RichTextDSLConstructor = RichTextDSLConstructor(predicate)

def asCheckbox: CheckboxDSLConstructor = CheckboxDSLConstructor(predicate)

def asSelect: SelectDSLConstructor = SelectDSLConstructor(predicate)

def asMultiSelect: MultiSelectDSLConstructor = MultiSelectDSLConstructor(predicate)

def asDate: DateDSLConstructor = DateDSLConstructor(predicate)

def asPeople: PeopleDSLConstructor = PeopleDSLConstructor(predicate)

def asFiles: FilesDSLConstructor = FilesDSLConstructor(predicate)

def asUrl: UrlDSLConstructor = UrlDSLConstructor(predicate)

def asEmail: EmailDSLConstructor = EmailDSLConstructor(predicate)

def asPhoneNumber: PhoneNumberDSLConstructor = PhoneNumberDSLConstructor(predicate)
}

object Columns {
final case class TitleDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnTitle = PatchedColumnTitle(Predicate(predicate))
}

final case class RichTextDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnRichText = PatchedColumnRichText(Predicate(predicate))
}

final case class NumberDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnNumber = PatchedColumnNumber(Predicate(predicate))
}

final case class CheckboxDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnCheckbox = PatchedColumnCheckbox(Predicate(predicate))
}

final case class SelectDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnSelect = PatchedColumnSelect(Predicate(predicate))
}

final case class MultiSelectDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnMultiSelect = PatchedColumnMultiSelect(Predicate(predicate))
}

final case class DateDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnDate = PatchedColumnDate(Predicate(predicate))
}

final case class PeopleDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnPeople = PatchedColumnPeople(Predicate(predicate))
}

final case class FilesDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnFiles = PatchedColumnFiles(Predicate(predicate))
}

final case class UrlDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnUrl = PatchedColumnUrl(Predicate(predicate))
}

final case class EmailDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnEmail = PatchedColumnEmail(Predicate(predicate))
}

final case class PhoneNumberDSLConstructor private (predicate: String => Boolean) {
def patch: PatchedColumnPhoneNumber = PatchedColumnPhoneNumber(Predicate(predicate))
}
}
Loading