-
Notifications
You must be signed in to change notification settings - Fork 715
Create an OrganizedListSerializer to allow for sorting of YAML lists. #6827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Princess-Cheeseballs
wants to merge
5
commits into
space-wizards:master
Choose a base branch
from
Princess-Cheeseballs:organizedlistserializer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+352
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6148856
burmbgewr
Princess-Cheeseballs e1f5e79
updat ecommnet
Princess-Cheeseballs e970982
burher
Princess-Cheeseballs efea779
Merge branch 'master' into organizedlistserializer
Princess-Cheeseballs 6184932
everyone say "thank you delta"
Princess-Cheeseballs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
245 changes: 245 additions & 0 deletions
245
...ared.IntegrationTests/Serialization/TypeSerializers/Custom/OrganizedListSerializerTest.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| using NUnit.Framework; | ||
| using Robust.Shared.GameObjects; | ||
| using Robust.Shared.IoC; | ||
| using Robust.Shared.Map; | ||
| using Robust.Shared.Prototypes; | ||
| using Robust.Shared.Serialization.Manager; | ||
| using Robust.Shared.Serialization.Manager.Attributes; | ||
| using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom; | ||
| using Robust.Shared.Utility; | ||
| using Robust.UnitTesting.Shared; | ||
|
|
||
| namespace Robust.Shared.IntegrationTests.Serialization.TypeSerializers.Custom; | ||
|
|
||
| /// <summary> | ||
| /// Tests to ensure that <see cref="OrganizedListSerializer{T}"/> properly organizes a very simple list | ||
| /// </summary> | ||
| [TestFixture] | ||
| [TestOf(typeof(OrganizedListSerializer<>))] | ||
| internal sealed partial class OrganizedListSerializerTest : OurRobustUnitTest | ||
| { | ||
| private const string ParentProto = "ProtoA"; | ||
|
|
||
| private const string ChildProto = "ProtoB"; | ||
|
|
||
| private List<int> _key0Values = [2, 4, 8]; | ||
|
|
||
| private List<int> _key1Values = [1, 2, 2, 3, 4, 8]; | ||
|
|
||
| private List<int> _key2Values = [1, 3, 9]; | ||
|
|
||
| private List<int> _key1ValuesB = [1, 2, 3]; | ||
|
|
||
| private List<int> _key2ValuesB = [2, 4, 8]; | ||
|
|
||
| protected override Type[] ExtraComponents => new[] {typeof(TestComponent)}; | ||
|
|
||
| private static readonly string Prototypes = $@" | ||
| - type: entity | ||
| id: {ParentProto} | ||
| components: | ||
| - type: Test | ||
| listA: | ||
| - key: 1 | ||
| values: | ||
| - 2 | ||
| - 4 | ||
| - 8 | ||
| - key: 2 | ||
| values: | ||
| - 1 | ||
| - 3 | ||
| - 9 | ||
| listB: | ||
| - key: 1 | ||
| values: | ||
| - 2 | ||
| - 4 | ||
| - 8 | ||
| - key: 2 | ||
| values: | ||
| - 1 | ||
| - 3 | ||
| - 9 | ||
|
|
||
| - type: entity | ||
| parent: [{ParentProto}] | ||
| id: {ChildProto} | ||
| components: | ||
| - type: Test | ||
| listA: | ||
| - key: 1 | ||
| values: | ||
| - 1 | ||
| - 2 | ||
| - 3 | ||
| - key: 0 | ||
| values: | ||
| - 2 | ||
| - 4 | ||
| - 8 | ||
| listB: | ||
| - key: 1 | ||
| values: | ||
| - 1 | ||
| - 2 | ||
| - 3 | ||
| - key: 2 | ||
| values: | ||
| - 2 | ||
| - 4 | ||
| - 8 | ||
| "; | ||
|
|
||
| [Test] | ||
| public void OrganizedListTest() | ||
| { | ||
| var serializationManager = IoCManager.Resolve<ISerializationManager>(); | ||
| serializationManager.Initialize(); | ||
| var prototypeManager = IoCManager.Resolve<IPrototypeManager>(); | ||
|
|
||
| // We use entity prototypes since Serialization recognizes them without fuss, and we need complex YAML behavior for this test. | ||
| prototypeManager.RegisterKind(typeof(EntityPrototype), typeof(EntityCategoryPrototype)); | ||
| prototypeManager.LoadString(Prototypes); | ||
| prototypeManager.ResolveResults(); | ||
|
|
||
| var entityManager = IoCManager.Resolve<IEntityManager>(); | ||
| entityManager.System<SharedMapSystem>().CreateMap(out var mapId); | ||
|
|
||
| var coordinates = new MapCoordinates(0, 0, mapId); | ||
|
|
||
| var parentEntity = entityManager.SpawnEntity(ParentProto, coordinates); | ||
| var childEntity = entityManager.SpawnEntity(ChildProto, coordinates); | ||
|
|
||
| Assert.That(entityManager.TryGetComponent<TestComponent>(parentEntity, out var parentComp)); | ||
| Assert.That(entityManager.TryGetComponent<TestComponent>(childEntity, out var childComponent)); | ||
|
|
||
| // Instead of 4 items, two of them should've combined into one. | ||
| Assert.That(childComponent!.ListA.Count == 3, $"Child Component's AList failed to inherit properly. List contained {childComponent!.ListA.Count} entries."); | ||
|
|
||
| var index = 0; | ||
| // Ensure that the lists inherited correctly, and became organized and squashed correctly | ||
| foreach (var obj in childComponent.ListA) | ||
| { | ||
| // Items should be ordered! | ||
| Assert.That(obj.Key == index); | ||
| switch (obj.Key) | ||
| { | ||
| case 0: | ||
| // Should be an unmodified list. | ||
| Assert.That(obj.Values.SequenceEqual(_key0Values)); | ||
| break; | ||
| case 1: | ||
| for (var i = obj.Values.Count - 1; i >= 0; i--) | ||
| { | ||
| var item = obj.Values[i]; | ||
| Assert.That(obj.Values.Count == _key1Values.Count); | ||
| for (var j = _key1Values.Count - 1; j >= 0; j--) | ||
| { | ||
| if (_key1Values[j] != item) | ||
| continue; | ||
|
|
||
| _key1Values.RemoveSwap(j); | ||
| obj.Values.RemoveSwap(i); | ||
| break; | ||
| } | ||
| } | ||
| Assert.That(obj.Values.Count == 0 && _key1Values.Count == 0); | ||
| break; | ||
| case 2: | ||
| Assert.That(obj.Values.SequenceEqual(_key2Values)); | ||
| break; | ||
| default: | ||
| Assert.Fail($"Key was an unexpected value {obj.Key}"); | ||
| break; | ||
| } | ||
|
|
||
| index++; | ||
| } | ||
|
|
||
| // We alternate between keys 1 and 2 :P | ||
| index = 1; | ||
| // Ensure that this list followed normal push inheritance rules | ||
| for (var i = 0; i < childComponent.ListB.Count; i++) | ||
| { | ||
| // The greatest code known to man :) | ||
| var childList = childComponent.ListB[i]; | ||
| switch (i) | ||
| { | ||
| case 0: | ||
| Assert.That(childList.Values.Count == _key1ValuesB.Count); | ||
| for (var j = 0; j < childList.Values.Count; j++) | ||
| { | ||
| Assert.That(childList.Values[j] == _key1ValuesB[j]); | ||
| } | ||
| index++; | ||
| break; | ||
| case 1: | ||
| Assert.That(childList.Values.Count == _key2ValuesB.Count); | ||
| for (var j = 0; j < childList.Values.Count; j++) | ||
| { | ||
| Assert.That(childList.Values[j] == _key2ValuesB[j]); | ||
| } | ||
| index--; | ||
| break; | ||
| case 2: | ||
| var parentList = parentComp!.ListB[i - 2]; | ||
| Assert.That(childList.Values.Count == parentList.Values.Count); | ||
| for (var j = 0; j < childList.Values.Count; j++) | ||
| { | ||
| Assert.That(childList.Values[j] == parentList.Values[j]); | ||
| } | ||
| index++; | ||
| break; | ||
| case 3: | ||
| parentList = parentComp!.ListB[i - 2]; | ||
| Assert.That(childList.Values.Count == parentList.Values.Count); | ||
| for (var j = 0; j < childList.Values.Count; j++) | ||
| { | ||
| Assert.That(childList.Values[j] == parentList.Values[j]); | ||
| } | ||
| index--; | ||
| break; | ||
| default: | ||
| Assert.Fail($"ListB contained more elements than expected: {childComponent.ListB.Count}"); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [DataDefinition] | ||
| public sealed partial class ComplexTestObject : IOrganizeableCollection<ComplexTestObject> | ||
| { | ||
| [DataField(required: true)] | ||
| public int Key; | ||
|
|
||
| [DataField(required: true)] | ||
| public List<int> Values = new (); | ||
|
|
||
| public int CompareTo(ComplexTestObject? other) | ||
| { | ||
| return Key.CompareTo(other?.Key); | ||
| } | ||
|
|
||
| public bool Equals(ComplexTestObject? other) | ||
| { | ||
| return Key.Equals(other?.Key); | ||
| } | ||
|
|
||
| public void Insert(ComplexTestObject other) | ||
| { | ||
| Values.AddRange(other.Values) ; | ||
| } | ||
| } | ||
|
|
||
| public sealed partial class TestComponent : Component | ||
| { | ||
| [DataField(customTypeSerializer: typeof(OrganizedListSerializer<ComplexTestObject>))] | ||
| [AlwaysPushInheritance] | ||
| public List<ComplexTestObject> ListA = new (); | ||
|
|
||
| [DataField] | ||
| [AlwaysPushInheritance] | ||
| public List<ComplexTestObject> ListB = new (); | ||
| } |
107 changes: 107 additions & 0 deletions
107
...st.Shared/Serialization/TypeSerializers/Implementations/Custom/OrganizedListSerializer.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Robust.Shared.IoC; | ||
| using Robust.Shared.Log; | ||
| using Robust.Shared.Serialization.Manager; | ||
| using Robust.Shared.Serialization.Manager.Attributes; | ||
| using Robust.Shared.Serialization.Markdown; | ||
| using Robust.Shared.Serialization.Markdown.Sequence; | ||
| using Robust.Shared.Serialization.Markdown.Validation; | ||
| using Robust.Shared.Serialization.TypeSerializers.Interfaces; | ||
|
|
||
| namespace Robust.Shared.Serialization.TypeSerializers.Implementations.Custom; | ||
|
|
||
| /// <summary> | ||
| /// A highly complex serializer that allows you to serialize a list with complex objects | ||
| /// as if it were an organized dictionary. | ||
| /// Should be used on a Datafield with the <see cref="AlwaysPushInheritanceAttribute"/> | ||
| /// </summary> | ||
| /// <typeparam name="T">An object which implements <see cref="IOrganizeableCollection{T}"/></typeparam> | ||
| public sealed class OrganizedListSerializer<T> : BaseTypeSerializer, ITypeSerializer<List<T>, SequenceDataNode> where T : IOrganizeableCollection<T> | ||
| { | ||
| public ValidationNode Validate(ISerializationManager serializationManager, | ||
| SequenceDataNode node, | ||
| IDependencyCollection dependencies, | ||
| ISerializationContext? context = null) | ||
| { | ||
| var list = new List<ValidationNode>(node.Count); | ||
| foreach (var elem in node.Sequence) | ||
| { | ||
| list.Add(serializationManager.ValidateNode<T>(elem, context)); | ||
| } | ||
|
|
||
| return new ValidatedSequenceNode(list); | ||
| } | ||
|
|
||
| public List<T> Read(ISerializationManager serializationManager, | ||
| SequenceDataNode node, | ||
| IDependencyCollection dependencies, | ||
| SerializationHookContext hookCtx, | ||
| ISerializationContext? context = null, | ||
| ISerializationManager.InstantiationDelegate<List<T>>? instanceProvider = null) | ||
| { | ||
| if (instanceProvider != null) | ||
| Log.Warning($"Provided value to a Read-call for a {nameof(List<T>)}. Ignoring..."); | ||
|
|
||
| var list = new List<T>(node.Count); | ||
|
|
||
| foreach (var sequence in node.Sequence) | ||
| { | ||
| list.Add(serializationManager.Read<T>(sequence, hookCtx, context)); | ||
| } | ||
|
|
||
| for (var i = 0; i < list.Count; i++) | ||
| { | ||
| var item = list[i]; | ||
| var modified = false; | ||
| for (var j = list.Count - 1; j > i; j--) | ||
| { | ||
| var item2 = serializationManager.Read<T>(node.Sequence[j], hookCtx, context); | ||
| if (item.Equals(item2)) | ||
| { | ||
| // Note that this does not organize the nested list items. | ||
| // If you deem that necessary, then feel free to write additional logic for that :P | ||
| // You'd likely want to do the equality checking at the read step rather than here if you were going to. | ||
| list.RemoveAt(j); | ||
| item.Insert(item2); | ||
| modified = true; | ||
| } | ||
| } | ||
|
|
||
| if (modified) | ||
| list[i] = item; | ||
| } | ||
|
|
||
| list.Sort(); | ||
| return list; | ||
| } | ||
|
|
||
| public DataNode Write(ISerializationManager serializationManager, | ||
| List<T> value, | ||
| IDependencyCollection dependencies, | ||
| bool alwaysWrite = false, | ||
| ISerializationContext? context = null) | ||
| { | ||
| var sequence = new SequenceDataNode(value.Count); | ||
|
|
||
| foreach (var elem in value) | ||
| { | ||
| sequence.Add(serializationManager.WriteValue(elem, alwaysWrite, context)); | ||
| } | ||
|
|
||
| return sequence; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Interface for an object which can be organized and sorted internally. | ||
| /// </summary> | ||
| /// <typeparam name="T"></typeparam> | ||
| public interface IOrganizeableCollection<T> : IComparable<T>, IEquatable<T> | ||
| { | ||
| /// <summary> | ||
| /// Combines these two objects together, typically called by a <see cref="OrganizedListSerializer{T}"/> | ||
| /// </summary> | ||
| /// <param name="other">The other object we are combining into this one.</param> | ||
| void Insert(T other); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.