Skip to content

Commit

Permalink
sync changes of consolidating TCGC's Array and Dictionary type (#3658)
Browse files Browse the repository at this point in the history
Sync up changes from this PR:
Azure/autorest.csharp#4803
  • Loading branch information
ArcturusZhang authored Jun 26, 2024
1 parent 367fa13 commit 0af2c98
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 187 deletions.
15 changes: 6 additions & 9 deletions packages/http-client-csharp/emitter/src/lib/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import { InputEnumTypeValue } from "../type/input-enum-type-value.js";
import { InputModelProperty } from "../type/input-model-property.js";
import { InputTypeKind } from "../type/input-type-kind.js";
import {
InputArrayType,
InputDateTimeType,
InputDictionaryType,
InputDurationType,
InputEnumType,
InputListType,
InputLiteralType,
InputModelType,
InputNullableType,
Expand Down Expand Up @@ -112,8 +112,7 @@ export function fromSdkModelType(

inputModelType.InheritedDictionaryType = modelType.additionalProperties
? {
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand Down Expand Up @@ -374,8 +373,7 @@ function fromSdkDictionaryType(
enums: Map<string, InputEnumType>
): InputDictionaryType {
return {
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: fromSdkType(dictionaryType.keyType, context, models, enums),
ValueType: fromSdkType(dictionaryType.valueType, context, models, enums),
};
Expand All @@ -386,11 +384,10 @@ function fromSdkArrayType(
context: SdkContext,
models: Map<string, InputModelType>,
enums: Map<string, InputEnumType>
): InputListType {
): InputArrayType {
return {
Kind: InputTypeKind.Array,
Name: InputTypeKind.Array,
ElementType: fromSdkType(arrayType.valueType, context, models, enums),
Kind: "array",
ValueType: fromSdkType(arrayType.valueType, context, models, enums),
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/http-client-csharp/emitter/src/lib/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import { InputOperationParameterKind } from "../type/input-operation-parameter-k
import { InputOperation } from "../type/input-operation.js";
import { InputParameter } from "../type/input-parameter.js";
import {
InputArrayType,
InputEnumType,
InputListType,
InputModelType,
InputType,
isInputEnumType,
Expand Down Expand Up @@ -245,7 +245,7 @@ export function loadOperation(
IsContentType: isContentType,
IsEndpoint: false,
SkipUrlEncoding: false, //TODO: retrieve out value from extension
Explode: (inputType as InputListType).ElementType && format === "multi" ? true : false,
Explode: (inputType as InputArrayType).ValueType && format === "multi" ? true : false,
Kind: kind,
ArraySerializationDelimiter: format ? collectionFormatToDelimMap[format] : undefined,
} as InputParameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@

export enum InputTypeKind {
Model = "Model",
Array = "Array",
Dictionary = "Dictionary",
Intrinsic = "Intrinsic",
}
18 changes: 8 additions & 10 deletions packages/http-client-csharp/emitter/src/type/input-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type InputType =
| InputUnionType
| InputModelType
| InputEnumType
| InputListType
| InputArrayType
| InputDictionaryType
| InputNullableType;

Expand Down Expand Up @@ -111,23 +111,21 @@ export function isInputEnumType(type: InputType): type is InputEnumType {
return type.Kind === "enum";
}

export interface InputListType extends InputTypeBase {
Kind: InputTypeKind.Array; // TODO -- will change to TCGC value in future refactor
Name: InputTypeKind.Array; // array type does not really have a name right now, we just use its kind
ElementType: InputType;
export interface InputArrayType extends InputTypeBase {
Kind: "array";
ValueType: InputType;
}

export function isInputListType(type: InputType): type is InputListType {
return type.Kind === InputTypeKind.Array;
export function isInputArrayType(type: InputType): type is InputArrayType {
return type.Kind === "array";
}

export interface InputDictionaryType extends InputTypeBase {
Kind: InputTypeKind.Dictionary; // TODO -- will change to TCGC value in future refactor
Name: InputTypeKind.Dictionary; // dictionary type does not really have a name right now, we just use its kind
Kind: "dict";
KeyType: InputType;
ValueType: InputType;
}

export function isInputDictionaryType(type: InputType): type is InputDictionaryType {
return type.Kind === InputTypeKind.Dictionary;
return type.Kind === "dict";
}
41 changes: 14 additions & 27 deletions packages/http-client-csharp/emitter/test/Unit/model-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import assert, { deepStrictEqual, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { createModel } from "../../src/lib/client-model-builder.js";
import { InputModelProperty } from "../../src/type/input-model-property.js";
import { InputTypeKind } from "../../src/type/input-type-kind.js";
import { InputDictionaryType } from "../../src/type/input-type.js";
import {
createEmitterContext,
Expand Down Expand Up @@ -442,8 +441,7 @@ op op5(@body body: ExtendsFooArray): ExtendsFooArray;
// assert the inherited dictionary type is expected
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand All @@ -456,8 +454,7 @@ op op5(@body body: ExtendsFooArray): ExtendsFooArray;
);
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand All @@ -470,8 +467,7 @@ op op5(@body body: ExtendsFooArray): ExtendsFooArray;
);
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand All @@ -484,8 +480,7 @@ op op5(@body body: ExtendsFooArray): ExtendsFooArray;
);
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand All @@ -495,15 +490,13 @@ op op5(@body body: ExtendsFooArray): ExtendsFooArray;
);
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
ValueType: {
Kind: InputTypeKind.Array,
Name: InputTypeKind.Array,
ElementType: fooModel,
Kind: "array",
ValueType: fooModel,
},
} as InputDictionaryType,
extendsFooArrayModel.InheritedDictionaryType
Expand Down Expand Up @@ -593,8 +586,7 @@ op op5(@body body: IsFooArray): IsFooArray;
// assert the inherited dictionary type is expected
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand All @@ -607,8 +599,7 @@ op op5(@body body: IsFooArray): IsFooArray;
);
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand All @@ -621,8 +612,7 @@ op op5(@body body: IsFooArray): IsFooArray;
);
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand All @@ -635,8 +625,7 @@ op op5(@body body: IsFooArray): IsFooArray;
);
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
Expand All @@ -646,15 +635,13 @@ op op5(@body body: IsFooArray): IsFooArray;
);
deepStrictEqual(
{
Kind: InputTypeKind.Dictionary,
Name: InputTypeKind.Dictionary,
Kind: "dict",
KeyType: {
Kind: "string",
},
ValueType: {
Kind: InputTypeKind.Array,
Name: InputTypeKind.Array,
ElementType: fooModel,
Kind: "array",
ValueType: fooModel,
},
} as InputDictionaryType,
isFooArrayModel.InheritedDictionaryType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { TestHost } from "@typespec/compiler/testing";
import assert, { deepStrictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { createModel } from "../../src/lib/client-model-builder.js";
import { InputTypeKind } from "../../src/type/input-type-kind.js";
import { InputEnumType, InputListType } from "../../src/type/input-type.js";
import { InputEnumType } from "../../src/type/input-type.js";
import {
createEmitterContext,
createEmitterTestHost,
Expand All @@ -29,16 +28,15 @@ describe("Test GetInputType for array", () => {
const context = createEmitterContext(program);
const sdkContext = createNetSdkContext(context);
const root = createModel(sdkContext);
deepStrictEqual(root.Clients[0].Operations[0].Parameters[0].Type.Kind, InputTypeKind.Array);
deepStrictEqual(root.Clients[0].Operations[0].Parameters[0].Type.Kind, "array");
deepStrictEqual(
{
Kind: InputTypeKind.Array,
Name: InputTypeKind.Array,
ElementType: {
Kind: "array",
ValueType: {
Kind: "string",
Encode: undefined,
},
} as InputListType,
},
root.Clients[0].Operations[0].Parameters[0].Type
);
});
Expand All @@ -53,16 +51,15 @@ describe("Test GetInputType for array", () => {
const context = createEmitterContext(program);
const sdkContext = createNetSdkContext(context);
const root = createModel(sdkContext);
deepStrictEqual(root.Clients[0].Operations[0].Responses[0].BodyType?.Kind, InputTypeKind.Array);
deepStrictEqual(root.Clients[0].Operations[0].Responses[0].BodyType?.Kind, "array");
deepStrictEqual(
{
Kind: InputTypeKind.Array,
Name: InputTypeKind.Array,
ElementType: {
Kind: "array",
ValueType: {
Kind: "string",
Encode: undefined,
},
} as InputListType,
},
root.Clients[0].Operations[0].Responses[0].BodyType
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void TestBuildConstructor_ValidateConstructors()
var properties = new List<InputModelProperty>{
new InputModelProperty("requiredString", "requiredString", "", InputPrimitiveType.String, true, false, false),
new InputModelProperty("OptionalInt", "optionalInt", "", InputPrimitiveType.Int32, false, false, false),
new InputModelProperty("requiredCollection", "requiredCollection", "", new InputListType("List", new InputPrimitiveType(InputPrimitiveTypeKind.String), false), true, false, false),
new InputModelProperty("requiredCollection", "requiredCollection", "", new InputArrayType("List", new InputPrimitiveType(InputPrimitiveTypeKind.String), false), true, false, false),
new InputModelProperty("requiredDictionary", "requiredDictionary", "", new InputDictionaryType("Dictionary", new InputPrimitiveType(InputPrimitiveTypeKind.String), new InputPrimitiveType(InputPrimitiveTypeKind.String)), true, false, false),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static InputParameterExample BuildParameterExample(InputParameter parame

private static InputExampleValue BuildExampleValue(InputType type, string? hint, bool useAllParameters, HashSet<InputModelType> visitedModels) => type switch
{
InputListType listType => BuildListExampleValue(listType, hint, useAllParameters, visitedModels),
InputArrayType listType => BuildListExampleValue(listType, hint, useAllParameters, visitedModels),
InputDictionaryType dictionaryType => BuildDictionaryExampleValue(dictionaryType, hint, useAllParameters, visitedModels),
InputEnumType enumType => BuildEnumExampleValue(enumType),
InputPrimitiveType primitiveType => BuildPrimitiveExampleValue(primitiveType, hint),
Expand All @@ -114,9 +114,9 @@ private static InputParameterExample BuildParameterExample(InputParameter parame
_ => InputExampleValue.Object(type, new Dictionary<string, InputExampleValue>())
};

private static InputExampleValue BuildListExampleValue(InputListType listType, string? hint, bool useAllParameters, HashSet<InputModelType> visitedModels)
private static InputExampleValue BuildListExampleValue(InputArrayType listType, string? hint, bool useAllParameters, HashSet<InputModelType> visitedModels)
{
var exampleElementValue = BuildExampleValue(listType.ElementType, hint, useAllParameters, visitedModels);
var exampleElementValue = BuildExampleValue(listType.ValueType, hint, useAllParameters, visitedModels);

return InputExampleValue.List(listType, new[] { exampleElementValue });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ namespace Microsoft.Generator.CSharp.Input
/// <summary>
/// Represents an input list type.
/// </summary>
public sealed class InputListType : InputType
public sealed class InputArrayType : InputType
{
/// <summary>Creates an instance of <see cref="InputListType"/>.</summary>
/// <summary>Creates an instance of <see cref="InputArrayType"/>.</summary>
/// <param name="Name">The name of the list type.</param>
/// <param name="ElementType">The element's <see cref="InputType"/>.</param>
/// <param name="IsEmbeddingsVector">Flag used to determine if the input list type is embedding vector.</param>
/// <param name="IsNullable">Flag used to determine if the input list type is nullable.</param>
public InputListType(string name, InputType elementType, bool isEmbeddingsVector) : base(name)
public InputArrayType(string name, InputType valueType, bool isEmbeddingsVector) : base(name)
{
ElementType = elementType;
ValueType = valueType;
IsEmbeddingsVector = isEmbeddingsVector;
}

public InputType ElementType { get; }
public InputType ValueType { get; }
public bool IsEmbeddingsVector { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ internal InputType GetCollectionEquivalent(InputType inputType)
{
switch (this)
{
case InputListType listType:
return new InputListType(
case InputArrayType listType:
return new InputArrayType(
listType.Name,
listType.ElementType.GetCollectionEquivalent(inputType),
listType.ValueType.GetCollectionEquivalent(inputType),
listType.IsEmbeddingsVector);
case InputDictionaryType dictionaryType:
return new InputDictionaryType(
Expand Down
Loading

0 comments on commit 0af2c98

Please sign in to comment.