Comments (13)
Composite keys are just keys that inherit from all of their keywords. If we wanted some way of combining specs, then we can't use multimethods, as inheritance doesn't combine.
I've actually written a library for combined inheritance, but whether this is the best way of handling things for Integrant I'm not certain, as it introduces a new dependency and new syntax.
from integrant.
Your example doesn't appear to match up to the rest of the issue. If you have a configuration:
{[:group/a :adapter/jetty] {:port 8080, :handler #ig/ref [:group/a :handler/greet]}
[:group/a :handler/greet] {:name "Alice"}
[:group/b :adapter/jetty] {:port 8081, :handler #ig/ref [:group/b :handler/greet]}
[:group/b :handler/greet] {:name "Bob"}}
And specs:
(defmethod ig/pre-init-spec :adapter/jetty [_]
(s/keys :req-un [::port ::handler]))
(defmethod ig/pre-init-spec :handler/greet [_]
(s/keys :req-un [::name]))
Then it should work fine; I just tried it. It's only when you have multiple specs for the same keyword that the inheritance is forced to pick one.
from integrant.
@weavejester I'm not sure I understand because the two defmethods you quoted above never get invoked for that config. pre-init-spec
dispatches on the four composite keys.
from integrant.
Composite keys are just keys that inherit from all of their keywords. If we wanted some way of combining specs, then we can't use multimethods, as inheritance doesn't combine.
I think I understand that constraint better now. Even my cursory glance at the problem already hinted at some sort of hierarchy-driven spec composition mechanism, which seems like it will inevitably lead to some tradeoffs.
But while this behavior is self-explanatory when looking at the source code, I think it should be mentioned in the README and/or the pre-init-spec
docstring.
On the other hand I'm now wondering what kind of hooks could be provided so that if I have N composite keys, that should all somehow use the same spec from one of their components, I could define a single method for that hook rather the N boilerplate methods.
from integrant.
@weavejester I'm not sure I understand because the two defmethods you quoted above never get invoked for that config. pre-init-spec dispatches on the four composite keys.
Ah, you're right. There's a bug; pre-init-spec
doesn't normalize the composite keys first.
from integrant.
@weavejester I actually noticed that discrepancy but ig/normalize-key
returns something like :integrant.composite/group.a+handler.greet_35275
which seemed like something that would be used for an internal dispatch mechanism.
Could you describe what you think the behavior should be from the perspective of a user trying to write pre-init-spec
methods on a composite key?
from integrant.
The behaviour should be the same as all the other multimethods, in that composite keys should be treated as keywords that derive from all the keywords contained in the vector.
In other words, this:
[:group/a :handler/greet]
Should have the same semantic meaning as:
(derive :temp/a :group/a)
(derive :temp/a :handler/greet)
:temp/a
from integrant.
I'm sorry it's still not clear to me what method(s) would get dispatched for that composite key, and if there are more than one, how would the different specs get combined?
from integrant.
It's the same rules as normal multimethods. The most specific key is chosen, and ties are broken by prefer-method
.
So in the above case we have a key that's derived from :group/a
and :handler/greet
. Clojure will look for a multimethod that handles either of those. If there is a method for both, it uses prefer-method
to determine which to use. If neither has a multimethod, it looks at their parents, walking down the ancestors until it finds one with a method.
from integrant.
Commit e97c4b3 should fix the dispatch of pre-init-spec
.
from integrant.
Thanks! Any chance you could cut a release for this fix?
from integrant.
Awesome, thank you @weavejester
from integrant.
Released 0.6.2
from integrant.
Related Issues (20)
- Uninitialized/literal configs HOT 1
- Tests don't run with 1.11 due to clojure.test/run-test already being defined HOT 1
- Access to Enter Integrant presentation HOT 2
- Clarity on handling prod/dev config HOT 1
- ClojureScript support for `read-string` HOT 4
- Initialisation Order HOT 5
- Deprecate :include-refsets? in favor of :include-reflikes? HOT 16
- Decouple top-level keys from configuration type definitions HOT 9
- [Feature] Ability to provide custom hierarchy to `init` method HOT 6
- Would you consider an integrant2 to support extensible build steps? HOT 10
- Why does resume halt unspecified system keys? HOT 4
- Support usage in Babashka environments HOT 20
- #{:idea} Pass the spec validations step on all defined states before calling init-key. HOT 1
- is it possible to access a component after it's init-key, but before it gets passed to the next key that depends on it? HOT 2
- Enter Integrant Video
- Add support for custom assertf definitions HOT 2
- Question: Extract "validation keys" logic from core/build implementation HOT 4
- Feature request: pre-init hook
- No method in multimethod 'init-key' for dispatch value HOT 2
- #ig/ref should allow for deep references HOT 1
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 integrant.