-
Notifications
You must be signed in to change notification settings - Fork 299
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
Common base class for Kubernetes resource models #101
Comments
Yeah, I see the utility, but I don't want to manually generate the code. Do you know if AutoRest supports an extension to add a parent class? If so we can look to add that extension... |
Agree 😊 The classes are marked as partial, so even if it doesn't I can easily use Roslyn for "post-processing". Will have a think about it this weekend |
Sorry, have been a bit flat-out at work, so I haven't had time to follow up on this; I'll try to have a proper look into it this weekend. |
@brendandburns - looking at the |
@brendandburns - I think part of the problem is that the swagger definitions don't directly support specifying a base class (JSON schema doesn't support inheritance). This can be done at the code level, but not the swagger level. FWIW, I did this in my own project when generating the code (after the Swagger JSON has been processed): And the detection of resources / resource-lists is done like so: Note that this also correctly handles |
@brendandburns - here's an example implementation. Only required a minor amount of yak-shaving (due to use of .NET Core 2.1-preview2 SDK): https://gist.github.com/tintoy/93a8f50fb3f4dafcfa835cbeb804b441 |
Otherwise, we might be able to do what we need using the allOf directive. |
I'll try modifying that Python script to add |
Would be nice to add a |
@brendandburns - looks like I'd have to modify the (for what it's worth, I still think the Roslyn-based postprocessing might be a more workable idea in the long run even if it does make the model-generation process a two-step affair) CC: @qmfrederik @tg123 |
Yeah possibly - but we'd still have the same problem of needing to somehow declare the interface implementation on each model class. So back to square one :) |
Will explain properly tomorrow morning :) |
Ok, now that I'm in front of a proper keyboard (rather than on my phone): The issue is that, either way, we have to be able to declare a base class or interface for each model (but the current process for generating the models does not allow for this). If we forked the Python script that modifies the Swagger to create a C#-specific version then that could do the trick (but I'm not sure how willing others are to do it this way). Otherwise, we could perhaps add a separate Python script to modify the swagger (to be run after the main one) that's only run when generating for C#? |
Got it. The advantage of interfaces over base classes is that you don't have to remove any autogenerated code - you just add the interface implementation, which can be done in a partial class definition. #152 tries to do that - would that approach work for you? |
Nice trick, yeah that'd work. BTW, for places where I need a common base class I still have the implementation in KubeClient (but figured it'd be useful for more general use-cases / consumers to be able to implement generic resource handling). The only reason I was using a base class was because that way I don't need to fill out |
#152 is merged, so closing. |
Hi.
I was wondering if it might be useful to have a shared base class for models representing Kubernetes resources / resource-lists (the script I wrote to generate models from Swagger does this, and I imagine autorest can do something similar).
My existing client library does this and I've found that it makes it easier to deal with resources in a generic fashion:
https://github.com/tintoy/dotnet-kube-client/blob/develop/src/KubeClient/Models/PodV1.cs
https://github.com/tintoy/dotnet-kube-client/blob/develop/src/KubeClient/Models/PodListV1.cs
https://github.com/tintoy/dotnet-kube-client/blob/develop/src/KubeClient/ResourceClients/KubeResourceClient.cs#L90
https://github.com/tintoy/dotnet-kube-client/blob/develop/src/KubeClient/ResourceClients/KubeResourceClient.cs#L122
TBH given that the official client is mostly generated code, I don't know how useful it will be within the client itself, but it's almost certainly going to be useful to some of its consumers.
Again, I get that there are existing users and this might be a breaking change (at the binary-compatibility level at least) but I still think it's something worth considering.
The text was updated successfully, but these errors were encountered: