def no_key

bend-schema-lib@0.4.1.0 · core.bend:1741

import bend-schema-lib@0.4.1.0/core.bend as Core
import Base

1741# ---- the object a schema's encoding lands in ----
1742#
1743# enc writes an object as one flat chain of keys. An SField writes its name and
1744# its rest continues that object; an SVariant writes its name and the value
1745# under it is a chain of its own (REnd ends it, so it is a value and not a
1746# continuation). A wrapper -- a rule, a strict object, a bound, an optional --
1747# writes no key of its own and hands its value straight to its inner schema,
1748# so a key reaches the top of the object from inside any of them. A tagged
1749# case's schema is the same object the tag key was written into, and so is the
1750# rest of its chain.
1751#
1752# no_key is that, written down: k does not reach the top of the object enc
1753# builds for s, for any value of s. wf asks it of a tagged case's own schema,
1754# which is what keeps the tag key from being written twice.
1755def no_key(+k: String, s: Schema) -> Bool:
1756  match s:
1757    case SField{+n, fs, rest}:
1758      Bool.and(Bool.not(String.eq(n, k)), Bool.and(Bool.not(String.eq(k, n)), no_key(k, rest)))
1759    case SVariant{+n, vs, rest}:
1760      Bool.and(Bool.not(String.eq(n, k)), Bool.and(Bool.not(String.eq(k, n)), no_key(k, rest)))
1761    case SStrict{s2}:
1762      no_key(k, s2)
1763    case SRule{s2, tag}:
1764      no_key(k, s2)
1765    case SStrLen{lo, hi, s2}:
1766      no_key(k, s2)
1767    case SListLen{lo, hi, s2}:
1768      no_key(k, s2)
1769    case SOpt{i}:
1770      no_key(k, i)
1771    case SOptional{i}:
1772      no_key(k, i)
1773    case SEither{l, r}:
1774      Bool.and(no_key(k, l), no_key(k, r))
1775    case STagged{+k2, n2, cs2, rest2}:
1776      Bool.and(Bool.not(String.eq(k2, k)), Bool.and(Bool.not(String.eq(k, k2)), Bool.and(no_key(k, cs2), no_key(k, rest2))))
1777    case _:
1778      True{}

Uses (6)

Used by (1)