in packages/codegen.go/src/fake/servers.ts [499:660]
const emitCase = function (caseValue: string, paramVar: string, type: go.PossibleType): string {
let caseContent = `\t\tcase "${caseValue}":\n`;
caseContent += '\t\t\tcontent, err = io.ReadAll(part)\n';
caseContent += '\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n';
let assignedValue: string | undefined;
if (go.isModelType(helpers.recursiveUnwrapMapSlice(type))) {
imports.add('encoding/json');
caseContent += `\t\t\tif err = json.Unmarshal(content, &${paramVar}); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n`;
} else if (go.isQualifiedType(type) && type.exportName === 'ReadSeekCloser') {
imports.add('bytes');
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming');
assignedValue = 'streaming.NopCloser(bytes.NewReader(content))';
} else if (go.isConstantType(type)) {
let from: string;
switch (type.type) {
case 'bool':
case 'float32':
case 'float64':
case 'int32':
case 'int64':
caseContent += parsePrimitiveType(type.type);
from = 'parsed';
break;
case 'string':
from = 'content';
break;
}
assignedValue = `${clientPkg}.${type.name}(${from})`;
} else if (go.isPrimitiveType(type)) {
switch (type.typeName) {
case 'bool':
imports.add('strconv');
// ParseBool happens in place, so no need to set assignedValue
caseContent += parsePrimitiveType(type.typeName, paramVar);
break;
case 'float32':
case 'float64':
case 'int8':
case 'int16':
case 'int32':
case 'int64':
caseContent += parsePrimitiveType(type.typeName);
assignedValue = `${type.typeName}(parsed)`;
break;
case 'string':
assignedValue = 'string(content)';
break;
default:
throw new CodegenError('InternalError', `unhandled multipart parameter primitive type ${type.typeName}`);
}
} else if (isMultipartContentType(type)) {
imports.add('bytes');
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming');
const bodyContent = 'streaming.NopCloser(bytes.NewReader(content))';
const contentType = 'part.Header.Get("Content-Type")';
const filename = 'part.FileName()';
if (go.isSliceType(type)) {
caseContent += `\t\t\t${paramVar} = append(${paramVar}, streaming.MultipartContent{\n`;
caseContent += `\t\t\t\tBody: ${bodyContent},\n`;
caseContent += `\t\t\t\tContentType: ${contentType},\n`;
caseContent += `\t\t\t\tFilename: ${filename},\n`;
caseContent += '\t\t\t})\n';
} else {
caseContent += `\t\t\t${paramVar}.Body = ${bodyContent}\n`;
caseContent += `\t\t\t${paramVar}.ContentType = ${contentType}\n`;
caseContent += `\t\t\t${paramVar}.Filename = ${filename}\n`;
}
} else if (go.isSliceType(type)) {
if (go.isQualifiedType(type.elementType) && type.elementType.exportName === 'ReadSeekCloser') {
imports.add('bytes');
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming');
assignedValue = `append(${paramVar}, streaming.NopCloser(bytes.NewReader(content)))`;
} else {
throw new CodegenError('InternalError', `uhandled multipart parameter array element type ${go.getTypeDeclaration(type.elementType)}`);
}
} else {
throw new CodegenError('InternalError', `uhandled multipart parameter type ${go.getTypeDeclaration(type)}`);
}
if (assignedValue) {
caseContent += `\t\t\t${paramVar} = ${assignedValue}\n`;
}
return caseContent;
};
for (const param of values(method.parameters)) {
if (go.isMultipartFormBodyParameter(param)) {
if (go.isModelType(param.type)) {
for (const field of param.type.fields) {
content += emitCase(field.serializedName, `${param.name}.${field.name}`, field.type);
}
} else {
content += emitCase(param.name, param.name, param.type);
}
}
}
content += '\t\tdefault:\n\t\t\treturn nil, fmt.Errorf("unexpected part %s", fn)\n';
content += '\t\t}\n'; // end switch
content += '\t}\n'; // end for
} else if (go.isFormBodyParameter(bodyParam)) {
for (const param of values(method.parameters)) {
if (go.isFormBodyParameter(param)) {
let pkgPrefix = '';
if (go.isConstantType(param.type)) {
pkgPrefix = clientPkg + '.';
}
content += `\tvar ${param.name} ${pkgPrefix}${go.getTypeDeclaration(param.type)}\n`;
}
}
content += '\tif err := req.ParseForm(); err != nil {\n\t\treturn nil, &nonRetriableError{fmt.Errorf("failed parsing form data: %v", err)}\n\t}\n';
content += '\tfor key := range req.Form {\n';
content += '\t\tswitch key {\n';
for (const param of values(method.parameters)) {
if (go.isFormBodyParameter(param)) {
content += `\t\tcase "${param.formDataName}":\n`;
let assignedValue: string;
if (go.isConstantType(param.type)) {
assignedValue = `${go.getTypeDeclaration(param.type, clientPkg)}(req.FormValue(key))`;
} else if (go.isPrimitiveType(param.type) && param.type.typeName === 'string') {
assignedValue = 'req.FormValue(key)';
} else {
throw new CodegenError('InternalError', `uhandled form parameter type ${go.getTypeDeclaration(param.type)}`);
}
content += `\t\t\t${param.name} = ${assignedValue}\n`;
}
}
content += '\t\t}\n'; // end switch
content += '\t}\n'; // end for
} else if (bodyParam.bodyFormat === 'binary') {
// nothing to do for binary media type
} else if (bodyParam.bodyFormat === 'Text') {
if (bodyParam && !go.isLiteralParameter(bodyParam)) {
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/fake', 'azfake');
content += '\tbody, err := server.UnmarshalRequestAsText(req)\n';
content += '\tif err != nil {\n\t\treturn nil, err\n\t}\n';
}
} else if (bodyParam.bodyFormat === 'JSON' || bodyParam.bodyFormat === 'XML') {
if (bodyParam && !go.isLiteralParameter(bodyParam)) {
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/fake', 'azfake');
if (go.isBytesType(bodyParam.type)) {
content += `\tbody, err := server.UnmarshalRequestAsByteArray(req, runtime.Base64${bodyParam.type.encoding}Format)\n`;
content += '\tif err != nil {\n\t\treturn nil, err\n\t}\n';
} else if (go.isSliceType(bodyParam.type) && bodyParam.type.rawJSONAsBytes) {
content += '\tbody, err := io.ReadAll(req.Body)\n';
content += '\tif err != nil {\n\t\treturn nil, err\n\t}\n';
content += '\treq.Body.Close()\n';
} else if (go.isInterfaceType(bodyParam.type)) {
requiredHelpers.readRequestBody = true;
content += '\traw, err := readRequestBody(req)\n';
content += '\tif err != nil {\n\t\treturn nil, err\n\t}\n';
content += `\tbody, err := unmarshal${bodyParam.type.name}(raw)\n`;
content += '\tif err != nil {\n\t\treturn nil, err\n\t}\n';
} else {
let bodyTypeName = go.getTypeDeclaration(bodyParam.type, clientPkg);
if (go.isTimeType(bodyParam.type)) {
bodyTypeName = bodyParam.type.dateTimeFormat;
}
content += `\tbody, err := server.UnmarshalRequestAs${bodyParam.bodyFormat}[${bodyTypeName}](req)\n`;
content += '\tif err != nil {\n\t\treturn nil, err\n\t}\n';
}
}
}