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

Function transformArray

grpc/codegen/protobuf_transform.go:430–501  ·  view source on GitHub ↗

transformArray returns the code to transform source attribute of array type to target attribute of array type. It returns an error if source and target are not compatible for transformation.

(source, target *expr.Array, sourceVar, targetVar string, newVar bool, targetPtr bool, sourcePtr bool, ta *transformAttrs)

Source from the content-addressed store, hash-verified

428// type to target attribute of array type. It returns an error if source
429// and target are not compatible for transformation.
430func transformArray(source, target *expr.Array, sourceVar, targetVar string, newVar bool, targetPtr bool, sourcePtr bool, ta *transformAttrs) (string, error) {
431 elem := target.ElemType
432 if ta.proto {
433 elem = unAlias(elem)
434 }
435 targetRef := ta.TargetCtx.Scope.Ref(elem, ta.TargetCtx.Pkg(elem))
436
437 var (
438 code string
439 err error
440 )
441
442 // If targetInit is set, the target array element is in a nested state.
443 // See grpc/docs/FAQ.md.
444 if ta.targetInit != "" {
445 assign := "="
446 if newVar {
447 assign = ":="
448 }
449 code = fmt.Sprintf("%s %s &%s{}\n", targetVar, assign, ta.targetInit)
450 ta.targetInit = ""
451 }
452 if ta.wrapped {
453 if ta.proto {
454 targetVar += ".Field"
455 newVar = false
456 } else {
457 sourceVar += ".Field"
458 }
459 ta.wrapped = false
460 }
461
462 src := source.ElemType
463 tgt := elem
464 if err = codegen.IsCompatible(src.Type, tgt.Type, "[0]", "[0]"); err != nil {
465 if ta.proto {
466 ta.targetInit = ta.TargetCtx.Scope.Name(tgt, ta.TargetCtx.Pkg(tgt), ta.TargetCtx.Pointer, ta.TargetCtx.UseDefault)
467 tgt = unwrapAttr(expr.DupAtt(tgt))
468 } else {
469 src = unwrapAttr(expr.DupAtt(src))
470 }
471 ta.wrapped = true
472 if err = codegen.IsCompatible(src.Type, tgt.Type, "[0]", "[0]"); err != nil {
473 return "", err
474 }
475 }
476 valVar := "val"
477 if obj := expr.AsObject(src.Type); obj != nil {
478 if len(*obj) == 0 {
479 valVar = ""
480 }
481 }
482
483 data := map[string]any{
484 "ElemTypeRef": targetRef,
485 "SourceElem": src,
486 "TargetElem": tgt,
487 "SourceVar": sourceVar,

Callers 2

transformAttributeFunction · 0.70
transformObjectFunction · 0.70

Calls 11

IsCompatibleFunction · 0.92
DupAttFunction · 0.92
AsObjectFunction · 0.92
unAliasFunction · 0.85
unwrapAttrFunction · 0.85
PkgMethod · 0.80
CountMethod · 0.80
ExecuteMethod · 0.80
RefMethod · 0.65
NameMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected