Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-rtf committed Apr 11, 2021
1 parent 9cc374a commit 2ae4913
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 1 deletion.
90 changes: 90 additions & 0 deletions Attributes/MorphicStringValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2021 Raising the Floor - US, Inc.
//
// Licensed under the New BSD license. You may not use this file except in
// compliance with this License.
//
// You may obtain a copy of the License at
// https://github.com/raisingthefloor/morphic-core-lib-cs/blob/main/LICENSE
//
// The R&D leading to these results received funding from the:
// * Rehabilitation Services Administration, US Dept. of Education under
// grant H421A150006 (APCP)
// * National Institute on Disability, Independent Living, and
// Rehabilitation Research (NIDILRR)
// * Administration for Independent Living & Dept. of Education under grants
// H133E080022 (RERC-IT) and H133E130028/90RE5003-01-00 (UIITA-RERC)
// * European Union's Seventh Framework Programme (FP7/2007-2013) grant
// agreement nos. 289016 (Cloud4all) and 610510 (Prosperity4All)
// * William and Flora Hewlett Foundation
// * Ontario Ministry of Research and Innovation
// * Canadian Foundation for Innovation
// * Adobe Foundation
// * Consumer Electronics Association Foundation

using System;
using System.Reflection;

namespace Morphic.Core
{
[System.AttributeUsage(AttributeTargets.Field)]
public class MorphicStringValueAttribute : Attribute
{
public string StringValue;

public MorphicStringValueAttribute(string stringValue)
{
this.StringValue = stringValue;
}
}

public partial class MorphicEnum<TEnum> where TEnum : struct, Enum
{
public static TEnum? FromStringValue(string stringValue, StringComparison comparisonType = StringComparison.Ordinal)
{
foreach (TEnum member in System.Enum.GetValues(typeof(TEnum)))
{
var memberName = typeof(TEnum).GetEnumName(member);
//
var fieldInfo = typeof(TEnum).GetField(memberName!);
//
var attribute = fieldInfo!.GetCustomAttribute<Morphic.Core.MorphicStringValueAttribute>();
if (attribute == null)
{
// this enum member does not have a string value
continue;
}

if (attribute.StringValue.Equals(stringValue, comparisonType))
{
return member;
}
}

// if we could not find the member (i.e. the member with the supplied string value does not exist), return null
return null;
}
}

public static partial class MorphicExtensions
{
public static string? ToStringValue<TEnum>(this TEnum value) where TEnum : Enum
{
var memberName = typeof(TEnum).GetEnumName(value);
if (memberName == null)
{
// member does not exist
return null;
}
//
var fieldInfo = typeof(TEnum).GetField(memberName);
//
var attribute = fieldInfo!.GetCustomAttribute<Morphic.Core.MorphicStringValueAttribute>();
if (attribute == null)
{
// this enum member does not have a string value
return null;
}
return attribute.StringValue;
}
}
}
175 changes: 175 additions & 0 deletions IMorphicResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2021 Raising the Floor - US, Inc.
//
// Licensed under the New BSD license. You may not use this file except in
// compliance with this License.
//
// You may obtain a copy of the License at
// https://github.com/raisingthefloor/morphic-core-lib-cs/blob/main/LICENSE
//
// The R&D leading to these results received funding from the:
// * Rehabilitation Services Administration, US Dept. of Education under
// grant H421A150006 (APCP)
// * National Institute on Disability, Independent Living, and
// Rehabilitation Research (NIDILRR)
// * Administration for Independent Living & Dept. of Education under grants
// H133E080022 (RERC-IT) and H133E130028/90RE5003-01-00 (UIITA-RERC)
// * European Union's Seventh Framework Programme (FP7/2007-2013) grant
// agreement nos. 289016 (Cloud4all) and 610510 (Prosperity4All)
// * William and Flora Hewlett Foundation
// * Ontario Ministry of Research and Innovation
// * Canadian Foundation for Innovation
// * Adobe Foundation
// * Consumer Electronics Association Foundation

using System;

