Skip to content

Commit 976fc45

Browse files
authored
Properly unalias array elem type (goadesign#3152)
when mapping an array to a proto generated Go slice.
1 parent 8d59b3e commit 976fc45

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

grpc/codegen/protobuf_transform.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func init() {
7777
// newVar if true initializes a target variable with the generated Go code
7878
// using `:=` operator. If false, it assigns Go code to the target variable
7979
// using `=`.
80-
//
8180
func protoBufTransform(source, target *expr.AttributeExpr, sourceVar, targetVar string, sourceCtx, targetCtx *codegen.AttributeContext, proto, newVar bool) (string, []*codegen.TransformFunctionData, error) {
8281
ta := &transformAttrs{TransformAttrs: &codegen.TransformAttrs{
8382
SourceCtx: sourceCtx,
@@ -387,7 +386,11 @@ func isNonZero(v interface{}) bool {
387386
// type to target attribute of array type. It returns an error if source
388387
// and target are not compatible for transformation.
389388
func transformArray(source, target *expr.Array, sourceVar, targetVar string, newVar bool, ta *transformAttrs) (string, error) {
390-
targetRef := ta.TargetCtx.Scope.Ref(target.ElemType, ta.TargetCtx.Pkg(target.ElemType))
389+
elem := target.ElemType
390+
if ta.proto {
391+
elem = unAlias(elem)
392+
}
393+
targetRef := ta.TargetCtx.Scope.Ref(elem, ta.TargetCtx.Pkg(elem))
391394

392395
var (
393396
code string
@@ -415,7 +418,7 @@ func transformArray(source, target *expr.Array, sourceVar, targetVar string, new
415418
}
416419

417420
src := source.ElemType
418-
tgt := target.ElemType
421+
tgt := elem
419422
if err = codegen.IsCompatible(src.Type, tgt.Type, "[0]", "[0]"); err != nil {
420423
if ta.proto {
421424
ta.targetInit = ta.TargetCtx.Scope.Name(tgt, ta.TargetCtx.Pkg(tgt), ta.TargetCtx.Pointer, ta.TargetCtx.UseDefault)
@@ -693,7 +696,6 @@ func transformUnionData(source, target *expr.AttributeExpr, ta *transformAttrs)
693696
// ta is the transform attributes
694697
//
695698
// seen keeps track of generated transform functions to avoid recursion
696-
//
697699
func transformAttributeHelpers(source, target *expr.AttributeExpr, ta *transformAttrs, seen map[string]*codegen.TransformFunctionData) ([]*codegen.TransformFunctionData, error) {
698700
var (
699701
helpers []*codegen.TransformFunctionData
@@ -952,7 +954,7 @@ func dupTransformAttrs(ta *transformAttrs) *transformAttrs {
952954
}
953955

954956
const (
955-
transformGoArrayTmpl = `{{ .TargetVar }} {{ if .NewVar }}:={{ else }}={{ end }} make([]{{ .ElemTypeRef }}, len({{ .SourceVar }}))
957+
transformGoArrayTmpl = `{{ .TargetVar }} {{ if .NewVar }}:={{ else }}={{ end }} make([]{{ .ElemTypeRef }}, len({{ .SourceVar }}))
956958
for {{ .LoopVar }}, val := range {{ .SourceVar }} {
957959
{{ transformAttribute .SourceElem .TargetElem "val" (printf "%s[%s]" .TargetVar .LoopVar) false .TransformAttrs -}}
958960
}

0 commit comments

Comments
 (0)