[MDSAL-239] YANG gen. binding classes with Optional<XYZ> newXYZIfValidPattern() kind of utility factory method Created: 15/Mar/17 Updated: 09/Mar/18 Resolved: 18/Apr/17 |
|
| Status: | Resolved |
| Project: | mdsal |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | ||
| Reporter: | Michael Vorburger | Assignee: | Unassigned |
| Resolution: | Won't Do | 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 Optional<Uuid> newUuidIfValidPattern(String possibleUuid) in class UuidUtil; about to be proposed into org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil also. I'm wondering if this is an extreme rare case, or not something one might reasonably often encounter wanting to do - perhaps you would consider generating such a utility factory method returning an Optional for all classes generated for regexp expression in YANG, so that in the case of above org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid would have this directly? Also see |
| Comments |
| Comment by Michael Vorburger [ 15/Mar/17 ] |
|
https://git.opendaylight.org/gerrit/#/c/53363/ has a one of manual related thing (for Uuid); this issue basically proposes to generalize and auto-generate that kind of helper. |
| Comment by Michael Vorburger [ 15/Mar/17 ] |
|
Not sure if this has to be depending on |
| Comment by Vratko Polak [ 15/Mar/17 ] |
|
I guess this code is too long or slow for you? try { maybeUuid = Optional<Uuid>.of(new Uuid(myString)); }catch (IllegalArgumentException exception) { maybeUuid = Optional<Uuid>.empty(); }Binding Specification Java v2 currently does not plan to offer any "createIfValidElseEmpty" methods. |
| Comment by Michael Vorburger [ 16/Mar/17 ] |
|
> } catch (IllegalArgumentException exception) { This isn't great, if for any reason code expects this path to be often travelled, then AFAIK catching an exception in a "normal" code path (not an "exceptional" one, which is supposed to happen rarely) is a bad anti-pattern with known performance issues on the JVM? I'm aware that there are some optimizations re. this deep in the JVM (e.g. if you throw/catch the same Exception a lot, then its skips creating a stack trace and you get weird exceptions which have no stack trace, apparently), but... this is a hack. IMHO you should check for something which you know may throw an exception by another mean, if you can. This is why I believe it could be valuable for gen. code to offer a util for this use case. |
| Comment by Robert Varga [ 16/Mar/17 ] |
|
Well, the question is, how do you expect to recover from having an illegal argument. To me it feels like the model and application are not aligned at all. |
| Comment by Michael Vorburger [ 16/Mar/17 ] |
|
> how do you expect to recover from having an illegal argument. dunno, up to the calling code, thus the Optional. The scenario here is code à la: "if this String which I got from somewhere is a (some type, like Uuid) then I want to do that, otherwise I want to do this [which may be nothing]". > To me it feels like the model and application are not aligned at all. That's entirely possible, but I have run into at least one (c/53118/ for |
| Comment by Vratko Polak [ 16/Mar/17 ] |
|
I agree with this sentence from [1]: For now, I think the performance hit is the appropriate punishment for an application "guessing" what the argument really is. The correct fix it to wait for |
| Comment by Martin Ciglan [ 27/Mar/17 ] |
|
Hi Michael Have you got any more thoughts/conclusions about 7991 & 7992 so we know if to confirm these bugs or not. Many thanks. |
| Comment by Michael Vorburger [ 27/Mar/17 ] |
|
> appropriate punishment I don't like punishment... > The correct fix it to wait for I see the idea (I think), but that hard-codes this into the model - I'm not sure you'd want that. There may truly be cases when from a model PoV something rightfully has regexp/lenght constraint, but you want to check some input against "could this be a" - without incurring the exception overhead? It seems, from what I understand, that > more thoughts/conclusions about personally I do still think both or at least one of these can be useful.. If it makes things easier if we choose just one of the two, then I think this issues captures the essence of the intended use case better than the lower level But I do sense that Robert's & Vratko's don't see this as something "critical" - and it's tree that you can work around it and achieve the same yourself in custom code; the point of this was to suggest an enhancement as a convenience short-cut when one wants to do this (which isn't every day, but the case has come up in the real world, thus I filed this). So if there is very strong disagreement, then just do just close it - this isn't the sort of issue I'd be inclined to "defend" or "fight for" |
| Comment by Vratko Polak [ 30/Mar/17 ] |
|
> you want to check some input against "could this be a" Is this check any different from "deserialize a union"? > but that hard-codes this into the model The union typedef can be in any model (that includes models typedefing the constituents). Any *-impl.yang will do. |
| Comment by Martin Ciglan [ 18/Apr/17 ] |
|
Michael, have you got any objections if I close this ticket? Thanks. |
| Comment by Michael Vorburger [ 18/Apr/17 ] |
|
> Michael, have you got any objections if I close this ticket? Thanks. No objection, your honour. |