in packages/autorest.go/src/transform/transform.ts [49:154]
async function process(session: Session<m4.CodeModel>) {
await processOperationRequests(session);
processOperationResponses(session);
// fix up dictionary element types (additional properties)
// this must happen before processing objects as we depend on the
// schema type being an actual Go type.
for (const dictionary of values(session.model.schemas.dictionaries)) {
dictionary.language.go!.name = schemaTypeToGoType(session.model, dictionary, 'Property');
dictionary.language.go!.elementIsPtr = !helpers.isTypePassedByValue(dictionary.elementType);
if (dictionary.language.go!.description) {
dictionary.language.go!.description = parseComments(dictionary.language.go!.description);
}
}
// fix up struct field types
for (const obj of values(session.model.schemas.objects)) {
if (obj.language.go!.description) {
obj.language.go!.description = parseComments(obj.language.go!.description);
if (!obj.language.go!.description.startsWith(obj.language.go!.name)) {
obj.language.go!.description = `${obj.language.go!.name} - ${obj.language.go!.description}`;
}
}
if (obj.discriminator) {
// discriminators will contain the root type of each discriminated type hierarchy
if (!session.model.language.go!.discriminators) {
session.model.language.go!.discriminators = new Array<m4.ObjectSchema>();
}
const defs = <Array<m4.ObjectSchema>>session.model.language.go!.discriminators;
const rootDiscriminator = getRootDiscriminator(obj);
if (defs.indexOf(rootDiscriminator) < 0) {
rootDiscriminator.language.go!.rootDiscriminator = true;
defs.push(rootDiscriminator);
// fix up discriminator value to use the enum type if available
const discriminatorEnums = getDiscriminatorEnums(rootDiscriminator);
// for each child type in the hierarchy, fix up the discriminator value
for (const child of values(rootDiscriminator.children?.all)) {
const asObj = <m4.ObjectSchema>child;
let discValue = getEnumForDiscriminatorValue(asObj.discriminatorValue!, discriminatorEnums);
if (!discValue) {
discValue = quoteString(asObj.discriminatorValue!);
}
asObj.discriminatorValue = discValue;
}
}
}
for (const prop of values(obj.properties)) {
if (prop.language.go!.description) {
prop.language.go!.description = parseComments(prop.language.go!.description);
}
const details = <m4.Language>prop.schema.language.go;
details.name = `${schemaTypeToGoType(session.model, prop.schema, 'InBody')}`;
prop.schema = substitueDiscriminator(prop);
if (prop.schema.type === m4.SchemaType.Any || prop.schema.type === m4.SchemaType.AnyObject || (helpers.isObjectSchema(prop.schema) && prop.schema.discriminator)) {
prop.language.go!.byValue = true;
} else if (prop.schema.type === m4.SchemaType.DateTime) {
obj.language.go!.needsDateTimeMarshalling = true;
} else if (prop.schema.type === m4.SchemaType.Date) {
obj.language.go!.needsDateMarshalling = true;
} else if (prop.schema.type === m4.SchemaType.UnixTime) {
obj.language.go!.needsUnixTimeMarshalling = true;
} else if (prop.schema.type === m4.SchemaType.Dictionary && obj.language.go!.marshallingFormat === 'xml') {
// mark that we need custom XML unmarshalling for a dictionary
prop.language.go!.needsXMLDictionaryUnmarshalling = true;
session.model.language.go!.needsXMLDictionaryUnmarshalling = true;
} else if (prop.schema.type === m4.SchemaType.ByteArray) {
prop.language.go!.byValue = true;
obj.language.go!.byteArrayFormat = (<m4.ByteArraySchema>prop.schema).format;
}
if (prop.schema.type === m4.SchemaType.Array || prop.schema.type === m4.SchemaType.Dictionary) {
obj.language.go!.hasArrayMap = true;
prop.language.go!.byValue = true;
if (prop.schema.type !== m4.SchemaType.Dictionary && obj.language.go!.marshallingFormat === 'xml') {
prop.language.go!.needsXMLArrayMarshalling = true;
}
}
}
if (!obj.language.go!.marshallingFormat) {
// TODO: workaround due to https://github.com/Azure/autorest.go/issues/412
// this type isn't used as a parameter/return value so it has no marshalling format.
// AutoRest doesn't make the global configuration available at present so hard-code
// the format to JSON as the vast majority of specs use JSON.
obj.language.go!.marshallingFormat = 'json';
}
const addPropsSchema = helpers.hasAdditionalProperties(obj);
if (addPropsSchema) {
// add an 'AdditionalProperties' field to the type
const addProps = newProperty('AdditionalProperties', 'OPTIONAL; Contains additional key/value pairs not defined in the schema.', addPropsSchema);
addProps.language.go!.isAdditionalProperties = true;
addProps.language.go!.byValue = true;
obj.properties?.push(addProps);
}
}
// fix up enum types
for (const choice of values(session.model.schemas.choices)) {
choice.choiceType.language.go!.name = schemaTypeToGoType(session.model, choice.choiceType, 'Property');
if (choice.language.go!.description) {
choice.language.go!.description = parseComments(choice.language.go!.description);
}
}
for (const choice of values(session.model.schemas.sealedChoices)) {
choice.choiceType.language.go!.name = schemaTypeToGoType(session.model, choice.choiceType, 'Property');
if (choice.language.go!.description) {
choice.language.go!.description = parseComments(choice.language.go!.description);
}
}
}