query
On this page

longestValidPathPrefix

lib.attrsets.longestValidPathPrefix

Docs pulled from | This Revision | about 1 hour ago


Return the longest prefix of an attribute path that refers to an existing attribute in a nesting of attribute sets.

Can be used after mapAttrsRecursiveCond to apply a condition, although this will evaluate the predicate function on sibling attributes as well.

Note that the empty attribute path is valid for all values, so this function only throws an exception if any of its inputs does.

Laws:

  1. attrsets.longestValidPathPrefix [] x == []
    
  2. hasAttrByPath (attrsets.longestValidPathPrefix p x) x == true
    

Inputs

attrPath

A list of strings representing the longest possible path that may be returned.

v

The nested attribute set to check.

Type

attrsets.longestValidPathPrefix :: [String] -> Value -> [String]

Examples

lib.attrsets.longestValidPathPrefix usage example

x = { a = { b = 3; }; }
attrsets.longestValidPathPrefix ["a" "b" "c"] x
=> ["a" "b"]
attrsets.longestValidPathPrefix ["a"] x
=> ["a"]
attrsets.longestValidPathPrefix ["z" "z"] x
=> []
attrsets.longestValidPathPrefix ["z" "z"] (throw "no need")
=> []

Noogle detected

Implementation

The following is the current implementation of this function.

longestValidPathPrefix =
    attrPath:
    v:
    let
      lenAttrPath = length attrPath;
      getPrefixForSetAtIndex =
        # The nested attribute set to check, if it is an attribute set, which
        # is not a given.
        remainingSet:
        # The index of the attribute we're about to check, as well as
        # the length of the prefix we've already checked.
        remainingPathIndex:

          if remainingPathIndex == lenAttrPath then
            # All previously checked attributes exist, and no attr names left,
            # so we return the whole path.
            attrPath
          else
            let
              attr = elemAt attrPath remainingPathIndex;
            in
            if remainingSet ? ${attr} then
              getPrefixForSetAtIndex
                remainingSet.${attr}      # advance from the set to the attribute value
                (remainingPathIndex + 1)  # advance the path
            else
              # The attribute doesn't exist, so we return the prefix up to the
              # previously checked length.
              take remainingPathIndex attrPath;
    in
      getPrefixForSetAtIndex v 0;