Skip to content

Commit 777a68f

Browse files
author
merge-queue-bot
committed
Merge PR #756: refactor(schema): deduplicate isClaimed — export as schema.IsClaimed (plan 2607191918)
2 parents 8716f54 + 9d61ee4 commit 777a68f

7 files changed

Lines changed: 48 additions & 38 deletions

File tree

PLAN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,5 @@ footer: |
256256
| 2607170527 | 🔳 | opus | [External link checking on WASM hosts (MDS072)](plan/2607170527_wasm-external-link-check.md) |
257257
| 2607171900 | 🔳 | opus | [Slidev structure rule (MDS073) — validate layouts, slots, fields, and frontmatter keys per slide](plan/2607171900_slidev-structure-rule.md) |
258258
| 2607191917 || haiku | [Add dedicated unit tests for printInitCatalog and setInitUsage](plan/2607191917_arch-fix-printinitcatalog-unit-test.md) |
259-
| 2607191918 | 🔲 | haiku | [Deduplicate isClaimed between internal/schema and requiredstructure](plan/2607191918_arch-fix-isclaimed-dedup.md) |
259+
| 2607191918 | | haiku | [Deduplicate isClaimed between internal/schema and requiredstructure](plan/2607191918_arch-fix-isclaimed-dedup.md) |
260260
<?/catalog?>

