mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Nested Type Annotations #127
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @yesteryearer on GitHub (Apr 2, 2024).
Good day,
I'm pretty new to Pkl, and despite perusing the docs, can't find a working solution for the following:
I'm trying to generate a template for an object of the following form:
In the above scenario, any
tableinstances, must be able to contain any number ofmappings.The solution I designed got as far as, a
mapping.pklfile:And an instance as follows:
The above only works when the mapping isn't contained within the larger
salesInvoicesobject, and either way, I will only be able to have a single mapping in this manner.Is there a clever work around that I'm missing? Or rather, what is the best way to go about this? I saw the following, but in this instance I think the object only required a single instance of the nested object.
@yesteryearer commented on GitHub (Apr 2, 2024):
Even if there was a solution as the original poster said here, but with an undefined number of internal objects of the declared type, that would be wonderful. Not looking for an elegant solution.
@holzensp commented on GitHub (Apr 2, 2024):
I'm not too sure what
means. If you know the field names ahead of time, you don't want a
Mapping, but rather aclassthat defines properties. Only when you don't know the names ahead of time do you tend to use aMapping.Be weary that
import "mappings.pkl"doesn't actually do anything (you don't usemappingsanywhere); at the moment, Pkl only checks thatsalesInvoices.documentNo_to_invoiceNumberis indeed aMapping, but nothing else.@yesteryearer commented on GitHub (Apr 2, 2024):
Mapping in this case is not as per the Pkl definition, but rather a mapping between two databases. Hence the different tables and fields.
The first code block was supposed to just illustrate the general form of the object I'm trying to create.
Does the second code block not define a class with specific properties?
And finally, the final code doesn't even run, it throws an error saying:
annotated types can't be applied to non-local objectsor something along those lines.All I'm trying to do is enforce a specific form on to the objects of type 'table', where any one table can have one or many objects contained within it of the form:
Where each gp_table, gp_field, etc, variables will have varying values attributing to them.
@odenix commented on GitHub (Apr 2, 2024):
I guess you want something like the following:
mappings.pkl:
PS: This is a better fit for "Discussions" than "Issues".
@yesteryearer commented on GitHub (Apr 3, 2024):
@translatenix
Thank you so much, this is excellent + exactly what I was looking for.
I understand the code off the bat, but if you are willing to give a better explanation of your thought process, I'm all ears. Does
mappinginstantiate a many-to-one relationship? The only thing I don't really get is the distinction between using the variable name such astablesand the keywordtypealias, but I guess this is due to the fact that there is only a singletablesobject and manyDbTables.Also, I wasn't even aware of the "Discussions" section, so thanks again. First time I've made a forum post on GitHub.
@odenix commented on GitHub (Apr 3, 2024):
The type alias only improves readability and reusability.
Otherwise it's the same as
tables: Mapping<String, Mapping<String, DbMapping>>.A Pkl
Mappingis often called "map" or "dictionary" in other programming languages.@StefMa commented on GitHub (Apr 3, 2024):
There is also a Map type in Pkl with an richer API 🤓
@holzensp commented on GitHub (Apr 3, 2024):
Entirely with @translatenix's suggestions (both code and Discussion; but now that it's here, that's alright... do please close this Issue when you're fully satisfied, @yesteryearer).
Since you're talking about database tables and fields, I bet there are restrictions on the names of those things. It depends entirely on which database you're using what those restrictions are, but just for the example, I'm assuming names may not contain whitespace. This is to demonstrate type constraints and to further illustrate @translatenix's point about
typealiases:@yesteryearer commented on GitHub (Apr 3, 2024):
So to clarify, the reason I originally posted this here was because of the following error:
At which point I didn't know a workaround exists.
Thanks for the help.