in packages/typespec-go/src/tcgcadapter/clients.ts [889:961]
private adaptExampleType(exampleType: tcgc.SdkExampleValue, goType: go.PossibleType): go.ExampleType {
switch (exampleType.kind) {
case 'string':
if (go.isConstantType(goType) || go.isBytesType(goType) || go.isLiteralValue(goType) || go.isTimeType(goType) || go.isPrimitiveType(goType)) {
return new go.StringExample(exampleType.value, goType);
}
break;
case 'number':
if (go.isConstantType(goType) || go.isLiteralValue(goType) || go.isTimeType(goType) || go.isPrimitiveType(goType)) {
return new go.NumberExample(exampleType.value, goType);
}
break;
case 'boolean':
if (go.isConstantType(goType) || go.isLiteralValue(goType) || go.isPrimitiveType(goType)) {
return new go.BooleanExample(exampleType.value, goType);
}
break;
case 'null':
return new go.NullExample(goType);
case 'unknown':
if (go.isPrimitiveType(goType) && goType.typeName === 'any') {
return new go.AnyExample(exampleType.value);
}
break;
case 'array':
if (go.isSliceType(goType)) {
const ret = new go.ArrayExample(goType);
for (const v of exampleType.value) {
ret.value.push(this.adaptExampleType(v, goType.elementType));
}
return ret;
}
break;
case 'dict':
if (go.isMapType(goType)) {
const ret = new go.DictionaryExample(goType);
for (const [k, v] of Object.entries(exampleType.value)) {
ret.value[k] = this.adaptExampleType(v, goType.valueType);
}
return ret;
}
break;
case 'union':
throw new AdapterError('UnsupportedTsp', 'unsupported example type kind union', NoTarget);
case 'model':
if (go.isModelType(goType) || go.isInterfaceType(goType)) {
let concreteType: go.ModelType | go.PolymorphicType | undefined;
if (go.isInterfaceType(goType)) {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
concreteType = goType.possibleTypes.find(t => t.discriminatorValue?.literal === exampleType.type.discriminatorValue || t.discriminatorValue?.literal.value === exampleType.type.discriminatorValue)!;
} else {
concreteType = goType;
}
if (concreteType === undefined) {
throw new AdapterError('InternalError', `can not find concrete type for example type ${exampleType.type.name}`, NoTarget);
}
const ret = new go.StructExample(concreteType);
for (const [k, v] of Object.entries(exampleType.value)) {
const field = concreteType.fields.find(f => f.serializedName === k)!;
ret.value[field.name] = this.adaptExampleType(v, field.type);
}
if (exampleType.additionalPropertiesValue) {
ret.additionalProperties = {};
for (const [k, v] of Object.entries(exampleType.additionalPropertiesValue)) {
ret.additionalProperties[k] = this.adaptExampleType(v, concreteType.fields.find(f => f.annotations.isAdditionalProperties)!.type);
}
}
return ret;
}
break;
}
throw new AdapterError('InternalError', `can not map go type into example type ${exampleType.kind}`, NoTarget);
}