protected rawValueToString()

in packages/autorest.gotest/src/generator/mockTestGenerator.ts [449:520]


  protected rawValueToString(rawValue: any, schema: Schema, isPtr: boolean): string {
    let ret = JSON.stringify(rawValue);
    const goType = this.getLanguageName(schema);
    if (schema.type === SchemaType.Choice) {
      if ((<ChoiceSchema>schema).choiceType.type === SchemaType.String) {
        try {
          const choiceValue = Helper.findChoiceValue(<ChoiceSchema>schema, rawValue);
          ret = this.context.packageName + '.' + this.getLanguageName(choiceValue);
        } catch (error) {
          ret = `${this.context.packageName}.${this.getLanguageName(schema)}(${this.getStringValue(rawValue)})`;
        }
      } else {
        ret = `${this.context.packageName}.${this.getLanguageName(schema)}(${this.getNumberValue(rawValue)})`;
      }
    } else if (schema.type === SchemaType.SealedChoice) {
      const choiceValue = Helper.findChoiceValue(<ChoiceSchema>schema, rawValue);
      ret = this.context.packageName + '.' + this.getLanguageName(choiceValue);
    } else if (goType === 'string') {
      ret = this.getStringValue(rawValue);
    } else if (schema.type === SchemaType.ByteArray) {
      ret = `[]byte(${this.getStringValue(rawValue)})`;
    } else if (['int32', 'int64', 'float32', 'float64'].indexOf(goType) >= 0) {
      ret = `${this.getNumberValue(rawValue)}`;
    } else if (goType === 'time.Time') {
      if (schema.type === SchemaType.UnixTime) {
        this.context.importManager.add('time');
        ret = `time.Unix(${this.getNumberValue(rawValue)}, 0)`;
      } else if (schema.type === SchemaType.Date) {
        this.context.importManager.add('time');
        ret = `func() time.Time { t, _ := time.Parse("2006-01-02", ${this.getStringValue(rawValue)}); return t}()`;
      } else {
        this.context.importManager.add('time');
        const timeFormat = (<DateTimeSchema>schema).format === 'date-time-rfc1123' ? 'time.RFC1123' : 'time.RFC3339Nano';
        ret = `func() time.Time { t, _ := time.Parse(${timeFormat}, ${formatRFC3339Nano(rawValue)}); return t}()`;
      }
    } else if (goType === 'map[string]any' || goType === 'map[string]interface{}') {
      ret = this.objectToString(rawValue);
    } else if ((goType === 'any' || goType === 'interface{}') && Array.isArray(rawValue)) {
      ret = this.arrayToString(rawValue);
    } else if ((goType === 'any' || goType === 'interface{}') && typeof rawValue === 'object') {
      ret = this.objectToString(rawValue);
    } else if ((goType === 'any' || goType === 'interface{}') && _.isNumber(rawValue)) {
      ret = `float64(${this.getNumberValue(rawValue)})`;
    } else if ((goType === 'any' || goType === 'interface{}') && _.isString(rawValue)) {
      ret = this.getStringValue(rawValue);
    } else if (goType === 'bool') {
      ret = this.getBoolValue(rawValue);
    }

    if (isPtr) {
      const ptrConverts = {
        string: 'Ptr',
        bool: 'Ptr',
        'time.Time': 'Ptr',
        int32: 'Ptr[int32]',
        int64: 'Ptr[int64]',
        float32: 'Ptr[float32]',
        float64: 'Ptr[float64]',
      };

      if ([SchemaType.Choice, SchemaType.SealedChoice].indexOf(schema.type) >= 0) {
        ret = `to.Ptr(${ret})`;
      } else if (Object.prototype.hasOwnProperty.call(ptrConverts, goType)) {
        ret = `to.${ptrConverts[goType]}(${ret})`;
        this.context.importManager.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/to');
      } else {
        ret = '&' + ret;
      }
    }

    return ret;
  }