export function formatValue()

in packages/codegen.go/src/helpers.ts [269:324]


export function formatValue(paramName: string, type: go.PossibleType, imports: ImportManager, defef?: boolean): string {
  // callers don't have enough context to know if paramName needs to be
  // deferenced so we track that here when specified. note that not all
  // cases will require paramName to be dereferenced.
  let star = '';
  if (defef === true) {
    star = '*';
  }
  if (go.isLiteralValue(type)) {
    // cannot use formatLiteralValue() since all values are treated as strings
    return `"${type.literal}"`;
  } else if (go.isBytesType(type)) {
    // ByteArray is a base-64 encoded value in string format
    imports.add('encoding/base64');
    return `base64.${formatBytesEncoding(type.encoding)}Encoding.EncodeToString(${paramName})`;
  } else if (go.isPrimitiveType(type)) {
    if (type.typeName === 'bool') {
      imports.add('strconv');
      return `strconv.FormatBool(${star}${paramName})`;
    } else if (type.typeName === 'int32') {
      imports.add('strconv');
      return `strconv.FormatInt(int64(${star}${paramName}), 10)`;
    } else if (type.typeName === 'int64') {
      imports.add('strconv');
      return `strconv.FormatInt(${star}${paramName}, 10)`;
    } else if (type.typeName === 'float32') {
      imports.add('strconv');
      return `strconv.FormatFloat(float64(${star}${paramName}), 'f', -1, 32)`;
    } else if (type.typeName === 'float64') {
      imports.add('strconv');
      return `strconv.FormatFloat(${star}${paramName}, 'f', -1, 64)`;
    }
  } else if (go.isTimeType(type)) {
    if (type.dateTimeFormat === 'dateType') {
      return `${paramName}.Format("${dateFormat}")`;
    } else if (type.dateTimeFormat === 'timeUnix') {
      return `timeUnix(${star}${paramName}).String()`;
    } else if (type.dateTimeFormat === 'timeRFC3339') {
      return `timeRFC3339(${star}${paramName}).String()`;
    } else {
      imports.add('time');
      let format = datetimeRFC3339Format;
      if (type.dateTimeFormat === 'dateTimeRFC1123') {
        format = datetimeRFC1123Format;
      }
      return `${paramName}.Format(${format})`;
    }
  } else if (go.isConstantType(type)) {
    if (type.type === 'string') {
      return `string(${star}${paramName})`;
    }
    imports.add('fmt');
    return `fmt.Sprintf("%v", ${star}${paramName})`;
  }
  return `${star}${paramName}`;
}