Skip to content

Commit

Permalink
update objecttodictionary to accomodate for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
hjgraca committed Oct 9, 2024
1 parent ec0b73a commit 1b5cd58
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
* permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json.Serialization;

namespace AWS.Lambda.Powertools.Logging.Internal.Helpers;

Expand All @@ -38,18 +41,31 @@ internal static object ObjectToDictionary(object anonymousObject)
return new Dictionary<string, object>();
}

if (anonymousObject.GetType().Namespace is not null)
var type = anonymousObject.GetType();

if (type.IsEnum)
{
return anonymousObject;
}

if (type.Namespace != null && !type.IsEnum)
{
return anonymousObject;
}

return anonymousObject.GetType().GetProperties()
return type.GetProperties()
.Where(prop => prop.GetValue(anonymousObject, null) != null)
.ToDictionary(
prop => prop.Name,
prop => {
var value = prop.GetValue(anonymousObject, null);
return value != null ? ObjectToDictionary(value) : string.Empty;
if (value == null)
return string.Empty;

Check warning on line 63 in libraries/src/AWS.Lambda.Powertools.Logging/Internal/Helpers/PowertoolsLoggerHelpers.cs

View check run for this annotation

Codecov / codecov/patch

libraries/src/AWS.Lambda.Powertools.Logging/Internal/Helpers/PowertoolsLoggerHelpers.cs#L63

Added line #L63 was not covered by tests

if (value.GetType().IsEnum)
return value;

Check warning on line 66 in libraries/src/AWS.Lambda.Powertools.Logging/Internal/Helpers/PowertoolsLoggerHelpers.cs

View check run for this annotation

Codecov / codecov/patch

libraries/src/AWS.Lambda.Powertools.Logging/Internal/Helpers/PowertoolsLoggerHelpers.cs#L66

Added line #L66 was not covered by tests

return ObjectToDictionary(value);
}
);
}
Expand Down

0 comments on commit 1b5cd58

Please sign in to comment.