-
Notifications
You must be signed in to change notification settings - Fork 660
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
support Kotlin/Wasm for apollo-adapters #5803
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
libraries/apollo-adapters/src/wasmJsMain/kotlin/adapter/BigDecimal.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.apollographql.apollo3.adapter | ||
|
||
@JsModule("big.js") | ||
internal external fun Big(raw: JsAny): Big | ||
|
||
@JsName("Number") | ||
internal external fun jsNumber(raw: Big): JsNumber | ||
|
||
internal external class Big { | ||
fun plus(other: Big): Big | ||
fun minus(other: Big): Big | ||
fun times(other: Big): Big | ||
fun div(other: Big): Big | ||
fun cmp(other: Big): Int | ||
fun eq(other: Big): Boolean | ||
fun round(dp: Int, rm: Int): Big | ||
} | ||
|
||
actual class BigDecimal { | ||
internal val raw: Big | ||
|
||
internal constructor(raw: Big) { | ||
this.raw = raw | ||
} | ||
|
||
constructor() : this(raw = Big()) | ||
|
||
actual constructor(strVal: String) : this(raw = Big(strVal.toJsString())) | ||
|
||
actual constructor(doubleVal: Double) { | ||
check(!doubleVal.isNaN() && !doubleVal.isInfinite()) | ||
raw = Big(doubleVal.toJsNumber()) | ||
} | ||
|
||
actual constructor(intVal: Int) : this(raw = Big(intVal.toJsNumber())) | ||
|
||
// JS does not support 64-bit integer natively. | ||
actual constructor(longVal: Long) : this(raw = Big(longVal.toString().toJsString())) | ||
|
||
actual fun add(augend: BigDecimal): BigDecimal = BigDecimal(raw = raw.plus(augend.raw)) | ||
|
||
actual fun subtract(subtrahend: BigDecimal): BigDecimal = BigDecimal(raw = raw.minus(subtrahend.raw)) | ||
|
||
actual fun multiply(multiplicand: BigDecimal): BigDecimal = BigDecimal(raw = raw.times(multiplicand.raw)) | ||
|
||
actual fun divide(divisor: BigDecimal): BigDecimal = BigDecimal(raw = raw.div(divisor.raw)) | ||
|
||
actual fun negate(): BigDecimal = BigDecimal().subtract(this) | ||
|
||
actual fun signum(): Int { | ||
return raw.cmp(Big()) | ||
} | ||
|
||
fun toInt(): Int { | ||
return jsNumber(raw).toInt() | ||
} | ||
|
||
fun toLong(): Long { | ||
// JSNumber is double precision, so it cannot exactly represent 64-bit `Long`. | ||
return toString().toLong() | ||
} | ||
|
||
fun toShort(): Short { | ||
return jsNumber(raw).toInt().toShort() | ||
} | ||
|
||
fun toByte(): Byte { | ||
return jsNumber(raw).toInt().toByte() | ||
} | ||
|
||
fun toChar(): Char { | ||
return jsNumber(raw).toInt().toChar() | ||
} | ||
|
||
fun toDouble(): Double { | ||
return jsNumber(raw).toDouble() | ||
} | ||
|
||
fun toFloat(): Float { | ||
return jsNumber(raw).toDouble().toFloat() | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (other is BigDecimal) { | ||
return raw.eq(other.raw) | ||
} | ||
return false | ||
} | ||
|
||
override fun hashCode(): Int = raw.toString().hashCode() | ||
|
||
override fun toString(): String = raw.toString() | ||
} | ||
|
||
actual fun BigDecimal.toNumber(): Number { | ||
val rounded = raw.round(0, 0) | ||
|
||
return if (raw.minus(rounded).eq(Big())) { | ||
toLong() | ||
} else { | ||
toDouble() | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...-adapters/src/wasmJsTest/kotlin/com/apollographql/apollo3/adapter/test/BigDecimalTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.apollographql.apollo3.adapter.test | ||
|
||
import com.apollographql.apollo3.adapter.BigDecimal | ||
import kotlin.test.* | ||
|
||
class BigDecimalTests { | ||
@Test | ||
fun equality() { | ||
assertEquals(BigDecimal("12345.12345678901234567890123"), BigDecimal("12345.12345678901234567890123")) | ||
assertEquals(BigDecimal("987654321098765432109876543210"), BigDecimal("987654321098765432109876543210")) | ||
assertEquals(BigDecimal("1024768"), BigDecimal("1024768")) | ||
assertEquals(BigDecimal(-Double.MAX_VALUE), BigDecimal(-Double.MAX_VALUE)) | ||
assertEquals(BigDecimal(Double.MAX_VALUE), BigDecimal(Double.MAX_VALUE)) | ||
assertEquals(BigDecimal(-Long.MAX_VALUE - 1), BigDecimal(-Long.MAX_VALUE - 1)) | ||
assertEquals(BigDecimal(Long.MAX_VALUE), BigDecimal(Long.MAX_VALUE)) | ||
} | ||
|
||
@Test | ||
fun overflow() { | ||
assertFails { | ||
BigDecimal("987654321098765432109876543210").toLong() | ||
} | ||
} | ||
|
||
@Test | ||
fun truncating() { | ||
|
||
assertEquals( | ||
BigDecimal("12345.12345678901234567890123").toDouble(), | ||
12345.123456789011 | ||
) | ||
} | ||
|
||
@Test | ||
fun roundTrip_Int() { | ||
val bridged = BigDecimal(Int.MAX_VALUE) | ||
assertEquals(bridged.toInt(), Int.MAX_VALUE) | ||
|
||
val bridgedNeg = BigDecimal( -Int.MAX_VALUE - 1) | ||
assertEquals(bridgedNeg.toInt(), -Int.MAX_VALUE - 1) | ||
} | ||
|
||
@Test | ||
fun roundTrip_Long() { | ||
val bridged = BigDecimal(Long.MAX_VALUE) | ||
assertEquals(bridged.toLong(), Long.MAX_VALUE) | ||
|
||
val bridgedNeg = BigDecimal(-Long.MAX_VALUE - 1) | ||
assertEquals(bridgedNeg.toLong(), -Long.MAX_VALUE - 1) | ||
} | ||
|
||
@Test | ||
fun roundTrip_Double() { | ||
val bridged = BigDecimal(Double.MAX_VALUE) | ||
assertEquals(bridged.toDouble(), Double.MAX_VALUE) | ||
|
||
val bridgedNeg = BigDecimal(-Double.MAX_VALUE) | ||
assertEquals(bridgedNeg.toDouble(), -Double.MAX_VALUE) | ||
|
||
assertFails { | ||
BigDecimal(Double.POSITIVE_INFINITY) | ||
} | ||
|
||
assertFails { | ||
BigDecimal(Double.NEGATIVE_INFINITY) | ||
} | ||
|
||
assertFails { | ||
BigDecimal(Double.NaN) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are those the exact same tests as for JS? If yes, I'll try to factor them in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, was wondering about that too (they are identical)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me try. Also, I wonder why they are
jsTest
and notcommonTest
, I'll digThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just pushed a commit to share the tests between JS and Wasm.
I tried moving to
commonTest
. Unfortunately, we still have behavioural discrepencies there :/...We should probably use a proper BigDecimal lib. Anyone with recommendations?