practicalli / practicalli/clojure

Similar specs

Open
#125 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

spec
Dominant language
Makefile
Stars
117
Forks
36
PR merge metrics
No merged PRs in 30d

Description

Similar specs

How do you define a map spec with keys of similar spec? e.g.:

(s/def potato-name string?)
(s/def potato-happiness number?);; This is a potato:
{:name "potato name"
 :happiness-1 12
 :happiness-2 54}

Similarly, I see that (s/keys) requires a fully qualified name for each key and it's strange to me. What if I got two specs with the same key but different specs?

the idea behind spec is to give attributes global semantic meaning, by using namespaced attributes to differentiate and not do that

So it's not intended, for example, to use things like ::mygameobject-id on other namespaces of specific 'gameobject' each?

Among the things I'm playing around with is having a ::gameobject-id defined in some-ns.game and use that in some-ns.game.dog and some-ns.game.bat, each of which has an :id which is a gameobject-id. Am I doing things in a non-Clojure way?

:: means to auto-resolve the keyword in the context of the current namespace
so if you're using ::id in different namespaces, you'll get different keywords specific to each namespace, and there is no conflicting name (edited)

and if I want to use the same spec definition for each? just use a common function?
you can (s/def ::id ::common/gameobject-id) in each namespace
so have one common spec and then make namespaced specs that refer to it
removing the potentially confusing auto-resolving keywords, something like:

(s/def :common/gameobject-id string?)
(s/def :some-ns.game.dog/id :common/gameobject-id)
(s/def :some-ns.game.bit/id :common/gameobject-id)

I see that when I do something like

(s/def ::thingy (s/keys :req-un [:common/gameobject-id]))

Then the thingy gets an :id property, and not { :common/gameobject-id "whatever" }
Which is good for me
(or maybe :gameobject-id, I am mixing up some namings between this chat and my actual code)

it doesn't "get" a :gameobject-id property, it describes a map expected to have a :gameobject-id key which matches the :common/gameobject-id spec

I meant that when I conform something to ::thingy, the keyword in the conformed map is { :gameobject-id "something" } and not { :common/gameobject-id "something" }. I guess it omits the namespace from the keyword?

:req-un means "required unqualified key"
(as opposed to :req)
so more importantly it's validating the incoming map expecting an unqualified key
conforming just follow that

I seeeee I made a thingy2 now with :req and it wants the fully qualified key
Do I have to require common for using :common/gameobject-id or something?
no

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the issue's examples around s/def, s/keys, :req, :req-un, and conform to determine whether a documentation clarification is intended. There are no files, tests, or concrete acceptance criteria named, so the work is not startable until the desired explanation and documentation location are established.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.