You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a Swagger library for ruby. I wanted a Swagger::Builder so projects like grape-swagger could walk the grape DSL and build a swagger object.
I'd already described the Swagger structure as a Dash so I could load complete Swagger files. I wanted to be able to use the same definition in order to build a Swagger object, but the required constraints would get in my way, forcing me to work with a normal Hash or Mash until the object was complete.
I implemented an experimental Bash mixin so I could get around that limitation:
A Bash is a 'builder' Hash that can be used to build a
# Dash ('defined' or 'discrete' Hash). It provides a
# `build` method that turns the Bash into a Dash. It enforces
# the same constraints and coercions as a Dash except for 'required' properties,
# which are not enforced until converting to a Dash via `build`.
See the Swagger::Builder spec for an example of how a Bash-based builder would work.
This let's me keep the "discrete" behavior of a Dash - so I get immediate errors if I try to use set an undefined property. Type coercions also happen immediately, and I'll get a Hashie:: CoercionError if I use the wrong type. These errors will happen on the line where I try to make the assignment, rather than much later when I'm converting a Hash to a Dash. I don't get the property 'x' is required errors until I call swagger_builder.build, but since that's an error of omission it wouldn't have a line number anyways.
The implementation has the advantage of being self-contained - it doesn't require changes to any Hashie classes (and doesn't monkeypatch them) but it achieves this through fragile and complex metaprogramming that dynamically subclasses Dash objects. I'm creating an issue rather than a PR in the hopes that someone has an idea for a simpler implementation that updates Dash to be more Bash friendly.
The text was updated successfully, but these errors were encountered:
I'm working on a Swagger library for ruby. I wanted a Swagger::Builder so projects like grape-swagger could walk the grape DSL and build a swagger object.
I'd already described the Swagger structure as a Dash so I could load complete Swagger files. I wanted to be able to use the same definition in order to build a Swagger object, but the
required
constraints would get in my way, forcing me to work with a normal Hash or Mash until the object was complete.I implemented an experimental Bash mixin so I could get around that limitation:
See the Swagger::Builder spec for an example of how a Bash-based builder would work.
This let's me keep the "discrete" behavior of a Dash - so I get immediate errors if I try to use set an undefined property. Type coercions also happen immediately, and I'll get a
Hashie:: CoercionError
if I use the wrong type. These errors will happen on the line where I try to make the assignment, rather than much later when I'm converting a Hash to a Dash. I don't get theproperty 'x' is required
errors until I callswagger_builder.build
, but since that's an error of omission it wouldn't have a line number anyways.The implementation has the advantage of being self-contained - it doesn't require changes to any Hashie classes (and doesn't monkeypatch them) but it achieves this through fragile and complex metaprogramming that dynamically subclasses Dash objects. I'm creating an issue rather than a PR in the hopes that someone has an idea for a simpler implementation that updates Dash to be more Bash friendly.
The text was updated successfully, but these errors were encountered: