Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Peter Leitzen
@splattael
@flash-gordon "need" is a strong word ;) I just wanted to sort by brain a bit %)
Is there a way to access the registered types after included into a custom module?
Example:
module MyTypes
  include Dry::Types.module
end

MyTypes['strict.int'] # ?
(as you may have noticed, we will finally use all dry-* awesomeness in one of our project)
\o/
Nikita Shilnikov
@flash-gordon
Awesome :)
Dry-types does export a container so no, [] are not there
Markus Unterwaditzer
@untitaker
How does dry-initializer work with optional options/params + types? Right now I need to do option :foo, MyType.optional, optional: true, which seems to be past the point.
if I remove .optional, the type can't "swallow" the nil it gets sent, if I leave out optional: true, I need to pass nil everytime.
also the naming seems completely broken to me... options are usually called options because they're optional...
Peter Leitzen
@splattael
@flash-gordon Ok, got it. So the only we is to use MyTypes::Strict::String, right?
Gustavo Caso
@GustavoCaso
@splattael Here some output maybe is helpful
type = Types.Instance(String)
type[nil] #=>  nil violates constraints (type?(String, nil) failed) (Dry::Types::ConstraintError)

Types::Strict::String[nil] #=> *** Dry::Types::ConstraintError Exception: nil violates constraints (type?(String, nil) failed)
As far as looks like they are the same type
Hope that helps
Peter Leitzen
@splattael
@GustavoCaso Awesome, thank you!
Nikita Shilnikov
@flash-gordon
@splattael yes
Andrew Kozin
@nepalez

@untitaker dry-initializer follows a logics slightly different from dry-types.

Dry types describes an attribute -- what values you can assign to it. Here "optional" means that you can send nil.

Dry-initializer describes dependencies where "optional" means that you can send nothing here. Here nothing does not mean nil, nil is explicit value in ruby -- you can ignore an attribute at all.

This makes sense when you need to treat cases "(re)set some value to nil" differently from "do not change (skip) an attribute". (Think of request params, for example).

The difference cannot be expressed in a type of some value, because in the second case there is no value to assign a type. Instead, you have to express it as a property of the initialized instance -- that's why 'optional: true' is needed.

So yes, you need both, here is no duplication.

Peter Leitzen
@splattael
Are there any other examples (besides https://github.com/icelab/berg) which use dry-system? I'd love to see more examples :smile:
Gustavo Caso
@GustavoCaso
Is not finish, but it use dry-web-roda as framework
Peter Leitzen
@splattael
@GustavoCaso :+1: Thanks!
Jeff Dickey
@jdickey
@GustavoCaso Very nice! Starred and watching :)
Hélio Costa e Silva
@hlegius
Hallo everyone! I was wondering if there’s a way to combine dry-validation with dry-types. I mean, validate input and coercing when possible to a defined type
Andriy Tyurnikov
@andriytyurnikov
Hola. I explore opportunity of generating GraphQL types and schemas from Dry-Types. I wonder if anyone tried some contact libraries/approaches in combination with Dry-Types ? General thought on contracts as part of dry-rb ?
Piotr Solnica
@solnic
@hlegius there's an experimental feature called 'type specs' (which will be default behavior in 1.0.0) http://dry-rb.org/gems/dry-validation/type-specs/
@andriytyurnikov what kind of contracts?
Andriy Tyurnikov
@andriytyurnikov
@solnic contracts that describe and ensure method signature - types of input values and types of returned values
Piotr Solnica
@solnic
@andriytyurnikov yes you could achieve that with dry-types
Andriy Tyurnikov
@andriytyurnikov
@solnic , are you sure?)
Piotr Solnica
@solnic
well, yes
Andriy Tyurnikov
@andriytyurnikov
@solnic , here is an example of contract library https://github.com/egonSchiele/contracts.ruby ; But in order to export code into other languages with typed return values one would need ability to ... inspect existing contract of the method
@solnic so far I came with idea of implementation that will just store output type, and maybe wrapping method invocation with routine that will throw an exception if output type doesn't match stored... which could be enough, I guess)
Piotr Solnica
@solnic
> t = Types::Strict::String.constrained(size: 2..16)
=> #<Dry::Types::Constrained type=#<Dry::Types::Definition primitive=String options={} meta={}> options={:rule=>#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[String]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#size?> options={:args=>[2..16]}>] options={}>} rule=#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[String]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#size?> options={:args=>[2..16]}>] options={}> meta={}>
> t.to_ast
=> [:constrained, [[:definition, [String, {}]], [:and, [[:predicate, [:type?, [[:type, String], [:input, Undefined]]]], [:predicate, [:size?, [[:size, 2..16], [:input, Undefined]]]]]], {}]]
@andriytyurnikov ^^
which means you can dump dry-types type objects to formal AST representation and convert them to whatever you want
Andriy Tyurnikov
@andriytyurnikov
@solnic thank you so much. This is second time I bother you with AST related question. First one was about generational ability of types (ability to generate instances that conform to spec). Is AST documented somewhere and I am missing something obvious?
Piotr Solnica
@solnic
create an AST from your spec, and compile it using type compiler from dry-types
Andriy Tyurnikov
@andriytyurnikov
@solnic , you lost me here (: this type compiler and 'formal AST' - are they documented somewhere? RDoc or something?
Piotr Solnica
@solnic
@andriytyurnikov it's not documented, dry-types is in beta, no time to keep docs up to date
you can look at specs
Andriy Tyurnikov
@andriytyurnikov
@solnic , ok, thanks for sharing knowledge of dark magic)
Piotr Solnica
@solnic
haha
it's actually the opposite of dark magic :)
it's just not so common in ruby libs
irb(main):009:0> require 'dry/types/compiler'
=> false
irb(main):010:0> tc = Dry::Types::Compiler.new(Dry::Types)
=> #<Dry::Types::Compiler:0x007fca038cacd8 @registry=Dry::Types>
irb(main):011:0> Undefined = Dry::Core::Constants::Undefined
=> Undefined
irb(main):012:0> tc.visit([:constrained, [[:definition, [String, {}]], [:and, [[:predicate, [:type?, [[:type, String], [:input, Undefined]]]], [:predicate, [:size?, [[:size, 2..16], [:input, Undefined]]]]]], {}]])
=> #<Dry::Types::Constrained type=#<Dry::Types::Definition primitive=String options={} meta={}> options={:rule=>#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[String]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#size?> options={:args=>[2..16]}>] options={}>} rule=#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[String]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#size?> options={:args=>[2..16]}>] options={}> meta={}>
@andriytyurnikov FYI ^
Andriy Tyurnikov
@andriytyurnikov
yeah, the fact that AST/CST tools are not in core lib of MRI is frustrating, jruby has some, btw
so