Skip to content

Commit

Permalink
feat(http-client-csharp): @encode(string) support for int types (#4998
Browse files Browse the repository at this point in the history
)

- bump latest mgc
- duplicate the same logic of mgc to support `@encode(string)` for int types

part of #4919

---------

Co-authored-by: Mingzhe Huang (from Dev Box) <[email protected]>
  • Loading branch information
archerzz and Mingzhe Huang (from Dev Box) committed Aug 26, 2024
1 parent 3455ff0 commit 53122fc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,13 @@ private static MethodBodyStatement SerializeFrameworkTypeValue(Utf8JsonWriterExp
if (valueType == typeof(decimal) ||
valueType == typeof(double) ||
valueType == typeof(float) ||
valueType == typeof(long) ||
valueType == typeof(int) ||
valueType == typeof(short) ||
valueType == typeof(sbyte) ||
valueType == typeof(byte))
IsIntType(valueType))
{
if (valueSerialization.Format is SerializationFormat.Int_String &&
IsIntType(valueType))
{
return utf8JsonWriter.WriteStringValue(value.InvokeToString());
}
return utf8JsonWriter.WriteNumberValue(value);
}

Expand Down Expand Up @@ -1154,17 +1155,8 @@ public static ValueExpression GetFrameworkTypeValueExpression(Type frameworkType
return element.GetBoolean();
if (frameworkType == typeof(char))
return element.GetChar();

if (frameworkType == typeof(sbyte))
return element.GetSByte();
if (frameworkType == typeof(byte))
return element.GetByte();
if (frameworkType == typeof(short))
return element.GetInt16();
if (frameworkType == typeof(int))
return element.GetInt32();
if (frameworkType == typeof(long))
return element.GetInt64();
if (IsIntType(frameworkType))
return GetIntTypeDeserializationValueExpression(element, frameworkType, format);
if (frameworkType == typeof(float))
return element.GetSingle();
if (frameworkType == typeof(double))
Expand Down Expand Up @@ -1205,6 +1197,27 @@ public static ValueExpression GetFrameworkTypeValueExpression(Type frameworkType
throw new NotSupportedException($"Framework type {frameworkType} is not supported, please add `CodeGenMemberSerializationHooks` to specify the serialization of this type with the customized property");
}

private static ValueExpression GetIntTypeDeserializationValueExpression(JsonElementExpression element, Type type, SerializationFormat format) => format switch
{
SerializationFormat.Int_String => new TypeReference(type).Invoke(nameof(int.Parse), new List<ValueExpression> { element.GetString() }),
_ => type switch
{
Type t when t == typeof(sbyte) => element.GetSByte(),
Type t when t == typeof(byte) => element.GetByte(),
Type t when t == typeof(short) => element.GetInt16(),
Type t when t == typeof(int) => element.GetInt32(),
Type t when t == typeof(long) => element.GetInt64(),
_ => throw new NotSupportedException($"Framework type {type} is not int.")
}
};

private static bool IsIntType(Type frameworkType) =>
frameworkType == typeof(sbyte) ||
frameworkType == typeof(byte) ||
frameworkType == typeof(short) ||
frameworkType == typeof(int) ||
frameworkType == typeof(long);

private static MethodBodyStatement InvokeListAdd(ValueExpression list, ValueExpression value)
=> new InvokeInstanceMethodStatement(list, nameof(List<object>.Add), value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public static SerializationFormat GetDefaultSerializationFormat(CSharpType type)
BytesKnownEncoding.Base64Url => SerializationFormat.Bytes_Base64Url,
_ => throw new IndexOutOfRangeException($"unknown encode {primitiveType.Encode}")
},
InputPrimitiveTypeKind.Int64 or InputPrimitiveTypeKind.Int32 or InputPrimitiveTypeKind.Int16 or InputPrimitiveTypeKind.Int8
or InputPrimitiveTypeKind.UInt64 or InputPrimitiveTypeKind.UInt32 or InputPrimitiveTypeKind.UInt16 or InputPrimitiveTypeKind.UInt8
or InputPrimitiveTypeKind.Integer or InputPrimitiveTypeKind.SafeInt when primitiveType.Encode is "string" => SerializationFormat.Int_String,
_ => SerializationFormat.Default
},
_ => SerializationFormat.Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal enum SerializationFormat
Duration_Seconds_Double,
Time_ISO8601,
Bytes_Base64Url,
Bytes_Base64
Bytes_Base64,
Int_String
}
}
2 changes: 1 addition & 1 deletion src/TypeSpec.Extension/Emitter.Csharp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"dependencies": {
"@autorest/csharp": "3.0.0-beta.20240625.4",
"json-serialize-refs": "0.1.0-0",
"@typespec/http-client-csharp": "0.1.9-alpha.20240822.1"
"@typespec/http-client-csharp": "0.1.9-alpha.20240826.1"
},
"peerDependencies": {
"@azure-tools/typespec-azure-core": ">=0.36.0 <1.0.0",
Expand Down

0 comments on commit 53122fc

Please sign in to comment.