plumb 0.4.0 → 0.4.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/README.md +43 -21
- data/lib/plumb/codec.rb +14 -0
- data/lib/plumb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 808d4d88d53c7e5c5cd273504f1faa146dfc75cf1965722abdc63bbfaad5a1a7
|
|
4
|
+
data.tar.gz: a2a1658bebd743544e6d9246ed4644775b44064d5f77865c8821d0ff2fa51db8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3ec5d6a028bb3c8d65a456b292b08c8e92fe609c7fb909d19217fdd1deee69bc0e63d58f4ba167e7474e7467e212c3ef0c4b6d69bf15ffcb50d1d2e4c1d45468
|
|
7
|
+
data.tar.gz: 604743c6c4d3327d2a3c462d431eeb3ac919210bce67387e665ac06592d0d94b08727fa065f48ac33e160cce09484258b2c261fa9312103aec83480d8f9ba7a6
|
data/README.md
CHANGED
|
@@ -305,39 +305,61 @@ You rarely write `Types::Never` by hand — it's what an impossible intersection
|
|
|
305
305
|
|
|
306
306
|
## Built-in types
|
|
307
307
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
* `Types::
|
|
308
|
+
Top and bottom:
|
|
309
|
+
|
|
310
|
+
* `Types::Any` — any value
|
|
311
|
+
* `Types::Never` — no value (see [`Types::Never`](#typesnever))
|
|
312
|
+
* `Types::Undefined` — the `Plumb::Undefined` sentinel (missing values)
|
|
313
|
+
|
|
314
|
+
Scalars:
|
|
315
|
+
|
|
316
|
+
* `Types::String`
|
|
311
317
|
* `Types::Symbol`
|
|
312
|
-
* `Types::Boolean`
|
|
313
|
-
* `Types::Interface`
|
|
314
|
-
* `Types::False`
|
|
315
|
-
* `Types::Tuple`
|
|
316
|
-
* `Types::Any`
|
|
317
|
-
* `Types::Static`
|
|
318
|
-
* `Types::Undefined`
|
|
319
|
-
* `Types::Nil`
|
|
320
|
-
* `Types::Integer`
|
|
321
318
|
* `Types::Numeric`
|
|
322
|
-
* `Types::
|
|
323
|
-
* `Types::
|
|
324
|
-
* `Types::
|
|
325
|
-
* `Types::
|
|
326
|
-
* `Types::
|
|
327
|
-
* `Types::
|
|
319
|
+
* `Types::Integer`
|
|
320
|
+
* `Types::Float`
|
|
321
|
+
* `Types::Decimal` (`BigDecimal`)
|
|
322
|
+
* `Types::Nil`
|
|
323
|
+
* `Types::True`
|
|
324
|
+
* `Types::False`
|
|
325
|
+
* `Types::Boolean`
|
|
328
326
|
* `Types::Date`
|
|
329
327
|
* `Types::Time`
|
|
328
|
+
* `Types::Email`
|
|
329
|
+
* `Types::UUID::V4`
|
|
330
330
|
* `Types::URI::Generic`
|
|
331
331
|
* `Types::URI::HTTP`
|
|
332
332
|
* `Types::URI::File`
|
|
333
|
-
|
|
333
|
+
|
|
334
|
+
Values and constraints:
|
|
335
|
+
|
|
336
|
+
* `Types::Static`
|
|
337
|
+
* `Types::Value`
|
|
338
|
+
* `Types::Not`
|
|
339
|
+
* `Types::Interface`
|
|
340
|
+
* `Types::Each` (anything that responds to `#each`)
|
|
341
|
+
* `Types::EachPair` (anything that responds to `#each_pair`)
|
|
342
|
+
|
|
343
|
+
Composites:
|
|
344
|
+
|
|
345
|
+
* `Types::Array`
|
|
346
|
+
* `Types::Tuple`
|
|
347
|
+
* `Types::Hash`
|
|
348
|
+
* `Types::SymbolizedHash`
|
|
349
|
+
* `Types::Stream`
|
|
350
|
+
* `Types::Range`
|
|
351
|
+
* `Types::Data`
|
|
352
|
+
|
|
353
|
+
Lenient coercions:
|
|
354
|
+
|
|
334
355
|
* `Types::Lax::String`
|
|
335
356
|
* `Types::Lax::Symbol`
|
|
357
|
+
* `Types::Lax::Numeric`
|
|
358
|
+
* `Types::Lax::Integer`
|
|
359
|
+
* `Types::Lax::Decimal`
|
|
336
360
|
|
|
337
361
|
For parsing stringy formats (HTML forms, query strings) into these types — what the `Types::Forms` namespace used to do, one way — see `Plumb::Codec::Forms` under [Encoders and Codecs](#encoders-and-codecs).
|
|
338
362
|
|
|
339
|
-
TODO: datetime, others.
|
|
340
|
-
|
|
341
363
|
## Policies
|
|
342
364
|
|
|
343
365
|
Policies are helpers that encapsulate common compositions. Plumb ships with some handy ones, listed below, and you can also define your own.
|
data/lib/plumb/codec.rb
CHANGED
|
@@ -342,6 +342,10 @@ module Plumb
|
|
|
342
342
|
enc = encoder_for(type, path)
|
|
343
343
|
return replace(type, enc, path) if enc
|
|
344
344
|
|
|
345
|
+
# #resolved_output collapses `Person.where(...)` to the bare AVM. Encode it as
|
|
346
|
+
# its base: as a leaf it passes the noop check (struct <: Hash) unencoded.
|
|
347
|
+
return visit(type.type, path) if @direction == :encode && type.is_a?(AttributeValueMatch)
|
|
348
|
+
|
|
345
349
|
case type
|
|
346
350
|
when HashClass then visit_hash(type, path)
|
|
347
351
|
when FilteredHash then visit_filtered_hash(type, path)
|
|
@@ -663,6 +667,11 @@ module Plumb
|
|
|
663
667
|
def visit_intersection(type, path)
|
|
664
668
|
steps = flatten_intersection(type)
|
|
665
669
|
refinements, data = steps.partition { |s| pure_refinement?(s) }
|
|
670
|
+
# Multi-key `#where` resolves to a meet of AVMs only. Encode their base, as #visit does.
|
|
671
|
+
if @direction == :encode && data.empty? && (avm = refinements.find { |s| s.is_a?(AttributeValueMatch) })
|
|
672
|
+
return visit(avm.type, path)
|
|
673
|
+
end
|
|
674
|
+
|
|
666
675
|
rewritten = data.map { |d| visit(d, path) }
|
|
667
676
|
return type if rewritten.each_with_index.all? { |r, i| r.equal?(data[i]) }
|
|
668
677
|
|
|
@@ -702,9 +711,14 @@ module Plumb
|
|
|
702
711
|
# `(opaque >> Integer)`, and rewriting that Integer into the codec's
|
|
703
712
|
# `String -> Integer` step would feed it the generated Integer and reject
|
|
704
713
|
# everything.
|
|
714
|
+
#
|
|
715
|
+
# ENCODE drops a refinement after a rewritten step: it would check the encoded
|
|
716
|
+
# form, e.g. `Person.where(name:)` is `And(Person, AVM)`, and `name` would hit the Hash.
|
|
705
717
|
def visit_composition(type, path)
|
|
706
718
|
left, right = type.children
|
|
707
719
|
l = pure_refinement?(left) ? left : visit(left, path)
|
|
720
|
+
return l if @direction == :encode && pure_refinement?(right) && !l.equal?(left)
|
|
721
|
+
|
|
708
722
|
r = pure_refinement?(right) || wire_consumed?(left, l) ? right : visit(right, path)
|
|
709
723
|
|
|
710
724
|
l.equal?(left) && r.equal?(right) ? type : Conjunction.build(l, r)
|
data/lib/plumb/version.rb
CHANGED