export function generateClassCode()

in apps/vs-code-designer/src/app/utils/unitTests.ts [943:1005]


export function generateClassCode(classDef: ClassDefinition): string {
  const sb: string[] = [];

  if (classDef.description) {
    sb.push('    /// <summary>');
    sb.push(`    /// ${classDef.description}`);
    sb.push('    /// </summary>');
  }

  sb.push(`    public class ${classDef.className}${classDef.inheritsFrom ? ` : ${classDef.inheritsFrom}` : ''}`);
  sb.push('    {');

  for (const prop of classDef.properties) {
    if (prop.description) {
      sb.push('        /// <summary>');
      sb.push(`        /// ${prop.description}`);
      sb.push('        /// </summary>');
    }
    if (prop.jsonPropertyName) {
      sb.push(`        [JsonProperty(PropertyName="${prop.jsonPropertyName}")]`);
    }
    sb.push(`        public ${prop.propertyType} ${prop.propertyName} { get; set; }`);
    sb.push('');
  }

  sb.push('        /// <summary>');
  sb.push(`        /// Initializes a new instance of the <see cref="${classDef.className}"/> class.`);
  sb.push('        /// </summary>');
  sb.push(`        public ${classDef.className}()`);
  sb.push('        {');
  if (classDef.inheritsFrom === 'MockOutput') {
    sb.push('            this.StatusCode = HttpStatusCode.OK;');
  }

  for (const prop of classDef.properties) {
    if (prop.propertyType === 'string') {
      sb.push(`            this.${prop.propertyName} = string.Empty;`);
    } else if (prop.propertyType === 'DateTime') {
      sb.push(`            this.${prop.propertyName} = new DateTime();`);
    } else if (prop.isObject) {
      sb.push(`            this.${prop.propertyName} = new ${prop.propertyType}();`);
    } else if (prop.propertyType === 'JObject') {
      sb.push(`            this.${prop.propertyName} = new JObject();`);
    } else if (prop.propertyType.startsWith('List<')) {
      sb.push(`            this.${prop.propertyName} = new ${prop.propertyType}();`);
    } else if (prop.propertyType === 'int') {
      sb.push(`            this.${prop.propertyName} = 0;`);
    } else if (prop.propertyType === 'HttpStatusCode') {
      sb.push(`            this.${prop.propertyName} = HttpStatusCode.OK;`);
    }
  }

  sb.push('        }');
  sb.push('');
  sb.push('    }');
  sb.push('');

  for (const child of classDef.children) {
    sb.push(generateClassCode(child));
  }

  return sb.join('\n');
}