Add ?. and ?[ optional-access operators#22
Merged
Merged
Conversation
What: - Add internal/optaccess: a token-level pre-parse rewrite that turns `obj?.field` into `__try_select__(obj, "field")` and `obj?[i]` into `__try_index__(obj, i)`. The walker matches balanced parens/brackets and treats earlier `?` tokens as continuation links so chained `a?.b?.c` rewrites cleanly. Strings, runes, and comments are not touched. - Wire optaccess into the Compile pipeline ahead of jsonlit and preprocessSource, so `?[1]` can carry the inner index expression through unmolested. - Register sentinel forms `__try_select__` / `__try_index__` in higher_order.go alongside the other special forms. Each form short-circuits on a nil receiver and treats missing-key / out-of-range as nil; wrong-kind type errors still surface. - Add language-level tests in optional_access_test.go (chained selects, fallback via ||, predicates, type-error propagation, string-literal preservation) and unit + fuzz tests in internal/optaccess. - Add fuzz seeds covering `?.` and `?[` to FuzzCompile / FuzzEval. - Update spec.md, llms.txt, and the higher-order-patterns and examples guides (plus their matching test) to use `find(...)?.x` instead of the unsafe `find(...).x`. Why: - `?.` plus operand-returning `||` plus `try` covers the same use cases as `??` without needing precedence-aware token rewriting. - The rewrite layer mirrors internal/jsonlit so the disambiguation logic stays simple and is reasoning about tokens, not strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
internal/optaccess: a token-level pre-parse rewrite that turnsobj?.fieldinto__try_select__(obj, "field")andobj?[i]into__try_index__(obj, i). The walker matches balanced parens/brackets and treats earlier?tokens as continuation links so chaineda?.b?.crewrites cleanly. Strings, runes, and comments are not touched.optaccessinto theCompilepipeline ahead ofjsonlitandpreprocessSource, so?[1]can carry the inner index expression through unmolested.__try_select__/__try_index__inhigher_order.go. Each form short-circuits on a nil receiver and treats missing-key / out-of-range asnil; wrong-kind type errors still surface asErrEvaluate.optional_access_test.go(chained selects, fallback via||, predicates, type-error propagation, string-literal preservation) plus unit + fuzz tests ininternal/optaccess.?.and?[toFuzzCompile/FuzzEval.spec.md,llms.txt, and the higher-order-patterns and examples guides (plus their matching test) to usefind(...)?.xinstead of the unsafefind(...).x.Why
?.plus operand-returning||plustrycovers the same use cases as??without needing precedence-aware token rewriting. The rewrite layer mirrorsinternal/jsonlitso the disambiguation logic stays simple and reasons about tokens, not strings.Test plan
go test ./...go test -race ./...go test -run=^$ -fuzz=FuzzCompile -fuzztime=20s .go test -run=^$ -fuzz=FuzzEval -fuzztime=20s .go test -run=^$ -fuzz=FuzzRewrite -fuzztime=20s ./internal/optaccess/