Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Feb 20 09:46
    codecov[bot] commented #146
  • Feb 20 09:45
    codecov[bot] commented #146
  • Feb 20 09:44
    codecov[bot] commented #146
  • Feb 20 09:44
    codecov[bot] commented #146
  • Feb 20 09:43
    codecov[bot] commented #146
  • Feb 20 09:41
    codecov[bot] commented #146
  • Feb 20 09:40
    codecov[bot] commented #146
  • Feb 20 09:40
    krzemin synchronize #146
  • Feb 20 09:40

    krzemin on lifted-transformers

    code formatting (compare)

  • Feb 20 09:39
    codecov[bot] commented #146
  • Feb 20 09:39
    codecov[bot] commented #146
  • Feb 20 09:38
    krzemin synchronize #146
  • Feb 20 09:38

    krzemin on lifted-transformers

    use -language:higherKinds compi… more test cases for 3-arg wrapp… TransformerF support for value … (compare)

  • Feb 19 15:25
    krzemin synchronize #146
  • Feb 19 15:25

    krzemin on lifted-transformers

    relax `traversable` type bound … bring back -Xfatal-warnings on … generalize traverse signature a… (compare)

  • Feb 19 13:54
    codecov[bot] commented #146
  • Feb 19 13:54
    codecov[bot] commented #146
  • Feb 19 13:53
    codecov[bot] commented #146
  • Feb 19 13:53
    codecov[bot] commented #146
  • Feb 19 13:53
    codecov[bot] commented #146
Alexander Van Hecke
@alexandervanhecke
first of all, chimney is a great tool!
is withFieldComputednot supported for transformation from java bean to java bean?
I'm getting compiler error : Invalid selector!
    override def filter(incident: Incident): Incident = incident
      .into[Incident]
      .enableBeanGetters
      .enableBeanSetters
      .withFieldComputed(incident => incident.getAlarmTekstPager, incident => NonEmptyStringFilter("alarmtekstpager").filter(incident.getAlarmTekstPager))
