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

Validation can't be used when FieldArgs is an inner classes #38

Open
Mortimerp9 opened this issue Feb 18, 2014 · 1 comment
Open

Validation can't be used when FieldArgs is an inner classes #38

Mortimerp9 opened this issue Feb 18, 2014 · 1 comment

Comments

@Mortimerp9
Copy link
Contributor

If the FieldArgs argument class is defined as an inner class, sumac is ok with it. For instance, with:

import com.quantifind.sumac._
class MyClass {
    class MyArgs extends FieldArgs {
        var test: String = _
    }
}

we can do:

val c = new MyClass
val arg = new c.MyArgs
arg.parse(Map("test"->"test"))

and everything seems to be working.

However, when using annotation for validation:

import com.quantifind.sumac._
import com.quantifind.sumac.validation.Required
class MyClass {
    class MyArgs extends FieldArgs {
        @Required
        var test: String = _
    }
}

parse will throw an exception:

val c = new MyClass
val arg = new c.MyArgs
arg.parse(Map("test"->"test"))
java.util.NoSuchElementException: key not found: test
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:58)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:58)
    at com.quantifind.sumac.FieldArgs$$anonfun$addAnnotationValidations$1.apply(FieldArgs.scala:56)
    at com.quantifind.sumac.FieldArgs$$anonfun$addAnnotationValidations$1.apply(FieldArgs.scala:55)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
    at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:108)
    at com.quantifind.sumac.FieldArgs$class.addAnnotationValidations(FieldArgs.scala:55)
    at MyClass$MyArgs.addAnnotationValidations(<console>:12)
    at com.quantifind.sumac.FieldArgs$$anonfun$1.applyOrElse(FieldArgs.scala:18)
    at com.quantifind.sumac.FieldArgs$$anonfun$1.applyOrElse(FieldArgs.scala:15)
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33)
    at scala.collection.TraversableLike$$anonfun$collect$1.apply(TraversableLike.scala:278)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.TraversableLike$class.collect(TraversableLike.scala:278)
    at scala.collection.AbstractTraversable.collect(Traversable.scala:105)
    at com.quantifind.sumac.FieldArgs$class.getArgs(FieldArgs.scala:15)
    at MyClass$MyArgs.getArgs(<console>:12)
    at MyClass$MyArgs.getArgs(<console>:12)
    at com.quantifind.sumac.Args$class.getArgs(Args.scala:7)
    at MyClass$MyArgs.getArgs(<console>:12)
    at com.quantifind.sumac.Args$class.parser(Args.scala:28)
    at MyClass$MyArgs.parser$lzycompute(<console>:12)
    at MyClass$MyArgs.parser(<console>:12)
    at com.quantifind.sumac.Args$class.parse(Args.scala:39)
    at MyClass$MyArgs.parse(<console>:12)
@squito
Copy link
Contributor

squito commented Jul 16, 2014

The reason for this is that Sumac figures out default values by calling the default (zero-arg) constructor, and grabbing the values from all fields.

However, inner-classes don't actually have a zero-arg constructor -- they really take a reference to the containing class (which java / scala normally take care of for you).

I don't know if there is anything we can do about this ... maybe the only TODO here is to update the docs to say you can't use inner classes. I'd definitely love to hear any ideas on how to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants