in packages/typespec-go/src/tcgcadapter/types.ts [655:761]
private getLiteralValue(constType: tcgc.SdkConstantType | tcgc.SdkEnumValueType): go.LiteralValue {
if (constType.kind === 'enumvalue') {
const valueName = `${naming.ensureNameCase(constType.enumType.name)}${naming.ensureNameCase(constType.name)}`;
const keyName = `literal-${valueName}`;
let literalConst = this.types.get(keyName);
if (literalConst) {
return <go.LiteralValue>literalConst;
}
const constValue = this.constValues.get(valueName);
if (!constValue) {
throw new AdapterError('InternalError', `failed to find const value for ${constType.name} in enum ${constType.enumType.name}`, constType.__raw?.node ?? tsp.NoTarget);
}
literalConst = new go.LiteralValue(this.getConstantType(constType.enumType), constValue);
this.types.set(keyName, literalConst);
return literalConst;
}
switch (constType.valueType.kind) {
case 'boolean': {
const keyName = `literal-boolean-${constType.value}`;
let literalBool = this.types.get(keyName);
if (literalBool) {
return <go.LiteralValue>literalBool;
}
literalBool = new go.LiteralValue(new go.PrimitiveType('bool'), constType.value);
this.types.set(keyName, literalBool);
return literalBool;
}
case 'bytes': {
const keyName = `literal-bytes-${constType.value}`;
let literalByteArray = this.types.get(keyName);
if (literalByteArray) {
return <go.LiteralValue>literalByteArray;
}
literalByteArray = new go.LiteralValue(this.adaptBytesType(constType.valueType), constType.value);
this.types.set(keyName, literalByteArray);
return literalByteArray;
}
case 'decimal':
case 'decimal128': {
const keyName = `literal-${constType.valueType.kind}-${constType.value}`;
let literalDecimal = this.types.get(keyName);
if (literalDecimal) {
return <go.LiteralValue>literalDecimal;
}
literalDecimal = new go.LiteralValue(new go.PrimitiveType('float64'), constType.value);
this.types.set(keyName, literalDecimal);
return literalDecimal;
}
/*case 'date':
case 'datetime': {
// TODO: tcgc doesn't expose the encoding for date/datetime constant types
const encoding = getDateTimeEncoding(constType.encode);
const keyName = `literal-${encoding}-${constType.value}`;
let literalTime = this.types.get(keyName);
if (literalTime) {
return <go.LiteralValue>literalTime;
}
literalTime = new go.LiteralValue(new go.TimeType(encoding), constType.value);
this.types.set(keyName, literalTime);
return literalTime;
}*/
case 'int8':
case 'int16':
case 'int32':
case 'int64':
case 'uint8':
case 'uint16':
case 'uint32':
case 'uint64': {
const keyName = `literal-${constType.valueType.kind}-${constType.value}`;
let literalInt = this.types.get(keyName);
if (literalInt) {
return <go.LiteralValue>literalInt;
}
literalInt = new go.LiteralValue(new go.PrimitiveType(constType.valueType.kind), constType.value);
this.types.set(keyName, literalInt);
return literalInt;
}
case 'float32':
case 'float64': {
const keyName = `literal-${constType.valueType.kind}-${constType.value}`;
let literalFloat = this.types.get(keyName);
if (literalFloat) {
return <go.LiteralValue>literalFloat;
}
literalFloat = new go.LiteralValue(new go.PrimitiveType(constType.valueType.kind), constType.value);
this.types.set(keyName, literalFloat);
return literalFloat;
}
case 'string':
case 'url': {
const keyName = `literal-string-${constType.value}`;
let literalString = this.types.get(keyName);
if (literalString) {
return <go.LiteralValue>literalString;
}
literalString = new go.LiteralValue(new go.PrimitiveType('string'), constType.value);
this.types.set(keyName, literalString);
return literalString;
}
default:
throw new AdapterError('UnsupportedTsp', `unsupported kind ${constType.valueType.kind} for LiteralValue`, constType.valueType.__raw?.node ?? tsp.NoTarget);
}
// TODO: tcgc doesn't support duration as a literal value
}