Alexander Van Hecke
@alexandervanhecke
this is with java 1.7 and scala 2.11 btw. we'll migrate to java 1.8 and scala 2.12 soon, I'm hoping something like scala-java8-compat could help me out
Piotr Krzemiński
@krzemin
Hi @alexandervanhecke, thank you.
As documentation says (https://scalalandio.github.io/chimney/#Limitations), .withField*** are not supported when writing to Java beans in current version.
Piotr Krzemiński
@krzemin
I have created #110 to track this limitation and eventually remove it
Alexander Van Hecke
@alexandervanhecke
@krzemin great, thanks, I didn't notice this. another quick question, is it correct to say custom transformations don't work either for java beans? I'm using a special case where Fromis equal to To, ie I have a transformer Transformer[Karakteristieken, Karakterisitieken] but is is never called.
I'm not sure whether that is because of the java beans or because a transformer from and to the same type is never called
(I admit I haven't checked that for scala case classes )
Piotr Krzemiński
@krzemin

I think that when types are equal, we don’t look for transformers in implicit scope. Here’s some example:

@ case class Foo(x: Int) 
defined class Foo

@ implicit val fooTransformer: Transformer[Foo, Foo] = foo => foo.copy(x = foo.x + 1) 
fooTransformer: Transformer[Foo, Foo] = ammonite.$sess.cmd2$$$Lambda$2424/1153180745@2faa55bb

@ case class Bar(foo: Foo) 
defined class Bar

@ case class Bar2(foo: Foo) 
defined class Bar2

@ Bar(Foo(10)).transformInto[Bar2] 
res5: Bar2 = Bar2(Foo(10))

In this case there exists trivial Foo => Foo transformer (identity function), which is picked in recursive transformation.

Java beans shouldn’t change anything here.
Alexander Van Hecke
@alexandervanhecke
aha ok. to clarify the scenario, I'm trying to filter certain data between java beans of the same type. ie the source bean could have 5 items in a list somewhere and the target bean would keep only 2.
in scala I could do this with lenses but I'm stuck with java beans
Piotr Krzemiński
@krzemin
I see. Chimney can’t help with filtering here, as it’s designed for transformations between similar types rather than transforming values of the same types - this is task for lenses.
You may try to converting between Java beans and Scala case classes, but I guess #110 blocked you from this. Remember you can still directly write implicit transformers where target is Java bean for cases where you need to provide missing values for target objects. But depending on your particular case, it might be easier to just filter the objects manually in Java.
Alexander Van Hecke
@alexandervanhecke
thanks, the goal is to move to an internal scala model but input and output will be jaxb generated java beans, so transformation from java => scala => java
Brian Hartin
@bjhartin
I have a question about defining a transform from Option[A] to B. The following code works for Option[A] => A but not Option[A] => B even when an A => B is in scope:
object ChimneyTest extends App {
  import io.scalaland.chimney.Transformer
  import io.scalaland.chimney.dsl._

  case class Bar1(i: Int)
  case class Bar2(i: String)

  case class Baz1(b: Bar1)
  case class Baz2(b: Option[Bar1])
  case class Baz3(b: Bar2)

  implicit def idTransform[A]: Transformer[A, A] = identity[A]

  implicit def intToString: Transformer[Int, String] = _.toString

  // Throws on `None` - not ideal but Chimney doesn't represent failure, e.g. `Either[Error, B]`
  implicit def transformFromOptional[A, B](implicit ev: Transformer[A, B]): Transformer[Option[A], B] =
    (a: Option[A]) => a.map(ev.transform).getOrElse(throw new RuntimeException(""))

  // Some case when field types match (A => A)
  assert(Baz2(Some(Bar1(1))).transformInto[Baz1] == Baz1(Bar1(1)))

  // We can transform a Bar1 to Bar2 because of `intToString` above
  assert(Bar1(1).transformInto[Bar2] == Bar2("1"))

  // Fails, even though Transformer[Int, String] is available
  assert(Baz2(Some(Bar1(1))).transformInto[Baz3] == Baz3(Bar2("1")))
}
The last example fails with:
Error:(25, 43) Chimney can't derive transformation from Baz2 to Baz3
Bar2
  i: java.lang.String - no accessor named i in source type scala.Option
Consult https://scalalandio.github.io/chimney for usage examples.
  assert(Baz2(Some(Bar1(1))).transformInto[Baz3] == Baz3(Bar2("1")))
I don't quite understand why the first example is able to resolve the A => A transformer but the last fails on the A => B.
Brian Hartin
@bjhartin
I've filed an issue with more details at scalalandio/chimney#113
Wojtek Pituła
@Krever
Hey, I tried to use chimney with java-defined bean and it didnt work regardless of using enableBeanGetters. Is it known limitation?
Alexandre Balon-Perin
@abalonperin
Hey, I am trying to use chimney with scala-newtype (https://github.com/estatico/scala-newtype) but it doesn't seem to work. Is it possible at all? I haven't digged too much into the implementation of both libraries so it might not be. Any clue would help. Thank you.
Jonas Chapuis
@jchapuis
^^
Running into similar issues as @bjhartin above with Optional types. I am forced to include explicit intermediary mapping definitions when dealing with proto3 generated types :cry:
Doug Roper
@htmldoug
Error:(85, 8) Chimney can't derive transformation from com.rallyhealth.chopshop.provider.model.PersonLocation to com.rallyhealth.chopshop.provider.es6.generated.EsPersonLocation
scala.Boolean
  derivation from scala.Option to scala.Boolean is not supported in Chimney!
java.lang.String
  derivation from scala.Option to java.lang.String is not supported in Chimney!
  derivation from scala.Int to java.lang.String is not supported in Chimney!
scala.collection.immutable.Set
  derivation from scala.Option to scala.collection.immutable.Set is not supported in Chimney!
scala.collection.Seq
  derivation from scala.Option to scala.collection.Seq is not supported in Chimney!
Consult https://scalalandio.github.io/chimney for usage examples.
      .transform
Is there a way to get field names from these error messages? These case classes have a lot of fields. Hard to figure out what's going on from these types alone.
Doug Roper
@htmldoug
scalalandio/chimney#121 improves the compile errors to the following:
error: Chimney can't derive transformation from com.rallyhealth.chopshop.provider.model.PersonLocation to com.rallyhealth.chopshop.provider.es6.generated.EsPersonLocation

scala.Boolean
  derivation from personlocation.networkEnrollment.isCapitated: scala.Option to scala.Boolean is not supported in Chimney!

java.lang.String
  derivation from personlocation.networkEnrollment.effectiveInterval.start: scala.Option to java.lang.String is not supported in Chimney!
  derivation from personlocation.networkEnrollment.pcpId: scala.Option to java.lang.String is not supported in Chimney!
  derivation from personlocation.networkEnrollment.roles: scala.Int to java.lang.String is not supported in Chimney!
  derivation from personlocation.painManagementTier: scala.Option to java.lang.String is not supported in Chimney!
  derivation from personlocation.providerDigestId: scala.Option to java.lang.String is not supported in Chimney!

scala.collection.immutable.Set
  derivation from personlocation.networksByRole: scala.Option to scala.collection.immutable.Set is not supported in Chimney!

scala.collection.Seq
  derivation from personlocation.preferredPlans: scala.Option to scala.collection.Seq is not supported in Chimney!
Wojtek Pituła
@Krever
Hey, is there something in chimney that will autoconvert camelcase to snake case for example?
Piotr Krzemiński
@krzemin
Hi, no - there’s nothing like that atm
Piotr Krzemiński
@krzemin
But it shouldn’t be much difficult to implement (in fact it doesn’t differ too much from supporting java beans)
Alpar Torok
@atorok
Hello, is there anything sbt specific about how the library works ? I'm converting from sbt to Gradle and running into Chimney can't derive transformation with Gradle. Compilation works with sbt
Piotr Krzemiński
@krzemin
Hi. No, there should be nothing build tool specific about it. For example, I was able to successfully compile bigger project with Chimney in Mill.
Alpar Torok
@atorok
Hm... something relating to compiling options ? Or anything else a build tool might influence indirectly ?
Nils Kilden-Pedersen
@nilskp
@atorok I use Gradle, haven't run into that problem. Which version of Gradle are you using? Also, keep in mind that Scala support on Gradle is not optimal. E.g. there's the old java plugin and the new java-library. The Scala plugin is based on the old java, thus you can only use dependency notation such as compile, compileOnly, etc, not the new api and implementation.
Avital Nahon
@avitaln
hi. I need one of the nested case class to have different field name.
supplying implicit transformer as suggested above, leads to stack overflow. is there another way to do it?
case class OrderItemFrom(nameFrom: String, price: BigDecimal)
case class OrderFrom(name: String, orderItems: Seq[OrderItemFrom])

case class OrderItemTo(nameTo: String, price: BigDecimal)
case class OrderTo(name: String, orderItems: Seq[OrderItemTo])

implicit val orderItemTransformer: Transformer[OrderItemFrom, OrderItemTo] =
    _.into[OrderItemTo].withFieldRenamed(_.nameFrom, _.nameTo).transform

val orderFrom = OrderFrom("somename", Seq(OrderItemFrom("name1", 33), OrderItemFrom("name2", 34)))
val orderTo = orderFrom.transformInto[OrderTo]
Piotr Krzemiński
@krzemin
Hi. For the time being you can’t really use transformer dsl directly in definition of implicit val - it’s known issue (#105).
In order to work around it, you need to define orderItemTransformer as ordinary val and later introduce it to implicit scope. But you need to be sure that orderItemTransformer is defined in scope that cannot see the implicit.
PR that solves this issue is opened - #129
Avital Nahon
@avitaln
Thanks!
Dermot Haughey
@hderms
What if I want to effectively split a case class into two separate fields in different parts of the data type in transforming to?
Piotr Krzemiński
@krzemin
Hi - can you elaborate by providing an example?
Piotr Krzemiński
@krzemin
Oh, I get it. It’s not possible at the moment, but’s on the roadmap. See https://github.com/scalalandio/chimney/issues/100#issuecomment-480563946 and #115
Ittai Zeidman
@ittaiz
Shouldn’t 100 be open then?
Piotr Krzemiński
@krzemin
They seem to be duplicates
changvvb
@changvvb
image.png
intellij idea cannot resolve scala_native2.13
How to resolve it ?
Piotr Krzemiński
@krzemin
Scala native is available only for 2.11. Sbt handles it correctly, but IDEs have problems on project import. You can comment out native stuff in build.sbt only for project import time. However better solution would be appreciated.
changvvb
@changvvb
OK, thank you!
Avital Nahon
@avitaln
Hi. I would like compilation to fail if target class has additional property (with or without default). Even by providing a wrapper function on top of Transformer.define and without the need to explicitly say disableDefaultValues everywhere.
any idea? (When using the following wrapper function - it does not work)
def defineMapping[From, To]: TransformerDefinition[From, To, TransformerCfg.Empty] = {
  val ret = new TransformerDefinition[From, To, TransformerCfg.Empty](Map.empty, Map.empty)
  ret.disableDefaultValues.asInstanceOf[TransformerDefinition[From, To, TransformerCfg.Empty]]
}
Piotr Krzemiński
@krzemin
Hi! Try to change return type to TransformerDefinition[From, To, TransformerCfg.DisableDefaultValues[TransformerCfg.Empty]] and remove explicit .asInstanceOf.
Avital Nahon
@avitaln
thanks. already figured it out but asInstanceOf is still needed
this works:
def defineMapping[From, To]: TransformerDefinition[From, To, DisableDefaultValues[TransformerCfg.Empty]] = {
    val ret = new TransformerDefinition[From, To, DisableDefaultValues[TransformerCfg.Empty]](Map.empty, Map.empty)
    ret.disableDefaultValues.asInstanceOf[TransformerDefinition[From, To, DisableDefaultValues[TransformerCfg.Empty]]]
  }