This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable accessors container style (#45)
Configurable accessors container style
- Loading branch information
Showing
13 changed files
with
781 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
core-examples/shared/src/main/scala/zio/macros/access/AccessibleExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 () | ||
} |
65 changes: 0 additions & 65 deletions
65
core-examples/shared/src/main/scala/zio/macros/access/Example.scala
This file was deleted.
Oops, something went wrong.
162 changes: 162 additions & 0 deletions
162
core-tests/shared/src/test/scala/zio/macros/access/AccessibleSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
) | ||
) |
Oops, something went wrong.