fix: adding some methods for getting text out of an entity easier

This commit is contained in:
Dylan R. E. Moonfire 2022-07-18 19:42:16 -05:00
parent 28c4ab850e
commit 42cae6b133
2 changed files with 33 additions and 7 deletions

View file

@ -33,13 +33,10 @@ public static class NitrideYamlEntityExtensions
this Entity entity, this Entity entity,
IDeserializer deserializer) IDeserializer deserializer)
{ {
if (!entity.TryGet(out ITextContent content)) string? text = entity.GetText();
{
return default;
}
string text = content.GetText(); return text != null
? deserializer.Deserialize<TType>(text)
return deserializer.Deserialize<TType>(text); : default;
} }
} }

View file

@ -1,5 +1,7 @@
using System.IO; using System.IO;
using Gallium;
namespace Nitride.Contents; namespace Nitride.Contents;
public static class TextContentExtensions public static class TextContentExtensions
@ -18,4 +20,31 @@ public static class TextContentExtensions
return writer.ToString(); return writer.ToString();
} }
public static string? GetText(this IBinaryContent content)
{
if (content is ITextContentConvertable convertableContent)
{
return convertableContent
.ToTextContent()
.GetText();
}
return null;
}
public static string? GetText(this Entity entity)
{
if (entity.TryGet(out ITextContent textContent))
{
return textContent.GetText();
}
if (entity.TryGet(out IBinaryContent binaryContent))
{
return binaryContent.GetText();
}
return null;
}
} }