Skip to content
This repository has been archived by the owner on Mar 16, 2020. It is now read-only.

Commit

Permalink
Configurable accessors container style (#45)
Browse files Browse the repository at this point in the history
Configurable accessors container style
  • Loading branch information
ioleo committed Nov 1, 2019
1 parent 0863ed3 commit fd5d362
Show file tree
Hide file tree
Showing 13 changed files with 781 additions and 340 deletions.
32 changes: 16 additions & 16 deletions build.sbt
Expand Up @@ -38,8 +38,8 @@ ThisBuild / publishTo := sonatypePublishToBundle.value

addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")
addCommandAlias("testJVM", ";coreExamplesJVM/test;testExamplesJVM/test")
addCommandAlias("testJS", ";coreExamplesJS/test;testExamplesJS/test")
addCommandAlias("testJVM", ";coreExamplesJVM/compile;testExamplesJVM/compile;coreTestsJVM/test;testTestsJVM/test")
addCommandAlias("testJS", ";coreExamplesJS/compile;testExamplesJS/compile;coreTestsJS/test;testTestsJS/test")
addCommandAlias("testRelease", ";set every isSnapshot := false;+clean;+compile")

lazy val root = project
Expand All @@ -58,45 +58,45 @@ lazy val root = project
test.jvm,
test.js,
testExamples.jvm,
testExamples.js
testExamples.js,
testTests.jvm,
testTests.js
)
.enablePlugins(ScalaJSPlugin)

lazy val core = crossProject(JSPlatform, JVMPlatform)
.in(file("core"))
.settings(stdSettings("zio-macros-core"))
.settings(macroSettings())
.settings(macroSettings)
.settings(libraryDependencies += "dev.zio" %% "zio" % zioVersion)

lazy val coreExamples = crossProject(JSPlatform, JVMPlatform)
.in(file("core-examples"))
.dependsOn(core)
.settings(stdSettings("zio-macros-core-examples"))
.settings(examplesSettings())
.settings(examplesSettings)

lazy val coreTests = crossProject(JSPlatform, JVMPlatform)
.in(file("core-tests"))
.dependsOn(core)
.settings(stdSettings("zio-macros-core-tests"))
.settings(
skip in publish := true,
scalacOptions ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, x)) if x <= 11 => Seq("-Ywarn-unused:false")
case _ => Seq("-Ywarn-unused:-explicits,_")
}
}
)
.settings(testSettings)

lazy val test = crossProject(JSPlatform, JVMPlatform)
.in(file("test"))
.dependsOn(core)
.settings(stdSettings("zio-macros-test"))
.settings(macroSettings())
.settings(macroSettings)
.settings(libraryDependencies += "dev.zio" %% "zio-test" % zioVersion)

lazy val testExamples = crossProject(JSPlatform, JVMPlatform)
.in(file("test-examples"))
.dependsOn(test)
.settings(stdSettings("zio-macros-test-examples"))
.settings(examplesSettings())
.settings(examplesSettings)

