diff --git a/src/LLTSharp/LLTParser.cs b/src/LLTSharp/LLTParser.cs index 435c7fc..b7f0197 100644 --- a/src/LLTSharp/LLTParser.cs +++ b/src/LLTSharp/LLTParser.cs @@ -703,23 +703,21 @@ private static void DeclareMainRules(ParserBuilder builder) var obj = v.GetValue(2); var metadata = new List(); - var additionalMetadata = new AdditionalMetadata(); foreach (var pair in obj.Dictionary) { IMetadata? result = null; + foreach (var factory in factories) if (factory.TryCreateMetadata(pair.Key, pair.Value, out result)) break; + if (result != null) metadata.Add(result); else - additionalMetadata.Set(pair.Key, pair.Value.GetValue()); + metadata.Add(new AdditionalMetadata(pair.Key, pair.Value.GetValue())); } - if (additionalMetadata.Count > 0) - metadata.Add(additionalMetadata.ToImmutable()); - return new MetadataCollection(metadata); }); diff --git a/src/LLTSharp/LLTSharp.csproj b/src/LLTSharp/LLTSharp.csproj index 44b8b38..e27d404 100644 --- a/src/LLTSharp/LLTSharp.csproj +++ b/src/LLTSharp/LLTSharp.csproj @@ -8,7 +8,7 @@ LLTSharp - 1.2.4 + 1.3.0 Roman K. RomeCore LLTSharp diff --git a/src/LLTSharp/Metadata/AdditionalMetadata.cs b/src/LLTSharp/Metadata/AdditionalMetadata.cs index 67e59a3..3af9afe 100644 --- a/src/LLTSharp/Metadata/AdditionalMetadata.cs +++ b/src/LLTSharp/Metadata/AdditionalMetadata.cs @@ -9,208 +9,43 @@ namespace LLTSharp.Metadata /// /// Represents a dynamic metadata object that can store additional properties at runtime. /// - public class AdditionalMetadata : DynamicObject, IAdditionalMetadata + public class AdditionalMetadata : IAdditionalMetadata, IEquatable, IEquatable { - private readonly Dictionary _metadata = new Dictionary(StringComparer.OrdinalIgnoreCase); + public string Key { get; } - /// - /// Gets a shared immutable empty instance of additional metadata object. - /// - public static IAdditionalMetadata Empty => ImmutableAdditionalMetadata.Empty; - - /// - /// Gets or sets the value associated with the specified key. - /// - /// The key of the value to get or set. - /// The value associated with the specified key. - public object this[string key] - { - get => _metadata.TryGetValue(key, out var value) ? value : null; - set => _metadata[key] = value; - } - - /// - /// Initializes a new instance of the class. - /// - public AdditionalMetadata() - { - } + public object? Value { get; } /// /// Initializes a new instance of the class with initial key-value pair. /// /// The key of the initial metadata item. /// The value of the initial metadata item. - public AdditionalMetadata(string key, object value) - { - _metadata[key] = value; - } - - /// - /// Initializes a new instance of the class with dictionary of initial values. - /// - /// Dictionary containing initial metadata values. - public AdditionalMetadata(IDictionary initialValues) - { - if (initialValues != null) - { - foreach (var kvp in initialValues) - { - _metadata[kvp.Key] = kvp.Value; - } - } - } - - /// - /// Initializes a new instance of the class with collection of key-value pairs. - /// - /// Collection of key-value pairs for initial metadata. - public AdditionalMetadata(IEnumerable> initialValues) - { - if (initialValues != null) - { - foreach (var kvp in initialValues) - { - _metadata[kvp.Key] = kvp.Value; - } - } - } - - /// - /// Gets the value associated with the specified key. - /// - /// The type to convert the value to. - /// The key of the value to get. - /// The converted value or default(T) if not found. - public T Get(string key) + public AdditionalMetadata(string key, object? value) { - if (_metadata.TryGetValue(key, out var value)) - { - try - { - return (T)Convert.ChangeType(value, typeof(T)); - } - catch - { - return default; - } - } - return default; + Key = key; + Value = value; } - /// - /// Tries to get the value associated with the specified key. - /// - /// The type to convert the value to. - /// The key of the value to get. - /// When this method returns, contains the value if found; otherwise, default(T). - /// true if the key was found; otherwise, false. - public bool TryGet(string key, out T value) - { - if (_metadata.TryGetValue(key, out var objValue)) - { - try - { - value = (T)Convert.ChangeType(objValue, typeof(T)); - return true; - } - catch - { - value = default; - return false; - } - } - value = default; - return false; - } - - /// - /// Adds or updates a metadata value. - /// - /// The key of the value to set. - /// The value to set. - public void Set(string key, object value) - { - _metadata[key] = value; - } - - /// - /// Determines whether the metadata contains the specified key. - /// - /// The key to locate. - /// true if the metadata contains the key; otherwise, false. - public bool ContainsKey(string key) - { - return _metadata.ContainsKey(key); - } - - /// - /// Removes the value with the specified key. - /// - /// The key of the value to remove. - /// true if the element was found and removed; otherwise, false. - public bool Remove(string key) - { - return _metadata.Remove(key); - } - - /// - /// Gets all the keys in the metadata. - /// - public IEnumerable Keys => _metadata.Keys; - - /// - /// Gets all the values in the metadata. - /// - public IEnumerable Values => _metadata.Values; - - /// - /// Gets the number of metadata items. - /// - public int Count => _metadata.Count; - - /// - /// Clears all metadata. - /// - public void Clear() - { - _metadata.Clear(); - } - - /// - /// Converts this instance to immutable copy. - /// - public ImmutableAdditionalMetadata ToImmutable() - { - return new ImmutableAdditionalMetadata(_metadata); - } - - public override bool TryGetMember(GetMemberBinder binder, out object result) - { - return _metadata.TryGetValue(binder.Name, out result); - } - - public override bool TrySetMember(SetMemberBinder binder, object value) + public override bool Equals(object? obj) { - _metadata[binder.Name] = value; - return true; + return obj is AdditionalMetadata other && Equals(other); } - public override IEnumerable GetDynamicMemberNames() + public bool Equals(AdditionalMetadata? other) { - return _metadata.Keys; + return other != null && Key == other.Key && Value == other.Value; } - public override bool Equals(object? obj) + public bool Equals(IAdditionalMetadata? other) { - return obj is AdditionalMetadata other && - DictionaryUtils.DictionariesEqual(_metadata, other._metadata); + return other != null && Key == other.Key && Value == other.Value; } public override int GetHashCode() { int hash = 17; - hash = hash * 397 + DictionaryUtils.GetDictionaryHashCode(_metadata); + hash = hash * 397 + Key.GetHashCode(); + hash = hash * 397 + (Value?.GetHashCode() ?? 0); return hash; } } diff --git a/src/LLTSharp/Metadata/IAdditionalMetadata.cs b/src/LLTSharp/Metadata/IAdditionalMetadata.cs index 4912e5d..20b6f23 100644 --- a/src/LLTSharp/Metadata/IAdditionalMetadata.cs +++ b/src/LLTSharp/Metadata/IAdditionalMetadata.cs @@ -6,25 +6,18 @@ namespace LLTSharp.Metadata { /// - /// Represents a dynamic metadata object that can store additional properties at runtime. + /// Represents a custom metadata object. /// - public interface IAdditionalMetadata : IMetadata, IDynamicMetaObjectProvider + public interface IAdditionalMetadata : IMetadata { /// - /// Gets the value associated with the specified key. + /// Gets the key associated with this metadata. /// - /// The type to convert the value to. - /// The key of the value to get. - /// The converted value or default(T) if not found. - T Get(string key); + string Key { get; } /// - /// Tries to get the value associated with the specified key. + /// Gets the value associated with this metadata. /// - /// The type to convert the value to. - /// The key of the value to get. - /// When this method returns, contains the value if found; otherwise, default(T). - /// true if the key was found; otherwise, false. - bool TryGet(string key, out T value); + object? Value { get; } } } \ No newline at end of file diff --git a/src/LLTSharp/Metadata/ImmutableAdditionalMetadata.cs b/src/LLTSharp/Metadata/ImmutableAdditionalMetadata.cs deleted file mode 100644 index 2212adb..0000000 --- a/src/LLTSharp/Metadata/ImmutableAdditionalMetadata.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Dynamic; -using System.Linq; -using System.Text; -using LLTSharp.Utils; - -namespace LLTSharp.Metadata -{ - /// - /// Represents an immutable dynamic metadata object. - /// - public class ImmutableAdditionalMetadata : DynamicObject, IAdditionalMetadata - { - private readonly Dictionary _metadata; - - /// - /// Gets a shared immutable empty instance of additional metadata object. - /// - public static ImmutableAdditionalMetadata Empty { get; } = new ImmutableAdditionalMetadata(); - - /// - /// Initializes a new instance of the class. - /// - public ImmutableAdditionalMetadata() - { - _metadata = new Dictionary(); - } - - /// - /// Initializes a new instance with initial key-value pair. - /// - public ImmutableAdditionalMetadata(string key, object value) - { - _metadata = new Dictionary(); - _metadata.Add(key, value); - } - - /// - /// Initializes a new instance with dictionary of initial values. - /// - public ImmutableAdditionalMetadata(IDictionary initialValues) - { - _metadata = initialValues?.ToDictionary(k => k.Key, v => v.Value); - } - - /// - /// Gets the value associated with the specified key. - /// - public object this[string key] => _metadata.TryGetValue(key, out var value) ? value : null; - - /// - /// Gets the value associated with the specified key. - /// - public T Get(string key) - { - if (_metadata.TryGetValue(key, out var value)) - { - try - { - return (T)Convert.ChangeType(value, typeof(T)); - } - catch - { - return default; - } - } - return default; - } - - /// - /// Tries to get the value associated with the specified key. - /// - public bool TryGet(string key, out T value) - { - if (_metadata.TryGetValue(key, out var objValue)) - { - try - { - value = (T)Convert.ChangeType(objValue, typeof(T)); - return true; - } - catch - { - value = default; - return false; - } - } - value = default; - return false; - } - - /// - /// Determines whether the metadata contains the specified key. - /// - public bool ContainsKey(string key) => _metadata.ContainsKey(key); - - /// - /// Gets all the keys in the metadata. - /// - public IEnumerable Keys => _metadata.Keys; - - /// - /// Gets all the values in the metadata. - /// - public IEnumerable Values => _metadata.Values; - - /// - /// Gets the number of metadata items. - /// - public int Count => _metadata.Count; - - public override bool TryGetMember(GetMemberBinder binder, out object result) - { - return _metadata.TryGetValue(binder.Name, out result); - } - - public override IEnumerable GetDynamicMemberNames() - { - return _metadata.Keys; - } - - public override bool Equals(object? obj) - { - return obj is ImmutableAdditionalMetadata other && - DictionaryUtils.DictionariesEqual(_metadata, other._metadata); - } - - public override int GetHashCode() - { - int hash = 17; - hash = hash * 397 + DictionaryUtils.GetDictionaryHashCode(_metadata); - return hash; - } - } -} \ No newline at end of file diff --git a/src/LLTSharp/Metadata/MetadataExtensions.cs b/src/LLTSharp/Metadata/MetadataExtensions.cs index bfc48f8..05af310 100644 --- a/src/LLTSharp/Metadata/MetadataExtensions.cs +++ b/src/LLTSharp/Metadata/MetadataExtensions.cs @@ -30,12 +30,9 @@ public static MetadataCollection ToMetadataCollection(this IEnumerable enu public static T? TryGetAdditional(this IMetadataCollection collection, string key) { foreach (var metadata in collection.GetAll()) - { - if (metadata.TryGet(key, out var result)) - { - return result; - } - } + if (metadata.Key == key) + if (metadata.Value is T value) + return value; return default; }