Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
80 changes: 46 additions & 34 deletions Editor/Validation/AuthoredAssetYaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,52 @@ out string value
return found;
}

/// <summary>Finds a uniquely named direct entry in a Unity YAML document.</summary>
/// <remarks>Nested entries are ignored. Duplicate direct entries return false.</remarks>
/// <param name="document">The document to inspect.</param>
/// <param name="field">The exact serialized key, with an optional trailing colon.</param>
/// <param name="entry">The matching entry, or the default entry when the lookup fails.</param>
/// <returns>Whether exactly one direct entry matches.</returns>
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<AuthoredAssetEntry> 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;
}

/// <summary>Reads a direct field's inline value, or an empty string when it is absent.</summary>
/// <param name="document">The document to inspect.</param>
/// <param name="field">The exact serialized key, with an optional trailing colon.</param>
Expand Down Expand Up @@ -756,40 +802,6 @@ out string value
return false;
}

internal static bool TryGetOnlyDirectEntry(
AuthoredAssetDocument document,
string field,
out AuthoredAssetEntry entry
)
{
bool found = false;
AuthoredAssetEntry selected = default;
IReadOnlyList<AuthoredAssetEntry> 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))
Expand Down
32 changes: 32 additions & 0 deletions Tests/Editor/Validation/AuthoredAssetYamlBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthoredSequenceElement> 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()
{
Expand Down
36 changes: 36 additions & 0 deletions Tests/Editor/Validation/AuthoredAssetYamlFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
}
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/features/editor-tools/authored-asset-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading