Skip to content

Commit 782654b

Browse files
fix(extgen): make Go type compatibility check symmetric and drop float32 coercion
1 parent af07c17 commit 782654b

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

internal/extgen/validator.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,21 +223,19 @@ func (v *Validator) phpTypeToGoType(t phpType, isNullable bool) string {
223223
}
224224

225225
// isCompatibleGoType checks if the actual Go type is compatible with the expected type.
226+
// PHP int maps to Go int64: we also accept plain int for ergonomics on 64-bit platforms
227+
// (where they share the same layout). The relation is symmetric so the direction in which
228+
// the check is written does not matter.
226229
func (v *Validator) isCompatibleGoType(expectedType, actualType string) bool {
227230
if expectedType == actualType {
228231
return true
229232
}
230233

231-
switch expectedType {
232-
case "int64":
233-
return actualType == "int"
234-
case "*int64":
235-
return actualType == "*int"
236-
case "*float64":
237-
return actualType == "*float32"
238-
}
234+
return isIntAlias(expectedType, actualType) || isIntAlias(actualType, expectedType)
235+
}
239236

240-
return false
237+
func isIntAlias(a, b string) bool {
238+
return (a == "int64" && b == "int") || (a == "*int64" && b == "*int")
241239
}
242240

243241
func (v *Validator) phpReturnTypeToGoType(phpReturnType phpType) string {

internal/extgen/validator_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,3 +911,29 @@ func TestPhpReturnTypeToGoType(t *testing.T) {
911911
})
912912
}
913913
}
914+
915+
func TestIsCompatibleGoType(t *testing.T) {
916+
tests := []struct {
917+
name string
918+
expected string
919+
actual string
920+
compatible bool
921+
}{
922+
{"exact match int64", "int64", "int64", true},
923+
{"int accepted for int64", "int64", "int", true},
924+
{"int64 accepted for int (symmetric)", "int", "int64", true},
925+
{"pointer int alias symmetric a->b", "*int64", "*int", true},
926+
{"pointer int alias symmetric b->a", "*int", "*int64", true},
927+
{"float32 not compatible with float64", "float64", "float32", false},
928+
{"pointer float32 not compatible with *float64", "*float64", "*float32", false},
929+
{"unrelated types", "int64", "string", false},
930+
{"bool vs int", "bool", "int64", false},
931+
}
932+
933+
validator := Validator{}
934+
for _, tt := range tests {
935+
t.Run(tt.name, func(t *testing.T) {
936+
assert.Equal(t, tt.compatible, validator.isCompatibleGoType(tt.expected, tt.actual))
937+
})
938+
}
939+
}

0 commit comments

Comments
 (0)