Skip to content

Conversation

@benmusson
Copy link
Collaborator

Adds both a randomUUID() expression function and a system.util.randomUUID() scripting function, that simply return the result of java.util.UUID.randomUUID().

My main use case is the expression function in Perspective.
When dealing with messaging between embedded views and their parents, it is useful to give the parent a unique identifier and pass it to the child views. When the child sends messages, it includes the uuid in the payload so the parent can validate it is the intended target. The uuid doesn't need to be persistent and can simply be assigned when the page loads.

Parent1 (with uuid)
  |    Child1 (knows parent uuid) -> `onClick` notify Parent1
  |    Child2 (knows parent uuid) -> `onClick` notify Parent1
  |    Child3 (knows parent uuid) -> `onClick` notify Parent1

Parent2 (with uuid)
  |    Child1 (knows parent uuid) -> `onClick` notify Parent2
  |    Child2 (knows parent uuid) -> `onClick` notify Parent2
  |    Child3 (knows parent uuid) -> `onClick` notify Parent2

@paul-griffith paul-griffith self-requested a review September 6, 2024 23:17
@paul-griffith
Copy link
Member

Ooh, I like it. I actually had an idea written down somewhere about adding a UUID function. Granted, this is contrived, but I wonder if it's worth baking some flexibility in - e.g. make the function uuid4 or uuid(4) or something. Probably not worth it, but what do you think?

From the other angle - is returning a direct UUID object preferred? Should the expression at least return a string, and maybe we also add a toUuid(str) casting function? And similarly for scripting?

@benmusson
Copy link
Collaborator Author

I can dig uuid4(), I think it's a good pattern. I thought about it but chose randomUUID because it matched with the underlying Java signature.

uuid(4) doesn't seem right; I can't think of any reason someone would want to parameterize the UUID version. Plus certain versions (specifically the non-random version 5 & 8) would need different signatures (like uuid5(name, namespace)).

My use case is for the string representation, but in my testing returning the UUID object seemed to give me the expected results everywhere I tried it (Perspective bindings, Vision bindings, expression tags) 🤷🏻‍♂️.

My thinking was that letting it get implicitly converted to a string was fine. You definitely know more than me about what's kosher for expression functions though, I don't have a strong preference.

@paul-griffith
Copy link
Member

Okay, cool. I think I'm convinced it's fine to hand back a 'true' UUID, probably more correct than anything else. Do you mind changing the expression and scripting functions to uuid4? That leaves us runway to implement our uuidv7, theoretically, which would otherwise be a bit confusing with "randomUUID" already in place.

@benmusson
Copy link
Collaborator Author

benmusson commented Sep 10, 2024 via email

@paul-griffith
Copy link
Member

Let's go uuid4 to save a character 😆

On the tests: Meh, there's not really anything to test (which I think is a good thing!) - we're just deferring to the JDK implementation, and there's no way to insert a testable RNG or anything into that, which I also think is good.

I'd honestly probably just test:

  1. that the return value is a UUID instance
  2. that the return value of two sequential calls is different, as you already have

I don't think there's anything else that would really add value.

@paul-griffith paul-griffith merged commit 5aba12e into IgnitionModuleDevelopmentCommunity:main Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants