コード例 #1
0
ファイル: InvalidInputEntity.cs プロジェクト: erikvk/RESTable
 internal MissingTerminalParameter(Type terminalType, InvalidEntity invalidEntity) : base
     (
         invalidEntity: invalidEntity,
         info: $"Missing or invalid terminal parameters in request to '{terminalType.GetRESTableTypeName()}'"
     )
 {
 }
コード例 #2
0
ファイル: InvalidInputEntity.cs プロジェクト: erikvk/RESTable
 internal InvalidInputEntity(InvalidEntity invalidEntity, string info = null) : base
     (
         code: ErrorCodes.InvalidResourceEntity,
         info: info ?? "An invalid input entity was encountered"
     )
 {
     InvalidEntity = invalidEntity;
 }
コード例 #3
0
ファイル: TerminalResource.cs プロジェクト: erikvk/RESTable
        private Terminal InvokeParameterizedConstructor(List <Condition <T> > assignmentList)
        {
            var constructorParameterList    = new object[ConstructorParameterInfos.Length];
            var parameterAssignments        = new Dictionary <int, object>();
            var missingParameterAssignments = default(List <ParameterInfo>);

            for (var i = 0; i < assignmentList.Count; i += 1)
            {
                var assignment = assignmentList[i];
                if (ConstructorParameterIndexes.TryGetValue(assignment.Key, out var index))
                {
                    if (assignment.Operator != Operators.EQUALS)
                    {
                        throw new BadConditionOperator(this, assignment.Operator);
                    }
                    parameterAssignments[index] = assignment.Value;
                    assignmentList.RemoveAt(i);
                    i -= 1;
                }
            }

            for (var i = 0; i < ConstructorParameterInfos.Length; i += 1)
            {
                if (parameterAssignments.TryGetValue(i, out var value))
                {
                    constructorParameterList[i] = value;
                }
                else
                {
                    var parameterInfo = ConstructorParameterInfos[i];
                    if (parameterInfo.IsOptional)
                    {
                        constructorParameterList[i] = Missing.Value;
                    }
                    else
                    {
                        missingParameterAssignments ??= new List <ParameterInfo>();
                        missingParameterAssignments.Add(parameterInfo);
                    }
                }
            }

            if (missingParameterAssignments?.Count > 0)
            {
                var invalidMembers = missingParameterAssignments
                                     .Select(parameter => new InvalidMember(
                                                 entityType: Type,
                                                 memberName: parameter.Name,
                                                 memberType: parameter.ParameterType,
                                                 message: $"Missing parameter of type '{parameter.ParameterType}'")
                                             ).ToList();
                var invalidEntity = new InvalidEntity(invalidMembers);
                throw new MissingTerminalParameter(Type, invalidEntity);
            }

            return(Constructor.Invoke(constructorParameterList) as Terminal);
        }
コード例 #4
0
ファイル: TerminalResource.cs プロジェクト: erikvk/RESTable
        internal Terminal MakeTerminal(RESTableContext context, IEnumerable <Condition <T> > assignments = null)
        {
            var assignmentList = assignments?.ToList() ?? new List <Condition <T> >();

            var newTerminal = HasConstructorParameters
                ? InvokeParameterizedConstructor(assignmentList)
                : Constructor.Invoke(null) as Terminal;

            foreach (var assignment in assignmentList)
            {
                if (assignment.Operator != Operators.EQUALS)
                {
                    throw new BadConditionOperator(this, assignment.Operator);
                }
                if (!Members.TryGetValue(assignment.Key, out var property))
                {
                    if (newTerminal is IDictionary <string, object> dynTerminal)
                    {
                        dynTerminal[assignment.Key] = assignment.Value;
                    }
                    else
                    {
                        throw new UnknownProperty(Type, this, assignment.Key);
                    }
                }
                else
                {
                    property.SetValue(newTerminal, assignment.Value);
                }
            }
            if (newTerminal is T terminal and IValidator <T> validator)
            {
                var invalidMembers = validator.Validate(terminal, context).ToList();
                if (invalidMembers.Count > 0)
                {
                    var invalidEntity = new InvalidEntity(invalidMembers);
                    throw new InvalidInputEntity(invalidEntity);
                }
            }
            return(newTerminal);
        }