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

Function transformPrimitive

codegen/go_transform.go:101–116  ·  view source on GitHub ↗

transformPrimitive returns the code to transform source primtive type to target primitive type. It returns an error if source and target are not compatible for transformation.

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

Source from the content-addressed store, hash-verified

99// target primitive type. It returns an error if source and target are not
100// compatible for transformation.
101func transformPrimitive(source, target *expr.AttributeExpr, sourceVar, targetVar string, newVar bool, ta *TransformAttrs) (string, error) {
102 if err := IsCompatible(source.Type, target.Type, sourceVar, targetVar); err != nil {
103 return "", err
104 }
105 assign := "="
106 if newVar {
107 assign = ":="
108 }
109
110 srcRef := ta.SourceCtx.Scope.Ref(source, ta.SourceCtx.Pkg(source))
111 tgtRef := ta.TargetCtx.Scope.Ref(target, ta.TargetCtx.Pkg(target))
112 if srcRef != tgtRef {
113 return fmt.Sprintf("%s %s %s(%s)\n", targetVar, assign, tgtRef, sourceVar), nil
114 }
115 return fmt.Sprintf("%s %s %s\n", targetVar, assign, sourceVar), nil
116}
117
118// transformObject generates Go code to transform source object to target
119// object.

Callers 1

transformAttributeFunction · 0.85

Calls 3

IsCompatibleFunction · 0.85
PkgMethod · 0.80
RefMethod · 0.65

Tested by

no test coverage detected