function setSchemaNames()

in powershell/plugins/sdk-cs-namer.ts [102:253]


function setSchemaNames(schemaGroups: Dictionary<Array<Schema>>, azure: boolean, serviceNamespace: string, helper: Helper) {
  const baseNamespace = new Set<string>();
  const subNamespace = new Map<string, Set<string>>();

  for (const group of values(schemaGroups)) {
    for (const schema of group) {
      if (schema.language.default.skip) {
        continue;
      }
      let thisNamespace = baseNamespace;
      let thisApiversion = '';

      // create the namespace if required
      if (azure) {
        const versions = [...values(schema.apiVersions).select(v => v.version)];
        if (schema.language.default?.uid !== 'universal-parameter-type') {
          if (versions && length(versions) > 0) {
            thisApiversion = minimum(versions);
            thisNamespace = subNamespace.get(thisApiversion) || new Set<string>();
            subNamespace.set(thisApiversion, thisNamespace);
          }
        }
      }


      // for each schema, we're going to set the name
      // to the suggested name, unless we have collisions
      // at which point, we're going to add a number (for now?)
      const details = schema.language.default;
      let schemaName = details.name;
      const apiName = (!thisApiversion) ? '' : getPascalIdentifier(`Api ${thisApiversion}`);


      let n = 1;
      while (thisNamespace.has(schemaName)) {
        schemaName = `${details.name}_${n++}`;
      }
      thisNamespace.add(schemaName);

      // object types.
      if (schema.type === SchemaType.Object) {
        schema.language.default.name = getEscapedReservedName(schemaName, 'Model');
        schema.language.csharp = {
          ...details,
          apiversion: thisApiversion,
          apiname: apiName,
          name: getEscapedReservedName(schemaName, 'Model'),
          namespace: pascalCase([serviceNamespace, '.', 'Models']),  // objects have a namespace
          fullname: getEscapedReservedName(schemaName, 'Model'),
        };
      } else if (schema.type === SchemaType.Any) {
        schema.language.csharp = {
          ...details,
          apiversion: thisApiversion,
          apiname: apiName,
          name: schemaName,
          fullname: 'object',
        };
      } else if (schema.type === SchemaType.Array) {
        schema.language.csharp = {
          ...details,
          apiversion: thisApiversion,
          apiname: apiName,
          name: schemaName,
          fullname: csharpForArray((<ArraySchema>schema).elementType, helper, (<ArraySchema>schema).nullableItems != false),
        };
      } else if (schema.type === SchemaType.Choice || schema.type === SchemaType.SealedChoice) {
        // oh, it's an enum type
        // ToDo: comment out for time being
        // const choiceSchema = <ChoiceSchema<StringSchema> | SealedChoiceSchema<StringSchema>>schema;
        // schema.language.csharp = <SchemaDetails>{
        //   ...details,
        //   interfaceName: 'I' + pascalCase(fixLeadingNumber([...deconstruct(schemaName)])),
        //   name: getPascalIdentifier(schemaName),
        //   namespace: pascalCase([serviceNamespace, '.', 'Support']),
        //   fullname: `${pascalCase([serviceNamespace, '.', 'Support'])}.${getPascalIdentifier(schemaName)}`,
        //   enum: {
        //     ...schema.language.default.enum,
        //     name: getPascalIdentifier(schema.language.default.name),
        //     values: choiceSchema.choices.map(each => {
        //       return {
        //         ...each,
        //         name: getPascalIdentifier(each.language.default.name),
        //         description: each.language.default.description
        //       };
        //     })
        //   }
        // };
        const choiceSchema = <ChoiceSchema<StringSchema> | SealedChoiceSchema<StringSchema>>schema;
        schema.language.csharp = <SchemaDetails>{
          ...details,
          interfaceName: 'I' + pascalCase(fixLeadingNumber([...deconstruct(schemaName)])),
          name: schemaName,
          namespace: pascalCase([serviceNamespace, '.', 'Support']),
          fullname: choiceSchema.extensions && !choiceSchema.extensions['x-ms-model-as-string'] && choiceSchema.choiceType.type === SchemaType.String ? schemaName : helper.GetCsharpType(choiceSchema.choiceType),
          enum: {
            ...schema.language.default.enum,
            name: schemaName,
            values: choiceSchema.choices.map(each => {
              return {
                ...each,
                name: each.language.default.name,
                description: each.language.default.description
              };
            })
          }
        };
      } else if (schema.type !== SchemaType.Dictionary) {
        // here are primitive types
        const schemaDetails = <SchemaDetails>{
          ...details,
          name: schemaName,
          fullname: helper.GetCsharpType(schema)
        };
        // add jonconverters for some types
        if (schema.type === SchemaType.Date) {
          schemaDetails.jsonConverters = schemaDetails.jsonConverters || [];
          schemaDetails.jsonConverters.push('Microsoft.Rest.Serialization.DateJsonConverter');
        } else if (schema.type === SchemaType.UnixTime) {
          schemaDetails.jsonConverters = schemaDetails.jsonConverters || [];
          schemaDetails.jsonConverters.push('Microsoft.Rest.Serialization.UnixTimeJsonConverter');
        }
        schema.language.csharp = schemaDetails;
        // xichen: for invalid namespace case, we won't create model class. So we do not need consider dup case
        thisNamespace.delete(schemaName);
      } else {
        // handle dictionary
        const rawElementType = (<DictionarySchema>schema).elementType;
        let elementType = rawElementType;
        if ((rawElementType.type === SchemaType.Choice || rawElementType.type === SchemaType.SealedChoice) && !helper.IsEnum(rawElementType)) {
          elementType = (<ChoiceSchema | SealedChoiceSchema>rawElementType).choiceType;
        }

        let valueType = helper.GetCsharpType(elementType) ? helper.GetCsharpType(elementType) : (rawElementType.type === SchemaType.Array ? csharpForArray((<ArraySchema>rawElementType).elementType, helper): rawElementType.language.default.name);
        if (rawElementType.type === 'any') {
          valueType = 'object';
        }
        if (((helper.GetCsharpType(elementType) && valueType !== 'string') || helper.IsEnum(rawElementType)) && (<DictionarySchema>schema).nullableItems != false) {
          valueType += '?';
        }
        schema.language.csharp = {
          ...details,
          apiversion: thisApiversion,
          apiname: apiName,
          name: schemaName,
          fullname: csharpForDict(<DictionarySchema>schema, helper),
        };
      }
    }
  }

}