Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

NameGenerator now honors the RegisterAttribute #482

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

Conversation

zgramana
Copy link
Contributor

Resolves #481 by allowing users to override
the generated objc class name by decorating their managed class
with the RegisterAttribute.

Resolves mono#481 by allowing users to override
the generated objc class name by decorating their managed class
with the RegisterAttribute.
@monojenkins
Copy link

Hello! I'm the build bot for the Mono project.

I need approval from a Mono team member to build this pull request. A team member should reply with "approve" to approve a build of this pull request, "whitelist" to whitelist this and all future pull requests from this contributor, or "build" to explicitly request a build, even if one has already been done. Additional trigger words are available here.

Contributors can ignore this message.

@tritao tritao requested a review from spouliot August 17, 2017 00:20
@tritao
Copy link
Contributor

tritao commented Aug 17, 2017

whitelist

@dalexsoto
Copy link
Member

Hello! Glad to see you contributing!! I am not entirely sold on reusing 'RegisterAttribute' for this because it could potentially interfere with the Xamarin registrar/runtime. Maybe we'll need embeddinator specific attributes.

@zgramana
Copy link
Contributor Author

Thanks, old friend! FWIW, I chose to re-use it since the official Xamarin docs state that RegisterAttribute is now used solely to customize the objc class name emitted by the XI generator. Adding a second attribute with the same responsibility would be a DRY violation. If I have a XI assembly using them I'd want it to Just Work. Having two attributes decorating the same class with the same values would be mildly awkward.

@tritao
Copy link
Contributor

tritao commented Aug 17, 2017

FWIW we also rely on [Register] for this purpose in the Java backend, a behaviour we inherit from Xamarin.Android Java interop implementation.

@dalexsoto
Copy link
Member

Awesome, agree on the intention of reusing it, if @spouliot and @rolfbjarne are ok with this I am too!

@lemonmojo
Copy link

I'm not sure if abusing the Register attribute is a good idea.

First, it means that libraries that want to customize their class names must reference Xamarin.Mac or Xamarin.iOS which is a heavy dependency if you just want to rename a method for use with ObjC.

Second, it only works on classes but I'd also like to be able to rename methods and properties.

And third: I think a dedicated (thin) E4k support library would make sense for other features too that wouldn't be available in Xamarin.Mac or iOS already. Like a new attribute to tell E4k to not generate bindings for specific classes, methods or properties.

@rolfbjarne
Copy link
Member

rolfbjarne commented Aug 17, 2017

I'm fine with using [RegisterAttribute]. However I think you should be more defensive (it might not have an argument ([Register]), in which case your code will throw an exception. I'd do something like this, except that I'd issue a warning instead of an error if some assumption (maybe someday there will be 3 arguments?) breaks:

https://github.com/xamarin/xamarin-macios/blob/a001cace6f1e77646cd63e0119ab344f3935851b/tools/common/StaticRegistrar.cs#L1109-L1143

@lemonmojo note that this code only does a string comparison for the attribute name; this means that you don't have to reference Xamarin.iOS/Xamarin.Mac, you can define your own RegisterAttribute class.

@zgramana
Copy link
Contributor Author

@rolfbjarne I like it. I'll update the PR. With respect to the other params, like IsWrapper, should the switch ignore them, log them at verbose log level, or some third option I'm not able to think of?

@rolfbjarne
Copy link
Member

@zgramana Just ignore the IsWrapperfield (no logging needed), it doesn't matter for the embeddinator (and I think you can ignore the SkipRegistration field as well, at least for now, but this one you might want to log, since users might want to try to use it for the obvious purpose). You might want to log other (unknown) fields as well.

In response to code review feedback, handled RegisterAttribute in the
spirit in which StaticRegistrar.cs does in xamarin-macios.

Also includes documentation of newly added error codes.
@zgramana
Copy link
Contributor Author

zgramana commented Aug 17, 2017

I just pushed a refactor based on @rolfbjarne's feedback for review.

@zgramana
Copy link
Contributor Author

Actually, hold off for another commit forthcoming. Apologies for the false start.

The previous refactor ommitted the null check after looking for
RegisterAttribute. This commit adds it back in.
@zgramana
Copy link
Contributor Author

zgramana commented Aug 17, 2017

Okay, now it's ready for review. In addition to the possible failure modes @rolfbjarne indicated, I also made sure to handle the case of conflicting name values, e.g. [Register("myFoo", Name = "foo")] as well as validating the layout of non-Xamarin.[iOS|Mac] provided Register attributes for users like @lemonmojo who might write their own.

@lemonmojo
Copy link

@zgramana @rolfbjarne Thx for clarifying that there's no external dependency required for this. In this case the PR sounds good to me.

I'd still like to be able to also rename specific properties/methods/etc. and exclude certain classes/properties/etc. from being generated but I guess that's fuel for another Issue/PR.

@lemonmojo
Copy link

@zgramana Just noticed a small typo in the updated documentation: Users already familiar with the RegisterAttribute might expect SkipRegistration to function ---> is <--- it does in Xamarin.iOS or Xamarin.Android. However, this is unsupported and the value of SkipRegistration will be ignored.

Copy link
Member

@rolfbjarne rolfbjarne left a comment

Choose a reason for hiding this comment

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

A few minor comments, and additionally it would be nice to get tests for this as well.

docs/errors.md Outdated

If an argument was not also passed for named parameter "Name" then the class will be named according to the default convention.

<h3><a name="EM1061"/>Invalid `RegisterAttribute` found on class `T`. Expected a constructor with either 1 or 2 parameters but found {argsCount} instead..</h3>
Copy link
Member

Choose a reason for hiding this comment

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

Typo: two periods at the end of the sentence.

docs/errors.md Outdated
@@ -217,6 +217,53 @@ Selectors on the [NSObjectProtocol](https://developer.apple.com/reference/object

Note: The list of reserved selectors will evolve with new versions of the tool.

<h3><a name="EM1060"/>Invalid `RegisterAttribute` found on class `T`. Expected the first constructor parameter to be of type `string` but found type `U` instead.</h3>

This is a **warning** that the class `T` was decorated with a `RegisterAttribute`, indicating the author wishes to rename the name of generated objective-c class.
Copy link
Member

Choose a reason for hiding this comment

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

Minor: use proper casing Objective-C instead of all lowercase (there are also more cases below).

docs/errors.md Outdated

This is a **warning** that the class `T` was decorated with a `RegisterAttribute`, indicating the author wishes to rename the name of generated objective-c class.

Users already familiar with the `RegisterAttribute` might expect `SkipRegistration` to function is it does in Xamarin.iOS or Xamarin.Android. However, this is unsupported and the value of `SkipRegistration` will be ignored.
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure SkipRegistration is applicable to Xamarin.Android (I don't know anything about XA). Did you mean Xamarin.Mac instead of Xamarin.Android?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I did, thanks for catching that. My fingers seem to have their own L4 cache…

@zgramana
Copy link
Contributor Author

@rolfbjarne I've pushed up the copy edits you and @lemonmojo found.

Unfortunately I'm going to have to leave the tests to someone else as I have to attend to business commitments. Hope this is PR is still helpful.

@dnfclas
Copy link

dnfclas commented Feb 6, 2018

CLA assistant check
All CLA requirements met.

Base automatically changed from master to main March 9, 2021 10:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants