Skip to content

Commit

Permalink
Don't use Java reflection for ScalaJS / Native (zio#8841)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou authored May 11, 2024
1 parent 22c8b59 commit 6bc6ae0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019-2024 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.test

import zio.stacktracer.TracingImplicits.disableAutoTrace

trait FieldExtractorPlatformSpecific {
def productFields(obj: Product): Iterator[String] = Iterator.fill(obj.productArity)("")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019-2024 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.test

import zio.stacktracer.TracingImplicits.disableAutoTrace

trait FieldExtractorPlatformSpecific {
def productFields(obj: Product): Iterator[String] = obj.getClass.getDeclaredFields.iterator.map(_.getName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019-2024 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.test

import zio.stacktracer.TracingImplicits.disableAutoTrace

trait FieldExtractorPlatformSpecific {
def productFields(obj: Product): Iterator[String] = Iterator.fill(obj.productArity)("")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.test.Assertion.Arguments.valueArgument
import zio.test.{ErrorMessage => M}

trait AssertionVariants {
trait AssertionVariants extends FieldExtractorPlatformSpecific {
private def diffProduct[T](
obj1: T,
obj2: T,
Expand Down Expand Up @@ -53,7 +53,7 @@ trait AssertionVariants {
case (obj1: Product, obj2: Product) if obj1.productArity == obj2.productArity =>
obj1.productIterator
.zip(obj2.productIterator)
.zip(obj1.getClass.getDeclaredFields.iterator.map(_.getName))
.zip(productFields(obj1))
.flatMap { case ((subObj1, subObj2), paramName) =>
val newParamName = if (paramName.nonEmpty) s".$paramName" else ""
if (subObj1 != subObj2 && !subObj1.isInstanceOf[Product])
Expand Down

0 comments on commit 6bc6ae0

Please sign in to comment.