protected void Visit(ExpNode caller, ExpNode node, XmlElement parentNode) { nodeStack.Push(node); if (node == null) { throw new ArgumentNullException("node"); } if (node is KeyedResourceInstance && (!(node is AssociationResourceInstance))) { KeyedResourceInstance e = (KeyedResourceInstance)node; CreateEntryElement(e, parentNode); } else if (node is AssociationResourceInstance) { AssociationResourceInstance e = (AssociationResourceInstance)node; if (caller == null) { CreateLinkPayload(e, parentNode); } else { CreateBinding(e, parentNode); } } //Below are two special cases else if (caller == null && node is ResourceInstanceSimpleProperty) { this.VisitResourceInstanceSimpleProperty(node as ResourceInstanceSimpleProperty, parentNode); } else if (caller == null && node is ResourceInstanceComplexProperty) { this.VisitResourceInstanceComplexProperty(node as ResourceInstanceComplexProperty, parentNode); } else if (caller == null && node is ResourceInstanceNavRefProperty) { ResourceInstanceNavRefProperty navRef = node as ResourceInstanceNavRefProperty; AssociationResourceInstance associationResourceInstance = navRef.TreeNode as AssociationResourceInstance; if (associationResourceInstance != null && associationResourceInstance.Operation == AssociationOperation.Remove) { this.CreateUnBinding(parentNode); } else { throw new Exception("Unknown node type:" + navRef.TreeNode.GetType()); } } else if (caller == null && node is ResourceInstanceCollection) { ResourceInstanceCollection collection = node as ResourceInstanceCollection; CreateLinksElement(collection, null); } else { throw new Exception("Unknown node type: " + node.GetType()); } nodeStack.Pop(); }
protected virtual string Visit(ExpNode caller, ExpNode node) { if (node == null) { throw new ArgumentNullException("node"); } if (node is KeyedResourceInstance && (!(node is AssociationResourceInstance))) { KeyedResourceInstance e = (KeyedResourceInstance)node; _resourceType = this.GetResourceType(e); _typeStack.Push(_resourceType); string properties = null; if (this.RequestVerb == RequestVerb.Post && e.ResourceInstanceKey != null) { foreach (ResourceInstanceProperty resourceProperty in e.ResourceInstanceKey.KeyProperties) { if (properties != null) { properties += "," + this.Visit(e, resourceProperty); } else { properties = this.Visit(e, resourceProperty); } } } foreach (ResourceInstanceProperty resourceProperty in e.Properties) { if (properties != null) { properties += "," + this.Visit(e, resourceProperty); } else { properties = this.Visit(e, resourceProperty); } } _resourceType = _typeStack.Pop(); string typeAttribute = null; //if( Workspace.DataLayerProviderKind == DataLayerProviderKind.NonClr ) // typeAttribute = String.Format("type:\"{0}\" ", e.ResourceTypeName); //else typeAttribute = String.Format("type:\"{0}.{1}\" ", this.Workspace.ContextNamespace, e.TypeName); string uriAttribute = null; string metadataString = null; if (e.ResourceInstanceKey != null && this.RequestVerb != RequestVerb.Post) { uriAttribute = String.Format("uri:\"{0}\"", CreateCanonicalUri(e.ResourceInstanceKey)); } if (uriAttribute != null) { if (e.IncludeTypeMetadataHint) { metadataString = String.Format("__metadata: {{ {0}, {1}}}", typeAttribute, uriAttribute); } else { metadataString = String.Format("__metadata: {{ {0} }}", uriAttribute); } } else { if (e.IncludeTypeMetadataHint) { metadataString = String.Format("__metadata: {{ {0} }}", typeAttribute); } } //if (uriAttribute != null && e.IncludeTypeMetadataHint) // metadataString = String.Format("__metadata: {{ {0}, {1}}}", typeAttribute, uriAttribute); //else if (e.IncludeTypeMetadataHint && uriAttribute == null) // metadataString = String.Format("__metadata: {{ {0} }}", typeAttribute); string payload = null; if (properties == null) { payload = ApplyErrors(node, String.Format("{{{0}}}", metadataString)); } else if (metadataString != null) { payload = ApplyErrors(node, String.Format("{{{0}, {1}}}", metadataString, properties)); } else { payload = ApplyErrors(node, String.Format("{{{0}}}", properties)); } return(payload); } else if (node is AssociationResourceInstance) { AssociationResourceInstance e = (AssociationResourceInstance)node; if (e.Operation == AssociationOperation.Add) { string uri = CreateCanonicalUri(e.ResourceInstanceKey); string payload = null; if (caller == null) { payload = "{ uri:\"" + uri + "\"}"; } else if (e.IncludeTypeInBind) { payload = String.Format("__metadata: {{ uri:\"{0}\", type:\"{1}.{2}\"}}", uri, this.Workspace.ContextNamespace, e.ResourceInstanceKey.ResourceTypeName); } else { payload = String.Format("__metadata: {{ uri:\"{0}\"}}", uri); } return(ApplyErrors(node, payload)); } else { string navString = null; if (caller != null) { navString = e.Name + ": null"; } else { navString = "{ uri: null }"; } return(ApplyErrors(node, navString)); } } /* * else if (node is ResourceInstanceKey) * { * ResourceInstanceKey e = (ResourceInstanceKey)node; * if (RequestVerb == RequestVerb.Post) * { * string payload = String.Format("__metadata: {{ type:\"{0}.{1}\" }}", this.Workspace.ContextNamespace, e.ResourceTypeName); * if (e.KeyProperties != null) * { * foreach (ResourceInstanceProperty resourceProperty in e.KeyProperties) * { * payload += String.Format(", {0}", this.Visit(e, resourceProperty)); * } * } * return ApplyErrors(node, payload); * } * else if (RequestVerb == RequestVerb.Put) * { * string keyValues = WriteCommaDelimitedKeyValues(e); * * string payloadUri = String.Format("/{0}({1})", e.ResourceSetName, keyValues); * return ApplyErrors(node, String.Format("__metadata: {{uri:\"{0}\" }}", payloadUri)); * } * throw new ArgumentException("Request Verb is incorrect can't build an update payload:" + this.RequestVerb.ToString()); * }*/ else if (node is ResourceInstanceSimpleProperty) { ResourceInstanceSimpleProperty e = (ResourceInstanceSimpleProperty)node; Type clrType = e.ClrType; object val = e.PropertyValue; if (val != null) { clrType = val.GetType(); } if (e.CreateDollarValue) { if (val == null) { return(null); } else if (clrType == typeof(byte[])) { return((new System.Text.UTF8Encoding()).GetString((byte[])val)); } else { return(AstoriaUnitTests.Data.TypeData.XmlValueFromObject(val)); } } string jsonStringValue; if (clrType == typeof(DateTime) && val is DateTime) { if (e.UseTickCountForJsonDateTime) { jsonStringValue = "'" + JsonPrimitiveTypesUtil.GetJsonDateTimeStringValue((DateTime)val) + "'"; } else { jsonStringValue = JsonPrimitiveTypesUtil.DateTimeToString(val); } } else { jsonStringValue = JsonPrimitiveTypesUtil.PrimitiveToString(val, clrType); if (clrType == typeof(double) || clrType == typeof(float)) { // PrimitiveToString will lose the trailing .0 if its a whole number long temp; if (long.TryParse(jsonStringValue, out temp)) { jsonStringValue += ".0"; } } } if (caller == null) { return("{" + ApplyErrors(node, String.Format("{0}: {1}", e.Name, jsonStringValue)) + "}"); } else { return(ApplyErrors(node, String.Format("{0}: {1}", e.Name, jsonStringValue))); } } else if (node is ResourceInstanceComplexProperty) { ResourceInstanceComplexProperty e = (ResourceInstanceComplexProperty)node; string properties = null; if (e.IncludeTypeMetadataHint) { properties += String.Format("__metadata: {{ type:\"{0}.{1}\" }}", this.Workspace.ContextNamespace, e.TypeName); } foreach (ResourceInstanceProperty resourceProperty in e.ComplexResourceInstance.Properties) { if (string.IsNullOrEmpty(properties)) { properties += this.Visit(e, resourceProperty); } else { properties += "," + this.Visit(e, resourceProperty); } } string results = null; if (caller == null) { results = "{" + e.Name + ": {" + properties + "}" + "}"; } else { results = e.Name + ": {" + properties + "}"; } return(ApplyErrors(node, results)); } else if (node is ResourceInstanceNavRefProperty) { ResourceInstanceNavRefProperty e = (ResourceInstanceNavRefProperty)node; AssociationResourceInstance associationResourceInstance = e.TreeNode as AssociationResourceInstance; string navString = null; if ((associationResourceInstance != null && associationResourceInstance.Operation != AssociationOperation.Remove)) { navString = e.Name + ": {"; navString += this.Visit(e, e.TreeNode); navString += "}"; } else if (associationResourceInstance != null && associationResourceInstance.Operation == AssociationOperation.Remove) { if (caller != null) { navString = e.Name + ": null"; } else { navString = "null"; } } else { navString = e.Name + ": "; navString += this.Visit(e, e.TreeNode); } return(ApplyErrors(node, navString)); } else if (node is ResourceInstanceNavColProperty) { ResourceInstanceNavColProperty e = (ResourceInstanceNavColProperty)node; //string navString = String.Format("{0}: {", e.Name); string navString = e.Name + ": ["; foreach (NamedNode namedNode in e.Collection.NodeList) { if (!(namedNode is AssociationResourceInstance)) { navString += this.Visit(e, namedNode) + ","; } else { navString += "{" + this.Visit(e, namedNode) + "},"; } } navString = navString.TrimEnd(','); navString += "]"; return(ApplyErrors(node, navString)); } else { throw new Exception("Unknown node type: " + node.GetType()); } }
protected override String Visit(ExpNode caller, ExpNode node) { if (node is ProjectExpression) { ProjectExpression e = (ProjectExpression)node; if (e.Projections.Count == 0) { return(String.Format("{0}", this.Visit(e, e.Input) )); } else if (e.Projections.Count == 1) { if (e.Projections[0] is PropertyExpression) { return(String.Format("{0}/{1}", this.Visit(e, e.Input), this.Visit(e, e.Projections[0]) )); } } else { throw new Exception("More than 1 projection, invalid"); } } else if (node is ServiceOperationExpression) { AstoriaTestLog.WriteLine("Calling Service Operation"); ServiceOperationExpression e = (ServiceOperationExpression)node; string serviceOp = this.Visit(e, e.Input); AstoriaTestLog.WriteLine(serviceOp); StringBuilder sbParametersString = new StringBuilder(); if (!serviceOp.Contains("?")) { sbParametersString.Append("?"); } for (int index = 0; index < e.Arguments.Length; index++) { ServiceOperationParameterExpression parameter = e.Arguments[index]; if (index > 0) { sbParametersString.Append("&"); } string strLiteralString = CreateKeyStringValue(parameter.ParameterValue); sbParametersString.AppendFormat("{0}={1}", parameter.ParameterName, strLiteralString); } return(String.Format("{0}{1}", serviceOp, sbParametersString.ToString())); } else if (node is ScanExpression) { ScanExpression e = (ScanExpression)node; if (e.Input is VariableExpression && ((VariableExpression)e.Input).Variable is ResourceContainer) { return(String.Format("{0}", this.Visit(e, e.Input) )); } throw new Exception("Unsupported on in scan expression"); } else if (node is PredicateExpression) { PredicateExpression e = (PredicateExpression)node; KeyExpression key = e.Predicate as KeyExpression; if (key != null) { return(String.Format("{0}({1})", this.Visit(e, e.Input), this.Visit(e, e.Predicate) )); } else { return(String.Format("{0}?$filter={1}", this.Visit(e, e.Input), this.Visit(e, e.Predicate) )); } } else if (node is CountExpression) { CountExpression e = (CountExpression)node; string sCount = ""; string visit = ""; if (!e.IsInline) { visit = this.Visit(e, e.Input); sCount = string.Format("{0}/$count", visit); } else { visit = this.Visit(e, e.Input); if (visit.IndexOf("?$") == -1) { sCount = string.Format("{0}?$inlinecount={1}", visit, e.CountKind); } else { sCount = string.Format("{0}&$inlinecount={1}", visit, e.CountKind); } } return(sCount); } else if (node is NewExpression) { NewExpression e = (NewExpression)node; string uri = this.Visit(e, e.Input); ExpNode[] pe = new ExpNode[] { }; List <ExpNode> nodes = new List <ExpNode>(); foreach (ExpNode parameter in e.Arguments) { nodes.Add(parameter as ExpNode); } pe = nodes.ToArray(); string _select = String.Format("{0}", this.Visit(e, pe[0])); if (nodes.Count > 1) { for (int j = 1; j < nodes.Count; j++) { _select = String.Format("{0},{1}", _select, this.Visit(e, pe[j])); } } if (uri.IndexOf("?$") == -1) { return(string.Format("{0}?$select={1}", uri, _select)); } else { return(string.Format("{0}&$select={1}", uri, _select)); } } else if (node is MemberBindExpression) { return(this.Visit(node, ((MemberBindExpression)node).SourceProperty)); } else if (node is TopExpression) { TopExpression e = (TopExpression)node; string visit = this.Visit(e, e.Input); if (visit.IndexOf("?$") == -1) { return(string.Format("{0}?$top={1}", visit, e.Predicate)); } else { return(string.Format("{0}&$top={1}", visit, e.Predicate)); } } else if (node is SkipExpression) { SkipExpression e = (SkipExpression)node; string visit = this.Visit(e, e.Input); if (visit.IndexOf("?$") == -1) { return(string.Format("{0}?$skip={1}", visit, e.Predicate)); } else { return(string.Format("{0}&$skip={1}", visit, e.Predicate)); } } else if (node is OrderByExpression) { OrderByExpression e = (OrderByExpression)node; string ordervalue = String.Empty; int i = 0; string visit = this.Visit(e, e.Input); if (e.ExcludeFromUri) { return(visit); } string propVisit = this.Visit(e, e.PropertiesExp[0]); switch (e.AscDesc) { case true: if (visit.IndexOf("?$") == -1) { ordervalue = String.Format("{0}?$orderby={1}", visit, propVisit); } else { ordervalue = String.Format("{0}&$orderby={1}", visit, propVisit); } break; case false: if (visit.IndexOf("?$") == -1) { ordervalue = String.Format("{0}?$orderby={1} desc", visit, propVisit); } else { ordervalue = String.Format("{0}&$orderby={1} desc", visit, propVisit); } break; } ; if (e.PropertiesExp.Length > 0) { for (i++; i < e.PropertiesExp.Length; i++) { String nextValue = this.Visit(e, e.PropertiesExp[i]); nextValue = String.Format("{0}", nextValue); switch (e.AscDesc) { case true: ordervalue = String.Format("{0},{1}", ordervalue, nextValue); break; case false: ordervalue = String.Format("{0},{1} desc", ordervalue, nextValue); break; } } } return(ordervalue); } else if (node is ThenByExpression) { ThenByExpression e = (ThenByExpression)node; string ordervalue = String.Empty; int i = 0; string visit = this.Visit(e, e.Input); if (e.ExcludeFromUri) { return(visit); } switch (e.AscDesc) { case true: ordervalue = String.Format("{0},{1}", visit, e.Properties[0].Name); break; case false: ordervalue = String.Format("{0},{1} desc", visit, e.Properties[0].Name); break; } if (e.Properties.Length > 0) { for (i++; i < e.Properties.Length; i++) { String nextValue = e.Properties[i].Name; nextValue = String.Format("{0}", nextValue); switch (e.AscDesc) { case true: ordervalue = String.Format("{0},{1}", ordervalue, nextValue); break; case false: ordervalue = String.Format("{0},{1} desc", ordervalue, nextValue); break; } } } return(ordervalue); } else if (node is ExpandExpression) { ExpandExpression e = (ExpandExpression)node; string uri = this.Visit(e, e.Input); string expand = String.Format("{0}", this.Visit(e, e.PropertiesExp[0])); if (e.Properties.Length > 1) { for (int i = 1; i < e.Properties.Length; i++) { expand = String.Format("{0},{1}", expand, this.Visit(e, e.PropertiesExp[i])); } } if (uri.IndexOf("?$") == -1) { return(string.Format("{0}?$expand={1}", uri, expand)); } else { return(string.Format("{0}&$expand={1}", uri, expand)); } } else if (node is NavigationExpression) { NavigationExpression e = (NavigationExpression)node; if (!e.IsLink) { return(String.Format("{0}/{1}", this.Visit(e, e.Input), this.Visit(e, e.PropertyExp))); } else { return(String.Format("{0}/{1}/$ref", this.Visit(e, e.Input), this.Visit(e, e.PropertyExp))); } } else if (node is KeyExpression) { KeyExpression key = node as KeyExpression; return(CreateKeyString(key)); } else if (node is NestedPropertyExpression) { NestedPropertyExpression e = (NestedPropertyExpression)node; string enitySetname = e.Name; string nestedProperty = ""; foreach (PropertyExpression p in e.PropertyExpressions) { string interim = this.Visit(e, p); //AstoriaTestLog.WriteLine(interim); if (p.ValueOnly) { nestedProperty = String.Format("{0}/{1}/{2},", nestedProperty, enitySetname, interim + "/$value").TrimStart('/'); } else { nestedProperty = String.Format("{0}/{1}/{2},", nestedProperty, enitySetname, interim).TrimStart('/'); } } return(nestedProperty.TrimEnd(',').Replace(",/", ",")); } else if (node is PropertyExpression) { PropertyExpression e = (PropertyExpression)node; if (e.ValueOnly) { return(e.Property.Name + "/$value"); } else { return(e.Property.Name); } } else if (node is VariableExpression) { VariableExpression e = (VariableExpression)node; return(e.Variable.Name); } if (node is LogicalExpression) { LogicalExpression e = (LogicalExpression)node; string left = this.Visit(e, e.Left); string right = null; if (e.Operator != LogicalOperator.Not) { right = this.Visit(e, e.Right); } string logical; switch (e.Operator) { case LogicalOperator.And: logical = string.Format("{0} and {1}", left, right); break; case LogicalOperator.Or: logical = string.Format("{0} or {1}", left, right); break; case LogicalOperator.Not: logical = string.Format("not {0}", left); break; default: throw new Exception("Unhandled Comparison Type: " + e.Operator); } return(logical); } else if (node is ComparisonExpression) { ComparisonExpression e = (ComparisonExpression)node; String left = this.Visit(e, e.Left); String right = this.Visit(e, e.Right); switch (e.Operator) { case ComparisonOperator.Equal: return(String.Format("{0} eq {1}", left, right)); case ComparisonOperator.NotEqual: return(String.Format("{0} ne {1}", left, right)); case ComparisonOperator.GreaterThan: return(String.Format("{0} gt {1}", left, right)); case ComparisonOperator.GreaterThanOrEqual: return(String.Format("{0} ge {1}", left, right)); case ComparisonOperator.LessThan: return(String.Format("{0} lt {1}", left, right)); case ComparisonOperator.LessThanOrEqual: return(String.Format("{0} le {1}", left, right)); default: throw new Exception("Unhandled Comparison Type: " + e.Operator); } ; } else if (node is IsOfExpression || node is CastExpression) { ExpNode target; NodeType targetType; string operation; if (node is IsOfExpression) { IsOfExpression e = (IsOfExpression)node; operation = "isof"; target = e.Target; targetType = e.TargetType; } else { CastExpression e = (CastExpression)node; operation = "cast"; target = e.Target; targetType = e.TargetType; } string targetTypeName = targetType.FullName; if (targetType is PrimitiveType) { targetTypeName = TypeData.FindForType(targetType.ClrType).GetEdmTypeName(); } if (target == null) { return(String.Format("{0}('{1}')", operation, targetTypeName)); } else { return(String.Format("{0}({1}, '{2}')", operation, this.Visit(node, target), targetTypeName)); } } else if (node is ArithmeticExpression) { ArithmeticExpression e = (ArithmeticExpression)node; String left = this.Visit(e, e.Left); String right = this.Visit(e, e.Right); switch (e.Operator) { case ArithmeticOperator.Add: return(String.Format("{0} add {1}", left, right)); case ArithmeticOperator.Div: return(String.Format("{0} div {1}", left, right)); case ArithmeticOperator.Mod: return(String.Format("{0} mod {1}", left, right)); case ArithmeticOperator.Mult: return(String.Format("{0} mul {1}", left, right)); case ArithmeticOperator.Sub: return(String.Format("{0} sub {1}", left, right)); default: throw new Exception("Unhandled Arithmetic Type: " + e.Operator); } ; } else if (node is MethodExpression) { MethodExpression e = (MethodExpression)node; return(BuildMemberOrMethodExpression(node, e.Caller, e.Arguments, e.Name)); } else if (node is MemberExpression) { MemberExpression e = (MemberExpression)node; return(BuildMemberOrMethodExpression(node, e.Caller, e.Arguments, e.Name)); } else if (node is ConstantExpression) { ConstantExpression e = (ConstantExpression)node; object value = e.Value.ClrValue; string val = TypeData.FormatForKey(value, this.UseSmallCasing, false); if (this.EscapeUriValues) { // FormatForKey already does this for doubles that don't have the D if (!(value is double) || Versioning.Server.SupportsV2Features) { val = Uri.EscapeDataString(val); } } if (value == null) { val = "null"; } else if (!(value is String)) { val = val.Replace("+", "").Replace("%2B", ""); } return(val); } else if (node is NegateExpression) { NegateExpression e = (NegateExpression)node; return("-(" + this.Visit(e, e.Argument) + ")"); } else if (node is FirstExpression) { FirstExpression first = node as FirstExpression; return(this.Visit(first, first.Input)); } else if (node is FirstOrDefaultExpression) { FirstOrDefaultExpression first = node as FirstOrDefaultExpression; return(this.Visit(first, first.Input)); } else if (node is SingleExpression) { SingleExpression first = node as SingleExpression; return(this.Visit(first, first.Input)); } else if (node is SingleOrDefaultExpression) { SingleOrDefaultExpression first = node as SingleOrDefaultExpression; return(this.Visit(first, first.Input)); } else { throw new Exception(this.GetType().Name + " Unhandled Node: " + node.GetType().Name); } }