[MDSAL-238] YANG gen. binding classes should offer a matches(CharSequence input) [or expose Pattern[] patterns public instead private] Created: 15/Mar/17 Updated: 09/Mar/18 Resolved: 29/Mar/17 |
|
| Status: | Resolved |
| Project: | mdsal |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | ||
| Reporter: | Michael Vorburger | Assignee: | Unassigned |
| Resolution: | Cannot Reproduce | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Operating System: All |
||
| Description |
|
In https://git.opendaylight.org/gerrit/#/c/53118/ for It's easy enough to recreate it from the public List<String> PATTERN_CONSTANTS, but perhaps you would consider exposing it as a public static final Pattern[] PATTERNS; as well? Not sure if this has to be depending on I'll also file a separate bug proposing to offer an additional utility method, this issue is to focus particularly just on exposing the Pattern publicly. |
| Comments |
| Comment by Michael Vorburger [ 15/Mar/17 ] |
|
https://git.opendaylight.org/gerrit/#/c/53363/ has a one of manual related thing (for Uuid); if this was available, that code would have to make a little bit less of a hand-stand to recreate Pattern from String. |
| Comment by Vratko Polak [ 15/Mar/17 ] |
|
> public static final Pattern[] PATTERNS; Do you mean this? public Pattern[] getPatterns() { return Arrays.copyOf(patterns, patterns.length); } |
| Comment by Michael Vorburger [ 15/Mar/17 ] |
|
> Do you mean this? yes, otherwise it's mutable (even if final, the array elements could be changed, which we certainly wouldn't want...) so yeah that is what actually I meant... thanks! |
| Comment by Michael Vorburger [ 16/Mar/17 ] |
|
actually on further thought, having to Arrays.copyOf() for every time you getPatterns() isn't ideal, if it can be avoided, so it would be better to just offer something like: public static final List<Pattern> PATTERNS = ImmutableList.of(PATTERN_CONSTANTS.transform(...)); This is more consistent with the existing PATTERN_CONSTANTS. Or, if a method like getPatterns(), then it should be "static" (above it is not). But having a public field for the String pattern but a static getter for the regexp Pattern object isn't consistent. |
| Comment by Robert Varga [ 16/Mar/17 ] |
|
I do not quite get the use case. Why do you need to programmatically access the pattern? |
| Comment by Robert Varga [ 16/Mar/17 ] |
|
Also details how the value validity is enforced is an implementation-private detail, which can change at any time and we certainly do not want application code to be tied to that. More specifically, the pattern strings exposed are the strings coming from the model and are useful for reporting. They are not guaranteed to pass Pattern.compile() – they just happen to do so because |
| Comment by Michael Vorburger [ 16/Mar/17 ] |
|
> Why do you need to programmatically access the pattern? to be able to check whether a String matches the pattern, as in https://git.opendaylight.org/gerrit/#/c/53363/ (originally motivated out of code in https://git.opendaylight.org/gerrit/#/c/53118/ for > details how the value validity is enforced is an implementation-private OK, all the more reason then to offer a better API than just exposing String? What would also work here, instead of exposing a Pattern instance as originally suggested, and may be better, is to just directly offer a method with a signature like: public static boolean matches(CharSequence input) because this is what I want to have access to the pattern for anyway - and I can't think of anything else one would want to do with a Pattern, anyway. (PS: Going this way may also prevent stupid API users from making a concurrency mistake; Pattern is thread safe, Matcher is not - just having a matches() takes away any possible room for misuse of this.) Or just completely forget about this and only do |
| Comment by Vratko Polak [ 16/Mar/17 ] |
|
If the intent is to create a binding object when the argument matches, the only reliable solution is [2]. For example the string subtype can have length constraints aside of patterns. |
| Comment by Robert Varga [ 27/Mar/17 ] |
|
Michael, I don't want to sound negative, but what kind of business logic relies on external constraints (defined by the model) to decide what class of object to instantiate? I mean this sounds like you really want to be instatianting a union of some kind, rather than having a clearly-defined model – if you have a fallback type which is more general than UUID, why not use that? |
| Comment by Michael Vorburger [ 27/Mar/17 ] |
|
> what kind of business logic relies on external constraints well in the case of how they solved As stated in |
| Comment by Vratko Polak [ 29/Mar/17 ] |
|
> you really want to be instatianting a union of some kind Exactly. I see benefits of recognizing specific types (perhaps the input is from some less strongly typed protocol), but unions (as opposed to ad-hoc Java binding enhancements) are there exactly for that kind of data. > in an ideal world, should be conceptually addressed more globally, You can create my-internal-maybe-uuid-union.yang in your sub-project to hold the typedef for your implementation to use, without affecting anyone else (perhaps after asking upstream projects if they wish to host that first). |