Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignoredBeanProperties annotation #22

Merged
merged 1 commit into from
Apr 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/src/main/scala/configs/beans/ignoredBeanProperties.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2013-2017 Tsukasa Kitachi
*
* 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 configs.beans

import scala.annotation.StaticAnnotation

class ignoredBeanProperties(propertyNames: String*) extends StaticAnnotation
39 changes: 34 additions & 5 deletions core/src/main/scala/configs/macros/Construct.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package configs.macros

import configs.beans.ignoredBeanProperties

trait Construct {
this: MacroBase =>

Expand Down Expand Up @@ -161,18 +163,45 @@ trait Construct {
}

private def listProperties(tpe: Type): List[Property] = {
val getters: Map[String, (MethodSymbol, Type)] =
val getters =
tpe.members.foldLeft(Map.empty[String, (MethodSymbol, Type)]) {
case (m, Property.Getter((n, g, t))) =>
if (m.contains(n) && decodedName(g).startsWith("get")) m
else m + ((n, (g, t)))
case (m, _) => m
}
tpe.members.sorted.collect {
case Property.Setter(s@(n, _, _)) => (s, getters.get(n))
}.collect {
case ((n, s, t1), Some((g, t2))) if t1 =:= t2 => Property(n, t1, g, s)
val props =
tpe.members.sorted.collect {
case Property.Setter(s@(n, _, _)) => (s, getters.get(n))
}.collect {
case ((n, s, t1), Some((g, t2))) if t1 =:= t2 => Property(n, t1, g, s)
}
val ignored = ignoredProperties(tpe, props.map(_.name).toSet)

props.filter(p => !ignored(p.name))
}

private def ignoredProperties(tpe: Type, propNames: Set[String]): Set[String] = {
val annotationType = typeOf[ignoredBeanProperties]
val args = {
val q"{ ${dummy: Tree}; () }" = c.typecheck(q"{ object ${freshName()}; () }")
dummy.symbol.owner.annotations
.map(_.tree)
.filter(_.tpe =:= annotationType)
.flatMap(_.children.tail)
}
val (errors, props) = args.foldLeft((Nil: List[(Position, String)], Nil: List[String])) {
case ((xs, ps), Literal(Constant(p: String))) if propNames(p) => (xs, p :: ps)
case ((xs, ps), x@Literal(Constant(p))) => ((x.pos, s"$tpe doesn't have property `$p`") :: xs, ps)
case ((xs, ps), x) => ((x.pos, "arguments must be string literal") :: xs, ps)
}
if (errors.nonEmpty) {
errors.reverse.foreach {
case (pos, msg) => c.error(pos, msg)
}
c.abort(c.enclosingPosition, s"invalid arguments to @$annotationType")
}
else props.toSet
}

}
66 changes: 66 additions & 0 deletions core/src/test/scala/configs/beans/IgnoredBeanPropertiesTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2013-2017 Tsukasa Kitachi
*
* 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 configs.beans

import com.typesafe.config.ConfigFactory
import configs.{ConfigReader, ConfigWriter}
import scala.beans.BeanProperty
import scalaprops.Property.forAll
import scalaprops.Scalaprops

object IgnoredBeanPropertiesTest extends Scalaprops {

trait NotHaveInstance

class SomeBean(
@BeanProperty var intValue: Int,
@BeanProperty var notHaveInstance: NotHaveInstance,
@BeanProperty var nested: Nested) {
def this() = this(0, null, null)
}

class Nested(@BeanProperty var intValue: Int) {
def this() = this(0)
}

val ignoreOnRead = forAll {
@ignoredBeanProperties("notHaveInstance")
implicit val reader: ConfigReader[SomeBean] = ConfigReader.derive[SomeBean]

val config = ConfigFactory.parseString(
"""int-value = 100
|nested.int-value = 200
|""".stripMargin)

reader.extract(config).exists { b =>
b.intValue == 100 && b.nested.intValue == 200
}
}

val ignoreOnWrite = forAll {
@ignoredBeanProperties("notHaveInstance")
implicit val writer: ConfigWriter[SomeBean] = ConfigWriter.derive[SomeBean]

val value = writer.write(new SomeBean(100, new NotHaveInstance {}, new Nested(200)))

value == ConfigFactory.parseString(
"""int-value = 100
|nested.int-value = 200
|""".stripMargin).root()
}

}