mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.2 KiB
68 lines
2.2 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Security.Claims;
|
|
using Jint;
|
|
using Jint.Native;
|
|
using Jint.Runtime.Interop;
|
|
using NodaTime;
|
|
using Squidex.Domain.Apps.Core.Contents;
|
|
using Squidex.Domain.Apps.Core.Scripting.ContentWrapper;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Shared.Users;
|
|
|
|
namespace Squidex.Domain.Apps.Core.Scripting
|
|
{
|
|
public sealed class DefaultConverter : IObjectConverter
|
|
{
|
|
public static readonly DefaultConverter Instance = new DefaultConverter();
|
|
|
|
private DefaultConverter()
|
|
{
|
|
}
|
|
|
|
public bool TryConvert(Engine engine, object value, [MaybeNullWhen(false)] out JsValue result)
|
|
{
|
|
result = null!;
|
|
|
|
if (value is Enum)
|
|
{
|
|
result = value.ToString();
|
|
return true;
|
|
}
|
|
|
|
switch (value)
|
|
{
|
|
case IUser user:
|
|
result = JintUser.Create(engine, user);
|
|
return true;
|
|
case ClaimsPrincipal principal:
|
|
result = JintUser.Create(engine, principal);
|
|
return true;
|
|
case DomainId domainId:
|
|
result = domainId.ToString();
|
|
return true;
|
|
case Guid guid:
|
|
result = guid.ToString();
|
|
return true;
|
|
case Instant instant:
|
|
result = JsValue.FromObject(engine, instant.ToDateTimeUtc());
|
|
return true;
|
|
case Status status:
|
|
result = status.ToString();
|
|
return true;
|
|
case ContentData content:
|
|
result = new ContentDataObject(engine, content);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|