Skip to content

Commit

Permalink
Added null handling (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbm0115 authored Nov 21, 2023
1 parent 05c4ea5 commit ae23bb3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions AdapterSdk.Core/AdapterSdk.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</PropertyGroup>

<ItemGroup>
<None Remove="AdapterSdk.Core.xml" />
<None Remove="icon.jpg" />
</ItemGroup>

Expand All @@ -47,6 +48,10 @@
</Content>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="AdapterSdk.Core.xml" />
</ItemGroup>

<ItemGroup>
<Folder Include="Contracts\Enums\Values\" />
<Folder Include="Contracts\Enums\Types\" />
Expand Down
10 changes: 10 additions & 0 deletions AdapterSdk/AdapterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ private static bool TryUpdateValues(this IAdapter adapter, object model, string
{
// Dictionary<string, T>
var dictionary = property.GetValue(model) as IDictionary;
if (dictionary == null)
{
dataItemUpdated = false;
continue;
}
foreach (DictionaryEntry entry in dictionary)
{
string dataItemSuffix = (string)entry.Key;
Expand All @@ -445,6 +450,11 @@ private static bool TryUpdateValues(this IAdapter adapter, object model, string
{
// List<T>
var list = property.GetValue(model) as IList;
if (list == null)
{
dataItemUpdated = false;
continue;
}
for (int i = 0; i < list.Count; i++)
{
adapter[dataItemName + i.ToString()].Value = list[i];
Expand Down
2 changes: 1 addition & 1 deletion AdapterSdk/AdapterSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<ApplicationIcon>icon.ico</ApplicationIcon>
<PackageProjectUrl>https://github.com/TrueAnalyticsSolutions/MtconnectCore.Adapter</PackageProjectUrl>
<RepositoryUrl>$(ProjectUrl)</RepositoryUrl>
<Version>3.0.1.0</Version>
<Version>3.0.1.1</Version>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<DebugSymbols>true</DebugSymbols>
Expand Down
10 changes: 5 additions & 5 deletions AdapterSdk/DataItemLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void Add(IDataItem dataItem)
deviceItems.Add(index);

// Add to byNameAndDevicePrefix dictionary
_byNameAndDevicePrefix[(dataItem.Name, dataItem.DevicePrefix)] = index;
_byNameAndDevicePrefix[(dataItem.Name, dataItem.DevicePrefix ?? string.Empty)] = index;
}

/// <summary>
Expand Down Expand Up @@ -145,7 +145,7 @@ public bool TryGetByNameAndDevicePrefix(string internalName, string devicePrefix
{
dataItem = null;

if (_byNameAndDevicePrefix.TryGetValue((internalName, devicePrefix), out int index))
if (_byNameAndDevicePrefix.TryGetValue((internalName, devicePrefix ?? string.Empty), out int index))
{
dataItem = _dataItems[index];
return true;
Expand Down Expand Up @@ -207,7 +207,7 @@ public void RemoveAt(int index)
if (dataItem != null)
{
// Update index in _byDeviceAndName dictionary
_byNameAndDevicePrefix[(dataItem.Name, dataItem.DevicePrefix)] = i;
_byNameAndDevicePrefix[(dataItem.Name, dataItem.DevicePrefix ?? string.Empty)] = i;

// Update indices in _byName dictionary
if (_byName.TryGetValue(dataItem.Name, out var list))
Expand All @@ -230,7 +230,7 @@ public void Clear()

/// <inheritdoc />
public bool Contains(IDataItem item)
=> _byNameAndDevicePrefix.ContainsKey((item.Name, item.DevicePrefix));
=> _byNameAndDevicePrefix.ContainsKey((item.Name, item.DevicePrefix ?? string.Empty));

/// <inheritdoc />
public bool ContainsName(string internalName) => _byName.ContainsKey(internalName);
Expand All @@ -239,7 +239,7 @@ public bool Contains(IDataItem item)
public bool ContainsDevicePrefix(string devicePrefix) => _byDevicePrefix.ContainsKey(devicePrefix);

/// <inheritdoc />
public bool Contains(string internalName, string devicePrefix) => _byNameAndDevicePrefix.ContainsKey((internalName, devicePrefix));
public bool Contains(string internalName, string devicePrefix) => _byNameAndDevicePrefix.ContainsKey((internalName, devicePrefix ?? string.Empty));

/// <inheritdoc />
public void CopyTo(IDataItem[] array, int arrayIndex) => _dataItems.CopyTo(array, arrayIndex);
Expand Down

0 comments on commit ae23bb3

Please sign in to comment.