Exemplo n.º 1
0
 public UndocumentedPropertyWarning(string source, string propertyName, ParameterDataType propertyType, string resourceName)
     : base(ValidationErrorCode.AdditionalPropertyDetected, source, "Undocumented property '{0}' [{1}] was not expected on resource {2}.", propertyName, propertyType, resourceName)
 {
     this.PropertyName = propertyName;
     this.PropertyType = propertyType;
     this.ResourceName = resourceName;
 }
Exemplo n.º 2
0
        //private static object SwaggerProperty(JsonDataType type, string odataTypeName)
        private static object SwaggerProperty(ParameterDefinition property)
        {
            ParameterDataType type = property.Type;
            string description = property.Description;

            return MakeSwaggerProperty(type, description);
        }
Exemplo n.º 3
0
 private static object SwaggerPropertyForResponse(MethodDefinition definition)
 {
     string customDataType = definition.ExpectedResponseMetadata.ResourceType;
     var type = new ParameterDataType(customDataType, true);
     string description = null;
     return MakeSwaggerProperty(type, description);
 }
Exemplo n.º 4
0
        private static bool IsValidType(this EntityFramework edmx, ParameterDataType type)
        {
            if (type.Type != SimpleDataType.Object && type.Type != SimpleDataType.None)
            {
                return(true);
            }

            if (type.Type == SimpleDataType.Object && !string.IsNullOrEmpty(type.CustomTypeName))
            {
                // See if we can find this custom type name in the EntityFramework
                var targetSchemas = (from s in edmx.DataServices.Schemas where type.CustomTypeName.StartsWith(s.Namespace + ".") select s);
                if (!targetSchemas.Any())
                {
                    return(false);
                }

                foreach (var s in targetSchemas)
                {
                    // See if we can find a matching EntityType or ComplexType in this schema
                    var typeName     = type.CustomTypeName.Substring(s.Namespace.Length + 1);
                    var matchingType = s.FindTypeByName(typeName);
                    if (matchingType != null)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
		/// <summary>
		/// When implemented by a deriving class, checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		protected override void OnValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			base.OnValidation(results, value, dataType, memberName, displayName);
			AssertDataType(dataType, ParameterDataType.String);

			string Temp = value as string;
			if (Temp.ToLowerInvariant().CompareTo(Temp) != 0)
			{
				results.Add(new ParameterValidationResult(Errors.CreateHResult(ErrorCodes.LowerCaseConstraint_Validate_ValueInvalid), string.Format(Properties.Resources.LowercaseConstraint_Validate_Failed, displayName), memberName, this));
			}
		}
Exemplo n.º 6
0
        /// <summary>
        /// 创建一个 jQuery UI 参数.
        /// </summary>
        /// <param name="name">参数名称.</param>
        /// <param name="type">参数类型.</param>
        /// <param name="value">参数值.</param>
        /// <param name="default">默认值.</param>
        /// <param name="dataType">参数的数据类型.</param>
        /// <param name="provider">备用的转换函数.</param>
        public Parameter(string name, ParameterType type, string value, string @default, ParameterDataType dataType, string provider)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "参数名称不能为空");
            }

            this.type     = type;
            this.dataType = dataType;
            this.name     = name;

            this.@default = @default;
            this.value    = value;
            this.provider = provider;
        }
Exemplo n.º 7
0
 public void CheckJsonProperty(ParameterDefinition prop, ParameterDataType expectedType = null, bool?customMembersIsNull = null)
 {
     if (expectedType != null)
     {
         Assert.AreEqual(expectedType, prop.Type);
     }
     if (customMembersIsNull != null && customMembersIsNull.Value)
     {
         Assert.IsNull(prop.Type.CustomMembers);
     }
     else if (customMembersIsNull != null && !customMembersIsNull.Value)
     {
         Assert.IsNotNull(prop.Type.CustomMembers);
     }
 }
		/// <summary>
		/// When implemented by a deriving class, checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		protected override void OnValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			base.OnValidation(results, value, dataType, memberName, displayName);
			AssertDataType(dataType, ParameterDataType.String);

			string Temp = value as string;
			if (string.IsNullOrWhiteSpace(Temp))
			{
				results.Add(new ParameterValidationResult(Errors.CreateHResult(ErrorCodes.HostNameConstraint_Validate_ValueEmpty), string.Format(Properties.Resources.Global_Validate_StringEmpty, displayName), memberName, this));
			}
			else if (Uri.CheckHostName(Temp) == UriHostNameType.Unknown)
			{
				results.Add(new ParameterValidationResult(Errors.CreateHResult(ErrorCodes.HostnameConstraint_Validate_NotHostOrIP), string.Format(Properties.Resources.HostNameConstraint_Validate_Failed, displayName, Temp), memberName, this));
			}
		}
Exemplo n.º 9
0
        /// <summary>
        /// Convert a ParameterDataType instance into the OData equivelent.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string ODataResourceName(this ParameterDataType type)
        {
            if (type.Type == SimpleDataType.Object && !string.IsNullOrEmpty(type.CustomTypeName))
            {
                return(type.CustomTypeName);
            }

            if (type.Type == SimpleDataType.Collection)
            {
                return(string.Format(
                           "Collection({0})",
                           type.CollectionResourceType.ODataResourceName(type.CustomTypeName)));
            }

            return(type.Type.ODataResourceName());
        }
		protected override void SetParameters(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			base.SetParameters(parameters, dataType);
			if (parameters.Count > 0)
			{
				if (parameters[0] == "argex")
				{
					throw new ArgumentException();
				}
				else if (parameters[0] == "formex")
				{
					throw new FormatException();
				}
				else
				{
					Parameters = new List<string>(parameters);
				}
			}
		}