lazy val testTests = crossProject(JSPlatform, JVMPlatform)
.in(file("test-tests"))
.dependsOn(test)
.settings(stdSettings("zio-macros-test-tests"))
.settings(testSettings)
@@ -0,0 +1,64 @@
/*
* Copyright 2017-2019 John A. De Goes and the ZIO Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package zio.macros.access

import zio.{ RIO, URIO, ZIO }

@accessible
trait Module {

val module: Module.Service[Any]
}

object Module {

trait Service[R] {

def get(id: Int): ZIO[R, Nothing, String]
def set(id: Int, value: String): ZIO[R, Nothing, Unit]
def getAndSet(id: Int, value: String): ZIO[R, Nothing, String]
def getAndSet2(id: Int)(value: String): ZIO[R, Nothing, String]
def clear(): ZIO[R, Nothing, Unit]
def clear2: ZIO[R, Nothing, Unit]
val clear3: ZIO[R, Nothing, Unit]
def overloaded(value: Int): ZIO[R, Nothing, String]
def overloaded(value: Long): ZIO[R, Nothing, String]
val rio: RIO[R, String]
val urio: URIO[R, String]
def nonAbstract(id: Int): ZIO[R, Nothing, String] = get(id)
}
}

package object module extends Module.Accessors

object AccessibleExample {

val program =
for {
_ <- module.get(1)
_ <- module.set(1, "foo")
_ <- module.getAndSet(1, "foo")
_ <- module.getAndSet2(1)("foo")
_ <- module.clear()
_ <- module.clear2
_ <- module.clear3
_ <- module.overloaded(1)
_ <- module.overloaded(1L)
_ <- module.rio.orDie
_ <- module.urio
_ <- module.nonAbstract(1)
} yield ()
}

This file was deleted.

@@ -0,0 +1,162 @@
/*
* Copyright 2017-2019 John A. De Goes and the ZIO Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package zio.macros.access

import zio.{ RIO, UIO, URIO, ZIO }
import zio.test.{ DefaultRunnableSpec, assert, suite, test, testM }
import zio.test.Assertion.{ anything, equalTo }

object AccessibleSpec
extends DefaultRunnableSpec(
suite("accessible annotation")(
suite("configuration")(
suite("default should generate only trait")(
testM("with parenthesis") {
@accessible()
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { val a: ZIO[R, Nothing, Unit] } }

for {
makeAccessors <- UIO(() => new Foo.Accessors {})
accessors <- UIO(makeAccessors())
} yield assert(accessors, anything)
},
testM("without parenthesis") {
@accessible
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { val a: ZIO[R, Nothing, Unit] } }

for {
makeAccessors <- UIO(() => new Foo.Accessors {})
accessors <- UIO(makeAccessors())
} yield assert(accessors, anything)
}
),
suite("named should generate accessors object")(
test(">") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { val a: ZIO[R, Nothing, Unit] } }

assert(Foo.>, anything)
},
test("proxy") {
@accessible("proxy")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { val a: ZIO[R, Nothing, Unit] } }

assert(Foo.proxy, anything)
}
)
),
suite("should generate accessors for")(
test("val") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { val a: ZIO[R, Nothing, Unit] } }

assert(Foo.>.a, anything)
},
suite("def")(
suite("no args")(
test("without parenthesis") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { def a: ZIO[R, Nothing, Unit] } }

assert(Foo.>.a, anything)
},
test("with parenthesis") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { def a(): ZIO[R, Nothing, Unit] } }

assert(Foo.>.a(), anything)
}
),
test("single argument") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { def a(v1: Int): ZIO[R, Nothing, Unit] } }

assert(Foo.>.a(1), anything)
},
suite("multiple arguments")(
test("single parameter list") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { def a(v1: Int, v2: Int): ZIO[R, Nothing, Unit] } }

assert(Foo.>.a(1, 2), anything)
},
test("multiple parameter lists") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { def a(v1: Int)(v2: Int): ZIO[R, Nothing, Unit] } }

assert(Foo.>.a(1)(2), anything)
}
),
test("overloaded") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo {
trait Service[R] {
def a(v1: Int): ZIO[R, Nothing, Unit]
def a(v1: Long): ZIO[R, Nothing, Unit]
}
}

assert(Foo.>.a(1), anything) && assert(Foo.>.a(1L), anything)
},
suite("zio aliases")(
test("RIO") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { val a: RIO[R, Unit] } }

assert(Foo.>.a, anything)
},
test("URIO") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { val a: URIO[R, Unit] } }

assert(Foo.>.a, anything)
}
),
test("non abstract methods") {
@accessible(">")
trait Foo { val foo: Foo.Service[Any] }
object Foo { trait Service[R] { val a: ZIO[R, Nothing, Unit] = ZIO.unit } }

assert(Foo.>.a, anything)
}
)
),
test("should keep user-defined code") {
@accessible
trait Foo { val foo: Foo.Service[Any] }
object Foo {
val preValue: Int = 42
trait Service[R] {}
val postValue: Int = 42
}

assert(Foo.preValue, equalTo(42)) && assert(Foo.postValue, equalTo(42))
}
)
)

0 comments on commit fd5d362

Please sign in to comment.