MCPcopy Create free account
hub / github.com/goadesign/goa / transformAttribute

Function transformAttribute

grpc/codegen/protobuf_transform.go:133–209  ·  view source on GitHub ↗

transformAttribute returns the code to initialize a target data structure from an instance of source data structure. It returns an error if source and target are not compatible for transformation (different types, fields of different type).

(source, target *expr.AttributeExpr, sourceVar, targetVar string, newVar bool, ta *transformAttrs)

Source from the content-addressed store, hash-verified

131// target are not compatible for transformation (different types, fields of
132// different type).
133func transformAttribute(source, target *expr.AttributeExpr, sourceVar, targetVar string, newVar bool, ta *transformAttrs) (string, error) {
134 var (
135 initCode string
136 err error
137 )
138
139 if err := codegen.IsCompatible(source.Type, target.Type, sourceVar, targetVar); err != nil {
140 if ta.proto {
141 name := ta.TargetCtx.Scope.Name(target, ta.TargetCtx.Pkg(target), ta.TargetCtx.Pointer, ta.TargetCtx.UseDefault)
142 initCode += fmt.Sprintf("%s := &%s{}\n", targetVar, name)
143 targetVar += ".Field"
144 newVar = false
145 target = unwrapAttr(expr.DupAtt(target))
146 } else {
147 source = unwrapAttr(expr.DupAtt(source))
148 sourceVar += ".Field"
149 }
150 if err = codegen.IsCompatible(source.Type, target.Type, sourceVar, targetVar); err != nil {
151 return "", err
152 }
153 }
154
155 if ta.proto {
156 if isUnionMessage(target) {
157 ta = dupTransformAttrs(ta)
158 ta.message = ta.TargetCtx.Scope.Name(target, ta.TargetCtx.Pkg(target), false, false)
159 }
160 } else {
161 if isUnionMessage(source) {
162 ta = dupTransformAttrs(ta)
163 ta.message = ta.SourceCtx.Scope.Ref(source, ta.SourceCtx.Pkg(source))
164 }
165 }
166
167 var code string
168 {
169 switch {
170 case expr.IsArray(source.Type):
171 // Top-level array transforms never assign into a pointer field.
172 code, err = transformArray(expr.AsArray(source.Type), expr.AsArray(target.Type), sourceVar, targetVar, newVar, false, false, ta)
173 case expr.IsMap(source.Type):
174 code, err = transformMap(expr.AsMap(source.Type), expr.AsMap(target.Type), sourceVar, targetVar, newVar, false, ta)
175 case expr.IsObject(source.Type):
176 code, err = transformObject(source, target, sourceVar, targetVar, newVar, ta)
177 case expr.IsUnion(source.Type):
178 if ta.proto {
179 // At top-level we do not expect pointer-to-interface unions.
180 code, err = transformUnionToProto(source, target, sourceVar, targetVar, false, ta)
181 } else {
182 code, err = transformUnionFromProto(source, target, sourceVar, targetVar, ta)
183 }
184 case source.Type.Kind() == expr.AnyKind || target.Type.Kind() == expr.AnyKind:
185 // Special handling for Any type conversions
186 assign := "="
187 if newVar {
188 assign = ":="
189 }
190 srcField := convertType(source, target, false, false, sourceVar, ta)

Callers 3

protoBufTransformFunction · 0.70
transformObjectFunction · 0.70
collectHelpersFunction · 0.70

Calls 15

IsCompatibleFunction · 0.92
DupAttFunction · 0.92
IsArrayFunction · 0.92
AsArrayFunction · 0.92
IsMapFunction · 0.92
AsMapFunction · 0.92
IsObjectFunction · 0.92
IsUnionFunction · 0.92
unwrapAttrFunction · 0.85
isUnionMessageFunction · 0.85
dupTransformAttrsFunction · 0.85
transformUnionToProtoFunction · 0.85

Tested by

no test coverage detected