Step by varargs[Value, toPreserves]

This commit is contained in:
Emery Hemingway 2024-01-02 20:53:41 +02:00
parent a83c9ad3a4
commit a52e84dd70
2 changed files with 10 additions and 11 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "20240101"
version = "20240102"
author = "Emery Hemingway"
description = "data model and serialization format"
license = "Unlicense"

View File

@ -816,14 +816,7 @@ when isMainModule:
var pr = t.toPreservesHook()
assert fromPreservesHook(t, pr)
func step*(pr, idx: Value): Option[Value] =
## Step into `pr` by index `idx`.
## Works for sequences, records, and dictionaries.
runnableExamples:
import std/options
assert step(parsePreserves("""<foo 1 2>"""), 1.toPreserve) == some(2.toPreserve)
assert step(parsePreserves("""{ foo: 1 bar: 2}"""), "foo".toSymbol) == some(1.toPreserve)
assert step(parsePreserves("""[ ]"""), 1.toPreserve) == none(Value)
func step(pr, idx: Value): Option[Value] =
if pr.isDictionary:
for (k, v) in pr.dict.items:
if k == idx:
@ -836,8 +829,14 @@ func step*(pr, idx: Value): Option[Value] =
if i < pr.len:
result = some(pr[i])
func step*(pr: Value; path: varargs[Value]): Option[Value] =
## Step into `pr` by indexes at `path`.
func step*(pr: Value; path: varargs[Value, toPreserves]): Option[Value] =
## Step into `pr` by index `idx`.
## Works for sequences, records, and dictionaries.
runnableExamples:
import std/options
assert step(parsePreserves("""<foo 1 2>"""), 1.toPreserve) == some(2.toPreserve)
assert step(parsePreserves("""{ foo: 1 bar: 2}"""), "foo".toSymbol) == some(1.toPreserve)
assert step(parsePreserves("""[ ]"""), 1.toPreserve) == none(Value)
result = some(pr)
for index in path:
if result.isSome: