Anapher/Strive

View on GitHub
src/Services/ConferenceManagement/Strive.Core/Extensions/PermissionDescriptorExtensions.cs

Summary

Maintainability
A
0 mins
Test Coverage
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Strive.Core.Services.Permissions;

namespace Strive.Core.Extensions
{
    public static class PermissionDescriptorExtensions
    {
        public static KeyValuePair<string, JValue> Configure<T>(this PermissionDescriptor<T> permissionDescriptor,
            T value) where T : notnull
        {
            return new(permissionDescriptor.Key, (JValue) JToken.FromObject(value));
        }

        public static bool ValidateValue(this PermissionDescriptor permissionDescriptor, JValue value)
        {
            switch (permissionDescriptor.Type)
            {
                case PermissionValueType.Boolean:
                    return value.Type == JTokenType.Boolean;
                case PermissionValueType.Integer:
                    return value.Type == JTokenType.Integer;
                case PermissionValueType.Decimal:
                    return value.Type == JTokenType.Float;
                case PermissionValueType.Text:
                    return value.Type == JTokenType.String;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}