internal/rules/requiredstructure/rule.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ func walkRequiredHeadings(
19971997
allowExtra = true
19981998
continue
19991999
}
2000-
if isClaimed(claimed, schIdx) {
2000+
if schema.IsClaimed(claimed, schIdx) {
20012001
continue
20022002
}
20032003
// Save the position before the scan so that a missing-section
@@ -2013,7 +2013,7 @@ func walkRequiredHeadings(
20132013
if found {
20142014
allowExtra = false
20152015
}
2016-
if !found && !isClaimed(claimed, schIdx) {
2016+
if !found && !schema.IsClaimed(claimed, schIdx) {
20172017
diags = append(diags, missingSectionDiagLegacy(
20182018
f, req, ref, legacyPrecedingLine(docHeadings, preScanIdx)))
20192019
}
@@ -2153,24 +2153,14 @@ func levelMismatchDiag(
21532153
return d.Emit(makeDiag, f.Path, dh.Line)
21542154
}
21552155

2156-
// isClaimed reports whether idx is a member of claimed. claimed is a
2157-
// set — every entry is written exactly once, via claimed[idx] =
2158-
// struct{}{} — so map[int]struct{} (zero-byte value) replaces the
2159-
// map[int]bool this file used to spell as a truthy map read. Mirrors
2160-
// internal/schema/validate.go's isClaimed for the identical pattern.
2161-
func isClaimed(claimed map[int]struct{}, idx int) bool {
2162-
_, ok := claimed[idx]
2163-
return ok
2164-
}
2165-
21662156
// nextUnclaimed returns the first index in candidates that is >= minIdx
21672157
// and not yet claimed, or -1 if none qualifies.
21682158
func nextUnclaimed(candidates []int, claimed map[int]struct{}, minIdx int) int {
21692159
for _, idx := range candidates {
21702160
if idx < minIdx {
21712161
continue
21722162
}
2173-
if !isClaimed(claimed, idx) {
2163+
if !schema.IsClaimed(claimed, idx) {
21742164
return idx
21752165
}
21762166
}

internal/schema/matchtree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func collectUnlistedBlockMatches(
183183
claimed map[int]struct{}, blocks []contentBlock, parent *ScopeMatch,
184184
) {
185185
for i, dh := range heads {
186-
if isClaimed(claimed, i) || dh.Level != rootLevel {
186+
if IsClaimed(claimed, i) || dh.Level != rootLevel {
187187
continue
188188
}
189189
end := contentScopeEndLine(heads, i, dh.Level, docEnd)

internal/schema/validate.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -672,13 +672,8 @@ func skipBelow(heads []DocHeading, rootLevel int) []DocHeading {
672672
return out
673673
}
674674

675-
// isClaimed reports whether idx is a member of claimed. claimed is a
676-
// set — every entry is written exactly once, via claimed[idx] =
677-
// struct{}{} — so map[int]struct{} (zero-byte value) replaces the
678-
// map[int]bool every read/write site in this file, matchtree.go, and
679-
// validate_content.go used to spell as a truthy map read. See
680-
// docs/development/high-performance-go.md "map[K]struct{} for sets".
681-
func isClaimed(claimed map[int]struct{}, idx int) bool {
675+
// IsClaimed reports whether idx is a member of the claimed set.
676+
func IsClaimed(claimed map[int]struct{}, idx int) bool {
682677
_, ok := claimed[idx]
683678
return ok
684679
}
@@ -715,7 +710,7 @@ func validateScopes(
715710
claimed[i] = struct{}{}
716711
continue
717712
}
718-
if isClaimed(claimed, i) {
713+
if IsClaimed(claimed, i) {
719714
continue
720715
}
721716
newIdx, scDiags, claimedThis := matchScope(
@@ -725,7 +720,7 @@ func validateScopes(
725720
docIdx = newIdx
726721
if claimedThis {
727722
allowExtra = false
728-
} else if !isClaimed(claimed, i) && sc.Required() {
723+
} else if !IsClaimed(claimed, i) && sc.Required() {
729724
// Anchor the missing section at the heading it should
730725
// follow (the preceding document heading), so the
731726
// squiggle lands where the section belongs rather than
@@ -898,7 +893,7 @@ func claimedScopeMatches(
898893
claimCounts map[int]int, docFM map[string]any,
899894
) (int, bool) {
900895
for i, sc := range scopes {
901-
if !isClaimed(claimed, i) {
896+
if !IsClaimed(claimed, i) {
902897
continue
903898
}
904899
if sc.Preamble || isSlotMatcher(sc.Matcher) {
@@ -961,7 +956,7 @@ func unclaimedListedScope(
961956
docFM map[string]any,
962957
) int {
963958
for i, sc := range scopes {
964-
if isClaimed(claimed, i) || sc.Preamble || isSlotMatcher(sc.Matcher) {
959+
if IsClaimed(claimed, i) || sc.Preamble || isSlotMatcher(sc.Matcher) {
965960
continue
966961
}
967962
if scopeMatchesHeading(sc, dh, docFM) {
@@ -1306,7 +1301,7 @@ func anyLaterScopeClaims(
13061301
) bool {
13071302
for i := startIdx; i < len(scopes); i++ {
13081303
sc := scopes[i]
1309-
if isClaimed(claimed, i) || sc.Preamble || isSlotMatcher(sc.Matcher) {
1304+
if IsClaimed(claimed, i) || sc.Preamble || isSlotMatcher(sc.Matcher) {
13101305
continue
13111306
}
13121307
if scopeMatchesHeading(sc, dh, docFM) {
@@ -1330,7 +1325,7 @@ func claimsLaterLiteral(
13301325
) bool {
13311326
for i := startIdx; i < len(scopes); i++ {
13321327
sc := scopes[i]
1333-
if isClaimed(claimed, i) || sc.Preamble ||
1328+
if IsClaimed(claimed, i) || sc.Preamble ||
13341329
isSlotMatcher(sc.Matcher) || isBroadMatcher(sc.Matcher) {
13351330
continue
13361331
}
@@ -1465,7 +1460,7 @@ func findOutOfOrderIdx(
14651460
) int {
14661461
for i := minIdx; i < len(scopes); i++ {
14671462
sc := scopes[i]
1468-
if isClaimed(claimed, i) || sc.Preamble || isSlotMatcher(sc.Matcher) {
1463+
if IsClaimed(claimed, i) || sc.Preamble || isSlotMatcher(sc.Matcher) {
14691464
continue
14701465
}
14711466
if scopeMatchesHeading(sc, dh, docFM) {
@@ -1537,7 +1532,7 @@ func scanScopeRunAtLevel(
15371532
var out []int
15381533
started := false
15391534
for i, h := range heads {
1540-
if isClaimed(claimed, i) {
1535+
if IsClaimed(claimed, i) {
15411536
continue
15421537
}
15431538
if h.Line < parentStart || h.Line >= parentEnd {
@@ -1585,7 +1580,7 @@ func firstWrongLevelMatch(
15851580
return -1
15861581
}
15871582
for i, h := range heads {
1588-
if isClaimed(claimed, i) {
1583+
if IsClaimed(claimed, i) {
15891584
continue
15901585
}
15911586
if h.Line < parentStart || h.Line >= parentEnd {

internal/schema/validate_content.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func (w *contentWalker) run(diags *[]lint.Diagnostic) {
394394
w.allowExtra = true
395395
continue
396396
}
397-
if isClaimed(w.claimed, i) {
397+
if IsClaimed(w.claimed, i) {
398398
continue
399399
}
400400
w.matchEntry(i, entry, diags)
@@ -445,7 +445,7 @@ func (w *contentWalker) matchEntry(
445445
}
446446
w.nodeIdx++
447447
}
448-
if !isClaimed(w.claimed, i) && entry.Required {
448+
if !IsClaimed(w.claimed, i) && entry.Required {
449449
*diags = append(*diags, w.mkDiag(
450450
w.f.Path, w.sectionLine,
451451
fmt.Sprintf("missing required content %q inside %s",
@@ -460,7 +460,7 @@ func (w *contentWalker) matchEntry(
460460
// node by kind.
461461
func (w *contentWalker) findLaterEntry(startIdx int, n ast.Node) int {
462462
for j := startIdx; j < len(w.sc.Content); j++ {
463-
if isClaimed(w.claimed, j) {
463+
if IsClaimed(w.claimed, j) {
464464
continue
465465
}
466466
e := w.sc.Content[j]

internal/schema/validate_coverage_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,28 @@ func TestDedupedCUEErrorDiags_DuplicateKeyCollapses(t *testing.T) {
409409
diags := dedupedCUEErrorDiags(f, sch, docFM, dup, keyLines, makeDiagForTest)
410410
assert.Len(t, diags, 1, "two identical cueErrs entries must collapse to one diagnostic")
411411
}
412+
413+
// TestIsClaimed covers both branches of IsClaimed: a present idx
414+
// reports true; an absent idx reports false.
415+
func TestIsClaimed(t *testing.T) {
416+
claimed := map[int]struct{}{3: {}}
417+
assert.True(t, IsClaimed(claimed, 3), "present idx must be claimed")
418+
assert.False(t, IsClaimed(claimed, 7), "absent idx must not be claimed")
419+
}
420+
421+
func TestIsClaimed_Nil(t *testing.T) {
422+
assert.False(t, IsClaimed(nil, 0))
423+
}
424+
425+
func TestIsClaimed_Empty(t *testing.T) {
426+
assert.False(t, IsClaimed(map[int]struct{}{}, 0))
427+
}
428+
429+
// TestIsClaimedAcceptsStructSet pins IsClaimed's "claimed" parameter to
430+
// map[int]struct{}, not map[int]bool. The struct{} value drops per-entry
431+
// map overhead versus bool. See
432+
// docs/development/high-performance-go.md "map[K]struct{} for sets".
433+
func TestIsClaimedAcceptsStructSet(t *testing.T) {
434+
accept := func(func(map[int]struct{}, int) bool) {}
435+
accept(IsClaimed)
436+
}

plan/2607191918_arch-fix-isclaimed-dedup.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: 2607191918
33
title: >-
44
Deduplicate isClaimed between internal/schema and
55
requiredstructure
6-
status: "🔲"
6+
status: ""
77
model: haiku
88
summary: >-
99
internal/rules/requiredstructure/rule.go carries a
@@ -58,7 +58,7 @@ found this duplication:
5858

5959
## Acceptance Criteria
6060

61-
- [ ] Only one implementation of the claimed-range check
61+
- [x] Only one implementation of the claimed-range check
6262
exists in the repository.
63-
- [ ] `go test ./...` is green.
64-
- [ ] `mdsmith check .` is green.
63+
- [x] `go test ./...` is green.
64+
- [x] `mdsmith check .` is green.

0 commit comments

Comments
 (0)