mirror of
https://github.com/apple/pkl.git
synced 2026-01-13 15:13:38 +01:00
Support for future objects #211
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 @jjuliano on GitHub (Oct 5, 2024).
I needed to preload a PKL file, that has a
@(...)convention that my code will translate to\(...)later.The "@( ... )" is a reference import that will happen in the future. For example, a file or an http client response.
The PKL that I need to preload, contains information to make an http request, like method and url.
HttpClient.pkl:
FutureClientResponse.pkl:
By the time I load the PKL file again, the responseBody will be
responseBody = "\(futureClient.response["http://example.com"].body)"responseHeaderXAPI = "\(futureClient.response["http://example.com"].header["X-API-Request"])"If I don't use that
@( ... )convention, and use the\( ... )interpolation directly, the PKL preload will error.Is there a SPICE or current feature that handles future events?
For example, if I have a Listing, and my code is obtaining a future value of that listing at index 10, then it will not error yet.
I'm thinking of something like
response.headers[10]?orheader["X-API-Request"])?, then this will not yet throw an error.@HT154 commented on GitHub (Oct 6, 2024):
Pkl evaluation is "one shot" or "atemporal". There is no "later" and no deferred/partial evaluation.
You might be interested in my work on SPICE-0009 (#660) that would allow you to implement the "your code" part of this as an external reader, avoiding the need for the mechanism you're describing. Your Pkl code would do something like this:
In this case where you want to pass potentially a lot structured data to the reader, I've found success in rendering a Pkl value describing the request to JSON, base64-encoding it, and using it as the entire URI after the colon. If you're implementing the reader using golang, it's easy to retrieve from the
url.URL'sOpaquefield.@jjuliano commented on GitHub (Oct 7, 2024):
Thanks, @HT154! The SPICE-0009 API seems quite intuitive. I can use the PKL standard library for HTTP client requests.
For now, I've realized that I need an idempotent function capable of handling null references for map keys and listings.
I've used Map's
isEmptyandcontainsKeyAPI, as a makeshift null propagation for Maps.@HT154 commented on GitHub (Oct 7, 2024):
I'm not 100% confident I'm understanding exactly what you mean, but it sounds like you're looking for the
getOrNullmethod (which exists onMap,Mapping, andList), which you can combine with Pkl's normal null propagation and coalescing features. You may also be interested indeepToTyped, which can convert untyped data (eg. Mapping/Listing/Dynamic instances representing HTTP response bodies parsed from JSON) to Pkl types.@jjuliano commented on GitHub (Oct 10, 2024):
Thanks @HT154 for point me out to getOrNull. Previously I use both the isEmpty and containsKey to get the related value.
954d32c4f9/deps/pkl/Http.pkl (L32)And deepToTyped will be very handy.