( ast: SchemaAST.AST, value: unknown, walk: Walk )
| 1993 | * any other: the walk goes on part by part, and the repeat is a placeholder. |
| 1994 | */ |
| 1995 | const encodeAt = ( |
| 1996 | ast: SchemaAST.AST, |
| 1997 | value: unknown, |
| 1998 | walk: Walk |
| 1999 | ): Schema.Json => { |
| 2000 | if (typeof value === "object" && value !== null && walk.path.has(value)) { |
| 2001 | return { "~unserialisable": "cycle" } |
| 2002 | } |
| 2003 | const overlap = holdsRedactedOverlap(ast) |
| 2004 | const encode = overlap ? null : encoderOf(ast) |
| 2005 | if (encode !== null) { |
| 2006 | try { |
| 2007 | // A Schema such as `Schema.Any` passes a value through, so the result goes through `structural` too. |
| 2008 | return structural(encode(value)) |
| 2009 | } catch { |
| 2010 | // Encoded part by part below. |
| 2011 | } |
| 2012 | } |
| 2013 | if ( |
| 2014 | walk.strict && |
| 2015 | (!overlap || (ast.checks !== undefined && !guardOf(ast)(value))) |
| 2016 | ) return walk.leaf(ast, value) |
| 2017 | if (ast.encoding === undefined) { |
| 2018 | switch (ast._tag) { |
| 2019 | case "Suspend": |
| 2020 | return encodeAt(ast.thunk(), value, walk) |
| 2021 | case "Objects": |
| 2022 | if (isPlainRecord(value)) { |
| 2023 | return within(walk.path, value, () => encodeObject(ast, value, walk)) |
| 2024 | } |
| 2025 | break |
| 2026 | case "Arrays": |
| 2027 | if (Array.isArray(value)) { |
| 2028 | return within(walk.path, value, () => encodeArray(ast, value, walk)) |
| 2029 | } |
| 2030 | break |
| 2031 | case "Union": { |
| 2032 | const member = ast.types.find((type) => guardOf(type)(value)) |
| 2033 | if (member !== undefined) return encodeAt(member, value, walk) |
| 2034 | break |
| 2035 | } |
| 2036 | } |
| 2037 | } |
| 2038 | return walk.leaf(ast, value) |
| 2039 | } |
| 2040 | const within = <A>(path: Set<object>, value: object, f: () => A): A => { |
| 2041 | path.add(value) |
| 2042 | try { |
no test coverage detected