aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/ExtensionRegistryLite.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-06-02 12:04:06 -0500
committerrogerk <devnull@localhost>2011-06-02 12:04:06 -0500
commit27bfcc5e1a7a3aacd828475c1996e114e34055d2 (patch)
tree8a4d0adf6cab44a934c21a7656fc8226b81556fc /src/ProtocolBuffers/ExtensionRegistryLite.cs
parent45a93fad4d7123887d14135ee15ee3e9b0d4ca58 (diff)
downloadprotobuf-27bfcc5e1a7a3aacd828475c1996e114e34055d2.tar.gz
protobuf-27bfcc5e1a7a3aacd828475c1996e114e34055d2.tar.bz2
protobuf-27bfcc5e1a7a3aacd828475c1996e114e34055d2.zip
Slight refactoring of Extensions to support lookup by name, added compatibility tests for text and binary formats.
Diffstat (limited to 'src/ProtocolBuffers/ExtensionRegistryLite.cs')
-rw-r--r--src/ProtocolBuffers/ExtensionRegistryLite.cs57
1 files changed, 38 insertions, 19 deletions
diff --git a/src/ProtocolBuffers/ExtensionRegistryLite.cs b/src/ProtocolBuffers/ExtensionRegistryLite.cs
index fc662f68..91a0ce29 100644
--- a/src/ProtocolBuffers/ExtensionRegistryLite.cs
+++ b/src/ProtocolBuffers/ExtensionRegistryLite.cs
@@ -36,6 +36,7 @@
using System.Collections.Generic;
using System;
+using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers
{
@@ -92,37 +93,39 @@ namespace Google.ProtocolBuffers
/// </remarks>
public sealed partial class ExtensionRegistry
{
- private readonly IDictionary<ExtensionIntPair, IGeneratedExtensionLite> extensionsByNumber;
+ class ExtensionByNameMap : Dictionary<object, Dictionary<string, IGeneratedExtensionLite>> { }
+ class ExtensionByIdMap : Dictionary<ExtensionIntPair, IGeneratedExtensionLite> { }
+
+ private static readonly ExtensionRegistry empty = new ExtensionRegistry(
+ new ExtensionByNameMap(),
+ new ExtensionByIdMap(),
+ true);
+
+ private readonly ExtensionByNameMap extensionsByName;
+ private readonly ExtensionByIdMap extensionsByNumber;
+
private readonly bool readOnly;
- private ExtensionRegistry(IDictionary<ExtensionIntPair, IGeneratedExtensionLite> extensionsByNumber,
- bool readOnly)
+ private ExtensionRegistry(ExtensionByNameMap byName, ExtensionByIdMap byNumber, bool readOnly)
{
- this.extensionsByNumber = extensionsByNumber;
+ this.extensionsByName = byName;
+ this.extensionsByNumber = byNumber;
this.readOnly = readOnly;
}
-#if LITE
- private static readonly ExtensionRegistry empty = new ExtensionRegistry(
- new Dictionary<ExtensionIntPair, IGeneratedExtensionLite>(),
- true);
-
/// <summary>
/// Construct a new, empty instance.
/// </summary>
public static ExtensionRegistry CreateInstance()
{
- return new ExtensionRegistry(
- new Dictionary<ExtensionIntPair, IGeneratedExtensionLite>(), false);
+ return new ExtensionRegistry(new ExtensionByNameMap(), new ExtensionByIdMap(), false);
}
public ExtensionRegistry AsReadOnly()
{
- return new ExtensionRegistry(extensionsByNumber, true);
+ return new ExtensionRegistry(extensionsByName, extensionsByNumber, true);
}
-#endif
-
/// <summary>
/// Get the unmodifiable singleton empty instance.
/// </summary>
@@ -145,6 +148,18 @@ namespace Google.ProtocolBuffers
}
}
+ public IGeneratedExtensionLite FindByName(IMessageLite defaultInstanceOfType, string fieldName)
+ { return FindExtensionByName(defaultInstanceOfType, fieldName); }
+
+ IGeneratedExtensionLite FindExtensionByName(object forwhat, string fieldName)
+ {
+ IGeneratedExtensionLite extension = null;
+ Dictionary<string, IGeneratedExtensionLite> map;
+ if (extensionsByName.TryGetValue(forwhat, out map) && map.TryGetValue(fieldName, out extension))
+ return extension;
+ return null;
+ }
+
/// <summary>
/// Add an extension from a generated file to the registry.
/// </summary>
@@ -154,9 +169,13 @@ namespace Google.ProtocolBuffers
{
throw new InvalidOperationException("Cannot add entries to a read-only extension registry");
}
- extensionsByNumber.Add(
- new ExtensionIntPair(extension.ContainingType, extension.Number),
- extension);
+ extensionsByNumber.Add(new ExtensionIntPair(extension.ContainingType, extension.Number), extension);
+
+ Dictionary<string, IGeneratedExtensionLite> map;
+ if (!extensionsByName.TryGetValue(extension.ContainingType, out map))
+ extensionsByName.Add(extension.ContainingType, map = new Dictionary<string, IGeneratedExtensionLite>());
+ map[extension.Descriptor.Name] = extension;
+ map[extension.Descriptor.FullName] = extension;
}
/// <summary>
@@ -176,7 +195,7 @@ namespace Google.ProtocolBuffers
public override int GetHashCode()
{
- return msgType.GetHashCode()*((1 << 16) - 1) + number;
+ return msgType.GetHashCode() * ((1 << 16) - 1) + number;
}
public override bool Equals(object obj)
@@ -185,7 +204,7 @@ namespace Google.ProtocolBuffers
{
return false;
}
- return Equals((ExtensionIntPair) obj);
+ return Equals((ExtensionIntPair)obj);
}
public bool Equals(ExtensionIntPair other)