private getModel()

in packages/typespec-go/src/tcgcadapter/types.ts [492:563]


  private getModel(model: tcgc.SdkModelType): go.ModelType | go.PolymorphicType {
    let modelName = naming.ensureNameCase(model.name);
    if (model.access === 'internal') {
      modelName = naming.getEscapedReservedName(uncapitalize(modelName), 'Model');
    }
    let modelType = this.types.get(modelName);
    if (modelType) {
      return <go.ModelType | go.PolymorphicType>modelType;
    }
  
    let usage = go.UsageFlags.None;
    if (model.usage & tsp.UsageFlags.Input) {
      usage = go.UsageFlags.Input;
    }
    if (model.usage & tsp.UsageFlags.Output) {
      usage |= go.UsageFlags.Output;
    }

    const annotations = new go.ModelAnnotations(false, <tcgc.UsageFlags>(model.usage & tcgc.UsageFlags.MultipartFormData) === tcgc.UsageFlags.MultipartFormData);
    if (model.discriminatedSubtypes || model.discriminatorValue) {
      let iface: go.InterfaceType | undefined;
      let discriminatorLiteral: go.LiteralValue | undefined;

      if (model.discriminatedSubtypes) {
        // root type, we can get the InterfaceType directly from it
        iface = this.getInterfaceType(model);
      } else {
        // walk the parents until we find the first root type
        let parent = model.baseModel;
        while (parent) {
          if (parent.discriminatedSubtypes) {
            iface = this.getInterfaceType(parent);
            break;
          }
          parent = parent.baseModel;
        }
        if (!iface) {
          throw new AdapterError('InternalError', `failed to find discriminator interface name for type ${model.name}`, tsp.NoTarget);
        }

        // find the discriminator property and create the discriminator literal based on it
        for (const prop of model.properties) {
          if (prop.kind === 'property' && prop.discriminator) {
            discriminatorLiteral = this.getDiscriminatorLiteral(prop);
            break;
          }
        }
      }

      modelType = new go.PolymorphicType(modelName, iface, annotations, usage);
      (<go.PolymorphicType>modelType).discriminatorValue = discriminatorLiteral;
    } else {
      modelType = new go.ModelType(modelName, annotations, usage);
      // polymorphic types don't have XMLInfo
      modelType.xml = adaptXMLInfo(model.decorators);
    }

    modelType.docs.summary = model.summary;
    modelType.docs.description = model.doc;
    if (modelType.docs.summary) {
      if (!modelType.docs.summary.startsWith(modelName)) {
        modelType.docs.summary = `${modelName} - ${modelType.docs.summary}`;
      }
    } else if (modelType.docs.description) {
      if (!modelType.docs.description.startsWith(modelName)) {
        modelType.docs.description = `${modelName} - ${modelType.docs.description}`;
      }
    }

    this.types.set(modelName, modelType);
    return modelType;
  }