From 4764c9801bff6922eb3af40df40b21939bd5893c Mon Sep 17 00:00:00 2001 From: asr2003 <162500856+asr2003@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:12:30 +0530 Subject: [PATCH] Fix ScalaJS Compilation by Removing JVM-specific java.util.Objects References in PathCodecPlatformSpecific (#3155) --- .../codec/PathCodecPlatformSpecific.scala | 18 ++++++---- .../http/PathCodecPlatformSpecificSpec.scala | 35 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 zio-http/js/src/test/scala/zio/http/PathCodecPlatformSpecificSpec.scala diff --git a/zio-http/js/src/main/scala/zio/http/codec/PathCodecPlatformSpecific.scala b/zio-http/js/src/main/scala/zio/http/codec/PathCodecPlatformSpecific.scala index a5c599958c..e3d8dcf977 100644 --- a/zio-http/js/src/main/scala/zio/http/codec/PathCodecPlatformSpecific.scala +++ b/zio-http/js/src/main/scala/zio/http/codec/PathCodecPlatformSpecific.scala @@ -1,11 +1,10 @@ package zio.http.codec -import java.util.Objects - trait PathCodecPlatformSpecific { private[codec] def parseLong(s: CharSequence, beginIndex: Int, endIndex: Int, radix: Int): Long = { - Objects.requireNonNull(s) - Objects.checkFromToIndex(beginIndex, endIndex, s.length) + require(s != null, "CharSequence cannot be null") + checkFromToIndex(beginIndex, endIndex, s.length) + if (radix < Character.MIN_RADIX) throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX") if (radix > Character.MAX_RADIX) @@ -42,8 +41,9 @@ trait PathCodecPlatformSpecific { } private[codec] def parseInt(s: CharSequence, beginIndex: Int, endIndex: Int, radix: Int): Int = { - Objects.requireNonNull(s) - Objects.checkFromToIndex(beginIndex, endIndex, s.length) + require(s != null, "CharSequence cannot be null") + checkFromToIndex(beginIndex, endIndex, s.length) + if (radix < Character.MIN_RADIX) throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX") if (radix > Character.MAX_RADIX) @@ -88,4 +88,10 @@ trait PathCodecPlatformSpecific { "For input string: \"" + s + "\"" + (if (radix == 10) "" else " under radix " + radix), ) + + private def checkFromToIndex(from: Int, to: Int, length: Int): Unit = { + if (from < 0 || to > length || from > to) { + throw new IndexOutOfBoundsException(s"Range [$from, $to) out of bounds for length $length") + } + } } diff --git a/zio-http/js/src/test/scala/zio/http/PathCodecPlatformSpecificSpec.scala b/zio-http/js/src/test/scala/zio/http/PathCodecPlatformSpecificSpec.scala new file mode 100644 index 0000000000..a41c202dd1 --- /dev/null +++ b/zio-http/js/src/test/scala/zio/http/PathCodecPlatformSpecificSpec.scala @@ -0,0 +1,35 @@ +package zio.http.codec + +import zio._ +import zio.test.Assertion._ +import zio.test._ + +object PathCodecPlatformSpecificSpec extends ZIOSpecDefault { + + def spec = suite("PathCodecJSPlatformSpecificSpec")( + test("parseInt should correctly parse a valid integer from a CharSequence") { + val charSequence = "12345" + val result = new PathCodecPlatformSpecific {}.parseInt(charSequence, 0, charSequence.length, 10) + assert(result)(equalTo(12345)) + }, + test("parseInt should throw an error for an invalid radix") { + val charSequence = "12345" + val result = ZIO.attempt { + new PathCodecPlatformSpecific {}.parseInt(charSequence, 0, charSequence.length, Character.MAX_RADIX + 1) + }.either + assertZIO(result)(isLeft(hasMessage(containsString("radix")))) + }, + test("parseLong should correctly parse a valid long from a CharSequence") { + val charSequence = "123456789012345" + val result = new PathCodecPlatformSpecific {}.parseLong(charSequence, 0, charSequence.length, 10) + assert(result)(equalTo(123456789012345L)) + }, + test("parseLong should throw an error for an invalid input") { + val charSequence = "invalid123" + val result = ZIO.attempt { + new PathCodecPlatformSpecific {}.parseLong(charSequence, 0, charSequence.length, 10) + }.either + assertZIO(result)(isLeft(hasMessage(containsString("Error at index")))) + }, + ) +}