From 2cbd10844fdb07135b16229653aba7c8d790bd04 Mon Sep 17 00:00:00 2001 From: wallstop Date: Mon, 23 Nov 2026 00:14:43 +0000 Subject: [PATCH 1/2] Expose the unique direct authored YAML entry reader --- CHANGELOG.md | 1 + Editor/Validation/AuthoredAssetYaml.cs | 14 +++++++- .../Validation/AuthoredAssetYamlBlockTests.cs | 32 +++++++++++++++++ .../Validation/AuthoredAssetYamlFieldTests.cs | 36 +++++++++++++++++++ .../editor-tools/authored-asset-validation.md | 1 + 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 926ce488b..a277f55af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Expose `AuthoredAssetYaml.TryGetOnlyDirectEntry` for finding a unique root field before reading its YAML block ([#1127](https://github.com/Ambiguous-Interactive/unity-helpers/issues/1127)). - Add `NonDrawingGraphic` for rectangular or circular uGUI hit targets without drawing content ([#1291](https://github.com/Ambiguous-Interactive/unity-helpers/issues/1291)). - Add grouped moves for saves and their sidecars, with reverse-order rollback on failure ([#1227](https://github.com/Ambiguous-Interactive/unity-helpers/issues/1227)). - Add SHA-256 hashing over byte spans, with a caller-provided digest buffer or a hex string, and byte-span equality ([#1226](https://github.com/Ambiguous-Interactive/unity-helpers/issues/1226)). diff --git a/Editor/Validation/AuthoredAssetYaml.cs b/Editor/Validation/AuthoredAssetYaml.cs index d2dd08718..02c63162a 100644 --- a/Editor/Validation/AuthoredAssetYaml.cs +++ b/Editor/Validation/AuthoredAssetYaml.cs @@ -756,12 +756,24 @@ out string value return false; } - internal static bool TryGetOnlyDirectEntry( + /// Finds a uniquely named direct entry in a Unity YAML document. + /// Nested entries are ignored. Duplicate direct entries return false. + /// The document to inspect. + /// The exact serialized key, with an optional trailing colon. + /// The matching entry, or the default entry when the lookup fails. + /// Whether exactly one direct entry matches. + public static bool TryGetOnlyDirectEntry( AuthoredAssetDocument document, string field, out AuthoredAssetEntry entry ) { + if (document == null || string.IsNullOrWhiteSpace(field)) + { + entry = default; + return false; + } + bool found = false; AuthoredAssetEntry selected = default; IReadOnlyList entries = document.Entries; diff --git a/Tests/Editor/Validation/AuthoredAssetYamlBlockTests.cs b/Tests/Editor/Validation/AuthoredAssetYamlBlockTests.cs index f515f3f26..eab0f1574 100644 --- a/Tests/Editor/Validation/AuthoredAssetYamlBlockTests.cs +++ b/Tests/Editor/Validation/AuthoredAssetYamlBlockTests.cs @@ -31,6 +31,38 @@ private static AuthoredAssetEntry Field(AuthoredAssetDocument document, string n return default; } + [Test] + public void UniqueDirectEntryIdentifiesTheRootBlockWhenAChildSharesItsName() + { + string[] lines = + { + "--- !u!114 &7", + "MonoBehaviour:", + " child:", + " values:", + " - nested", + " values:", + " - root", + }; + AuthoredAssetDocument document = Parse(lines); + + Assert.That( + AuthoredAssetYaml.TryGetOnlyDirectEntry( + document, + "values:", + out AuthoredAssetEntry entry + ), + Is.True + ); + IReadOnlyList items = AuthoredAssetYaml.ReadBlockItems( + lines, + entry + ); + Assert.That(items.Count, Is.EqualTo(1)); + Assert.That(items[0].Value, Is.EqualTo("root")); + Assert.That(AuthoredAssetYaml.CountBlockItems(lines, entry), Is.EqualTo(1)); + } + [Test] public void BlockReadsKeepDirectElementsAndSourceLines() { diff --git a/Tests/Editor/Validation/AuthoredAssetYamlFieldTests.cs b/Tests/Editor/Validation/AuthoredAssetYamlFieldTests.cs index 87335b8d1..c512d8cac 100644 --- a/Tests/Editor/Validation/AuthoredAssetYamlFieldTests.cs +++ b/Tests/Editor/Validation/AuthoredAssetYamlFieldTests.cs @@ -46,6 +46,15 @@ public void MissingFieldReturnsEmpty(string field) Assert.That(AuthoredAssetYaml.TryGetOnlyField(document, field, out value), Is.False); Assert.That(value, Is.EqualTo(string.Empty)); Assert.That(AuthoredAssetYaml.Field(document, field), Is.EqualTo(string.Empty)); + Assert.That( + AuthoredAssetYaml.TryGetOnlyDirectEntry( + document, + field, + out AuthoredAssetEntry entry + ), + Is.False + ); + Assert.That(entry, Is.EqualTo(default(AuthoredAssetEntry))); } [Test] @@ -61,6 +70,15 @@ public void DirectFieldDoesNotFallBackToNestedOrLegacyName() Is.False ); Assert.That(value, Is.Empty); + Assert.That( + AuthoredAssetYaml.TryGetOnlyDirectEntry( + document, + "value", + out AuthoredAssetEntry entry + ), + Is.False + ); + Assert.That(entry, Is.EqualTo(default(AuthoredAssetEntry))); Assert.That(AuthoredAssetYaml.TryGetField(document, "_renamed", out value), Is.False); Assert.That(value, Is.Empty); } @@ -75,6 +93,15 @@ public void DuplicateDirectFieldKeepsFirstButUniqueReadRefusesIt() Is.False ); Assert.That(value, Is.Empty); + Assert.That( + AuthoredAssetYaml.TryGetOnlyDirectEntry( + document, + "value:", + out AuthoredAssetEntry entry + ), + Is.False + ); + Assert.That(entry, Is.EqualTo(default(AuthoredAssetEntry))); } [Test] @@ -134,6 +161,15 @@ public void NullDocumentFailsSoft() Assert.That(value, Is.Empty); Assert.That(AuthoredAssetYaml.Field(null, "value"), Is.Empty); Assert.That(AuthoredAssetYaml.ValueWithDirectChildren(null, "value"), Is.Empty); + Assert.That( + AuthoredAssetYaml.TryGetOnlyDirectEntry( + null, + "value", + out AuthoredAssetEntry entry + ), + Is.False + ); + Assert.That(entry, Is.EqualTo(default(AuthoredAssetEntry))); Assert.That( AuthoredAssetYaml.TryDescend(null, default, "value", out AuthoredAssetEntry child), Is.False diff --git a/docs/features/editor-tools/authored-asset-validation.md b/docs/features/editor-tools/authored-asset-validation.md index 3a1602011..4418a63f4 100644 --- a/docs/features/editor-tools/authored-asset-validation.md +++ b/docs/features/editor-tools/authored-asset-validation.md @@ -346,6 +346,7 @@ if (document.TryGetEntry("settings", out AuthoredAssetEntry settings) key returns the first value. `Field` returns an empty string for absence; use the Try method to distinguish an absent field from a present empty value. - `TryGetOnlyField` searches every parsed depth and fails when the name occurs more than once. +- `TryGetOnlyDirectEntry` finds a unique root field and returns its source span for block reads. - `TryDescend` checks the supplied parent's document membership and line span, accepts only children one indentation level below it, and refuses duplicate matching children. - `ValueWithDirectChildren` joins the inline value and nonempty direct child values, skipping From b3febf1ecdbdc7e4f41aafe0445fb99f5ed0ad8e Mon Sep 17 00:00:00 2001 From: wallstop Date: Mon, 23 Nov 2026 00:25:17 +0000 Subject: [PATCH 2/2] Group public authored YAML field readers together --- Editor/Validation/AuthoredAssetYaml.cs | 92 +++++++++++++------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/Editor/Validation/AuthoredAssetYaml.cs b/Editor/Validation/AuthoredAssetYaml.cs index 02c63162a..cfe9829b6 100644 --- a/Editor/Validation/AuthoredAssetYaml.cs +++ b/Editor/Validation/AuthoredAssetYaml.cs @@ -610,6 +610,52 @@ out string value return found; } + /// Finds a uniquely named direct entry in a Unity YAML document. + /// Nested entries are ignored. Duplicate direct entries return false. + /// The document to inspect. + /// The exact serialized key, with an optional trailing colon. + /// The matching entry, or the default entry when the lookup fails. + /// Whether exactly one direct entry matches. + public static bool TryGetOnlyDirectEntry( + AuthoredAssetDocument document, + string field, + out AuthoredAssetEntry entry + ) + { + if (document == null || string.IsNullOrWhiteSpace(field)) + { + entry = default; + return false; + } + + bool found = false; + AuthoredAssetEntry selected = default; + IReadOnlyList entries = document.Entries; + int count = entries.Count; + // The parser supplies a list; native measurements favor cached indexing for entries. + for (int index = 0; index < count; ++index) + { + AuthoredAssetEntry candidate = entries[index]; + if ( + candidate.SequenceDepth != 0 + || candidate.Indent != ColumnStep + || !MatchesFieldName(candidate.Key, field) + ) + { + continue; + } + if (found) + { + entry = default; + return false; + } + selected = candidate; + found = true; + } + entry = selected; + return found; + } + /// Reads a direct field's inline value, or an empty string when it is absent. /// The document to inspect. /// The exact serialized key, with an optional trailing colon. @@ -756,52 +802,6 @@ out string value return false; } - /// Finds a uniquely named direct entry in a Unity YAML document. - /// Nested entries are ignored. Duplicate direct entries return false. - /// The document to inspect. - /// The exact serialized key, with an optional trailing colon. - /// The matching entry, or the default entry when the lookup fails. - /// Whether exactly one direct entry matches. - public static bool TryGetOnlyDirectEntry( - AuthoredAssetDocument document, - string field, - out AuthoredAssetEntry entry - ) - { - if (document == null || string.IsNullOrWhiteSpace(field)) - { - entry = default; - return false; - } - - bool found = false; - AuthoredAssetEntry selected = default; - IReadOnlyList entries = document.Entries; - int count = entries.Count; - // The parser supplies a list; native measurements favor cached indexing for entries. - for (int index = 0; index < count; ++index) - { - AuthoredAssetEntry candidate = entries[index]; - if ( - candidate.SequenceDepth != 0 - || candidate.Indent != ColumnStep - || !MatchesFieldName(candidate.Key, field) - ) - { - continue; - } - if (found) - { - entry = default; - return false; - } - selected = candidate; - found = true; - } - entry = selected; - return found; - } - internal static bool TrySplitEntry(string content, out string key, out string inlineValue) { if (string.IsNullOrWhiteSpace(content))