Skip to content
New issue

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

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

Already on GitHub? # to your account

s/enum doesn't preserve order #453

Open
lasiltan opened this issue Apr 15, 2024 · 3 comments
Open

s/enum doesn't preserve order #453

lasiltan opened this issue Apr 15, 2024 · 3 comments

Comments

@lasiltan
Copy link

s/enum doesn't preserve the order of the elements provided.
The order is lost at https://github.com/plumatic/schema/blob/master/src/cljc/schema/core.cljc#L295 when set is called.

This screws up ordering, for example, in swagger.

Replacing set with LinkedHashSet. would preserve order

@w01fe
Copy link
Member

w01fe commented Apr 16, 2024

Open to a PR, thanks for the report!

Is there a x-platform equivalent of LinkedHashSet? If not, we could store the ordered list and the set separately potentially?

@frenchy64
Copy link
Contributor

frenchy64 commented Apr 27, 2024

I think attaching the original vs onto the defrecord with assoc should work for all platforms.

(macros/defrecord-schema EnumSchema [vs]
  Schema
  (explain [this] (or (::explain this)
                      ;; backwards compat for users of ->EnumSchema
                      (cons 'enum vs))))

(clojure.core/defn enum
  "A value that must be = to some element of vs."
  [& vs]
  (assoc (EnumSchema. (set vs))
         ::explain (cons 'enum vs)))

EDIT: hmm, well EnumSchema is a record so an assoc can bring one of those fields out of sync. Unclear if that's a problem.

@frenchy64
Copy link
Contributor

Well, we could just treat the ordering as a hint that we can discard if the actual entries differ.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants