Skip to content

Commit

Permalink
Merge pull request #69 from kubukoz/timestamp-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Aug 10, 2022
2 parents ec170dc + 398fa98 commit 1990eb3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
10 changes: 4 additions & 6 deletions core/src/main/scala/playground/NodeEncoderVisitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ object NodeEncoderVisitor extends SchemaVisitor[NodeEncoder] { self =>
case PDouble => int.contramap(_.toInt) // todo: wraps
case PDocument => document
case PFloat => unsupported("float")
case PUnit =>
// todo: inconsistent with decoder (takes everything)
_ => obj(Nil)
case PUUID => string.contramap(_.toString())
case PByte => unsupported("byte")
case PTimestamp => string.contramap(_.toString)
case PUnit => struct(shapeId, hints, Vector.empty, _ => ())
case PUUID => string.contramap(_.toString())
case PByte => unsupported("byte")
case PTimestamp => string.contramap(_.toString)
}

def collection[C[_], A](
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/playground/QueryCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ object QueryCompiler extends SchemaVisitor[PartialCompiler] {
PartialCompiler
.typeCheck(NodeKind.Bool) { case b @ BooleanLiteral(_) => b }
.map(_.value.value)
case PUnit => _ => ().rightIor
case PUnit => struct(shapeId, hints, Vector.empty, _ => ())
case PInt =>
PartialCompiler
.typeCheck(NodeKind.IntLiteral) { case i @ IntLiteral(_) => i }
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/playground/smithyql/AST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import cats.Applicative
import cats.data.NonEmptyList
import smithy4s.ShapeId
import cats.Show
import cats.kernel.Eq
import cats.Id

sealed trait AST[F[_]] extends Product with Serializable {
def mapK[G[_]: Functor](fk: F ~> G): AST[G]
def kind: NodeKind
}

object AST {
implicit val astIdEq: Eq[AST[Id]] = Eq.fromUniversalEquals
}

sealed trait InputNode[F[_]] extends AST[F] {

def fold[A](
Expand Down
55 changes: 55 additions & 0 deletions core/src/test/scala/playground/NodeEncoderTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package playground

import cats.Id
import demo.smithy.Good
import playground.NodeEncoder
import playground.smithyql.AST
import playground.smithyql.DSL._
import smithy4s.schema.Schema
import weaver._
import demo.smithy.Hero
import smithy4s.Timestamp
import smithy.api.TimestampFormat

object NodeEncoderTests extends FunSuite {

def assertEncodes[A](
schema: Schema[A],
value: A,
expected: AST[Id],
)(
implicit loc: SourceLocation
) = {
val enc = NodeEncoder.derive(schema)

assert.eql(enc.toNode(value), expected)
}

test("unit") {
assertEncodes(Schema.unit, (), struct())
}

test("String") {
assertEncodes(Schema.string, "test", "test")
}

test("int") {
assertEncodes(Schema.int, 42, 42)
}

test("simple struct") {
assertEncodes(Good.schema, Good(42), struct("howGood" -> 42))
}

test("union") {
assertEncodes(Hero.schema, Hero.GoodCase(Good(42)), struct("good" -> struct("howGood" -> 42)))
}

test("timestamp") {
assertEncodes(
Schema.timestamp,
Timestamp.parse("2022-07-11T17:42:28Z", TimestampFormat.DATE_TIME).get,
"2022-07-11T17:42:28Z",
)
}
}
24 changes: 24 additions & 0 deletions core/src/test/scala/playground/smithyql/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ object CompilationTests extends SimpleIOSuite with Checkers {
assert(result == Ior.leftNec(e))
}

pureTest("unit") {
assert(
compile {
WithSource.liftId(struct().mapK(WithSource.liftId))
}(Schema.unit).isRight
)
}

pureTest("unit - doesn't accept string") {
assert(
compile {
WithSource.liftId("test".mapK(WithSource.liftId))
}(Schema.unit).isLeft
)
}

pureTest("unit - doesn't accept struct with a field present") {
assert(
compile {
WithSource.liftId(struct("test" -> 42).mapK(WithSource.liftId))
}(Schema.unit).isBoth
)
}

pureTest("string") {
assert(
compile {
Expand Down

0 comments on commit 1990eb3

Please sign in to comment.