Specs for conformed specs / ASTs
Asked Answered
G

1

6

I have a DSL specification which is a sequence as usual (cat). I want to take advantage of spec's parsing (i.e. conforming) to get the AST of an expression that conforms with my DSL. E.g.

user> (s/def ::person (s/cat :person-sym '#{person} :name string? :age number?))
=> :user/person
user> (s/conform ::person '(person "Henry The Sloth" 55))
=> {:person-sym person, :name "Henry The Sloth", :age 55}

Now that it's parsed and I have my AST, I would want to do interesting things with it, so I would want to test it and whatnot. So now I need to write a spec for that AST, and that's basically duplicating everything. Actually it's worse than that because now I have to s/def specs for predicates that I didn't have to before, because as the docs for keys says: "there is no support for inline value specification, by design." / "It is the (enforced) opinion of spec that the specification of values associated with a namespaced keyword, like :my.ns/k, should be registered under that keyword itself..". So duplicating (with omitting the person-sym part):

user> (s/def ::name string?)
=> :user/name
user> (s/def ::age number?)
=> :user/age
user> (s/def ::person-ast (s/keys :req-un [::name ::age]))
:user/person-ast

And now it seems to be compatible:

user> (s/conform ::person-ast (s/conform ::person '(person "Henry The Sloth" 55)))
=> {:person-sym person, :name "Henry The Sloth", :age 55}

In practice, I have more complicated data of course, and I wonder what should I do? AFAIK spec doesn't give me the spec for the AST that it creates (actually personally I would figure that this is something it should do). Any suggestions?

Gurnard answered 16/1, 2017 at 23:31 Comment(1)
Did you consider using spec/unform to test if a value was conformed using a spec? It seems to throw an exception if it is not possible to unform a value, so that would indicate whether it was conformed or not.Enthral
T
1

I'd say right now you have two options - one is to do what you're doing and create two sets of specs for the before/after.

The other option is to create a model of your domain in data and generate both specs (I've seen many people are doing something like this).

I have not heard Rich talk about generating the output spec of conformed results so I don't think that is likely in the current roadmap.

Termination answered 17/1, 2017 at 15:20 Comment(2)
Yeah, I figured so, thank you. You're implying that everything is Rich's decision, if so and if I may ask, did anyone ever bring this up to him? I think it would be worth it.Gurnard
I have not heard Rich discuss this idea.Termination

© 2022 - 2024 — McMap. All rights reserved.