Skip to content

Commit

Permalink
Извлечение контекста перечислений
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Rm committed Sep 22, 2023
1 parent f561cd0 commit b770ac9
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/OneScript.StandardLibrary/Xml/XmlGlobalFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This Source Code Form is subject to the terms of the
using OneScript.StandardLibrary.TypeDescriptions;
using OneScript.Types;
using OneScript.Values;
using ScriptEngine;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;

Expand All @@ -23,12 +24,21 @@ namespace OneScript.StandardLibrary.Xml
[GlobalContext(Category="Функции работы с XML")]
public class XmlGlobalFunctions : GlobalContextBase<XmlGlobalFunctions>
{
private static readonly Dictionary<Type, Type> _allowedEnums = new Dictionary<Type, Type>
{{typeof(ClrEnumValueWrapper<AllowedSignEnum>),typeof(AllowedSignEnum)},
{typeof(ClrEnumValueWrapper<AllowedLengthEnum>),typeof(AllowedLengthEnum)},
{typeof(ClrEnumValueWrapper<DateFractionsEnum>),typeof(DateFractionsEnum)}
};

private static readonly Dictionary<Type, EnumerationContext> _allowedEnums
= new Dictionary<Type, EnumerationContext>();

private XmlGlobalFunctions(IGlobalsManager mgr)
{
foreach (var e in new[] {
(typeof(ClrEnumValueWrapper<AllowedSignEnum>), typeof(AllowedSignEnum)),
(typeof(ClrEnumValueWrapper<AllowedLengthEnum>), typeof(AllowedLengthEnum)),
(typeof(ClrEnumValueWrapper<DateFractionsEnum>), typeof(DateFractionsEnum))
})
{
_allowedEnums.Add(e.Item1, (EnumerationContext)mgr.GetInstance(e.Item2));
}
}

/// <summary>
/// Получает XML представление значения для помещения в текст элемента или значение атрибута XML.
/// </summary>
Expand Down Expand Up @@ -156,11 +166,10 @@ public IValue XMLValue(IValue givenType, string presentation)
byte[] bytes = Convert.FromBase64String(presentation);
return new BinaryDataContext(bytes);
}
else if (_allowedEnums.TryGetValue(typeValue.ImplementingClass, out var enumType))
else if (_allowedEnums.TryGetValue(typeValue.ImplementingClass, out var enumerationContext))
{
try
{
var enumerationContext = GlobalsHelper.GetEnum(enumType);
return enumerationContext[presentation];
}
catch (RuntimeException)
Expand All @@ -170,12 +179,11 @@ public IValue XMLValue(IValue givenType, string presentation)
}

throw RuntimeException.InvalidNthArgumentType(1);

}

public static IAttachableContext CreateInstance()
public static IAttachableContext CreateInstance(IGlobalsManager mgr)
{
return new XmlGlobalFunctions();
return new XmlGlobalFunctions(mgr);
}

}
Expand Down

0 comments on commit b770ac9

Please sign in to comment.