parsing uses Maybe not Wrap
This commit is contained in:
parent
d867b91bc2
commit
06f21415e1
1 changed files with 33 additions and 35 deletions
|
|
@ -1,15 +1,9 @@
|
||||||
# XXX: TODO: use the Maybe/Null type instance instead of the Wrap type
|
|
||||||
{this, ...}: let
|
{this, ...}: let
|
||||||
inherit
|
inherit
|
||||||
(builtins)
|
(builtins)
|
||||||
foldl'
|
foldl'
|
||||||
hasAttr
|
|
||||||
isAttrs
|
isAttrs
|
||||||
;
|
mapAttrs
|
||||||
|
|
||||||
inherit
|
|
||||||
(this)
|
|
||||||
is
|
|
||||||
;
|
;
|
||||||
|
|
||||||
inherit
|
inherit
|
||||||
|
|
@ -18,50 +12,54 @@
|
||||||
;
|
;
|
||||||
|
|
||||||
inherit
|
inherit
|
||||||
(this.types)
|
(this.maybe)
|
||||||
Wrap
|
isSome
|
||||||
|
mapSome
|
||||||
|
unwrapMaybe
|
||||||
|
Some
|
||||||
|
None
|
||||||
;
|
;
|
||||||
in rec {
|
in rec {
|
||||||
# form: getAttrAt :: list string -> set -> null | Wrap Any
|
# getAttrAt :: [String] -> Attrs -> Maybe a
|
||||||
# given path as a list of strings, return that value of an
|
# Given an attribute set path as a list of strings,
|
||||||
# attribute set at that path
|
# returns the value of an attribute set at that path (Some)
|
||||||
|
# if it exists, otherwise returns None.
|
||||||
getAttrAt = path: xs:
|
getAttrAt = path: xs:
|
||||||
assert enfIsAttrs xs "getAttrAt";
|
assert enfIsAttrs xs "getAttrAt";
|
||||||
foldl' (left: right:
|
foldl' (acc: el:
|
||||||
if left != null && isAttrs left.value && hasAttr right left.value
|
acc
|
||||||
then Wrap left.value.${right}
|
|> mapSome (x:
|
||||||
else null)
|
if x ? ${el}
|
||||||
(Wrap xs)
|
then Some x.${el}
|
||||||
|
else None))
|
||||||
|
(Some xs)
|
||||||
path;
|
path;
|
||||||
|
|
||||||
# form: hasAttrAt :: list string -> set -> bool
|
# hasAttrAt :: [String] -> Attrs -> Bool
|
||||||
# given path as a list of strings, return that value of an
|
# Given an attribute set path as a list of strings,
|
||||||
# attribute set at that path
|
# returns a boolean indicating whether that path exists.
|
||||||
hasAttrAt = path: xs:
|
hasAttrAt = path: xs:
|
||||||
assert enfIsAttrs xs "hasAttrAt";
|
assert enfIsAttrs xs "hasAttrAt";
|
||||||
getAttrAt path xs != null; # NOTE: inefficient (im lazy)
|
getAttrAt path xs |> isSome; # NOTE: inefficient (im lazy)
|
||||||
|
|
||||||
|
# recmapFrom :: [String] -> ([String] -> Attrs -> a) -> Attrs -> a | Attrs a
|
||||||
# Alternative to mapAttrsRecursiveCond
|
# Alternative to mapAttrsRecursiveCond
|
||||||
# Allows mapping directly from a child path
|
# Allows mapping directly from a child path
|
||||||
recmap = let
|
recmapFrom = path: f: T:
|
||||||
recmapFrom = path: f: T:
|
if isAttrs T
|
||||||
if builtins.isAttrs T && ! is Wrap T
|
then mapAttrs (attr: leaf: recmapFrom (path ++ [attr]) f leaf) T
|
||||||
then builtins.mapAttrs (attr: leaf: recmapFrom (path ++ [attr]) f leaf) T
|
else f path T;
|
||||||
else f path T;
|
|
||||||
in
|
# recmap :: ([String] -> Attrs -> a) -> Attrs -> a | Attrs a
|
||||||
recmapFrom [];
|
recmap = recmapFrom [];
|
||||||
|
|
||||||
projectOnto = dst: src:
|
projectOnto = dst: src:
|
||||||
dst
|
dst
|
||||||
|> recmap
|
|> recmap
|
||||||
(path: dstLeaf: let
|
(path: dstLeaf: let
|
||||||
srcLeaf = getAttrAt path src;
|
srcLeaf = getAttrAt path src;
|
||||||
newLeaf =
|
|
||||||
if srcLeaf != null
|
|
||||||
then srcLeaf
|
|
||||||
else dstLeaf;
|
|
||||||
in
|
in
|
||||||
if is Wrap newLeaf
|
if isSome srcLeaf
|
||||||
then newLeaf.value
|
then unwrapMaybe srcLeaf
|
||||||
else newLeaf);
|
else dstLeaf);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue