Skip to content

Commit

Permalink
sync emitter/generator changes from Azure/autorest.csharp#4800 (#3548)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang authored Jun 7, 2024
1 parent 0f344ac commit 97aa495
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 81 deletions.
20 changes: 9 additions & 11 deletions packages/http-client-csharp/emitter/src/lib/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,18 @@ function fromUnionType(
models: Map<string, InputModelType>,
enums: Map<string, InputEnumType>
): InputUnionType | InputType {
const itemTypes: InputType[] = [];
const variantTypes: InputType[] = [];
for (const value of union.values) {
const inputType = fromSdkType(value, context, models, enums);
itemTypes.push(inputType);
const variantType = fromSdkType(value, context, models, enums);
variantTypes.push(variantType);
}

return itemTypes.length > 1
? {
Kind: InputTypeKind.Union,
Name: InputTypeKind.Union,
UnionItemTypes: itemTypes,
IsNullable: false,
}
: itemTypes[0];
return {
Kind: "union",
Name: union.name,
VariantTypes: variantTypes,
IsNullable: false,
};
}

function fromSdkConstantType(
Expand Down
2 changes: 1 addition & 1 deletion packages/http-client-csharp/emitter/src/lib/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function loadOperation(
if (isInputLiteralType(contentTypeParameter.Type)) {
mediaTypes.push(contentTypeParameter.DefaultValue?.Value);
} else if (isInputUnionType(contentTypeParameter.Type)) {
for (const unionItem of contentTypeParameter.Type.UnionItemTypes) {
for (const unionItem of contentTypeParameter.Type.VariantTypes) {
if (isInputLiteralType(unionItem)) {
mediaTypes.push(unionItem.Value as string);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

export enum InputTypeKind {
Union = "Union",
Model = "Model",
Array = "Array",
Dictionary = "Dictionary",
Expand Down
8 changes: 4 additions & 4 deletions packages/http-client-csharp/emitter/src/type/input-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ export interface InputDurationType extends InputTypeBase {
}

export interface InputUnionType extends InputTypeBase {
Kind: InputTypeKind.Union; // TODO -- will change to TCGC value in future refactor
Name: InputTypeKind.Union; // union type does not really have a name right now, we just use its kind
UnionItemTypes: InputType[];
Kind: "union";
Name: string;
VariantTypes: InputType[];
}

export function isInputUnionType(type: InputType): type is InputUnionType {
return type.Kind === InputTypeKind.Union;
return type.Kind === "union";
}

export interface InputModelType extends InputTypeBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class ScmTypeFactory : TypeFactory
public override CSharpType CreateCSharpType(InputType inputType) => inputType switch
{
InputLiteralType literalType => CSharpType.FromLiteral(CreateCSharpType(literalType.ValueType), literalType.Value),
InputUnionType unionType => CSharpType.FromUnion(unionType.UnionItemTypes.Select(CreateCSharpType).ToArray(), unionType.IsNullable),
InputUnionType unionType => CSharpType.FromUnion(unionType.VariantTypes.Select(CreateCSharpType).ToArray(), unionType.IsNullable),
InputListType { IsEmbeddingsVector: true } listType => new CSharpType(typeof(ReadOnlyMemory<>), listType.IsNullable, CreateCSharpType(listType.ElementType)),
InputListType listType => new CSharpType(typeof(IList<>), listType.IsNullable, CreateCSharpType(listType.ElementType)),
InputDictionaryType dictionaryType => new CSharpType(typeof(IDictionary<,>), inputType.IsNullable, typeof(string), CreateCSharpType(dictionaryType.ValueType)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static InputParameterExample BuildParameterExample(InputParameter parame
// when it is constant, it could have DefaultValue
value = InputExampleValue.Value(parameter.Type, parameter.DefaultValue.Value);
}
else if (parameter.Type is InputUnionType unionType && unionType.UnionItemTypes.First() is InputLiteralType literalType)
else if (parameter.Type is InputUnionType unionType && unionType.VariantTypes[0] is InputLiteralType literalType)
{
// or it could be a union of literal types
value = InputExampleValue.Value(parameter.Type, literalType.Value);
Expand Down Expand Up @@ -106,7 +106,7 @@ private static InputParameterExample BuildParameterExample(InputParameter parame
InputPrimitiveType primitiveType => BuildPrimitiveExampleValue(primitiveType, hint),
InputLiteralType literalType => InputExampleValue.Value(literalType, literalType.Value),
InputModelType modelType => BuildModelExampleValue(modelType, useAllParameters, visitedModels),
InputUnionType unionType => BuildExampleValue(unionType.UnionItemTypes.First(), hint, useAllParameters, visitedModels),
InputUnionType unionType => BuildExampleValue(unionType.VariantTypes[0], hint, useAllParameters, visitedModels),
InputDateTimeType dateTimeType => BuildDateTimeExampleValue(dateTimeType),
InputDurationType durationType => BuildDurationExampleValue(durationType),
_ => InputExampleValue.Object(type, new Dictionary<string, InputExampleValue>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Microsoft.Generator.CSharp.Input
{
public class InputUnionType : InputType
{
public InputUnionType(string name, IReadOnlyList<InputType> unionItemTypes, bool isNullable) : base(name, isNullable)
public InputUnionType(string name, IReadOnlyList<InputType> variantTypes, bool isNullable) : base(name, isNullable)
{
UnionItemTypes = unionItemTypes;
VariantTypes = variantTypes;
}

public IReadOnlyList<InputType> UnionItemTypes { get; }
public IReadOnlyList<InputType> VariantTypes { get; internal set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,8 @@ public static InputModelType CreateModelType(ref Utf8JsonReader reader, string?

id = id ?? throw new JsonException();

// skip every other property until we have a name
while (name == null)
{
var hasName = reader.TryReadString(nameof(InputModelType.Name), ref name);
if (!hasName)
{
reader.SkipProperty();
}
}
name = name ?? throw new JsonException("Model must have name");

// create an empty model to resolve circular references
var model = new InputModelType(name, null, null, null, null, InputModelTypeUsage.None, null!, null, new List<InputModelType>(), null, null, null, false);
var model = new InputModelType(name!, null, null, null, null, InputModelTypeUsage.None, null!, null, new List<InputModelType>(), null, null, null, false);
resolver.AddReference(id, model);

bool isNullable = false;
Expand All @@ -62,7 +51,8 @@ public static InputModelType CreateModelType(ref Utf8JsonReader reader, string?
// read all possible properties and throw away the unknown properties
while (reader.TokenType != JsonTokenType.EndObject)
{
var isKnownProperty = reader.TryReadBoolean(nameof(InputModelType.IsNullable), ref isNullable)
var isKnownProperty = reader.TryReadString(nameof(InputModelType.Name), ref name)
|| reader.TryReadBoolean(nameof(InputModelType.IsNullable), ref isNullable)
|| reader.TryReadString(nameof(InputModelType.Namespace), ref ns)
|| reader.TryReadString(nameof(InputModelType.Accessibility), ref accessibility)
|| reader.TryReadString(nameof(InputModelType.Deprecated), ref deprecated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private InputType CreateObject(ref Utf8JsonReader reader, JsonSerializerOptions
}

private const string LiteralKind = "constant";
private const string UnionKind = "Union";
private const string UnionKind = "union";
private const string ModelKind = "Model";
private const string EnumKind = "enum";
private const string ArrayKind = "Array";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
Expand All @@ -24,59 +24,39 @@ public override void Write(Utf8JsonWriter writer, InputUnionType value, JsonSeri

public static InputUnionType CreateInputUnionType(ref Utf8JsonReader reader, string? id, string? name, JsonSerializerOptions options, ReferenceResolver resolver)
{
var isFirstProperty = id == null;
if (id == null)
{
reader.TryReadReferenceId(ref id);
}

id = id ?? throw new JsonException();

// create an empty model to resolve circular references
var union = new InputUnionType(null!, null!, false);
resolver.AddReference(id, union);

bool isNullable = false;
var unionItemTypes = new List<InputType>();
IReadOnlyList<InputType>? variantTypes = null;
while (reader.TokenType != JsonTokenType.EndObject)
{
var isKnownProperty = reader.TryReadReferenceId(ref isFirstProperty, ref id)
|| reader.TryReadString(nameof(InputUnionType.Name), ref name)
var isKnownProperty = reader.TryReadString(nameof(InputUnionType.Name), ref name)
|| reader.TryReadWithConverter(nameof(InputUnionType.VariantTypes), options, ref variantTypes)
|| reader.TryReadBoolean(nameof(InputUnionType.IsNullable), ref isNullable);

if (isKnownProperty)
{
continue;
}

if (reader.GetString() == nameof(InputUnionType.UnionItemTypes))
{
reader.Read();
CreateUnionItemTypes(ref reader, unionItemTypes, options);
}
else
if (!isKnownProperty)
{
reader.SkipProperty();
}
}

name = name ?? throw new JsonException($"{nameof(InputLiteralType)} must have a name.");
if (unionItemTypes == null || unionItemTypes.Count == 0)
union.Name = name ?? throw new JsonException($"{nameof(InputLiteralType)} must have a name.");
if (variantTypes == null || variantTypes.Count == 0)
{
throw new JsonException("Union must have a least one union type");
}

var unionType = new InputUnionType(name, unionItemTypes, isNullable);
if (id != null)
{
resolver.AddReference(id, unionType);
}
return unionType;
}

private static void CreateUnionItemTypes(ref Utf8JsonReader reader, ICollection<InputType> itemTypes, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray)
{
throw new JsonException();
}
reader.Read();

while (reader.TokenType != JsonTokenType.EndArray)
{
var type = reader.ReadWithConverter<InputType>(options);
itemTypes.Add(type ?? throw new JsonException($"null {nameof(InputType)} isn't allowed"));
}
reader.Read();
union.VariantTypes = variantTypes;
union.IsNullable = isNullable;
return union;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@
"Description": "required Union",
"Type": {
"$id": "64",
"Kind": "Union",
"Name": "Union",
"UnionItemTypes": [
"Kind": "union",
"Name": "ThingRequiredUnion",
"VariantTypes": [
{
"$id": "65",
"Kind": "string",
Expand Down

0 comments on commit 97aa495

Please sign in to comment.