コード例 #1
0
        public void ParseCastExpressionWorksAsExpected()
        {
            string json = @" {
  ""$Cast"": {
    ""$Path"": ""Average""
  },
  ""$Type"": ""Edm.Decimal"",
  ""$Precision"": 42
}";

            CsdlExpressionBase expressionBase = ParseExpression(json);

            Assert.NotNull(expressionBase);
            CsdlCastExpression castExp = Assert.IsType <CsdlCastExpression>(expressionBase);

            Assert.NotNull(castExp.Operand);
            CsdlPathExpression pathExp = Assert.IsType <CsdlPathExpression>(castExp.Operand);

            Assert.Equal("Average", pathExp.Path);

            Assert.NotNull(castExp.Type);
            CsdlDecimalTypeReference decimalType = Assert.IsType <CsdlDecimalTypeReference>(castExp.Type);

            Assert.NotNull(decimalType.Precision);
            Assert.Equal(42, decimalType.Precision.Value);
            Assert.Null(decimalType.Scale);
        }
コード例 #2
0
        /// <summary>
        /// Parse the Cast expression using <see cref="JsonElement"/>.
        /// </summary>
        /// <param name="element">The input JSON element.</param>
        /// <param name="context">The parser context.</param>
        /// <param name="castExp">The built cast expression.</param>
        /// <returns>true/false.</returns>
        private static bool TryParseCastExpression(JsonElement element, JsonParserContext context, out CsdlCastExpression castExp)
        {
            Debug.Assert(context != null);

            castExp = null;
            // Cast expressions are represented as an object with a member $Cast
            JsonElement propertyValue;

            if (element.ValueKind != JsonValueKind.Object || !element.TryGetProperty("$Cast", out propertyValue))
            {
                return(false);
            }

            // with a member $Cast whose value is an annotation expression
            CsdlExpressionBase expression = propertyValue.ProcessProperty("$Cast", context, ParseExpression);

            // a member $Type whose value is a string containing the qualified type name, and optionally a member $Collection with a value of true.
            CsdlTypeReference typeReference = CsdlJsonParseHelper.ParseCsdlTypeReference(element, context);

            castExp = new CsdlCastExpression(typeReference, expression, context.Location());
            return(true);
        }
コード例 #3
0
 public CsdlSemanticsCastExpression(CsdlCastExpression expression, IEdmEntityType bindingContext, CsdlSemanticsSchema schema)
     : base(schema, expression)
 {
     this.expression     = expression;
     this.bindingContext = bindingContext;
 }
コード例 #4
0
        public void ParseCsdlOutOfLineAnnotationWithMembersWorksAsExpected()
        {
            string json = @"""$Annotations"": {
   ""self.Person"": {
       ""@Core.Description#Tablet"": ""Dummy"",
       ""@UI.DisplayName#MyTablet"": {
           ""$Path"": ""FirstName""
       }
   },
   ""self.MyEntityType"": {
      ""@self.Dummy"": true,
      ""@UI.Threshold#Example73"": {
         ""$Type"": ""Edm.Decimal"",
         ""$Cast"": {
           ""$Path"": ""Average""
           }
      }
   }
}";

            IList <CsdlAnnotations> annotations = ParseCsdlSchemaElement(json, SchemaJsonParser.ParseCsdlOutOfLineAnnotations);

            Assert.NotNull(annotations);
            Assert.Equal(2, annotations.Count);

            // #1
            CsdlAnnotations csdlAnnotations = annotations.First(c => c.Target == "self.Person");

            Assert.Null(csdlAnnotations.Qualifier);
            Assert.Equal(2, csdlAnnotations.Annotations.Count());

            // #1.1
            CsdlAnnotation csdlAnnotation = csdlAnnotations.Annotations.First(c => c.Term == "Core.Description");

            Assert.Equal("Tablet", csdlAnnotation.Qualifier);
            CsdlConstantExpression constExp = Assert.IsType <CsdlConstantExpression>(csdlAnnotation.Expression);

            Assert.Equal("Dummy", constExp.Value);
            Assert.Equal(EdmValueKind.String, constExp.ValueKind);

            // #1.2
            csdlAnnotation = csdlAnnotations.Annotations.First(c => c.Term == "UI.DisplayName");
            Assert.Equal("MyTablet", csdlAnnotation.Qualifier);
            CsdlPathExpression pathExp = Assert.IsType <CsdlPathExpression>(csdlAnnotation.Expression);

            Assert.Equal("FirstName", pathExp.Path);

            // #2
            csdlAnnotations = annotations.First(c => c.Target == "self.MyEntityType");
            Assert.Null(csdlAnnotations.Qualifier);
            Assert.Equal(2, csdlAnnotations.Annotations.Count());

            // #2.1
            csdlAnnotation = csdlAnnotations.Annotations.First(c => c.Term == "self.Dummy");
            Assert.Null(csdlAnnotation.Qualifier);
            constExp = Assert.IsType <CsdlConstantExpression>(csdlAnnotation.Expression);
            Assert.Equal("true", constExp.Value);
            Assert.Equal(EdmValueKind.Boolean, constExp.ValueKind);

            // #2.2
            csdlAnnotation = csdlAnnotations.Annotations.First(c => c.Term == "UI.Threshold");
            Assert.Equal("Example73", csdlAnnotation.Qualifier);
            CsdlCastExpression castExp = Assert.IsType <CsdlCastExpression>(csdlAnnotation.Expression);

            Assert.IsType <CsdlDecimalTypeReference>(castExp.Type);

            pathExp = Assert.IsType <CsdlPathExpression>(castExp.Operand);
            Assert.Equal("Average", pathExp.Path);
        }