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

Add support for client options generation #4019

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

jorgerangel-msft
Copy link
Contributor

@jorgerangel-msft jorgerangel-msft commented Jul 25, 2024

fixes: #3688

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/emitter-client-csharp label Jul 25, 2024
@azure-sdk
Copy link
Collaborator

azure-sdk commented Jul 25, 2024

API change check

API changes are not detected in this pull request.

@jorgerangel-msft jorgerangel-msft changed the title feat: add support for client options generation Add support for client options generation Jul 25, 2024
@jorgerangel-msft jorgerangel-msft marked this pull request as ready for review July 25, 2024 20:22

public ClientOptionsProvider(InputClient inputClient)
public ClientOptionsProvider(InputClient inputClient, ClientProvider clientProvider)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid passing in the ClientProvider?

TF should give you an idempotent answer if you asked for the ClientProvider vis the InputClient so should be safe to simply pass in the InputClient

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to do the same for RestClientProvider ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do the same in RestClientProvider

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah so making this change creates an issue which leads to a stack overflow. CreateClientCore will call the ClientProvider ctor if it can't find the provider in cache. ClientProvider now has 2 properties for 2 different providers RestClient and ClientOptions. If we use the TF in those providers to get the client, this will lead to a loop of instantiating ClientProvider and it will never be cached. I will keep this as is for now and open a follow up issue to find a solution for this unless there are concerns.


namespace Microsoft.Generator.CSharp.ClientModel.Primitives
{
internal sealed class ApiVersion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we need this class, since this is almost a duplicate of the Enum types we already have. The ApiVersions are just a FixedEnumProvider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm so convert the list of apiversions from the ns to a list of InputEnumTypes so we can use FixedEnumProvider?

private readonly IReadOnlyList<ApiVersion> _apiVersions;
private IReadOnlyList<EnumTypeMember>? _members;

public ServiceVersionDefinition(ClientOptionsProvider declaringType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason we can't use fixedenumprovider?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That provider takes in an InputEnumType in the ctor. Are you suggesting I convert the api version string to an InputEnumType ?

Copy link
Member

@ArcturusZhang ArcturusZhang Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we cannot reuse FixedEnumProvider because it is internal in MGC project.
We could make it public and inherit it here because we want to add a declaring type provider here, and I think it does not make much sense we only change the definition of FixedEnumProvider to let it take a DeclaringTypeProvider but leave others unchanged
And another issue of reusing FixedEnumProvider would be the identifier name.
FixedEnumProvider is implemented as "it calls ToCleanName to get the member name", but here we want a name like V2024_03_01 (I explained how it is constructed here). We need to figure out a solution for this as well if we want to reuse.
I think it is not worth it to reuse if it takes too much changes, this definition is simple enough right now, reusing might make it more complicated.

f.Type.PropertyInitializationType,
f.Name.ToCleanName(),
new AutoPropertyBody(true),
backingField: f));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are using the backing field in the wrong way here. A property and its corresponding backing field MUST belong to the same type. You are associating a field that belongs to one type with a property that belongs to another.

We should find another way to do this and remove BackingField if this is the only usage for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what kind of code this is generating? Could we write some comment here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this for now. Please have a look at the latest implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have some tests to validate this on the generated files
There are quite a few scenarios on ClientOptions and the service versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:csharp Issue for the C# client emitter: @typespec/emitter-client-csharp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ClientOptions class
5 participants