Skip to content

Commit 5e39469

Browse files
authored
Merge pull request #3355 from square/bquenaudon.2025-08-04.negativedefaults
Handle negative hexadecimal as default values
2 parents 864c6c3 + 2e5c303 commit 5e39469

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

wire-java-generator/src/test/java/com/squareup/wire/java/JavaGeneratorTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ public void defaultValues() throws IOException {
502502
+ " optional double f = 6 [default = -inf ];\n"
503503
+ " optional double g = 7 [default = nan ];\n"
504504
+ " optional double h = 8 [default = -nan ];\n"
505+
+ " optional int32 i = 9 [default = -0x80000000\n];\n"
506+
+ " optional int64 j = 10 [default = -0x7FFFFFFF\n];\n"
505507
+ "}\n")
506508
.build();
507509
String code = new JavaWithProfilesGenerator(schema).generateJava("Message");
@@ -513,6 +515,8 @@ public void defaultValues() throws IOException {
513515
assertThat(code).contains(" public static final Double DEFAULT_F = Double.NEGATIVE_INFINITY;");
514516
assertThat(code).contains(" public static final Double DEFAULT_G = Double.NaN;");
515517
assertThat(code).contains(" public static final Double DEFAULT_H = Double.NaN;");
518+
assertThat(code).contains(" public static final Integer DEFAULT_I = -2147483648;");
519+
assertThat(code).contains(" public static final Long DEFAULT_J = -2147483647L;");
516520
}
517521

518522
@Test

wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class KotlinGeneratorTest {
136136
| optional double n = 14 [default = -inf];
137137
| optional double o = 15 [default = nan];
138138
| optional double p = 16 [default = -nan];
139+
| optional int32 q = 17 [default = -0x80000000];
140+
| optional int64 r = 18 [default = -0x7FFFFFFF];
139141
|}
140142
""".trimMargin(),
141143
)
@@ -160,6 +162,8 @@ class KotlinGeneratorTest {
160162
assertThat(code).contains("const val DEFAULT_N: Double = Double.NEGATIVE_INFINITY")
161163
assertThat(code).contains("const val DEFAULT_O: Double = Double.NaN")
162164
assertThat(code).contains("const val DEFAULT_P: Double = Double.NaN")
165+
assertThat(code).contains("const val DEFAULT_Q: Int = Int.MIN_VALUE")
166+
assertThat(code).contains("const val DEFAULT_R: Long = -2_147_483_647L")
163167
}
164168

165169
@Test fun nameAllocatorIsUsed() {

wire-schema/src/jvmMain/kotlin/com/squareup/wire/schema/internal/JvmLanguages.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,15 @@ fun optionValueToInt(value: Any?): Int {
119119
if (value == null) return 0
120120

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

123124
return when {
124125
// Hexadecimal.
125-
string.startsWith("0x") || string.startsWith("0X") -> string.substring("0x".length).toInt(16)
126+
string.startsWith("${negativeSign}0x", ignoreCase = true) ->
127+
buildString {
128+
append(negativeSign)
129+
append(string.substring("${negativeSign}0x".length))
130+
}.toInt(16)
126131

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

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

140146
return when {
141147
// Hexadecimal.
142-
string.startsWith("0x") || string.startsWith("0X") -> string.substring("0x".length).toLong(16)
148+
string.startsWith("${negativeSign}0x", ignoreCase = true) ->
149+
buildString {
150+
append(negativeSign)
151+
append(string.substring("${negativeSign}0x".length))
152+
}.toLong(16)
143153

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

0 commit comments

Comments
 (0)