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)
| 99 | // target primitive type. It returns an error if source and target are not |
| 100 | // compatible for transformation. |
| 101 | func 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. |
no test coverage detected