Exemplo n.º 11
0
 internal ReportParameterInfo(string name, ParameterDataType dataType, bool isNullable, bool allowBlank, bool isMultiValue, bool isQueryParameter, string prompt, bool promptUser, bool areDefaultValuesQueryBased, bool areValidValuesQueryBased, string errorMessage, string[] currentValues, IList <ValidValue> validValues, string[] dependencies, ParameterState state)
 {
     m_name                       = name;
     m_dataType                   = dataType;
     m_isNullable                 = isNullable;
     m_allowBlank                 = allowBlank;
     m_isMultiValue               = isMultiValue;
     m_isQueryParameter           = isQueryParameter;
     m_prompt                     = prompt;
     m_promptUser                 = promptUser;
     m_areDefaultValuesQueryBased = areDefaultValuesQueryBased;
     m_areValidValuesQueryBased   = areValidValuesQueryBased;
     m_errorMessage               = errorMessage;
     m_currentValues              = new ReadOnlyCollection <string>(currentValues ?? new string[0]);
     m_validValues                = validValues;
     m_dependencies               = dependencies;
     m_state                      = state;
     m_visible                    = true;
 }
Exemplo n.º 12
0
 private static object MakeSwaggerProperty(ParameterDataType type, string description)
 {
     Dictionary<string, object> definition = null;
     if (type.IsObject)
     {
         definition = new Dictionary<string, object> {
             { "$ref", "#/definitions/" + type.CustomTypeName.SwaggerResourceName() }
         };
     }
     else
     {
         definition = new Dictionary<string, object>
         {
             { "type", type.ToSwaggerTypeString() }
         };
     }
     if (!string.IsNullOrEmpty(description))
     {
         definition.Add("description", description);
     }
     return definition;
 }
Exemplo n.º 13
0
        private System.Data.DbType GetDataTypeFromParameterType(ParameterDataType type)
        {
            switch (type)
            {
            case ParameterDataType.Boolean:
                return(System.Data.DbType.Boolean);

            case ParameterDataType.DateTime:
                return(System.Data.DbType.DateTime);

            case ParameterDataType.Float:
                return(System.Data.DbType.Double);

            case ParameterDataType.Integer:
                return(System.Data.DbType.Int32);

            case ParameterDataType.String:
                return(System.Data.DbType.String);

            default:
                return(System.Data.DbType.String);
            }
        }
		/// <summary>
		/// Sets the parameters that the <see cref="Constraint"/> requires to work.
		/// </summary>
		/// <param name="parameters">A enumeration of string parameters.</param>
		/// <param name="dataType">The data type that the constraint needs to restrict.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="parameters"/> is <see langword="null"/>.</exception>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		/// <exception cref="ConstraintConfigurationException"><paramref name="parameters"/> contains no elements, or an invalid element.</exception>
		protected override void SetParameters(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			base.SetParameters(parameters, dataType);
			AssertDataType(dataType, ParameterDataType.Xml);
			if (parameters.Count != 1)
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.TypeConstraint_SetParameters_OnlyOneParam), string.Format(Properties.Resources.Global_SetParameters_InvalidCount, this.Name, 1), this);
			}

			if (string.IsNullOrWhiteSpace(parameters[0]))
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.TypeConstraint_SetParameters_TypeNullEmpty), string.Format(Properties.Resources.Global_SetParameters_Invalid, this.Name), this);
			}
			mTypeName = parameters[0];
			mResolvedType = null;
			IsTypeResolved = false;
		}
Exemplo n.º 15
0
 /// <summary>
 /// 创建一个 jQuery UI 参数, 参数值对应一个选择器.
 /// </summary>
 /// <param name="name">参数名称.</param>
 /// <param name="value">一个选择器.</param>
 /// <param name="dataType">参数的数据类型.</param>
 public Parameter(string name, string value, ParameterDataType dataType)
     : this(name, ParameterType.Selector, value, null, dataType, null)
 {
 }
