From 73c8f167b3210a0390dba0f26bb9aeef621d6738 Mon Sep 17 00:00:00 2001 From: Travis Brown Date: Tue, 21 Apr 2020 09:48:31 +0200 Subject: [PATCH] Minor renaming and rearrangements (#51) * More imports API changes * Beta-normalize before alpha-normalization in tests * Update README --- README.md | 2 +- .../scala/org/dhallj/imports/ResolveImports.scala | 12 ++++++------ .../src/main/scala/org/dhallj/imports/package.scala | 7 ------- .../scala/org/dhallj/imports/syntax/package.scala | 7 +++++++ .../org/dhallj/imports/ImportResolutionSuite.scala | 11 ++++++----- .../tests/acceptance/AcceptanceSuccessSuite.scala | 7 +++---- .../tests/acceptance/ImportResolutionSuite.scala | 4 ++-- 7 files changed, 25 insertions(+), 25 deletions(-) delete mode 100644 modules/imports/src/main/scala/org/dhallj/imports/package.scala create mode 100644 modules/imports/src/main/scala/org/dhallj/imports/syntax/package.scala diff --git a/README.md b/README.md index 4c97ee3c..dda4546d 100644 --- a/README.md +++ b/README.md @@ -406,7 +406,7 @@ It requires a bit of ceremony to set up: ```scala import cats.effect.{ContextShift, IO, Resource} import org.dhallj.core.Expr -import org.dhallj.imports._ +import org.dhallj.imports.syntax._ import org.dhallj.parser.DhallParser import org.http4s.client.Client import org.http4s.client.blaze.BlazeClientBuilder diff --git a/modules/imports/src/main/scala/org/dhallj/imports/ResolveImports.scala b/modules/imports/src/main/scala/org/dhallj/imports/ResolveImports.scala index c55bfe45..51f2d208 100644 --- a/modules/imports/src/main/scala/org/dhallj/imports/ResolveImports.scala +++ b/modules/imports/src/main/scala/org/dhallj/imports/ResolveImports.scala @@ -4,23 +4,23 @@ import cats.effect.Sync import org.dhallj.core.Expr import org.http4s.client.Client -object ResolveImports { - def apply[F[_] <: AnyRef](expr: Expr)(implicit Client: Client[F], F: Sync[F]): F[Expr] = +object Resolver { + def resolve[F[_] <: AnyRef](expr: Expr)(implicit Client: Client[F], F: Sync[F]): F[Expr] = F.flatMap(ResolveImportsVisitor[F])(expr.accept(_)) - def apply[F[_] <: AnyRef]( + def resolve[F[_] <: AnyRef]( semanticCache: ImportCache[F], semiSemanticCache: ImportCache[F] )(expr: Expr)(implicit Client: Client[F], F: Sync[F]): F[Expr] = expr.accept(ResolveImportsVisitor[F](semanticCache, semiSemanticCache)) - def apply[F[_] <: AnyRef]( + def resolve[F[_] <: AnyRef]( semanticCache: ImportCache[F] )(expr: Expr)(implicit Client: Client[F], F: Sync[F]): F[Expr] = - expr.accept(ResolveImportsVisitor[F](semanticCache, new ImportCache.NoopImportCache)) + expr.accept(ResolveImportsVisitor[F](semanticCache, new ImportCache.NoopImportCache[F])) final class Ops(val expr: Expr) extends AnyVal { def resolveImports[F[_] <: AnyRef](implicit Client: Client[F], F: Sync[F]): F[Expr] = - ResolveImports.apply[F](expr) + resolve[F](expr) } } diff --git a/modules/imports/src/main/scala/org/dhallj/imports/package.scala b/modules/imports/src/main/scala/org/dhallj/imports/package.scala deleted file mode 100644 index 413d6097..00000000 --- a/modules/imports/src/main/scala/org/dhallj/imports/package.scala +++ /dev/null @@ -1,7 +0,0 @@ -package org.dhallj - -import org.dhallj.core.Expr - -package object imports { - implicit def toResolveImportOps(expr: Expr): ResolveImports.Ops = new ResolveImports.Ops(expr) -} diff --git a/modules/imports/src/main/scala/org/dhallj/imports/syntax/package.scala b/modules/imports/src/main/scala/org/dhallj/imports/syntax/package.scala new file mode 100644 index 00000000..820df74e --- /dev/null +++ b/modules/imports/src/main/scala/org/dhallj/imports/syntax/package.scala @@ -0,0 +1,7 @@ +package org.dhallj.imports + +import org.dhallj.core.Expr + +package object syntax { + implicit def toResolveImportOps(expr: Expr): Resolver.Ops = new Resolver.Ops(expr) +} diff --git a/modules/imports/src/test/scala/org/dhallj/imports/ImportResolutionSuite.scala b/modules/imports/src/test/scala/org/dhallj/imports/ImportResolutionSuite.scala index e3fa02e6..2912c6b9 100644 --- a/modules/imports/src/test/scala/org/dhallj/imports/ImportResolutionSuite.scala +++ b/modules/imports/src/test/scala/org/dhallj/imports/ImportResolutionSuite.scala @@ -8,6 +8,7 @@ import cats.implicits._ import munit.FunSuite import org.dhallj.core.Expr import org.dhallj.core.binary.Decode +import org.dhallj.imports.syntax._ import org.dhallj.parser.DhallParser.parse import org.http4s.client._ import org.http4s.client.blaze._ @@ -149,7 +150,7 @@ class ImportResolutionSuite extends FunSuite { val expr = parse( "classpath:/hashed/package.dhall sha256:d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15" ) - val expected = parse("let x = 1 in x").alphaNormalize.normalize + val expected = parse("let x = 1 in x").normalize.alphaNormalize assert(resolve(expr) == expected) } @@ -159,7 +160,7 @@ class ImportResolutionSuite extends FunSuite { val expr = parse( "let x = classpath:/hashed/package.dhall sha256:e60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15 in x" ) - val expected = parse("let x = 1 in x").alphaNormalize.normalize + val expected = parse("let x = 1 in x").normalize.alphaNormalize assert(resolve(expr) == expected) } @@ -168,7 +169,7 @@ class ImportResolutionSuite extends FunSuite { val cache = InMemoryCache() val expected = parse("let x = 2 in x") - val encoded = expected.alphaNormalize.normalize.getEncodedBytes + val encoded = expected.normalize.alphaNormalize.getEncodedBytes val hash = MessageDigest.getInstance("SHA-256").digest(encoded) val expr = @@ -184,7 +185,7 @@ class ImportResolutionSuite extends FunSuite { val cached = parse("let x = 1 in x") val expected = parse("let x = 2 in x") - val encoded = cached.alphaNormalize.normalize.getEncodedBytes + val encoded = cached.normalize.alphaNormalize.getEncodedBytes val hash = MessageDigest.getInstance("SHA-256").digest(expected.normalize.getEncodedBytes) //Hash doesn't match what is stored val expr = @@ -199,7 +200,7 @@ class ImportResolutionSuite extends FunSuite { val cache = InMemoryCache() val expected = parse("let x = 2 in x") - val encoded = expected.alphaNormalize.normalize.getEncodedBytes + val encoded = expected.normalize.alphaNormalize.getEncodedBytes val hash = MessageDigest.getInstance("SHA-256").digest(encoded) val expr = parse( diff --git a/tests/src/main/scala/org/dhallj/tests/acceptance/AcceptanceSuccessSuite.scala b/tests/src/main/scala/org/dhallj/tests/acceptance/AcceptanceSuccessSuite.scala index 62b92120..0693ff60 100644 --- a/tests/src/main/scala/org/dhallj/tests/acceptance/AcceptanceSuccessSuite.scala +++ b/tests/src/main/scala/org/dhallj/tests/acceptance/AcceptanceSuccessSuite.scala @@ -5,10 +5,9 @@ import cats.effect.{ContextShift, IO} import java.nio.file.{Files, Paths} import org.dhallj.core.Expr import org.dhallj.core.binary.Decode.decode -import org.dhallj.imports.mini.Resolver import org.dhallj.parser.DhallParser -import org.dhallj.imports.{ImportCache, ResolveImports} +import org.dhallj.imports.{ImportCache, Resolver} import org.http4s.client._ import org.http4s.client.blaze._ @@ -59,7 +58,7 @@ trait CachedResolvingInput extends Input[Expr] { implicit val cs: ContextShift[IO] = IO.contextShift(global) BlazeClientBuilder[IO](global).resource.use { client => implicit val c: Client[IO] = client - ResolveImports(parsed) + Resolver.resolve[IO](parsed) }.unsafeRunSync } } @@ -76,7 +75,7 @@ trait ResolvingInput extends Input[Expr] { implicit val cs: ContextShift[IO] = IO.contextShift(global) BlazeClientBuilder[IO](global).resource.use { client => implicit val c: Client[IO] = client - ResolveImports(new ImportCache.NoopImportCache[IO], new ImportCache.NoopImportCache[IO])(parsed) + Resolver.resolve[IO](new ImportCache.NoopImportCache[IO], new ImportCache.NoopImportCache[IO])(parsed) }.unsafeRunSync } } diff --git a/tests/src/main/scala/org/dhallj/tests/acceptance/ImportResolutionSuite.scala b/tests/src/main/scala/org/dhallj/tests/acceptance/ImportResolutionSuite.scala index ab82d76f..790317b8 100644 --- a/tests/src/main/scala/org/dhallj/tests/acceptance/ImportResolutionSuite.scala +++ b/tests/src/main/scala/org/dhallj/tests/acceptance/ImportResolutionSuite.scala @@ -5,7 +5,7 @@ import java.nio.file.Files import cats.effect.{ContextShift, IO} import org.dhallj.core.Expr -import org.dhallj.imports.{ImportCache, ResolveImports} +import org.dhallj.imports.{ImportCache, Resolver} import org.dhallj.parser.DhallParser import org.http4s.client._ @@ -29,7 +29,7 @@ class ImportResolutionSuite(val base: String) val cache = initializeCache BlazeClientBuilder[IO](global).resource.use { client => implicit val c: Client[IO] = client - ResolveImports[IO](cache, new ImportCache.NoopImportCache[IO])(parsed) + Resolver.resolve[IO](cache, new ImportCache.NoopImportCache[IO])(parsed) }.unsafeRunSync } }