Comments (9)
(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)
from leaf.
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?
from leaf.
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.
from leaf.
What's wrong with creating an inline Codable
type to represent the information? Do you not know the contents at compile time?
from leaf.
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.
from leaf.
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
from leaf.
@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.
from leaf.
@fwgreen which parts to you want made public?
from leaf.
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.
from leaf.
Related Issues (20)
- Leaf Cache should be disabled in dev
- Rationale behind using '#' character? HOT 3
- Add leaf format support in Xcode HOT 1
- [ NOTICE ] Starting Leaf Renderer with caching enabled HOT 4
- Custom header for a single view HOT 2
- Leaf crashes encoding array of optional UUIDs
- Render to String HOT 4
- [Question]How to set button onClick? HOT 2
- Questions about syntax highlighting and formatting for leaf HOT 3
- Bugs on Xcode 13.3 HOT 1
- Being able to loop over arrays that are the results of function calls HOT 1
- import statement unresolved causing error HOT 2
- [4.2.1] Fails to build with: Value of type '[CodingKey?]' has no member 'compacted' HOT 2
- Memory Leak on View Render with Encodable Struct/Enum HOT 2
- Leaf/LeafEncoder.swift:162: Fatal error after passing DB-Data to a Leaf-View HOT 2
- Consider adding a built-in view context HOT 3
- Xcode syntax highlighting HOT 1
- I hope Xcode can better support Leaf.
- New label request
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leaf.