public void ParseCsdlEnumTypeWithMembersWorksAsExpected() { string json = @"""IsolationLevel"": { ""$Kind"": ""EnumType"", ""$IsFlags"": true, ""Snapshot"": 1, ""*****@*****.**"": ""All data returned for a request"", ""@Core.Description"": ""Supported isolation levels"" }"; CsdlEnumType enumType = ParseCsdlSchemaElement(json, SchemaJsonParser.ParseCsdlEnumType); Assert.NotNull(enumType); Assert.Equal("IsolationLevel", enumType.Name); Assert.True(enumType.IsFlags); CsdlAnnotation annotation = Assert.Single(enumType.VocabularyAnnotations); Assert.Equal("Core.Description", annotation.Term); CsdlConstantExpression descripExp = Assert.IsType <CsdlConstantExpression>(annotation.Expression); Assert.Equal("Supported isolation levels", descripExp.Value); CsdlEnumMember enumMember = Assert.Single(enumType.Members); Assert.Equal("Snapshot", enumMember.Name); Assert.Equal(1, enumMember.Value.Value); annotation = Assert.Single(enumMember.VocabularyAnnotations); Assert.Equal("Core.DisplayName", annotation.Term); CsdlConstantExpression displayNameExp = Assert.IsType <CsdlConstantExpression>(annotation.Expression); Assert.Equal("All data returned for a request", displayNameExp.Value); }
public void ParseIfExpressionWorksAsExpected() { string json = @" { ""$If"": [ { ""$Path"": ""IsFemale"" }, ""Female"", ""Male"" ] }"; CsdlExpressionBase expressionBase = ParseExpression(json); Assert.NotNull(expressionBase); CsdlIfExpression ifExp = Assert.IsType <CsdlIfExpression>(expressionBase); Assert.NotNull(ifExp.Test); CsdlPathExpression pathExp = Assert.IsType <CsdlPathExpression>(ifExp.Test); Assert.Equal("IsFemale", pathExp.Path); Assert.NotNull(ifExp.IfTrue); CsdlConstantExpression trueExp = Assert.IsType <CsdlConstantExpression>(ifExp.IfTrue); Assert.Equal(EdmValueKind.String, trueExp.ValueKind); Assert.Equal("Female", trueExp.Value); Assert.NotNull(ifExp.IfFalse); CsdlConstantExpression falseExp = Assert.IsType <CsdlConstantExpression>(ifExp.IfFalse); Assert.Equal(EdmValueKind.String, falseExp.ValueKind); Assert.Equal("Male", falseExp.Value); }
[InlineData("1.7976931348623157E+308", EdmValueKind.Floating, "1.7976931348623157E+308")] // double.MaxValue public void ParseConstantExpressionWorksAsExpected(string json, EdmValueKind kind, string expected) { CsdlExpressionBase expressionBase = ParseExpression(json); Assert.NotNull(expressionBase); CsdlConstantExpression constExp = Assert.IsType <CsdlConstantExpression>(expressionBase); Assert.Equal(kind, constExp.ValueKind); Assert.Equal(expected, constExp.Value); }
private static CsdlExpressionBase AdjustStringConstantUsingTermType(CsdlExpressionBase expression, IEdmTypeReference termType) { if (expression == null || termType == null) { return(expression); } switch (expression.ExpressionKind) { case EdmExpressionKind.Collection: if (termType.IsCollection()) { IEdmTypeReference elementType = termType.AsCollection().ElementType(); IList <CsdlExpressionBase> newElements = new List <CsdlExpressionBase>(); CsdlCollectionExpression collectionExp = (CsdlCollectionExpression)expression; foreach (CsdlExpressionBase exp in collectionExp.ElementValues) { if (exp != null && exp.ExpressionKind == EdmExpressionKind.StringConstant) { newElements.Add(AdjustStringConstantUsingTermType(exp, elementType)); } else { newElements.Add(exp); } } return(new CsdlCollectionExpression(collectionExp.Type, newElements, collectionExp.Location as CsdlLocation)); } break; case EdmExpressionKind.StringConstant: CsdlConstantExpression constantExp = (CsdlConstantExpression)expression; switch (termType.TypeKind()) { case EdmTypeKind.Primitive: IEdmPrimitiveTypeReference primitiveTypeReference = (IEdmPrimitiveTypeReference)termType; return(BuildPrimitiveExpression(primitiveTypeReference, constantExp)); case EdmTypeKind.Path: IEdmPathType pathType = (IEdmPathType)termType.Definition; return(BuildPathExpression(pathType, constantExp)); case EdmTypeKind.Enum: IEdmEnumType enumType = (IEdmEnumType)termType.Definition; return(BuildEnumExpression(enumType, constantExp)); } break; } return(expression); }
private bool TryGetOptionalParameterOutOfLineAnnotation(string fullTargetName, string targetName, out string defaultValue) { defaultValue = null; bool isOptional = false; List <CsdlSemanticsAnnotations> annotations; // OData 4.0 applies annotations to all overloads of a function or action. // We still need to support annotating the non-overloaded version. // So, we should probably first check the fullTargetName and, if that doesn't match, check just the function or action name. bool found = Model.OutOfLineAnnotations.TryGetValue(fullTargetName, out annotations) ? true : Model.OutOfLineAnnotations.TryGetValue(targetName, out annotations); if (found) { foreach (var annotation in annotations) { var optionalParameterAnnotation = annotation.Annotations.Annotations.FirstOrDefault(a => a.Term == CoreVocabularyModel.OptionalParameterTerm.ShortQualifiedName() || a.Term == CoreVocabularyModel.OptionalParameterTerm.FullName()); if (optionalParameterAnnotation != null) { isOptional = true; CsdlRecordExpression optionalValueExpression = optionalParameterAnnotation.Expression as CsdlRecordExpression; if (optionalValueExpression != null) { foreach (CsdlPropertyValue property in optionalValueExpression.PropertyValues) { if (property.Property == CsdlConstants.Attribute_DefaultValue) { CsdlConstantExpression propertyValue = property.Expression as CsdlConstantExpression; if (propertyValue != null) { defaultValue = propertyValue.Value; } } } } break; } } } return(isOptional); }
public void ParseCsdlAnnotationWorksAsExpected() { string annotation = "\"@Measures.ISOCurrency\": \"USD\""; CsdlAnnotation csdlAnnotation = ParseAnnotation(annotation, out _); Assert.NotNull(csdlAnnotation); Assert.Equal("Measures.ISOCurrency", csdlAnnotation.Term); Assert.Null(csdlAnnotation.Qualifier); Assert.NotNull(csdlAnnotation.Expression); CsdlConstantExpression constantExp = Assert.IsType <CsdlConstantExpression>(csdlAnnotation.Expression); Assert.Equal(EdmValueKind.String, constantExp.ValueKind); Assert.Equal("USD", constantExp.Value); }
private static CsdlExpressionBase BuildPathExpression(IEdmPathType pathType, CsdlConstantExpression expression) { Debug.Assert(expression.ExpressionKind == EdmExpressionKind.StringConstant); CsdlLocation location = expression.Location as CsdlLocation; switch (pathType.PathKind) { case EdmPathTypeKind.AnnotationPath: return(new CsdlAnnotationPathExpression(expression.Value, location)); case EdmPathTypeKind.PropertyPath: return(new CsdlPropertyPathExpression(expression.Value, location)); case EdmPathTypeKind.NavigationPropertyPath: return(new CsdlNavigationPropertyPathExpression(expression.Value, location)); case EdmPathTypeKind.None: default: return(expression); } }
private CsdlOperationParameter OnParameterElement(XmlElementInfo element, XmlElementValueCollection childValues) { string name = Required(CsdlConstants.Attribute_Name); string typeName = OptionalType(CsdlConstants.Attribute_Type); string defaultValue = null; bool isOptional = false; CsdlTypeReference type = this.ParseTypeReference(typeName, childValues, element.Location, Optionality.Required); // TODO (Issue #855): handle out-of-line annotations XmlElementValue optionalAnnotationValue = childValues.Where(c => c is XmlElementValue <CsdlAnnotation> && (c.ValueAs <CsdlAnnotation>().Term == CoreVocabularyModel.OptionalParameterTerm.ShortQualifiedName() || c.ValueAs <CsdlAnnotation>().Term == CoreVocabularyModel.OptionalParameterTerm.FullName())) .FirstOrDefault(); if (optionalAnnotationValue != null) { isOptional = true; CsdlRecordExpression optionalValueExpression = optionalAnnotationValue.ValueAs <CsdlAnnotation>().Expression as CsdlRecordExpression; if (optionalValueExpression != null) { foreach (CsdlPropertyValue property in optionalValueExpression.PropertyValues) { CsdlConstantExpression propertyValue = property.Expression as CsdlConstantExpression; if (propertyValue != null) { if (property.Property == CsdlConstants.Attribute_DefaultValue) { defaultValue = propertyValue.Value; } } } } childValues.Remove(optionalAnnotationValue); } return(new CsdlOperationParameter(name, type, element.Location, isOptional, defaultValue)); }
private static CsdlConstantExpression BuildPrimitiveExpression(IEdmPrimitiveTypeReference primitiveTypeReference, CsdlConstantExpression expression) { Debug.Assert(expression.ExpressionKind == EdmExpressionKind.StringConstant); CsdlLocation location = expression.Location as CsdlLocation; switch (primitiveTypeReference.PrimitiveKind()) { case EdmPrimitiveTypeKind.Binary: return(new CsdlConstantExpression(EdmValueKind.Binary, expression.Value, location)); case EdmPrimitiveTypeKind.Date: return(new CsdlConstantExpression(EdmValueKind.Date, expression.Value, location)); case EdmPrimitiveTypeKind.DateTimeOffset: return(new CsdlConstantExpression(EdmValueKind.DateTimeOffset, expression.Value, location)); case EdmPrimitiveTypeKind.Decimal: // it maybe use the string for decimal // The IEEE754Compatible=true parameter indicates that the service MUST serialize Edm.Decimal numbers as strings. // The special values INF, -INF, or NaN are represented as strings. return(new CsdlConstantExpression(EdmValueKind.Decimal, expression.Value, location)); case EdmPrimitiveTypeKind.Int64: // The IEEE754Compatible=true parameter indicates that the service MUST serialize Edm.Int64 numbers as strings. return(new CsdlConstantExpression(EdmValueKind.Integer, expression.Value, location)); case EdmPrimitiveTypeKind.Duration: return(new CsdlConstantExpression(EdmValueKind.Duration, expression.Value, location)); case EdmPrimitiveTypeKind.Single: case EdmPrimitiveTypeKind.Double: // may have a string containing one of the special values INF, -INF, or NaN. return(new CsdlConstantExpression(EdmValueKind.Floating, expression.Value, location)); case EdmPrimitiveTypeKind.Guid: return(new CsdlConstantExpression(EdmValueKind.Guid, expression.Value, location)); case EdmPrimitiveTypeKind.TimeOfDay: return(new CsdlConstantExpression(EdmValueKind.TimeOfDay, expression.Value, location)); case EdmPrimitiveTypeKind.String: case EdmPrimitiveTypeKind.Byte: case EdmPrimitiveTypeKind.SByte: case EdmPrimitiveTypeKind.Stream: case EdmPrimitiveTypeKind.PrimitiveType: case EdmPrimitiveTypeKind.Geography: case EdmPrimitiveTypeKind.GeographyPoint: case EdmPrimitiveTypeKind.GeographyLineString: case EdmPrimitiveTypeKind.GeographyPolygon: case EdmPrimitiveTypeKind.GeographyCollection: case EdmPrimitiveTypeKind.GeographyMultiPolygon: case EdmPrimitiveTypeKind.GeographyMultiLineString: case EdmPrimitiveTypeKind.GeographyMultiPoint: case EdmPrimitiveTypeKind.Geometry: case EdmPrimitiveTypeKind.GeometryPoint: case EdmPrimitiveTypeKind.GeometryLineString: case EdmPrimitiveTypeKind.GeometryPolygon: case EdmPrimitiveTypeKind.GeometryCollection: case EdmPrimitiveTypeKind.GeometryMultiPolygon: case EdmPrimitiveTypeKind.GeometryMultiLineString: case EdmPrimitiveTypeKind.GeometryMultiPoint: case EdmPrimitiveTypeKind.None: default: return(expression); } }
private static CsdlExpressionBase BuildEnumExpression(IEdmEnumType enumType, CsdlConstantExpression expression) { return(new CsdlEnumMemberExpression(expression.Value, enumType, expression.Location as CsdlLocation)); }
public CsdlSemanticsStringConstantExpression(CsdlConstantExpression expression, CsdlSemanticsSchema schema) : base(schema, expression) { this.valueCache = new Cache <CsdlSemanticsStringConstantExpression, string>(); this.expression = expression; }
public CsdlSemanticsBinaryConstantExpression(CsdlConstantExpression expression, CsdlSemanticsSchema schema) : base(schema, expression) { this.valueCache = new Cache <CsdlSemanticsBinaryConstantExpression, byte[]>(); this.errorsCache = new Cache <CsdlSemanticsBinaryConstantExpression, IEnumerable <EdmError> >(); this.expression = expression; }
public void ParseApplyExpressionWorksAsExpected() { string json = @" { ""$Apply"": [ ""Product: "", { ""$Path"": ""ProductName"" }, ""("", { ""$Path"": ""Available/Quantity"" }, "" "", { ""$Path"": ""Available /Unit"" }, "" available)"" ], ""$Function"": ""odata.concat"" }"; CsdlExpressionBase expressionBase = ParseExpression(json); Assert.NotNull(expressionBase); CsdlApplyExpression applyExp = Assert.IsType <CsdlApplyExpression>(expressionBase); Assert.NotNull(applyExp.Function); Assert.Equal("odata.concat", applyExp.Function); Assert.NotNull(applyExp.Arguments); Assert.Equal(7, applyExp.Arguments.Count()); // 0 CsdlConstantExpression constantArgument = Assert.IsType <CsdlConstantExpression>(applyExp.Arguments.ElementAt(0)); Assert.Equal("Product: ", constantArgument.Value); // 1 CsdlPathExpression pathArgument = Assert.IsType <CsdlPathExpression>(applyExp.Arguments.ElementAt(1)); Assert.Equal("ProductName", pathArgument.Path); // 2 constantArgument = Assert.IsType <CsdlConstantExpression>(applyExp.Arguments.ElementAt(2)); Assert.Equal("(", constantArgument.Value); // 3 pathArgument = Assert.IsType <CsdlPathExpression>(applyExp.Arguments.ElementAt(3)); Assert.Equal("Available/Quantity", pathArgument.Path); // 4 constantArgument = Assert.IsType <CsdlConstantExpression>(applyExp.Arguments.ElementAt(4)); Assert.Equal(" ", constantArgument.Value); // 5 pathArgument = Assert.IsType <CsdlPathExpression>(applyExp.Arguments.ElementAt(5)); Assert.Equal("Available /Unit", pathArgument.Path); // 6 constantArgument = Assert.IsType <CsdlConstantExpression>(applyExp.Arguments.ElementAt(6)); Assert.Equal(" available)", constantArgument.Value); }
public CsdlSemanticsFloatingConstantExpression(CsdlConstantExpression expression, CsdlSemanticsSchema schema) : base(schema, expression) { this.valueCache = new Cache <CsdlSemanticsFloatingConstantExpression, double>(); this.errorsCache = new Cache <CsdlSemanticsFloatingConstantExpression, IEnumerable <EdmError> >(); this.expression = expression; }
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); }
public CsdlSemanticsDateConstantExpression(CsdlConstantExpression expression, CsdlSemanticsSchema schema) : base(schema, expression) { this.expression = expression; }
public CsdlSemanticsDateTimeOffsetConstantExpression(CsdlConstantExpression expression, CsdlSemanticsSchema schema) : base(schema, expression) { this.valueCache = new Cache <CsdlSemanticsDateTimeOffsetConstantExpression, DateTimeOffset>(); this.errorsCache = new Cache <CsdlSemanticsDateTimeOffsetConstantExpression, IEnumerable <EdmError> >(); this.expression = expression; }