dry-types 0.13.2 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +763 -233
- data/LICENSE +17 -17
- data/README.md +15 -13
- data/dry-types.gemspec +28 -28
- data/lib/dry-types.rb +3 -1
- data/lib/dry/types.rb +156 -76
- data/lib/dry/types/any.rb +32 -12
- data/lib/dry/types/array.rb +19 -6
- data/lib/dry/types/array/constructor.rb +32 -0
- data/lib/dry/types/array/member.rb +75 -16
- data/lib/dry/types/builder.rb +131 -15
- data/lib/dry/types/builder_methods.rb +49 -20
- data/lib/dry/types/coercions.rb +76 -22
- data/lib/dry/types/coercions/json.rb +43 -7
- data/lib/dry/types/coercions/params.rb +118 -31
- data/lib/dry/types/compat.rb +0 -2
- data/lib/dry/types/compiler.rb +56 -41
- data/lib/dry/types/constrained.rb +81 -32
- data/lib/dry/types/constrained/coercible.rb +36 -6
- data/lib/dry/types/constraints.rb +18 -4
- data/lib/dry/types/constructor.rb +127 -54
- data/lib/dry/types/constructor/function.rb +216 -0
- data/lib/dry/types/constructor/wrapper.rb +94 -0
- data/lib/dry/types/container.rb +7 -0
- data/lib/dry/types/core.rb +54 -21
- data/lib/dry/types/decorator.rb +38 -17
- data/lib/dry/types/default.rb +61 -16
- data/lib/dry/types/enum.rb +43 -20
- data/lib/dry/types/errors.rb +75 -9
- data/lib/dry/types/extensions.rb +7 -1
- data/lib/dry/types/extensions/maybe.rb +74 -16
- data/lib/dry/types/extensions/monads.rb +29 -0
- data/lib/dry/types/fn_container.rb +6 -1
- data/lib/dry/types/hash.rb +86 -67
- data/lib/dry/types/hash/constructor.rb +33 -0
- data/lib/dry/types/inflector.rb +3 -1
- data/lib/dry/types/json.rb +18 -16
- data/lib/dry/types/lax.rb +75 -0
- data/lib/dry/types/map.rb +76 -33
- data/lib/dry/types/meta.rb +51 -0
- data/lib/dry/types/module.rb +120 -0
- data/lib/dry/types/nominal.rb +210 -0
- data/lib/dry/types/options.rb +13 -26
- data/lib/dry/types/params.rb +39 -25
- data/lib/dry/types/predicate_inferrer.rb +238 -0
- data/lib/dry/types/predicate_registry.rb +34 -0
- data/lib/dry/types/primitive_inferrer.rb +97 -0
- data/lib/dry/types/printable.rb +16 -0
- data/lib/dry/types/printer.rb +315 -0
- data/lib/dry/types/result.rb +29 -3
- data/lib/dry/types/schema.rb +408 -0
- data/lib/dry/types/schema/key.rb +156 -0
- data/lib/dry/types/spec/types.rb +103 -33
- data/lib/dry/types/sum.rb +84 -35
- data/lib/dry/types/type.rb +49 -0
- data/lib/dry/types/version.rb +3 -1
- metadata +68 -79
- data/.gitignore +0 -10
- data/.rspec +0 -2
- data/.travis.yml +0 -29
- data/.yardopts +0 -5
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -24
- data/Rakefile +0 -20
- data/benchmarks/hash_schemas.rb +0 -51
- data/lib/dry/types/compat/form_types.rb +0 -27
- data/lib/dry/types/compat/int.rb +0 -14
- data/lib/dry/types/definition.rb +0 -113
- data/lib/dry/types/hash/schema.rb +0 -199
- data/lib/dry/types/hash/schema_builder.rb +0 -75
- data/lib/dry/types/safe.rb +0 -59
@@ -1,75 +0,0 @@
|
|
1
|
-
require 'dry/types/hash/schema'
|
2
|
-
require 'dry/types/fn_container'
|
3
|
-
|
4
|
-
module Dry
|
5
|
-
module Types
|
6
|
-
class Hash < Definition
|
7
|
-
# A bulder for legacy schemas
|
8
|
-
# @api private
|
9
|
-
class SchemaBuilder
|
10
|
-
NIL_TO_UNDEFINED = -> v { v.nil? ? Undefined : v }
|
11
|
-
OMITTABLE_KEYS = %i(schema weak symbolized).freeze
|
12
|
-
STRICT = %i(strict strict_with_defaults).freeze
|
13
|
-
|
14
|
-
# @param primitive [Type]
|
15
|
-
# @option options [Hash{Symbol => Definition}] :member_types
|
16
|
-
# @option options [Symbol] :hash_type
|
17
|
-
def call(primitive, options)
|
18
|
-
hash_type = options.fetch(:hash_type)
|
19
|
-
member_types = {}
|
20
|
-
|
21
|
-
options.fetch(:member_types).each do |k, t|
|
22
|
-
member_types[k] = build_type(hash_type, t)
|
23
|
-
end
|
24
|
-
|
25
|
-
instantiate(primitive, **options, member_types: member_types)
|
26
|
-
end
|
27
|
-
|
28
|
-
def instantiate(primitive, hash_type: :base, meta: EMPTY_HASH, **options)
|
29
|
-
meta = meta.dup
|
30
|
-
|
31
|
-
meta[:strict] = true if strict?(hash_type)
|
32
|
-
meta[:key_transform_fn] = Schema::SYMBOLIZE_KEY if hash_type == :symbolized
|
33
|
-
|
34
|
-
Schema.new(primitive, **options, meta: meta)
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def omittable?(constructor)
|
40
|
-
OMITTABLE_KEYS.include?(constructor)
|
41
|
-
end
|
42
|
-
|
43
|
-
def strict?(constructor)
|
44
|
-
STRICT.include?(constructor)
|
45
|
-
end
|
46
|
-
|
47
|
-
def build_type(constructor, type)
|
48
|
-
type = safe(constructor, type)
|
49
|
-
type = default(constructor, type) if type.default?
|
50
|
-
type = type.meta(omittable: true) if omittable?(constructor)
|
51
|
-
type
|
52
|
-
end
|
53
|
-
|
54
|
-
def safe(constructor, type)
|
55
|
-
if constructor == :weak || constructor == :symbolized
|
56
|
-
type.safe
|
57
|
-
else
|
58
|
-
type
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def default(constructor, type)
|
63
|
-
case constructor
|
64
|
-
when :strict_with_defaults
|
65
|
-
type
|
66
|
-
when :strict
|
67
|
-
type.type
|
68
|
-
else
|
69
|
-
type.constructor(NIL_TO_UNDEFINED)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
data/lib/dry/types/safe.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'dry/types/decorator'
|
2
|
-
|
3
|
-
module Dry
|
4
|
-
module Types
|
5
|
-
class Safe
|
6
|
-
include Type
|
7
|
-
include Dry::Equalizer(:type)
|
8
|
-
include Decorator
|
9
|
-
include Builder
|
10
|
-
private :options, :meta
|
11
|
-
|
12
|
-
# @param [Object] input
|
13
|
-
# @return [Object]
|
14
|
-
def call(input)
|
15
|
-
result = try(input)
|
16
|
-
|
17
|
-
if result.respond_to?(:input)
|
18
|
-
result.input
|
19
|
-
else
|
20
|
-
input
|
21
|
-
end
|
22
|
-
end
|
23
|
-
alias_method :[], :call
|
24
|
-
|
25
|
-
# @param [Object] input
|
26
|
-
# @param [#call,nil] block
|
27
|
-
# @yieldparam [Failure] failure
|
28
|
-
# @yieldreturn [Result]
|
29
|
-
# @return [Result,Logic::Result]
|
30
|
-
def try(input, &block)
|
31
|
-
type.try(input, &block)
|
32
|
-
rescue TypeError, ArgumentError => e
|
33
|
-
result = failure(input, e.message)
|
34
|
-
block ? yield(result) : result
|
35
|
-
end
|
36
|
-
|
37
|
-
# @api public
|
38
|
-
#
|
39
|
-
# @see Definition#to_ast
|
40
|
-
def to_ast(meta: true)
|
41
|
-
[:safe, [type.to_ast(meta: meta), EMPTY_HASH]]
|
42
|
-
end
|
43
|
-
|
44
|
-
# @api public
|
45
|
-
# @return [Safe]
|
46
|
-
def safe
|
47
|
-
self
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
# @param [Object, Dry::Types::Constructor] response
|
53
|
-
# @return [Boolean]
|
54
|
-
def decorate?(response)
|
55
|
-
super || response.kind_of?(Constructor)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|