Exemplo n.º 16
0
 /// <summary>
 /// 创建一个空的 jQuery UI 参数, 参数值采用选择器.
 /// </summary>
 /// <param name="dataType">参数的数据类型.</param>
 /// <param name="provider">备用的转换函数.</param>
 public Parameter(ParameterDataType dataType, string provider)
     : this("new parameter", ParameterType.Selector, null, null, dataType, provider)
 {
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="MinimumValueConstraint"/> class.
		/// </summary>
		/// <param name="dataType">The data type that the constraint can validate.</param>
		/// <exception cref="CodedArgumentException"><paramref name="dataType"/> is not supported by the constraint.</exception>
		public MinimumValueConstraint(ParameterDataType dataType)
			: base(MinimumValueConstraintName)
		{
			mDataType = dataType;
			mMinimumValue = CheckDataType();
		}
Exemplo n.º 18
0
        public static void LoadExtractionInfo(SLTestWorkspace testWorkspace, string folderPath, string fileName)
        {
            XmlReaderSettings xmlSettings = new XmlReaderSettings();

            xmlSettings.ValidationType = ValidationType.None;
            XmlReader infoReader = XmlReader.Create(folderPath + fileName, xmlSettings);

            while (infoReader.Read())
            {
                if (infoReader.NodeType == XmlNodeType.Element &&
                    infoReader.Name == "SimInfo")
                {
                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "SimTime");
                    infoReader.Read();
                    float simTimeMs = float.Parse(infoReader.Value);

                    infoReader.Read();

                    testWorkspace.SetSimulationTime(simTimeMs);
                }

                if (infoReader.NodeType == XmlNodeType.Element &&
                    infoReader.Name == "Input")
                {
                    ParameterDataType parameterDataType;

                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "Name");
                    infoReader.Read();
                    string blcokName = infoReader.Value;

                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "DataTypeName");
                    infoReader.Read();
                    string dataTypeName = infoReader.Value;

                    if (!dataTypeName.Equals("TbBOOLEAN"))
                    {
                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "IsSigned");
                        infoReader.Read();
                        bool isSigned = Convert.ToBoolean(Int16.Parse(infoReader.Value));

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "WordLength");
                        infoReader.Read();
                        int wordLength = Int16.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "FractionLength");
                        infoReader.Read();
                        int fracLength = Int16.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "MinType");
                        infoReader.Read();
                        float minDataType = (float)Double.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "MaxType");
                        infoReader.Read();
                        float maxDataType = (float)Double.Parse(infoReader.Value);

                        parameterDataType = new ParameterDataType(dataTypeName, isSigned, wordLength, fracLength, minDataType, maxDataType);
                    }
                    else
                    {
                        parameterDataType = new ParameterDataType(dataTypeName);
                    }

                    BlockInfo blockInfo;
                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "Path");
                    infoReader.Read();
                    string bloackPath = infoReader.Value;

                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "PortNum");
                    infoReader.Read();
                    int blockPortNum = Int16.Parse(infoReader.Value);

                    infoReader.Read();

                    blockInfo = new BlockInfo(bloackPath, blcokName, blockPortNum);


                    testWorkspace.inputVariables.Add(new TestParameter(blockInfo, parameterDataType));
                }


                if (infoReader.NodeType == XmlNodeType.Element &&
                    infoReader.Name == "Calib")
                {
                    ParameterDataType parameterDataType;

                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "Name");
                    infoReader.Read();
                    string blcokName = infoReader.Value;

                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "DataTypeName");
                    infoReader.Read();
                    string dataTypeName = infoReader.Value;

                    if (!dataTypeName.Equals("TbBOOLEAN"))
                    {
                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "IsSigned");
                        infoReader.Read();
                        bool isSigned = Convert.ToBoolean(Int16.Parse(infoReader.Value));

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "WordLength");
                        infoReader.Read();
                        int wordLength = Int16.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "FractionLength");
                        infoReader.Read();
                        int fracLength = Int16.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "MinType");
                        infoReader.Read();
                        float minDataType = (float)Double.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "MaxType");
                        infoReader.Read();
                        float maxDataType = (float)Double.Parse(infoReader.Value);

                        parameterDataType = new ParameterDataType(dataTypeName, isSigned, wordLength, fracLength, minDataType, maxDataType);
                    }
                    else
                    {
                        parameterDataType = new ParameterDataType(dataTypeName);
                    }

                    BlockInfo blockInfo;
                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "Path");
                    infoReader.Read();
                    string bloackPath = infoReader.Value;

                    infoReader.Read();

                    blockInfo = new BlockInfo(bloackPath, blcokName);


                    testWorkspace.calibrationVariables.Add(new TestParameter(blockInfo, parameterDataType));
                }


                if (infoReader.NodeType == XmlNodeType.Element &&
                    infoReader.Name == "Output")
                {
                    ParameterDataType parameterDataType;

                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "Name");
                    infoReader.Read();
                    string blcokName = infoReader.Value;

                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "DataTypeName");
                    infoReader.Read();
                    string dataTypeName = infoReader.Value;

                    if (!dataTypeName.Equals("TbBOOLEAN"))
                    {
                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "IsSigned");
                        infoReader.Read();
                        bool isSigned = Convert.ToBoolean(Int16.Parse(infoReader.Value));

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "WordLength");
                        infoReader.Read();
                        int wordLength = Int16.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "FractionLength");
                        infoReader.Read();
                        int fracLength = Int16.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "MinType");
                        infoReader.Read();
                        float minDataType = (float)Double.Parse(infoReader.Value);

                        do
                        {
                            infoReader.Read();
                        }while (infoReader.Name != "MaxType");
                        infoReader.Read();
                        float maxDataType = (float)Double.Parse(infoReader.Value);

                        parameterDataType = new ParameterDataType(dataTypeName, isSigned, wordLength, fracLength, minDataType, maxDataType);
                    }
                    else
                    {
                        parameterDataType = new ParameterDataType(dataTypeName);
                    }

                    BlockInfo blockInfo;
                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "Path");
                    infoReader.Read();
                    string bloackPath = infoReader.Value;

                    do
                    {
                        infoReader.Read();
                    }while (infoReader.Name != "PortNum");
                    infoReader.Read();
                    int blockPortNum = Int16.Parse(infoReader.Value);

                    infoReader.Read();

                    blockInfo = new BlockInfo(bloackPath, blcokName, blockPortNum);


                    testWorkspace.outputVariables.Add(new TestParameter(blockInfo, parameterDataType));
                }
            }
            infoReader.Close();
            return;
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="StepConstraint"/> class.
		/// </summary>
		/// <param name="dataType">The data type that the constraint can validate.</param>
		/// <exception cref="CodedArgumentException"><paramref name="dataType"/> is not supported by the constraint.</exception>
		public StepConstraint(ParameterDataType dataType)
			: base(StepConstraintName)
		{
			mDataType = dataType;
			mStepSize = CheckDataType();
		}
		/// <summary>
		/// When implemented by a deriving class, checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		protected override void OnValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			base.OnValidation(results, value, dataType, memberName, displayName);
			AssertDataType(dataType, mDataType);

			int CompareResult = 0;
			Type type = value.GetType();
			if (type == ExpectedNetType)
			{
				// Type matches
				CompareResult = ((IComparable)mMinimumValue).CompareTo(value);
			}
			else
			{
				if (IsConvertibleType(mDataType))
				{
					if (type == AltNetType)
					{
						// Type is already equal to alternative data type, no conversion necessary.
						CompareResult = ((IComparable)AltMinimumValue).CompareTo(value);
					}
					else
					{
						object ConvertedValue = null;
						// Try to convert and compare to actual data type
						try
						{
							ConvertedValue = Convert.ChangeType(value, ExpectedTypeCode, System.Globalization.CultureInfo.InvariantCulture);
						}
						catch (Exception ex) when (ex is InvalidCastException || ex is FormatException || ex is OverflowException)
						{
						}

						if (ConvertedValue == null)
						{
							// Conversion to actual data type failed, try to convert and compare to alternative data type.
							try
							{
								ConvertedValue = Convert.ChangeType(value, AltTypeCode, System.Globalization.CultureInfo.InvariantCulture);
							}
							catch (Exception ex) when (ex is InvalidCastException || ex is FormatException || ex is OverflowException)
							{
								throw new CodedArgumentException(Errors.CreateHResult(ErrorCodes.MinimumValueConstraint_Validate_TypeMismatch), string.Format(Properties.Resources.Global_Validate_TypeNotConvertible, type.Name, ExpectedNetType.Name));
							}
							CompareResult = ((IComparable)AltMinimumValue).CompareTo(ConvertedValue);
						}
						else
						{
							// Converting to actual data type successful
							CompareResult = ((IComparable)mMinimumValue).CompareTo(ConvertedValue);
						}
					}
				}
				else
				{
					throw new CodedArgumentException(Errors.CreateHResult(ErrorCodes.MinimumValueConstraint_Validate_TypeMismatch), string.Format(Properties.Resources.Global_Validate_TypeNotConvertible, type.Name, ExpectedNetType.Name));
				}

			}

			if (CompareResult > 0)
			{
				results.Add(new ParameterValidationResult(Errors.CreateHResult(ErrorCodes.MinimumValueConstraint_Validate_TooSmall), string.Format(Properties.Resources.MinimumValueConstraint_Validate_Failed, displayName, mMinimumValue, value), memberName, this));
			}
		}
		/// <summary>
		/// Sets the parameters that the <see cref="Constraint"/> requires to work.
		/// </summary>
		/// <param name="parameters">A enumeration of string parameters.</param>
		/// <param name="dataType">The data type that the constraint needs to restrict.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="parameters"/> is <see langword="null"/>.</exception>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		/// <exception cref="ConstraintConfigurationException"><paramref name="parameters"/> contains no elements, or an invalid element.</exception>
		protected override void SetParameters(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			base.SetParameters(parameters, dataType);
			AssertDataType(dataType, ParameterDataType.String);
			if (parameters.Count != 1)
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.CharacterSetConstraint_SetParameters_OnlyOneParam), string.Format(Properties.Resources.Global_SetParameters_InvalidCount, this.Name, 1), this);
			}

			if (!Enum.TryParse<CharSet>(parameters[0], out mCharacterSet))
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.CharacterSetConstraint_SetParameters_CharSetInvalid), string.Format(Properties.Resources.CharacterSetConstraint_SetParameters_InvalidValue, parameters[0]), this);
			}
		}
