in src/generate.ts [194:271]
function exampleValue(context: ExampleContext, typeRef: reflect.TypeReference, name: string, level: number): Code {
// Process primitive types, base case
if (typeRef.primitive !== undefined) {
switch (typeRef.primitive) {
case spec.PrimitiveType.String:
return new Code(`'${name}'`);
case spec.PrimitiveType.Number:
return new Code('123');
case spec.PrimitiveType.Boolean:
return new Code('false');
case spec.PrimitiveType.Date:
return new Code('new Date()');
default:
const defaultName = escapeIdentifier(name);
return new Code(defaultName, [new AnyAssumption(defaultName)]);
}
}
// Just pick the first type if it is a union type
if (typeRef.unionOfTypes !== undefined) {
const newType = getBaseUnionType(typeRef.unionOfTypes);
return exampleValue(context, newType, name, level);
}
// If its a collection create a collection of one element
if (typeRef.arrayOfType !== undefined) {
return Code.concatAll('[', exampleValue(context, typeRef.arrayOfType, name, level), ']');
}
if (typeRef.mapOfType !== undefined) {
return exampleValueForMap(context, typeRef.mapOfType, name, level);
}
if (typeRef.fqn) {
const fqn = typeRef.fqn;
// See if we have information on this type in the assembly
const newType = context.typeSystem.findFqn(fqn);
if (fqn in SPECIAL_TYPE_EXAMPLES) {
return new Code(SPECIAL_TYPE_EXAMPLES[fqn], [new Import(newType)]);
}
if (newType.isEnumType()) {
return Code.concatAll(
typeReference(newType),
'.',
newType.members[0].name);
}
// If this is struct and we're not already rendering it (recursion breaker), expand
if (isStructType(newType)) {
if (context.rendered.has(newType.fqn)) {
// Recursion breaker -- if we go by the default behavior end up saying something like:
//
// const myProperty = {
// stringProp: 'stringProp',
// deepProp: myProperty, // <-- value recursion!
// };
//
// Which TypeScript's type analyzer can't automatically derive a type for. We need to
// annotate SOMETHING. A simple fix is to use a different variable name so the value
// isn't self-recursive.
return addAssumedVariableDeclaration(newType, '_');
}
context.rendered.add(newType.fqn);
const ret = exampleValueForStruct(context, newType, level);
context.rendered.delete(newType.fqn);
return ret;
}
// For all other types we will assume you already have a variable of the appropriate type.
return addAssumedVariableDeclaration(newType);
}
throw new Error('If this happens, then reflect.typeRefernce must have a new value');
}