From 883c28fe29293dc2af66b2541a79462b03a172ea Mon Sep 17 00:00:00 2001 From: Raphael Simon Date: Sat, 9 Mar 2024 17:55:17 -0800 Subject: [PATCH] Correctly handle union to union transforms (#3491) * Correctly handle union to union transforms These happen when using views and `OneOf`. * Remove unused code --- codegen/go_transform.go | 3 ++- codegen/go_transform_union_test.go | 12 ++++++------ codegen/service/testdata/service_code.go | 20 ++++++++++---------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/codegen/go_transform.go b/codegen/go_transform.go index 805ea43ed5..821d477ea0 100644 --- a/codegen/go_transform.go +++ b/codegen/go_transform.go @@ -698,7 +698,8 @@ for key, val := range {{ .SourceVar }} { {{ end }}switch actual := {{ .SourceVar }}.(type) { {{- range $i, $ref := .SourceTypeRefs }} case {{ $ref }}: - {{- transformAttribute (index $.SourceTypes $i).Attribute (index $.TargetTypes $i).Attribute "actual" $.TargetVar false $.TransformAttrs -}} + {{- transformAttribute (index $.SourceTypes $i).Attribute (index $.TargetTypes $i).Attribute "actual" "obj" true $.TransformAttrs -}} + {{ $.TargetVar }} = obj {{- end }} } ` diff --git a/codegen/go_transform_union_test.go b/codegen/go_transform_union_test.go index c864b3d6f0..4d02aec1a6 100644 --- a/codegen/go_transform_union_test.go +++ b/codegen/go_transform_union_test.go @@ -91,8 +91,8 @@ const unionToUnionCode = `func transform() { var target *UnionString2 switch actual := source.(type) { case UnionStringString: - target = UnionString2String(actual) - + obj := UnionString2String(actual) + target = obj } } ` @@ -101,11 +101,11 @@ const unionMultiToUnionMultiCode = `func transform() { var target *UnionStringInt2 switch actual := source.(type) { case UnionStringIntString: - target = UnionStringInt2String(actual) - + obj := UnionStringInt2String(actual) + target = obj case UnionStringIntInt: - target = UnionStringInt2Int(actual) - + obj := UnionStringInt2Int(actual) + target = obj } } ` diff --git a/codegen/service/testdata/service_code.go b/codegen/service/testdata/service_code.go index 353cc1176c..26b9a9bd04 100644 --- a/codegen/service/testdata/service_code.go +++ b/codegen/service/testdata/service_code.go @@ -1516,16 +1516,16 @@ func newResultOneof(vres *resultwithoneoftypeviews.ResultOneofView) *ResultOneof if vres.Result != nil { switch actual := vres.Result.(type) { case *resultwithoneoftypeviews.TView: - res.Result = &T{ + obj := &T{ Message: actual.Message, } - + res.Result = obj case *resultwithoneoftypeviews.UView: - res.Result = &U{} + obj := &U{} if actual.Item != nil { - res.Result.(*U).Item = transformResultwithoneoftypeviewsItemViewToItem(actual.Item) + obj.(*U).Item = transformResultwithoneoftypeviewsItemViewToItem(actual.Item) } - + res.Result = obj } } return res @@ -1538,16 +1538,16 @@ func newResultOneofView(res *ResultOneof) *resultwithoneoftypeviews.ResultOneofV if res.Result != nil { switch actual := res.Result.(type) { case *T: - vres.Result = &resultwithoneoftypeviews.TView{ + obj := &resultwithoneoftypeviews.TView{ Message: actual.Message, } - + vres.Result = obj case *U: - vres.Result = &resultwithoneoftypeviews.UView{} + obj := &resultwithoneoftypeviews.UView{} if actual.Item != nil { - vres.Result.(*resultwithoneoftypeviews.UView).Item = transformItemToResultwithoneoftypeviewsItemView(actual.Item) + obj.(*resultwithoneoftypeviews.UView).Item = transformItemToResultwithoneoftypeviewsItemView(actual.Item) } - + vres.Result = obj } } return vres