Skip to content

Commit

Permalink
Correctly handle union to union transforms (#3491)
Browse files Browse the repository at this point in the history
* Correctly handle union to union transforms

These happen when using views and `OneOf`.

* Remove unused code
  • Loading branch information
raphael authored Mar 10, 2024
1 parent fb865b2 commit 883c28f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion codegen/go_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}
`
Expand Down
12 changes: 6 additions & 6 deletions codegen/go_transform_union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
`
Expand All @@ -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
}
}
`
Expand Down
20 changes: 10 additions & 10 deletions codegen/service/testdata/service_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 883c28f

Please sign in to comment.