Exemplo n.º 22
0
 /// <summary>
 /// 创建一个空的 jQuery UI 参数, 参数值采用选择器.
 /// </summary>
 /// <param name="dataType">参数的数据类型.</param>
 public Parameter(ParameterDataType dataType)
     : this("new parameter", ParameterType.Selector, null, null, dataType, null)
 {
 }
		/// <summary>
		/// Initializes a new instance of the SystemSetting class with the specified parameters.
		/// </summary>
		/// <param name="name">The name of the setting.</param>
		/// <param name="displayName">The display name of the setting.</param>
		/// <param name="dataType">The data type of the setting value.</param>
		/// <param name="constraints">A string of constraints that specify the specific boundaries of Value. May be null.</param>
		/// <param name="value">The serialized settings value.</param>
		/// <remarks>This constructor is useful to read a setting from the data store (e.g. XML file, database, etc.).</remarks>
		public SystemSetting(string name, string displayName, ParameterDataType dataType, string constraints, string value)
		{
			Name = name;
			DisplayName = displayName;
			DataType = dataType;
			mConstraintString = constraints;
			if (value != null)
			{
				SerializedValue = value;
			}
		}
Exemplo n.º 24
0
 /// <summary>
 /// 创建一个 jQuery UI 参数, 参数值对应一个选择器.
 /// </summary>
 /// <param name="name">参数名称.</param>
 /// <param name="value">一个选择器.</param>
 /// <param name="default">默认值.</param>
 /// <param name="dataType">参数的数据类型.</param>
 /// <param name="provider">备用的转换函数.</param>
 public Parameter(string name, string value, string @default, ParameterDataType dataType, string provider)
     : this(name, ParameterType.Selector, value, @default, dataType, provider)
 {
 }
