/// <summary>
        /// Resolves <see cref="DirectiveSchema"/>.
        /// </summary>
        public static DirectiveSchema Resolve(Type type, IReadOnlyList <Type> modeTypes)
        {
            if (!KnownTypesHelpers.IsDirectiveType(type))
            {
                throw DirectiveResolverExceptions.InvalidDirectiveType(type);
            }

            DirectiveAttribute attribute = type.GetCustomAttribute <DirectiveAttribute>() !;

            if (modeTypes is not null)
            {
                if (attribute.SupportedModes is not null && attribute.SupportedModes.Except(modeTypes).Any())
                {
                    throw DirectiveResolverExceptions.InvalidSupportedModesInDirective(type, attribute);
                }

                if (attribute.ExcludedModes is not null && attribute.ExcludedModes.Except(modeTypes).Any())
                {
                    throw DirectiveResolverExceptions.InvalidExcludedModesInDirective(type, attribute);
                }
            }

            return(new DirectiveSchema(
                       type,
                       attribute.Name,
                       attribute.Description,
                       attribute.SupportedModes,
                       attribute.ExcludedModes
                       ));
        }
Example #2
0
        public static TypinException InvalidExcludedModesInDirective(Type type, DirectiveAttribute attribute)
        {
            string message = $@"
Directive '{type.FullName}' contains invalid excluded mode(s) ({attribute.ExcludedModes}).
Either the type does not implement {nameof(ICliMode)} or CLI mode was not registered.";

            return(new TypinException(message.Trim()));
        }
        /// <summary>
        /// Resolves <see cref="DirectiveSchema"/>.
        /// </summary>
        public static DirectiveSchema Resolve(Type type)
        {
            if (!DirectiveSchema.IsDirectiveType(type))
            {
                throw InternalTypinExceptions.InvalidDirectiveType(type);
            }

            DirectiveAttribute attribute = type.GetCustomAttribute <DirectiveAttribute>() !;

            string name = attribute.Name.TrimStart('[').TrimEnd(']');

            if (string.IsNullOrWhiteSpace(name))
            {
                throw InternalTypinExceptions.DirectiveNameIsInvalid(name, type);
            }

            return(new DirectiveSchema(
                       type,
                       name,
                       attribute.Description,
                       attribute.InteractiveModeOnly
                       ));
        }