From 76466079cadcc2bc94abef92c3bf35e38576a580 Mon Sep 17 00:00:00 2001 From: Sai Govada Date: Mon, 4 Dec 2023 17:58:40 -0800 Subject: [PATCH] Fix Optional Map go code generation // If the union is ["null", map[string]T] then we can't omit the union // Example: mapTest *map[string]interface{} `json:"mapTest"` will omit the union without this check for map --- cmd/avrogo/generate.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/avrogo/generate.go b/cmd/avrogo/generate.go index 46a3683..4aa21db 100644 --- a/cmd/avrogo/generate.go +++ b/cmd/avrogo/generate.go @@ -192,8 +192,11 @@ func canOmitUnionInfo(u typeInfo) bool { // Check that either there's no union or the union is ["null", T] // (the default union type for a pointer) and the Go type is also // a pointer, meaning the avro package can infer that it's a - // pointer union. - return len(u.Union) == 0 || (len(u.Union) == 2 && u.Union[0].GoType == nullType && u.GoType[0] == '*') + // pointer union. + // If the union is ["null", map[string]T] then we can't omit the union + // Example: mapTest *map[string]interface{} `json:"mapTest"` will omit the union without this check for map + return len(u.Union) == 0 || (len(u.Union) == 2 && u.Union[0].GoType == nullType && u.GoType[0] == '*' && len(u.GoType) > 3 && u.GoType[:4] != "*map") + } func writeUnionInfo(w io.Writer, info typeInfo) {