private getModelField()

in packages/typespec-go/src/tcgcadapter/types.ts [575:619]


  private getModelField(prop: tcgc.SdkModelPropertyType, modelType: tcgc.SdkModelType): go.ModelField {
    if (prop.kind !== 'path' && prop.kind !== 'property') {
      throw new AdapterError('UnsupportedTsp', `unsupported kind ${prop.kind} for property ${prop.name} in model ${modelType.name}`, prop.__raw?.node ?? tsp.NoTarget);
    }
    const annotations = new go.ModelFieldAnnotations(prop.optional === false, false, false, false);
    // for multipart/form data containing models, default to fields not being pointer-to-type as we
    // don't have to deal with JSON patch shenanigans. only the optional fields will be pointer-to-type.
    const isMultipartFormData = <tcgc.UsageFlags>(modelType.usage & tcgc.UsageFlags.MultipartFormData) === tcgc.UsageFlags.MultipartFormData;
    let fieldByValue = isMultipartFormData ? true : isTypePassedByValue(prop.type);
    if (isMultipartFormData && prop.kind === 'property' && prop.optional) {
      fieldByValue = false;
    }
    let type = this.getPossibleType(prop.type, isMultipartFormData, true);
    if (prop.kind === 'property') {
      if (prop.isMultipartFileInput) {
        type = this.getMultipartContent(prop.type.kind === 'array');
      }
      if (prop.visibility) {
        // the field is read-only IFF the only visibility attribute present is Read.
        // a field can have Read & Create set which means it's required on input and
        // returned on output.
        if (prop.visibility.length === 1 && prop.visibility[0] === http.Visibility.Read) {
          annotations.readOnly = true;
        }
      }
    }
    const field = new go.ModelField(naming.capitalize(naming.ensureNameCase(prop.name)), type, fieldByValue, prop.serializedName, annotations);
    field.docs.summary = prop.summary;
    field.docs.description = prop.doc;
    if (prop.kind === 'path') {
      // for ARM resources, a property of kind path is usually the model
      // key and will be exposed as a discrete method parameter. this also
      // means that the value is read-only.
      annotations.readOnly = true;
    } else if (prop.discriminator && modelType.discriminatorValue) {
      // the presence of modelType.discriminatorValue tells us that this
      // property is on a model that's not the root discriminator
      annotations.isDiscriminator = true;
      field.defaultValue = this.getDiscriminatorLiteral(prop);
    }
  
    field.xml = adaptXMLInfo(prop.decorators, field);
  
    return field;
  }