Skip to content

Commit

Permalink
Add RecordSchema.reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
Primetalk committed Feb 11, 2024
1 parent 1e93438 commit 94ef844
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ object Product extends TableBuilder:
val idNameSchema = fields(id, name)
val primaryKeySchema = fields(id)

val fullSchema = infer[TableSchema]

object Order extends TableBuilder:
object id extends column[Int]
object date extends column[LocalDateTime]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object RecordProperty0:
at match
case Record[p] => Record[p]
case Scalar[p] => p
case _ => Nothing
case _ => Nothing

trait PropertiesBuilder extends RecordSchemaBuilderBase:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,6 @@ object RecordSchema:
// case SchemaCons[`P`, s] => `P` *: s#Values => RecordProperty0.PropertyValueType[P]
// case SchemaCons[_, s] => PropertyGetter[s, P]

// IsPropertyInSchema[P, Schema] match
// case true =>
// Schema#Values => RecordProperty0.PropertyValueType[P]
// case false =>
// Schema#Values => Nothing

// // DOESN'T WORK
// transparent inline def removeDoesntWork
// [P1 <: RecordProperty0, S <: RecordSchema]
Expand All @@ -471,6 +465,22 @@ object RecordSchema:
// case SchemaCons(`p1`, s): SchemaCons[P1, st] => s
// case SchemaCons(p, s): SchemaCons[pt, st] => SchemaCons(p, removeDoesntWork(p1, s))

type Reverse0[S <: RecordSchema, Accum <: RecordSchema] <: RecordSchema = S match
case EmptySchema => Accum
case SchemaCons[p, s] =>
Reverse0[s, SchemaCons[p, Accum]]

type Reverse[S <: RecordSchema] = Reverse0[S, EmptySchema]

transparent inline def reverse0[S <: RecordSchema, Accum <: RecordSchema](s: S, accum: Accum): Reverse0[S, Accum] =
s match
case _ : EmptySchema => accum
case sc : SchemaCons[p, s] =>
reverse0[s, SchemaCons[p,Accum]](sc.schema, SchemaCons(sc.p, accum))

transparent inline def reverse[S <: RecordSchema](s: S): Reverse[S] =
reverse0(s, EmptySchema)

def showExprImpl(a: Expr[Any])(using Quotes): Expr[String] =
Expr(a.show)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ trait SchemaBuilder extends RecordSchemaBuilderBase:
def fieldsReverseImpl[S <: RecordSchema](
propertyList: Expr[Seq[RecordProperty0]],
schemaExpr: Expr[S]
)(using Type[S])(using Quotes): Expr[RecordSchema] =
)(using st: Type[S])(using Quotes): Expr[RecordSchema] =
propertyList match
case Varargs(Seq()) =>
schemaExpr
case Varargs(Seq('{ $a: ta }, as*)) =>
val expr = fieldsReverseImpl(Varargs(as), '{ RecordSchema.prepend(${ a }, ${ schemaExpr }) })
expr
case Varargs(seq) =>
seq match
case Seq() =>
schemaExpr
case Seq('{ $a: at }, as*) => // здесь важно сохранить тип, чтобы
fieldsReverseImpl(Varargs(as), '{ RecordSchema.prepend(${ a }, ${ schemaExpr }) })

def fieldsImpl[S <: RecordSchema](
propertyList: Expr[Seq[RecordProperty0]],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.primetalk.typed.ontology.simple.relalg

// import cats.Map
import cats.Functor
import cats.FlatMap
import cats.Foldable
Expand Down

0 comments on commit 94ef844

Please sign in to comment.