Skip to content

Commit

Permalink
GetObjFieldSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
guirava committed Jul 5, 2024
1 parent 483e15b commit a8ff06c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions RubrikSecurityCloud/RubrikSecurityCloud.Common/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Reflection;
using System.Linq;
using System.Globalization;
using RubrikSecurityCloud.Types;
using System.Collections;

namespace RubrikSecurityCloud
{
Expand Down Expand Up @@ -397,5 +399,27 @@ int depth
_FlattenFieldCache[typeName] = fields;
return fields;
}

public static string GetObjFieldSpec(object obj, FieldSpecConfig? conf = null)
{
// If obj implements IFieldSpec, use AsFieldSpec method
if (obj is IFieldSpec fieldSpec)
{
return fieldSpec.AsFieldSpec(conf);
}

// If obj is a collection, recurse on the first element
if (obj is IEnumerable enumerable)
{
var enumerator = enumerable.GetEnumerator();
if (enumerator.MoveNext())
{
return GetObjFieldSpec(enumerator.Current, conf);
}
}

// Fallback to ToString (for simple types like int, string, etc.)
return obj.ToString();
}
}
}

0 comments on commit a8ff06c

Please sign in to comment.