Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ public void defaultValues() throws IOException {
+ " optional double f = 6 [default = -inf ];\n"
+ " optional double g = 7 [default = nan ];\n"
+ " optional double h = 8 [default = -nan ];\n"
+ " optional int32 i = 9 [default = -0x80000000\n];\n"
+ " optional int64 j = 10 [default = -0x7FFFFFFF\n];\n"
+ "}\n")
.build();
String code = new JavaWithProfilesGenerator(schema).generateJava("Message");
Expand All @@ -513,6 +515,8 @@ public void defaultValues() throws IOException {
assertThat(code).contains(" public static final Double DEFAULT_F = Double.NEGATIVE_INFINITY;");
assertThat(code).contains(" public static final Double DEFAULT_G = Double.NaN;");
assertThat(code).contains(" public static final Double DEFAULT_H = Double.NaN;");
assertThat(code).contains(" public static final Integer DEFAULT_I = -2147483648;");
assertThat(code).contains(" public static final Long DEFAULT_J = -2147483647L;");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class KotlinGeneratorTest {
| optional double n = 14 [default = -inf];
| optional double o = 15 [default = nan];
| optional double p = 16 [default = -nan];
| optional int32 q = 17 [default = -0x80000000];
| optional int64 r = 18 [default = -0x7FFFFFFF];
|}
""".trimMargin(),
)
Expand All @@ -160,6 +162,8 @@ class KotlinGeneratorTest {
assertThat(code).contains("const val DEFAULT_N: Double = Double.NEGATIVE_INFINITY")
assertThat(code).contains("const val DEFAULT_O: Double = Double.NaN")
assertThat(code).contains("const val DEFAULT_P: Double = Double.NaN")
assertThat(code).contains("const val DEFAULT_Q: Int = Int.MIN_VALUE")
assertThat(code).contains("const val DEFAULT_R: Long = -2_147_483_647L")
}

@Test fun nameAllocatorIsUsed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,15 @@ fun optionValueToInt(value: Any?): Int {
if (value == null) return 0

val string = value.toString()
val negativeSign = if (string.startsWith('-')) { "-" } else { "" }

return when {
// Hexadecimal.
string.startsWith("0x") || string.startsWith("0X") -> string.substring("0x".length).toInt(16)
string.startsWith("${negativeSign}0x", ignoreCase = true) ->
buildString {
append(negativeSign)
append(string.substring("${negativeSign}0x".length))
}.toInt(16)

// Octal.
string.startsWith("0") && string != "0" -> error("Octal literal unsupported: $value")
Expand All @@ -136,10 +141,15 @@ fun optionValueToLong(value: Any?): Long {
if (value == null) return 0L

val string = value.toString()
val negativeSign = if (string.startsWith('-')) { "-" } else { "" }

return when {
// Hexadecimal.
string.startsWith("0x") || string.startsWith("0X") -> string.substring("0x".length).toLong(16)
string.startsWith("${negativeSign}0x", ignoreCase = true) ->
buildString {
append(negativeSign)
append(string.substring("${negativeSign}0x".length))
}.toLong(16)

// Octal.
string.startsWith("0") && string != "0" -> error("Octal literal unsupported: $value")
Expand Down
Loading