Support for future objects #211

Closed
opened 2025-12-30 01:22:16 +01:00 by adam · 4 comments
Owner

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:

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.

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: ``` 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.
adam closed this issue 2025-12-30 01:22:16 +01:00
Author
Owner

@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:

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.

@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: ``` 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.
Author
Owner

@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 isEmpty and containsKey API, as a makeshift null propagation for Maps.

@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 `isEmpty` and `containsKey` API, as a makeshift null propagation for Maps.
Author
Owner

@HT154 commented on GitHub (Oct 7, 2024):

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.

@HT154 commented on GitHub (Oct 7, 2024): > 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`](https://pkl-lang.org/package-docs/pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped/current/deepToTyped/index.html), which can convert untyped data (eg. Mapping/Listing/Dynamic instances representing HTTP response bodies parsed from JSON) to Pkl types.
Author
Owner

@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.

@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. https://github.com/kdeps/schema/blob/954d32c4f99f585cd12198bae35778fff1aceed2/deps/pkl/Http.pkl#L32 And deepToTyped will be very handy.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#211