Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync up the changes of bumping version of TCGC #3678

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions packages/http-client-csharp/emitter/src/lib/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
} from "../type/input-type.js";
import { LiteralTypeContext } from "../type/literal-type-context.js";
import { Usage } from "../type/usage.js";
import { getFullNamespaceString } from "./utils.js";

export function fromSdkType(
sdkType: SdkType,
Expand Down Expand Up @@ -87,7 +86,7 @@ export function fromSdkModelType(
inputModelType = {
Kind: "model",
Name: modelTypeName,
Namespace: getFullNamespaceString((modelType.__raw as Model).namespace), // TODO -- use the value from TCGC when this is included in TCGC
CrossLanguageDefinitionId: modelType.crossLanguageDefinitionId,
Access: getAccessOverride(
context,
modelType.__raw as Model
Expand All @@ -113,7 +112,6 @@ export function fromSdkModelType(
property,
{
ModelName: modelTypeName,
Namespace: inputModelType.Namespace,
} as LiteralTypeContext,
[]
);
Expand Down Expand Up @@ -205,10 +203,7 @@ export function fromSdkEnumType(
Name: enumName,
ValueType: fromSdkBuiltInType(enumType.valueType),
Values: enumType.values.map((v) => fromSdkEnumValueType(v)),
Namespace: getFullNamespaceString(
// Enum and Union have optional namespace property
(enumType.__raw! as any).namespace
),
CrossLanguageDefinitionId: enumType.crossLanguageDefinitionId,
Accessibility: getAccessOverride(
context,
enumType.__raw as any
Expand Down Expand Up @@ -312,7 +307,7 @@ function fromSdkConstantType(
Name: enumName,
ValueType: fromSdkBuiltInType(constantType.valueType),
Values: allowValues,
Namespace: literalTypeContext.Namespace,
CrossLanguageDefinitionId: "",
Accessibility: undefined,
Deprecated: undefined,
Description: `The ${enumName}`, // TODO -- what should we put here?
Expand Down
24 changes: 1 addition & 23 deletions packages/http-client-csharp/emitter/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ import {
getLibraryName,
getSdkModel,
} from "@azure-tools/typespec-client-generator-core";
import {
Enum,
EnumMember,
Model,
ModelProperty,
Namespace,
Operation,
Scalar,
} from "@typespec/compiler";
import { Enum, EnumMember, Model, ModelProperty, Operation, Scalar } from "@typespec/compiler";
import { InputConstant } from "../type/input-constant.js";
import { InputOperationParameterKind } from "../type/input-operation-parameter-kind.js";
import { InputParameter } from "../type/input-parameter.js";
Expand Down Expand Up @@ -47,20 +39,6 @@ export function getTypeName(
return name;
}

export function getFullNamespaceString(namespace: Namespace | undefined): string {
if (!namespace || !namespace.name) {
return "";
}

let namespaceString: string = namespace.name;
let current: Namespace | undefined = namespace.namespace;
while (current && current.name) {
namespaceString = `${current.name}.${namespaceString}`;
current = current.namespace;
}
return namespaceString;
}

export function createContentTypeOrAcceptParameter(
mediaTypes: string[],
name: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/http-client-csharp/emitter/src/type/input-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface InputModelType extends InputTypeBase {
Kind: "model";
Properties: InputModelProperty[];
Name: string;
Namespace?: string;
CrossLanguageDefinitionId: string;
Access?: AccessFlags;
Usage: string; // TODO -- replace this with UsageFlags in TCGC
AdditionalProperties?: InputType;
Expand All @@ -93,7 +93,7 @@ export interface InputEnumType extends InputTypeBase {
Name: string;
ValueType: InputPrimitiveType;
Values: InputEnumTypeValue[];
Namespace?: string;
CrossLanguageDefinitionId: string;
Accessibility?: string;
Deprecated?: string;
IsExtensible: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
export interface LiteralTypeContext {
ModelName: string;
PropertyName: string;
Namespace?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ op test(@body input: Pet): Pet;
Type: {
Kind: "enum",
Name: "PetKind",
Namespace: "Azure.Csharp.Testing",
CrossLanguageDefinitionId: "Azure.Csharp.Testing.PetKind",
Description: "The pet kind",
Accessibility: undefined,
Deprecated: undefined,
Expand Down Expand Up @@ -279,7 +279,7 @@ op test(@body input: Pet): Pet;
Type: {
Kind: "enum",
Name: "PetKind",
Namespace: "Azure.Csharp.Testing",
CrossLanguageDefinitionId: "Azure.Csharp.Testing.PetKind",
Accessibility: undefined,
Deprecated: undefined,
Description: "The pet kind",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 { InputEnumType } from "../../src/type/input-type.js";
import { InputArrayType, InputEnumType } from "../../src/type/input-type.js";
import {
createEmitterContext,
createEmitterTestHost,
Expand Down Expand Up @@ -36,7 +36,7 @@ describe("Test GetInputType for array", () => {
Kind: "string",
Encode: undefined,
},
},
} as InputArrayType,
root.Clients[0].Operations[0].Parameters[0].Type
);
});
Expand All @@ -59,7 +59,7 @@ describe("Test GetInputType for array", () => {
Kind: "string",
Encode: undefined,
},
},
} as InputArrayType,
root.Clients[0].Operations[0].Responses[0].BodyType
);
});
Expand Down Expand Up @@ -100,7 +100,7 @@ describe("Test GetInputType for enum", () => {
{
Kind: "enum",
Name: "SimpleEnum",
Namespace: "Azure.Csharp.Testing",
CrossLanguageDefinitionId: "Azure.Csharp.Testing.SimpleEnum",
Accessibility: undefined,
Deprecated: undefined,
Description: "fixed string enum",
Expand Down Expand Up @@ -166,7 +166,7 @@ describe("Test GetInputType for enum", () => {
{
Kind: "enum",
Name: "FixedIntEnum",
Namespace: "Azure.Csharp.Testing",
CrossLanguageDefinitionId: "Azure.Csharp.Testing.FixedIntEnum",
Accessibility: undefined,
Deprecated: undefined,
Description: "Fixed int enum",
Expand Down Expand Up @@ -225,7 +225,7 @@ describe("Test GetInputType for enum", () => {
{
Kind: "enum",
Name: "FixedEnum",
Namespace: "Azure.Csharp.Testing",
CrossLanguageDefinitionId: "Azure.Csharp.Testing.FixedEnum",
Accessibility: undefined,
Deprecated: undefined,
Description: "Fixed enum",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Generator.CSharp.Input
{
public class InputEnumType : InputType
{
public InputEnumType(string name, string? enumNamespace, string? accessibility, string? deprecated, string description, InputModelTypeUsage usage, InputPrimitiveType valueType, IReadOnlyList<InputEnumTypeValue> values, bool isExtensible)
public InputEnumType(string name, string crossLanguageDefinitionId, string? accessibility, string? deprecated, string description, InputModelTypeUsage usage, InputPrimitiveType valueType, IReadOnlyList<InputEnumTypeValue> values, bool isExtensible)
: base(name)
{
Namespace = enumNamespace;
CrossLanguageDefinitionId = crossLanguageDefinitionId;
Accessibility = accessibility;
Deprecated = deprecated;
Description = description;
Expand All @@ -22,56 +20,13 @@ public InputEnumType(string name, string? enumNamespace, string? accessibility,
IsExtensible = isExtensible;
}

public string? Namespace { get; }
public string CrossLanguageDefinitionId { get; }
public string? Accessibility { get; }
public string? Deprecated { get; }
public string Description { get; }
public InputModelTypeUsage Usage { get; }
public InputPrimitiveType ValueType { get; }
public IReadOnlyList<InputEnumTypeValue> Values { get; }
public bool IsExtensible { get; }

public static IEqualityComparer<InputEnumType> IgnoreNullabilityComparer { get; } = new IgnoreNullabilityComparerImplementation();

private struct IgnoreNullabilityComparerImplementation : IEqualityComparer<InputEnumType>
{
public bool Equals(InputEnumType? x, InputEnumType? y)
{
if (x is null || y is null)
{
return ReferenceEquals(x, y);
}

if (x.GetType() != y.GetType())
{
return false;
}

return x.Name == y.Name
&& x.Namespace == y.Namespace
&& x.Accessibility == y.Accessibility
&& x.Description == y.Description
&& x.ValueType.Equals(y.ValueType)
&& x.Values.SequenceEqual(y.Values)
&& x.IsExtensible == y.IsExtensible;
}

public int GetHashCode(InputEnumType obj)
{
var hashCode = new HashCode();
hashCode.Add(obj.Name);
hashCode.Add(obj.Namespace);
hashCode.Add(obj.Accessibility);
hashCode.Add(obj.Description);
hashCode.Add(obj.ValueType);
hashCode.Add(obj.IsExtensible);
foreach (var item in obj.Values)
{
hashCode.Add(item);
}

return hashCode.ToHashCode();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Microsoft.Generator.CSharp.Input
public class InputModelType : InputType
{
// TODO: Follow up issue https://github.com/microsoft/typespec/issues/3619. After https://github.com/Azure/typespec-azure/pull/966 is completed, update this type and remove the "modelAsStruct" parameter.
public InputModelType(string name, string? modelNamespace, string? access, string? deprecation, string? description, InputModelTypeUsage usage, IReadOnlyList<InputModelProperty> properties, InputModelType? baseModel, IReadOnlyList<InputModelType> derivedModels, string? discriminatorValue, InputModelProperty? discriminatorProperty, IReadOnlyDictionary<string, InputModelType> discriminatedSubtypes, InputType? additionalProperties, bool modelAsStruct)
public InputModelType(string name, string crossLanguageDefinitionId, string? access, string? deprecation, string? description, InputModelTypeUsage usage, IReadOnlyList<InputModelProperty> properties, InputModelType? baseModel, IReadOnlyList<InputModelType> derivedModels, string? discriminatorValue, InputModelProperty? discriminatorProperty, IReadOnlyDictionary<string, InputModelType> discriminatedSubtypes, InputType? additionalProperties, bool modelAsStruct)
: base(name)
{
Namespace = modelNamespace;
CrossLanguageDefinitionId = crossLanguageDefinitionId;
Access = access;
Deprecation = deprecation;
Description = description;
Expand All @@ -30,7 +30,7 @@ public InputModelType(string name, string? modelNamespace, string? access, strin
ModelAsStruct = modelAsStruct;
}

public string? Namespace { get; internal set; }
public string CrossLanguageDefinitionId { get; internal set; }
public string? Access { get; internal set; }
public string? Deprecation { get; internal set; }
public string? Description { get; internal set; }
Expand All @@ -40,7 +40,7 @@ public InputModelType(string name, string? modelNamespace, string? access, strin
public InputModelType? BaseModel { get; internal set; }
public IReadOnlyList<InputModelType> DerivedModels { get; internal set; }
public string? DiscriminatorValue { get; internal set; }
public InputModelProperty? DiscriminatorProperty{ get; internal set; }
public InputModelProperty? DiscriminatorProperty { get; internal set; }
public IReadOnlyDictionary<string, InputModelType> DiscriminatedSubtypes { get; internal set; }
public InputType? AdditionalProperties { get; internal set; }
public bool IsUnknownDiscriminatorModel { get; init; }
Expand Down Expand Up @@ -72,7 +72,7 @@ private static IEnumerable<InputModelType> EnumerateBase(InputModelType? model)

private string GetDebuggerDisplay()
{
return $"Model (Name: {Name}, {Namespace})";
return $"Model (Name: {Name})";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void Write(Utf8JsonWriter writer, InputEnumType value, JsonSeria
public static InputEnumType CreateEnumType(ref Utf8JsonReader reader, string? id, string? name, JsonSerializerOptions options, ReferenceResolver resolver)
{
var isFirstProperty = id == null && name == null;
string? ns = null;
string? crossLanguageDefinitionId = null;
string? accessibility = null;
string? deprecated = null;
string? description = null;
Expand All @@ -39,7 +39,7 @@ public static InputEnumType CreateEnumType(ref Utf8JsonReader reader, string? id
{
var isKnownProperty = reader.TryReadReferenceId(ref isFirstProperty, ref id)
|| reader.TryReadString(nameof(InputEnumType.Name), ref name)
|| reader.TryReadString(nameof(InputEnumType.Namespace), ref ns)
|| reader.TryReadString(nameof(InputEnumType.CrossLanguageDefinitionId), ref crossLanguageDefinitionId)
|| reader.TryReadString(nameof(InputEnumType.Accessibility), ref accessibility)
|| reader.TryReadString(nameof(InputEnumType.Deprecated), ref deprecated)
|| reader.TryReadString(nameof(InputEnumType.Description), ref description)
Expand Down Expand Up @@ -76,7 +76,7 @@ public static InputEnumType CreateEnumType(ref Utf8JsonReader reader, string? id
throw new JsonException("The ValueType of an EnumType must be a primitive type.");
}

var enumType = new InputEnumType(name, ns, accessibility, deprecated, description!, usage, inputValueType, NormalizeValues(values, inputValueType), isExtendable);
var enumType = new InputEnumType(name, crossLanguageDefinitionId ?? string.Empty, accessibility, deprecated, description!, usage, inputValueType, NormalizeValues(values, inputValueType), isExtendable);
if (id != null)
{
resolver.AddReference(id, enumType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public static InputModelType CreateModelType(ref Utf8JsonReader reader, string?
id = id ?? throw new JsonException();

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

string? ns = null;
string? crossLanguageDefinitionId = null;
string? accessibility = null;
string? deprecated = null;
string? description = null;
Expand All @@ -53,7 +53,7 @@ public static InputModelType CreateModelType(ref Utf8JsonReader reader, string?
while (reader.TokenType != JsonTokenType.EndObject)
{
var isKnownProperty = reader.TryReadString(nameof(InputModelType.Name), ref name)
|| reader.TryReadString(nameof(InputModelType.Namespace), ref ns)
|| reader.TryReadString(nameof(InputModelType.CrossLanguageDefinitionId), ref crossLanguageDefinitionId)
|| reader.TryReadString(nameof(InputModelType.Access), ref accessibility)
|| reader.TryReadString(nameof(InputModelType.Deprecation), ref deprecated)
|| reader.TryReadString(nameof(InputModelType.Description), ref description)
Expand All @@ -73,7 +73,7 @@ public static InputModelType CreateModelType(ref Utf8JsonReader reader, string?
}

model.Name = name ?? throw new JsonException("InputModelType must have name");
model.Namespace = ns;
model.CrossLanguageDefinitionId = crossLanguageDefinitionId ?? string.Empty;
model.Access = accessibility;
model.Deprecation = deprecated;
model.Description = description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public CodeWriterBenchmark()
{
new InputModelProperty("MyProperty", "myProperty", "The property of mine", new InputPrimitiveType(InputPrimitiveTypeKind.Int32), true, false, false)
};
var inputModel = new InputModelType("MyModel", null, null, null, "Test model", InputModelTypeUsage.RoundTrip, properties, null, Array.Empty<InputModelType>(), null, null, new Dictionary<string, InputModelType>(), null, false);
var inputModel = new InputModelType("MyModel", string.Empty, null, null, "Test model", InputModelTypeUsage.RoundTrip, properties, null, Array.Empty<InputModelType>(), null, null, new Dictionary<string, InputModelType>(), null, false);
var modelProvider = new ModelProvider(inputModel);
_writer = new TypeProviderWriter(modelProvider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void TypeProviderWriter_WriteModel()
var properties = new List<InputModelProperty> { RequiredStringProperty, RequiredIntProperty };
MockPluginSetValue(properties);

var inputModel = new InputModelType("TestModel", null, "public", null, "Test model.", InputModelTypeUsage.RoundTrip,
var inputModel = new InputModelType("TestModel", string.Empty, "public", null, "Test model.", InputModelTypeUsage.RoundTrip,
properties, null, new List<InputModelType>(), null, null, new Dictionary<string, InputModelType>(), null, false);

var modelProvider = new ModelProvider(inputModel);
Expand All @@ -80,7 +80,7 @@ public void TypeProviderWriter_WriteModelAsStruct()
var properties = new List<InputModelProperty> { RequiredStringProperty, RequiredIntProperty };
MockPluginSetValue(properties);

var inputModel = new InputModelType("TestModel", null, "public", null, "Test model.", InputModelTypeUsage.RoundTrip,
var inputModel = new InputModelType("TestModel", string.Empty, "public", null, "Test model.", InputModelTypeUsage.RoundTrip,
properties, null, new List<InputModelType>(), null, null, new Dictionary<string, InputModelType>(), null, modelAsStruct: true);

var modelProvider = new ModelProvider(inputModel);
Expand Down
Loading
Loading