private adaptHttpOperationExamples()

in packages/typespec-go/src/tcgcadapter/clients.ts [823:887]


  private adaptHttpOperationExamples(sdkMethod: tcgc.SdkServiceMethod<tcgc.SdkHttpOperation>, method: go.Method, paramMapping: Map<tcgc.SdkHttpParameter, Array<go.Parameter>>) {
    if (sdkMethod.operation.examples) {
      for (const example of sdkMethod.operation.examples) {
        const goExample = new go.MethodExample(example.name, {summary: example.doc}, example.filePath);
        for (const param of example.parameters) {
          if (param.parameter.isApiVersionParam && param.parameter.clientDefaultValue) {
            // skip the api-version param as it's not a formal parameter
            continue;
          }
          const goParams = paramMapping.get(param.parameter);
          if (!goParams) {
            throw new AdapterError('InternalError', `can not find go param for example param ${param.parameter.name}`, NoTarget);
          }
          if (goParams.length > 1) {
            // spread case
            for (const goParam of goParams) {
              const propertyValue = (<tcgc.SdkModelExampleValue>param.value).value[(<go.PartialBodyParameter>goParam).serializedName];
              const paramExample = new go.ParameterExample(goParam, this.adaptExampleType(propertyValue, goParam?.type));
              if (goParam.group) {
                goExample.optionalParamsGroup.push(paramExample);
              } else {
                goExample.parameters.push(paramExample);
              }
            }
          } else {
            const paramExample = new go.ParameterExample(goParams[0], this.adaptExampleType(param.value, goParams[0]?.type));
            if (goParams[0]?.group) {
              goExample.optionalParamsGroup.push(paramExample);
            } else {
              goExample.parameters.push(paramExample);
            }
          }
        }
        // only handle 200 response
        const response = example.responses.find((v) => { return v.statusCode === 200; });
        if (response) {
          goExample.responseEnvelope = new go.ResponseEnvelopeExample(method.responseEnvelope);
          for (const header of response.headers) {
            const goHeader = method.responseEnvelope.headers.find(h => h.headerName === header.header.serializedName);
            if (!goHeader) {
              throw new AdapterError('InternalError', `can not find go header for example header ${header.header.serializedName}`, NoTarget);
            }
            goExample.responseEnvelope.headers.push(new go.ResponseHeaderExample(goHeader, this.adaptExampleType(header.value, goHeader.type)));
          }
          // there are some problems with LROs at present which can cause the result
          // to be undefined even though the operation returns a response.
          // TODO: https://github.com/Azure/typespec-azure/issues/1688
          if (response.bodyValue && method.responseEnvelope.result) {
            if (go.isAnyResult(method.responseEnvelope.result)) {
              goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, new go.PrimitiveType('any'));
            } else if (go.isModelResult(method.responseEnvelope.result)) {
              goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.responseEnvelope.result.modelType);
            } else if (go.isBinaryResult(method.responseEnvelope.result)) {
              goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, new go.PrimitiveType('byte'));
            } else if (go.isMonomorphicResult(method.responseEnvelope.result)) {
              goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.responseEnvelope.result.monomorphicType);
            } else if (go.isPolymorphicResult(method.responseEnvelope.result)) {
              goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.responseEnvelope.result.interfaceType);
            }
          }
        }
        method.examples.push(goExample);
      }
    }
  }