Exemplo n.º 25
0
 /// <summary>
 /// 创建一个 jQuery UI 参数.
 /// </summary>
 /// <param name="name">参数名称.</param>
 /// <param name="type">参数类型.</param>
 /// <param name="value">参数值.</param>
 /// <param name="default">默认值.</param>
 /// <param name="dataType">参数的数据类型.</param>
 public Parameter(string name, ParameterType type, string value, string @default, ParameterDataType dataType)
     : this(name, type, value, @default, dataType, null)
 {
 }
		/// <summary>
		/// Sets the parameters that the <see cref="Constraint"/> requires to work.
		/// </summary>
		/// <param name="parameters">A enumeration of string parameters.</param>
		/// <param name="dataType">The data type that the constraint needs to restrict.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="parameters"/> is <see langword="null"/>.</exception>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		/// <exception cref="ConstraintConfigurationException"><paramref name="parameters"/> contains no elements, or an invalid element.</exception>
		protected override void SetParameters(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			base.SetParameters(parameters, dataType);
			AssertDataType(dataType, ParameterDataType.Uri);
			CheckSchemes(parameters);
			mAllowedSchemes = new List<string>(parameters);
		}
		/// <summary>
		/// Sets the parameters that the <see cref="Constraint"/> requires to work.
		/// </summary>
		/// <param name="parameters">A enumeration of string parameters.</param>
		/// <param name="dataType">The data type that the constraint needs to restrict.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="parameters"/> is <see langword="null"/>.</exception>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		/// <exception cref="ConstraintConfigurationException"><paramref name="parameters"/> contains no elements, or an invalid element.</exception>
		protected override void SetParameters(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			base.SetParameters(parameters, dataType);
			AssertDataType(dataType, ParameterDataType.Bytes, ParameterDataType.String, ParameterDataType.Uri);
			if (parameters.Count != 1)
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.MaximumLengthConstraint_SetParameters_OnlyOneParam), string.Format(Properties.Resources.Global_SetParameters_InvalidCount, this.Name, 1), this);
			}

			try
			{
				mMaximumLength = ParameterConvert.ToInt32(parameters[0]);
				if (mMaximumLength < 0)
				{
					throw new ParameterConversionException(Errors.CreateHResult(ErrorCodes.MaximumLengthConstraint_SetParameters_LengthNegative), Properties.Resources.MaximumLengthConstraint_LengthNegative, ParameterDataType.Int32, mMaximumLength);
				}
			}
			catch (ParameterConversionException ex)
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.MaximumLengthConstraint_SetParameters_ParamInvalid), string.Format(Properties.Resources.Global_SetParameters_Invalid, this.Name), this, ex);
			}
		}
