in codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpBindingProtocolGenerator.java [1002:1055]
private String conditionallyEscapeHeader(
GenerationContext context,
GoWriter writer,
MemberShape memberShape,
String operand
) {
var targetShape = context.getModel().expectShape(memberShape.getTarget());
if (!targetShape.isStringShape()) {
return operand;
}
if (targetShape.hasTrait(MediaTypeTrait.class)) {
return conditionallyBase64Encode(context, writer, targetShape, operand);
}
writer.pushState();
var returnVar = "escaped";
var pointableIndex = GoPointableIndex.of(context.getModel());
var shouldDereference = pointableIndex.isDereferencable(memberShape);
if (shouldDereference) {
operand = CodegenUtils.getAsValueIfDereferencable(pointableIndex, memberShape, operand);
writer.putContext("escapedVar", "escapedVal");
returnVar = "escapedPtr";
} else {
writer.putContext("escapedVar", returnVar);
}
writer.putContext("returnVar", returnVar);
if (targetShape.hasTrait(EnumTrait.class)) {
operand = "string(" + operand + ")";
}
writer.putContext("value", operand);
writer.putContext("quoteValue", SymbolUtils.createValueSymbolBuilder(
"Quote", SmithyGoDependency.STRCONV).build());
writer.putContext("indexOf", SymbolUtils.createValueSymbolBuilder(
"Index", SmithyGoDependency.STRINGS).build());
writer.putContext("ptrString", SymbolUtils.createValueSymbolBuilder(
"String", SmithyGoDependency.SMITHY_PTR).build());
writer.write("""
$escapedVar:L := $value:L
if $indexOf:T($value:L, `,`) != -1 || $indexOf:T($value:L, `"`) != -1 {
$escapedVar:L = $quoteValue:T($value:L)
}
""");
if (shouldDereference) {
writer.write("$returnVar:L := $ptrString:T($escapedVar:L)");
}
writer.popState();
return returnVar;
}