buildInitData builds the transformation code to convert source to target. source, target are the source and target attributes used in the transformation sourceVar, targetVar are the source and target variable names used in the transformation svcCtx is the attribute context for service type proto if
(source, target *expr.AttributeExpr, sourceVar, targetVar string, svcCtx *codegen.AttributeContext, proto, _, usesrc bool, sd *ServiceData)
| 1066 | // proto if true indicates the target type is a protocol buffer type |
| 1067 | // svr if true indicates the code is generated for conversion server side |
| 1068 | func (d *ServicesData) buildInitData(source, target *expr.AttributeExpr, sourceVar, targetVar string, svcCtx *codegen.AttributeContext, proto, _, usesrc bool, sd *ServiceData) *InitData { |
| 1069 | pbCtx := protoBufTypeContext(sd.PkgName, sd.Scope, false) |
| 1070 | name := "New" |
| 1071 | srcCtx := pbCtx |
| 1072 | tgtCtx := svcCtx |
| 1073 | if proto { |
| 1074 | srcCtx = svcCtx |
| 1075 | tgtCtx = pbCtx |
| 1076 | name += "Proto" |
| 1077 | } |
| 1078 | isStruct := expr.IsObject(target.Type) || expr.IsUnion(target.Type) |
| 1079 | if _, ok := source.Type.(expr.UserType); ok && usesrc { |
| 1080 | name += protoBufGoTypeName(source, sd.Scope) |
| 1081 | } |
| 1082 | n := protoBufGoTypeName(target, sd.Scope) |
| 1083 | if !isStruct { |
| 1084 | // If target is array, map, or primitive the name will be suffixed with |
| 1085 | // the definition (e.g int, []string, map[int]string) which is incorrect. |
| 1086 | n = protoBufGoTypeName(source, sd.Scope) |
| 1087 | } |
| 1088 | name += n |
| 1089 | code, helpers, err := protoBufTransform(source, target, sourceVar, targetVar, srcCtx, tgtCtx, proto, true) |
| 1090 | if err != nil { |
| 1091 | panic(err) // bug |
| 1092 | } |
| 1093 | sd.transformHelpers = codegen.AppendHelpers(sd.transformHelpers, helpers) |
| 1094 | var args []*InitArgData |
| 1095 | if (!proto && !isEmpty(source.Type)) || (proto && !isEmpty(target.Type)) { |
| 1096 | args = []*InitArgData{{ |
| 1097 | Name: sourceVar, |
| 1098 | Ref: sourceVar, |
| 1099 | TypeName: srcCtx.Scope.Name(source, srcCtx.Pkg(source), srcCtx.Pointer, srcCtx.UseDefault), |
| 1100 | TypeRef: srcCtx.Scope.Ref(source, srcCtx.Pkg(source)), |
| 1101 | Example: source.Example(d.Root.API.ExampleGenerator), |
| 1102 | }} |
| 1103 | } |
| 1104 | return &InitData{ |
| 1105 | Name: name, |
| 1106 | ReturnVarName: targetVar, |
| 1107 | ReturnTypeRef: tgtCtx.Scope.Ref(target, tgtCtx.Pkg(target)), |
| 1108 | ReturnIsStruct: isStruct, |
| 1109 | ReturnTypePkg: tgtCtx.Pkg(target), |
| 1110 | Code: code, |
| 1111 | Args: args, |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | // buildErrorsData builds the error data for all the error responses in the |
| 1116 | // endpoint expression. The response message for each error response are |
no test coverage detected