Exemplo n.º 28
0
 /// <summary>
 /// 创建一个 jQuery UI 参数.
 /// </summary>
 /// <param name="name">参数名称.</param>
 /// <param name="type">参数类型.</param>
 /// <param name="value">参数值.</param>
 /// <param name="dataType">参数的数据类型.</param>
 /// <param name="provider">备用的转换函数.</param>
 public Parameter(string name, ParameterType type, string value, ParameterDataType dataType, string provider)
     : this(name, type, value, null, dataType, provider)
 {
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="StepConstraint"/> class with the specified maximum value.
		/// </summary>
		/// <param name="dataType">The data type that the constraint can validate.</param>
		/// <param name="stepSize">The increment step.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="stepSize"/> is <see langword="null"/>.</exception>
		public StepConstraint(ParameterDataType dataType, object stepSize)
			: base(StepConstraintName)
		{
			if (stepSize == null)
			{
				throw new CodedArgumentNullException(Errors.CreateHResult(ErrorCodes.StepConstraint_ctor_StepSizeNull), nameof(stepSize));
			}

			mDataType = dataType;
			mStepSize = CheckDataType();
			mStepSize = CheckValueType(stepSize);
		}
		/// <summary>
		/// When implemented by a deriving class, checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		protected override void OnValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			base.OnValidation(results, value, dataType, memberName, displayName);
			AssertDataType(dataType, ParameterDataType.String);

			if (!CheckString((string)value))
			{
				results.Add(new ParameterValidationResult(Errors.CreateHResult(ErrorCodes.CharacterSetConstraint_Validate_CharNotDefined), string.Format(Properties.Resources.CharacterSetConstraint_Validate_Failed, displayName, GetCharSetName()), memberName, this));
			}

		}
		/// <summary>
		/// Calls <see cref="Constraint.OnValidation(IList{ParameterValidationResult}, object, ParameterDataType, string, string)"/> of the base class.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		/// <remarks>This method is a small hack to be used by the derived <see cref="EnumTypeConstraint"/> class.</remarks>
		protected void OnBaseValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			base.OnValidation(results, value, dataType, memberName, displayName);
		}
		/// <summary>
		/// When implemented by a deriving class, checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		protected override void OnValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			base.OnValidation(results, value, dataType, memberName, displayName);
			AssertDataType(dataType, ParameterDataType.Bytes, ParameterDataType.String, ParameterDataType.Uri);

			int CurrentLength = 0;
			bool IsString = false;
			Type type = value.GetType();
			if (type == typeof(byte[]))
			{
				CurrentLength = ((byte[])value).Length;
			}
			else if (type == typeof(string))
			{
				IsString = true;
				CurrentLength = ((string)value).Length;
			}
			else if (type == typeof(Uri))
			{
				IsString = true;
				CurrentLength = ((Uri)value).ToString().Length;
			}
			else
			{
				throw new CodedArgumentException(Errors.CreateHResult(ErrorCodes.MaximumLengthConstraint_Validate_TypeNotSupported), string.Format(Properties.Resources.Global_Validate_TypeMismatch, type.Name, this.Name));
			}

			if (CurrentLength > mMaximumLength)
			{
				results.Add(new ParameterValidationResult(Errors.CreateHResult(ErrorCodes.MaximumLengthConstraint_Validate_TooLong), string.Format(
					IsString ? Properties.Resources.MaximumLengthConstraint_Validate_FailedString
					: Properties.Resources.MaximumLengthConstraint_Validate_FailedBytes,
					displayName, mMaximumLength, CurrentLength), memberName, this));
			}
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="MinimumValueConstraint"/> class with serialized data.
		/// </summary>
		/// <param name="info">The object that holds the serialized object data.</param>
		/// <param name="context">The contextual information about the source or destination.</param>
		/// <exception cref="System.ArgumentNullException">The <paramref name="info"/> argument is null.</exception>
		/// <exception cref="System.Runtime.Serialization.SerializationException">The constraint could not be deserialized correctly.</exception>
		protected MinimumValueConstraint(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
			: base(info, context)
		{
			mDataType = (ParameterDataType)info.GetValue(nameof(DataType), typeof(ParameterDataType));
			mMinimumValue = CheckDataType();
			mMinimumValue = CheckValueType(info.GetValue(nameof(MinimumValue), ExpectedNetType));
		}
		/// <summary>
		/// When implemented by a deriving class, checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		protected override void OnValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			base.OnValidation(results, value, dataType, memberName, displayName);
			AssertDataType(dataType, ParameterDataType.Uri);

			if (mAllowedSchemes == null)
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.AllowedSchemeConstraint_CheckSchemes_SchemeNullEmpty), Properties.Resources.AllowedSchemeConstraint_Validate_NotConfigured, this);
			}
			string Scheme = ((Uri)value).Scheme;
			bool SchemeFound = false;
			foreach (string scheme in mAllowedSchemes)
			{
				if (string.Compare(scheme, Scheme, StringComparison.OrdinalIgnoreCase) == 0)
				{
					SchemeFound = true;
					break;
				}
			}
			if (!SchemeFound)
			{
				results.Add(new ParameterValidationResult(Errors.CreateHResult(ErrorCodes.AllowedSchemeConstraint_Validate_SchemeNotAllowed), string.Format(Properties.Resources.AllowedSchemeConstraint_Validate_Failed, displayName, Scheme, ConcatSchemes()), memberName, this));
			}
		}
		/// <summary>
		/// Checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <remarks>The default implementation of the method performs no validation.</remarks>
		/// <returns>An enumeration of <see cref="ParameterValidationResult"/>s. The enumeration is be empty if the <paramref name="value"/> is within the constraint's boundaries.</returns>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		/// <exception cref="CodedArgumentNullOrWhiteSpaceException"><paramref name="memberName"/> is <see langword="null"/> or empty or white-space.</exception>
		/// <exception cref="InvalidDataTypeException"><paramref name="dataType"/> is not supported by the <see cref="Constraint"/>.</exception>
		public IEnumerable<ParameterValidationResult> Validate(object value, ParameterDataType dataType, string memberName)
		{
			return Validate(value, dataType, memberName, memberName);
		}
		/// <summary>
		/// Initializes a new instance of the SystemSetting class with the specified parameters.
		/// </summary>
		/// <param name="name">The name of the setting.</param>
		/// <param name="displayName">The display name of the setting.</param>
		/// <param name="dataType">The data type of the setting value.</param>
		/// <param name="constraints">A list of objects derived from Constraint that specify the specific boundaries of Value. May be null.</param>
		/// <param name="value">The settings value.</param>
		/// <remarks>This constructor is useful to create new settings.</remarks>
		public SystemSetting(string name, string displayName, ParameterDataType dataType, IReadOnlyList<Constraint> constraints, object value)
		{
			Name = name;
			DisplayName = displayName;
			DataType = dataType;
			mConstraints = constraints;
			// Create the constraint string right away for later serialization, so we don't need a separate update check as it is used for Value and SerializedValue.
			if (mConstraints != null)
				mConstraintString = ConstraintParser.ConcatConstraints(mConstraints);
			AreConstraintsParsed = true;
			if (value != null)
			{
				// Checking the data type to match DataType was omitted here, but may be a good idea.
				Value = value;
			}
		}
		/// <summary>
		/// Checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		/// <remarks>The default implementation of the method performs no validation.</remarks>
		/// <returns>An enumeration of <see cref="ParameterValidationResult"/>s. The enumeration is be empty if the <paramref name="value"/> is within the constraint's boundaries.</returns>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		/// <exception cref="CodedArgumentNullOrWhiteSpaceException"><paramref name="memberName"/> is <see langword="null"/> or empty or white-space.</exception>
		/// <exception cref="InvalidDataTypeException"><paramref name="dataType"/> is not supported by the <see cref="Constraint"/>.</exception>
		public IEnumerable<ParameterValidationResult> Validate(object value, ParameterDataType dataType, string memberName, string displayName)
		{
			if (dataType == ParameterDataType.None)
			{
				throw new CodedArgumentOutOfRangeException(Errors.CreateHResult(ErrorCodes.Constraint_Validate_TypeNone), nameof(dataType), Properties.Resources.Global_ParameterDataType_None);
			}

			if (string.IsNullOrWhiteSpace(memberName))
			{
				throw new CodedArgumentNullOrWhiteSpaceException(Errors.CreateHResult(ErrorCodes.Constraint_Validate_NameNullEmpty), nameof(memberName));
			}
			if (displayName == null)
			{
				displayName = memberName;
			}

			List<ParameterValidationResult> ReturnValue = new List<ParameterValidationResult>();
			OnValidation(ReturnValue, value, dataType, memberName, displayName);
			return ReturnValue;
		}