namespace Morphic.Core
{
public interface IMorphicResult<TValue, TError>
{
public bool IsSuccess { get; }
public bool IsError { get; }

public TValue? Value { get; }
public TError? Error { get; }

public static IMorphicResult<TValue, TError> SuccessResult(TValue value) => new MorphicSuccess<TValue, TError>(value);
public static IMorphicResult<TValue, TError> ErrorResult(TError error) => new MorphicError<TValue, TError>(error);
}

public class MorphicSuccess<TValue, TError> : IMorphicResult<TValue, TError>
{
public bool IsSuccess => true;
public bool IsError => false;

public TValue Value { get; private set; }
public TError Error
{
get
{
// this property should not be retrieved on MorphicSuccess
throw new NotSupportedException();
}
}

public MorphicSuccess(TValue value)
{
this.Value = value;
}
}

public class MorphicError<TValue, TError> : IMorphicResult<TValue, TError>
{
public bool IsSuccess => false;
public bool IsError => true;

public TValue Value
{
get
{
// this property should not be retrieved on MorphicError
throw new NotSupportedException();
}
}
public TError Error { get; private set; }

public MorphicError(TError error)
{
this.Error = error;
}
}

//

public interface IMorphicResult<TValue>
{
public bool IsSuccess { get; }
public bool IsError { get; }

public TValue? Value { get; }

public static IMorphicResult<TValue> SuccessResult(TValue value) => new MorphicSuccess<TValue>(value);
public static IMorphicResult<TValue> ErrorResult() => new MorphicError<TValue>();
}

public class MorphicSuccess<TValue> : IMorphicResult<TValue>
{
public bool IsSuccess => true;
public bool IsError => false;

public TValue Value { get; private set; }

public MorphicSuccess(TValue value)
{
this.Value = value;
}
}

public class MorphicError<TValue> : IMorphicResult<TValue>
{
public bool IsSuccess => false;
public bool IsError => true;

public TValue Value
{
get
{
// this property should not be retrieved on MorphicError
throw new NotSupportedException();
}
}

public MorphicError()
{
}
}

//

public interface IMorphicResult
{
public bool IsSuccess { get; }
public bool IsError { get; }

public static IMorphicResult SuccessResult => new MorphicSuccess();
public static IMorphicResult ErrorResult => new MorphicError();
}

public class MorphicSuccess : IMorphicResult
{
public bool IsSuccess => true;
public bool IsError => false;

public MorphicSuccess()
{
}
}

public class MorphicError : IMorphicResult
{
public bool IsSuccess => false;
public bool IsError => true;

public MorphicError()
{
}
}

//

// MorphicUnhandledErrorException is just used to catch anywhere that we forget to capture an error result; we throw it in a "default" block following our error handling
public class MorphicUnhandledErrorException : Exception
{
public object? Error { get; private set; }

public MorphicUnhandledErrorException()
{
this.Error = null;
}

public MorphicUnhandledErrorException(object error)
{
this.Error = error;
}
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2021 Raising the Floor - US, Inc.
Copyright 2021 Raising the Floor - US, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
14 changes: 14 additions & 0 deletions Morphic.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<Authors>Raising the Floor - US, Inc.</Authors>
<Company>Raising the Floor - US, Inc.Raising the Floor - US, Inc.</Company>
<Product>Morphic Core Support Library</Product>
<Copyright>Copyright 2021 Raising the Floor - US, Inc.</Copyright>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
<RepositoryUrl>https://github.com/raisingthefloor/morphic-core-lib-cs</RepositoryUrl>
</PropertyGroup>

</Project>
42 changes: 42 additions & 0 deletions MorphicAssociatedValueEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2021 Raising the Floor - US, Inc.
//
// Licensed under the New BSD license. You may not use this file except in
// compliance with this License.
//
// You may obtain a copy of the License at
// https://github.com/raisingthefloor/morphic-core-lib-cs/blob/main/LICENSE
//
// The R&D leading to these results received funding from the:
// * Rehabilitation Services Administration, US Dept. of Education under
// grant H421A150006 (APCP)
// * National Institute on Disability, Independent Living, and
// Rehabilitation Research (NIDILRR)
// * Administration for Independent Living & Dept. of Education under grants
// H133E080022 (RERC-IT) and H133E130028/90RE5003-01-00 (UIITA-RERC)
// * European Union's Seventh Framework Programme (FP7/2007-2013) grant
// agreement nos. 289016 (Cloud4all) and 610510 (Prosperity4All)
// * William and Flora Hewlett Foundation
// * Ontario Ministry of Research and Innovation
// * Canadian Foundation for Innovation
// * Adobe Foundation
// * Consumer Electronics Association Foundation

using System;

namespace Morphic.Core
{
public abstract record MorphicAssociatedValueEnum<TValue> where TValue : struct, Enum
{
public TValue Value { get; private set; }

protected MorphicAssociatedValueEnum(TValue value)
{
this.Value = value;
}

public static bool IsMember(TValue value)
{
return MorphicEnum<TValue>.IsMember(value);
}
}
}
35 changes: 35 additions & 0 deletions MorphicEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 Raising the Floor - US, Inc.
//
// Licensed under the New BSD license. You may not use this file except in
// compliance with this License.
//
// You may obtain a copy of the License at
// https://github.com/raisingthefloor/morphic-core-lib-cs/blob/main/LICENSE
//
// The R&D leading to these results received funding from the:
// * Rehabilitation Services Administration, US Dept. of Education under
// grant H421A150006 (APCP)
// * National Institute on Disability, Independent Living, and
// Rehabilitation Research (NIDILRR)
// * Administration for Independent Living & Dept. of Education under grants
// H133E080022 (RERC-IT) and H133E130028/90RE5003-01-00 (UIITA-RERC)
// * European Union's Seventh Framework Programme (FP7/2007-2013) grant
// agreement nos. 289016 (Cloud4all) and 610510 (Prosperity4All)
// * William and Flora Hewlett Foundation
// * Ontario Ministry of Research and Innovation
// * Canadian Foundation for Innovation
// * Adobe Foundation
// * Consumer Electronics Association Foundation

using System;

namespace Morphic.Core
{
public partial class MorphicEnum<TEnum> where TEnum : struct, Enum
{
public static bool IsMember(TEnum value)
{
return (typeof(TEnum).GetEnumName(value) != null);
}
}
}
Loading

0 comments on commit 2ae4913

Please sign in to comment.