Support for future objects
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:
import "FutureClientResponse.pkl" as "futureClient"
requestMethod = "GET"
requestUrl= "http://example.com"
responseBody = #"@(futureClient.response["http://example.com"].body)"#
responseHeaderXAPI = #"""
@(futureClient.response["http://example.com"].headers["X-API-Request"])
"""#
FutureClientResponse.pkl:
response {
["http://google.com"] {
response {
body = "I am a body \"\" with a lots of double-quotes \\/' and special characters"
headers {
["X-API-Request"] = "an x-api-request"
}
}
}
}
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]? or header["X-API-Request"])?, then this will not yet throw an error.
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:
response = read("my-http:...")
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's Opaque field.
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 isEmpty and containsKey API, as a makeshift null propagation for Maps.
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 isEmpty and containsKey API, as a makeshift null propagation for Maps.
I'm not 100% confident I'm understanding exactly what you mean, but it sounds like you're looking for the getOrNull method (which exists on Map, Mapping, and List), which you can combine with Pkl's normal null propagation and coalescing features. You may also be interested in deepToTyped, which can convert untyped data (eg. Mapping/Listing/Dynamic instances representing HTTP response bodies parsed from JSON) to Pkl types.
Thanks @HT154 for point me out to getOrNull. Previously I use both the isEmpty and containsKey to get the related value. https://github.com/kdeps/schema/blob/954d32c4f99f585cd12198bae35778fff1aceed2/deps/pkl/Http.pkl#L32 And deepToTyped will be very handy.