Exemplo n.º 38
0
 public DataParameterDescription(string name, string label, ParameterDataType dataType)
     : this(name, label, (int)dataType)
 {
 }
		/// <summary>
		/// Sets the parameters that the <see cref="Constraint"/> requires to work.
		/// </summary>
		/// <param name="parameters">A enumeration of string parameters.</param>
		/// <param name="dataType">The data type that the constraint needs to restrict.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="parameters"/> is <see langword="null"/>.</exception>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		protected virtual void SetParameters(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			if (parameters == null)
			{
				throw new CodedArgumentNullException(Errors.CreateHResult(ErrorCodes.Constraint_SetParameters_ParametersNull), nameof(parameters));
			}

			if (dataType == ParameterDataType.None)
			{
				throw new CodedArgumentOutOfRangeException(Errors.CreateHResult(ErrorCodes.Constraint_SetParameters_TypeNone), nameof(dataType), Properties.Resources.Global_ParameterDataType_None);
			}
		}
		/// <summary>
		/// Sets the parameters that the <see cref="Constraint"/> requires to work.
		/// </summary>
		/// <param name="parameters">A enumeration of string parameters.</param>
		/// <param name="dataType">The data type that the constraint needs to restrict.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="parameters"/> is <see langword="null"/>.</exception>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		/// <exception cref="ConstraintConfigurationException"><paramref name="parameters"/> contains no elements, or an invalid element.</exception>
		protected override void SetParameters(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			base.SetParameters(parameters, dataType);
			CheckParameters(parameters);
			mHints = new List<string>(parameters);
		}
		/// <summary>
		/// Checks if the specified data type is supported by the <see cref="Constraint"/>.
		/// </summary>
		/// <param name="dataType">The data type to check.</param>
		/// <param name="expectedType">The data type supported by the <see cref="Constraint"/>.</param>
		/// <exception cref="InvalidDataTypeException"><paramref name="dataType"/> is not supported by the <see cref="Constraint"/>.</exception>
		protected void AssertDataType(ParameterDataType dataType, ParameterDataType expectedType)
		{
			if (dataType == expectedType)
				return;
			throw new InvalidDataTypeException(Errors.CreateHResult(ErrorCodes.Constraint_AssertDataType_TypeNotSupported), this, dataType);
		}
		/// <summary>
		/// When implemented by a deriving class, checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		protected override void OnValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			OnBaseValidation(results, value, dataType, memberName, displayName);
			AssertDataType(dataType, ParameterDataType.Xml);
		}
		/// <summary>
		/// Checks if the specified data type is supported by the <see cref="Constraint"/>.
		/// </summary>
		/// <param name="dataType">The data type to check.</param>
		/// <param name="expectedTypes">The data types supported by the <see cref="Constraint"/>.</param>
		/// <exception cref="InvalidDataTypeException"><paramref name="dataType"/> is not supported by the <see cref="Constraint"/>.</exception>
		protected void AssertDataType(ParameterDataType dataType, params ParameterDataType[] expectedTypes)
		{
			if (expectedTypes == null)
			{
				throw new CodedArgumentNullException(Errors.CreateHResult(ErrorCodes.Constraint_AssertDataType_TypesNull), nameof(expectedTypes));
			}
			foreach (ParameterDataType expectedType in expectedTypes)
			{
				if (dataType == expectedType)
					return;
			}
			throw new InvalidDataTypeException(Errors.CreateHResult(ErrorCodes.Constraint_AssertDataType_TypeNotSupported), this, dataType);
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="MinimumValueConstraint"/> class with the specified minimum value.
		/// </summary>
		/// <param name="dataType">The data type that the constraint can validate.</param>
		/// <param name="minimumValue">The minimum value to enforce.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="minimumValue"/> is <see langword="null"/>.</exception>
		public MinimumValueConstraint(ParameterDataType dataType, object minimumValue)
			: base(MinimumValueConstraintName)
		{
			if (minimumValue == null)
			{
				throw new CodedArgumentNullException(Errors.CreateHResult(ErrorCodes.MinimumValueConstraint_ctor_MinValueNull), nameof(minimumValue));
			}

			mDataType = dataType;
			mMinimumValue = CheckDataType();
			mMinimumValue = CheckValueType(minimumValue);
		}
		/// <summary>
		/// When overridden by a deriving class, checks that the provided value is within the bounds of the constraint.
		/// </summary>
		/// <param name="results">A list that of <see cref="ParameterValidationResult"/>s; when the method returns, it contains the validation errors generated by the method.</param>
		/// <param name="value">The value to check.</param>
		/// <param name="dataType">The data type of the value.</param>
		/// <param name="memberName">The name of the property or field that is validated.</param>
		/// <param name="displayName">The (localized) display name of the property or field that is validated. May be <see langword="null"/>.</param>
		protected virtual void OnValidation(IList<ParameterValidationResult> results, object value, ParameterDataType dataType, string memberName, string displayName)
		{
			if (results == null)
			{
				throw new CodedArgumentNullException(Errors.CreateHResult(ErrorCodes.Constraint_OnValidation_ResultsNull), nameof(results));
			}
			if (value == null)
			{
				throw new CodedArgumentNullException(Errors.CreateHResult(ErrorCodes.Constraint_OnValidation_ValueNull), nameof(value));
			}
		}
		/// <summary>
		/// Sets the parameters that the <see cref="Constraint"/> requires to work.
		/// </summary>
		/// <param name="parameters">A enumeration of string parameters.</param>
		/// <param name="dataType">The data type that the constraint needs to restrict.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="parameters"/> is <see langword="null"/>.</exception>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		/// <exception cref="ConstraintConfigurationException"><paramref name="parameters"/> contains no elements, or an invalid element.</exception>
		protected override void SetParameters(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			base.SetParameters(parameters, dataType);
			if (parameters.Count != 1)
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.MinimumValueConstraint_SetParameters_OnlyOneParam), string.Format(Properties.Resources.Global_SetParameters_InvalidCount, this.Name, 1), this);
			}

			try
			{
				mMinimumValue = CheckValueType(ParameterConvert.ToDataType(parameters[0], dataType, null));
			}
			catch (ParameterConversionException ex)
			{
				throw new ConstraintConfigurationException(Errors.CreateHResult(ErrorCodes.MinimumValueConstraint_SetParameters_ParamInvalid), string.Format(Properties.Resources.Global_SetParameters_Invalid, this.Name), this, ex);
			}
		}
		/// <summary>
		/// Sets the parameters that the <see cref="Constraint"/> requires to work.
		/// </summary>
		/// <param name="parameters">A enumeration of string parameters.</param>
		/// <param name="dataType">The data type that the constraint needs to restrict.</param>
		/// <exception cref="CodedArgumentNullException"><paramref name="parameters"/> is <see langword="null"/>.</exception>
		/// <exception cref="CodedArgumentOutOfRangeException"><paramref name="dataType"/> is <see cref="ParameterDataType.None"/>.</exception>
		internal void SetParametersInternal(IReadOnlyList<string> parameters, ParameterDataType dataType)
		{
			SetParameters(parameters, dataType);
		}
		/// <summary>
		/// Checks if the data type can be converted into another data type before comparison.
		/// </summary>
		/// <param name="dataType">The data type to check.</param>
		/// <returns><see langword="false"/>, if <paramref name="dataType"/> <see cref="ParameterDataType.DateTimeOffset"/>, <see cref="ParameterDataType.TimeSpan"/> or <see cref="ParameterDataType.Version"/>; otherwise, <see langword="true"/>.</returns>
		private static bool IsConvertibleType(ParameterDataType dataType)
		{
			return !(dataType == ParameterDataType.DateTimeOffset || dataType == ParameterDataType.TimeSpan || dataType == ParameterDataType.Version);
		}
Exemplo n.º 49
0
 public int GetDataParameterIndex(ParameterDataType dataType)
 {
     return(GetDataParameterIndex((int)dataType));
 }
Exemplo n.º 50
0
        /// <summary>
        /// Build a ParameterDefinition instance based on a property name and provide value and schema.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="containerSchema"></param>
        /// <returns></returns>
        internal static ParameterDefinition ParseProperty(string name, JToken value, JsonSchema containerSchema)
        {
            ParameterDefinition param = new ParameterDefinition();

            param.Name          = name;
            param.OriginalValue = value.ToString();

            switch (value.Type)
            {
            case JTokenType.Boolean:
                param.Type = ParameterDataType.Boolean;
                break;

            case JTokenType.Float:
                param.Type = ParameterDataType.Double;
                break;

            case JTokenType.Integer:
                param.Type = ParameterDataType.Int64;
                break;

            case JTokenType.String:
                var               propValue  = value.Value <string>();
                SimpleDataType    customType = ExtensionMethods.ParseSimpleTypeString(propValue.ToLowerInvariant());
                ParameterDataType paramType  = (customType != SimpleDataType.None) ? new ParameterDataType(customType) : ParameterDataType.String;
                param.Type = paramType;
                break;

            case JTokenType.Date:
                param.Type = ParameterDataType.DateTimeOffset;
                break;

            case JTokenType.Object:
            {
                var objectSchema = GeneratePropertyCollection((JObject)value);
                if (objectSchema.ContainsKey("@odata.type"))
                {
                    param.Type = objectSchema["@odata.type"].OriginalValue.ParseParameterDataType();
                }
                else if (objectSchema.ContainsKey("@type"))
                {
                    param.Type = objectSchema["@type"].OriginalValue.ParseParameterDataType();
                }
                else
                {
                    // See if we can infer type from the parent scehma
                    var propertyCollection = GeneratePropertyCollection((JObject)value);
                    ParameterDefinition[] customMembers = null;
                    if (propertyCollection != null)
                    {
                        customMembers = propertyCollection.Values.ToArray();
                    }

                    ParameterDefinition schemaProperty;
                    if (null != containerSchema && containerSchema.ExpectedProperties.TryGetValue(name, out schemaProperty))
                    {
                        param.Type = new ParameterDataType(schemaProperty.Type, customMembers);
                    }
                    else
                    {
                        param.Type = new ParameterDataType(customMembers);
                    }
                }
                break;
            }

            case JTokenType.Array:
            {
                ParameterDataType propertyType = ParameterDataType.GenericCollection;

                // Try to infer type from the items in the array
                var firstChild = value.First;
                if (null != firstChild)
                {
                    var objectType = ParseProperty("array[0]", firstChild, null);
                    if (null != objectType)
                    {
                        propertyType = ParameterDataType.CollectionOfType(objectType.Type);
                    }
                }

                // See if we can do better than GenericCollection if that's the situation we're in.
                if (propertyType == ParameterDataType.GenericCollection)
                {
                    ParameterDefinition schemaProperty;
                    if (null != containerSchema && containerSchema.ExpectedProperties.TryGetValue(name, out schemaProperty))
                    {
                        // Use the parent schema's type indication
                        //propertyType = ParameterDataType.CollectionOfType(schemaProperty.Type);
                        propertyType = schemaProperty.Type;
                    }
                }

                Dictionary <string, ParameterDefinition> members = null;
                if ((propertyType.IsObject || (propertyType.IsCollection && propertyType.CollectionResourceType == SimpleDataType.Object)) &&
                    string.IsNullOrEmpty(propertyType.CustomTypeName))
                {
                    // If we don't know what kind of object is here, let's record what we see as custom members
                    var firstValue = value.First as JObject;
                    members = firstValue != null?GeneratePropertyCollection(firstValue) : new Dictionary <string, ParameterDefinition>();
                }
                ParameterDefinition[] customMembers = null;
                if (members != null)
                {
                    customMembers = members.Values.ToArray();
                }
                param.Type = new ParameterDataType(propertyType, customMembers);
                break;
            }

            case JTokenType.Null:
                param.Type          = ParameterDataType.GenericObject;
                param.OriginalValue = null;
                break;

            default:
                Console.WriteLine("Unsupported: Property {0} is of type {1} which is not currently supported.", name, value.Type);
                throw new NotSupportedException(string.Format("Unsupported: Property {0} is of type {1} which is not currently supported.", name, value.Type));
            }

            return(param);
        }
Exemplo n.º 51
0
 internal static string ToSwaggerTypeString(this ParameterDataType datatype)
 {
     return datatype.Type.ToSwaggerTypeString();
 }