Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding the ability to use a dictionary as a view context #228

Open
fwgreen opened this issue Nov 15, 2023 · 9 comments
Open

Consider adding the ability to use a dictionary as a view context #228

fwgreen opened this issue Nov 15, 2023 · 9 comments
Labels
enhancement New feature or request

Comments

@fwgreen
Copy link

fwgreen commented Nov 15, 2023

This might be a welcomed alternative to finding a unique name for something that, in most situations, doesn't require naming. Plus it has the added benefit of being familiar to someone coming from one of the many frameworks that use this approach.

My current approach, sadly, uses AnyCodable as Leaf's encoder isn't public.

import Vapor
import AnyCodable

extension ViewRenderer {
    public func render(_ name: String, _ context: [String: Any]) async throws -> View {
        let data = context.reduce(into: [:]) { $0[$1.0] = AnyCodable($1.1) }
        return try await self.render(name, data)
    }
}

Maybe making Leaf's builtin encoder public might be a possible compromise.

@fwgreen fwgreen added the enhancement New feature or request label Nov 15, 2023
@gwynne
Copy link
Member

gwynne commented Nov 15, 2023

This is already possible, see https://github.com/vapor/leaf/blob/main/Tests/LeafTests/LeafTests.swift#L20 for an example. What are you running into that's preventing you from doing this?

@fwgreen
Copy link
Author

fwgreen commented Nov 15, 2023

That test seems to only cover [String: String] but not [String: Any]. What I need is to be able to use a dictionary with a string key and a value of any encodable type.

@0xTim
Copy link
Member

0xTim commented Nov 18, 2023

What's wrong with creating an inline Codable type to represent the information? Do you not know the contents at compile time?

@fwgreen
Copy link
Author

fwgreen commented Nov 18, 2023

There's nothing wrong with that approach, and I'm glad to have the option. The problem is being forced to find a unique name for a view context which doesn't require a name. It's fine for small projects but becomes unwieldy for larger ones. I'm assuming that's why, at least in the Java world, most (if not all) frameworks offer some sort of HashMap backed model type for this purpose.

@0xTim
Copy link
Member

0xTim commented Nov 20, 2023

AnyCodable definitely won't work here, just investigated with another project and it doesn't have a stable API at the moment so there's no way we could use it.

Additionally the use of Any, (or even Sendable because Leaf will likely require Sendable contexts in the near future) kind of goes against Swift's strong type safety. You can create a nested context:

func someRouteHandler(_ req: Request) async throws -> View {
  struct Context: Encodable {
    // Whatever you want here
  }
  let context = Context(...)
  return try await req.view.render("template", context)
}

That provides the flexibility without needing to extract and name everything (though I'm still a big proponent for that, especially in large projects as it makes it clear what's needed and makes it easier to test). I think any addition would need to reach quite a high bar given the current APIs available

@fwgreen
Copy link
Author

fwgreen commented Nov 20, 2023

@0xTim My apologies if my choice of words made it seem as if I wanted to include AnyCodable in Vapor. To be clear, I do not. It was my reliance on AnyCodable that drove me to make the feature request in the first place. Is it possible (for my own personal extension) to use Leaf's own encoding facilities? I looked but did not see any public API for an encoder.

@0xTim
Copy link
Member

0xTim commented Nov 24, 2023

@fwgreen which parts to you want made public?

@0xTim
Copy link
Member

0xTim commented Nov 24, 2023

(Just to clarify, we could probably make LeafEncoder itself public, though I'm reluctant to do it until we've completed the Sendable work but a lot of the extensions are way to coupled with Leaf's internals to be made public)

@fwgreen
Copy link
Author

fwgreen commented Nov 25, 2023

Making LeafEncoder public seems more than enough for what I hope to achieve. However, I'm more than willing to relinquish the dream if the price is a major overhaul. Thanks for taking the time to look into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants