/// <summary> /// Writes the CreateResource and SetValue calls to create the given instance, recursing where necessary /// </summary> /// <param name="variableName">Name of variable to code-gen</param> /// <param name="instance">Entity/complex type to generate code for</param> /// <returns>generated lines of code</returns> private IEnumerable<string> WriteObject(string variableName, ComplexResourceInstance instance) { // determine the set and type for the CreateResource call // string typeName = workspace.ContextNamespace + "." + instance.TypeName; string setName; if (instance is KeyedResourceInstance) setName = '"' + (instance as KeyedResourceInstance).ResourceSetName + '"'; else setName = "null"; // return the CreateResource line // yield return "object " + variableName + " = updatable.CreateResource(" + setName + ", \"" + typeName + "\");"; // if its an entity, and not a complex type, then generate the key properties as well // if (instance is KeyedResourceInstance) { foreach (ResourceInstanceSimpleProperty property in (instance as KeyedResourceInstance).KeyProperties.OfType<ResourceInstanceSimpleProperty>()) { yield return "updatable.SetValue(" + variableName + ", \"" + property.Name + "\", " + ConvertToCode((property as ResourceInstanceSimpleProperty).PropertyValue) + ");"; } } // generate the SetValue calls for each property foreach (ResourceInstanceProperty property in instance.Properties) { // based on the type of property, generate the appropriate code // if (property is ResourceInstanceNavProperty) { // not supported right now, this is non-trivial throw new NotImplementedException(); } else if (property is ResourceInstanceComplexProperty) { // generate an instance of this property's value for passing to SetValue // string subVariableName = variableName + "_" + property.Name; // recurse foreach (string line in WriteObject(subVariableName, (property as ResourceInstanceComplexProperty).ComplexResourceInstance)) yield return line; // return the SetValue call // yield return "updatable.SetValue(" + variableName + ", \"" + property.Name + "\", " + subVariableName + ");"; } else if (property is ResourceInstanceSimpleProperty) { // return the SetValue call // yield return "updatable.SetValue(" + variableName + ", \"" + property.Name + "\", " + ConvertToCode((property as ResourceInstanceSimpleProperty).PropertyValue) + ");"; } else { // should never hit this throw new NotSupportedException(); } } }
private static ResourceInstanceComplexProperty ConvertComplexPayloadObjectToComplexProperty(ResourceProperty rp, PayloadComplexProperty payloadComplexProperty) { List<ResourceInstanceProperty> properties = new List<ResourceInstanceProperty>(); foreach (ResourceProperty childProperty in (rp.Type as ComplexType).Properties.OfType<ResourceProperty>()) { if (childProperty.IsComplexType) { PayloadComplexProperty fromPayload = payloadComplexProperty.PayloadProperties[childProperty.Name] as PayloadComplexProperty; properties.Add(ConvertComplexPayloadObjectToComplexProperty(childProperty, fromPayload)); } else { PayloadSimpleProperty fromPayload = payloadComplexProperty.PayloadProperties[childProperty.Name] as PayloadSimpleProperty; if (fromPayload != null) { ResourceInstanceProperty newProperty = null; object val = CommonPayload.DeserializeStringToObject(fromPayload.Value, childProperty.Type.ClrType, false, fromPayload.ParentObject.Format); newProperty = new ResourceInstanceSimpleProperty(childProperty.Name, new NodeValue(val, childProperty.Type)); properties.Add(newProperty); } } } ComplexResourceInstance complexResourceInstance = new ComplexResourceInstance(rp.Type.Name, properties.ToArray()); return new ResourceInstanceComplexProperty(rp.Type.Name, rp.Name, complexResourceInstance); }
private void AddProperties(ComplexResourceInstance updateTree, RowComplexType newResource) { foreach (ResourceInstanceProperty property in updateTree.Properties) { if (property is ResourceInstanceSimpleProperty) { ResourceInstanceSimpleProperty tempProperty = (ResourceInstanceSimpleProperty)property; newResource.Properties.Add(tempProperty.Name, tempProperty.PropertyValue); } else if (property is ResourceInstanceComplexProperty) { ResourceInstanceComplexProperty tempProperty = (ResourceInstanceComplexProperty)property; RowComplexType newComplexType = new RowComplexType(tempProperty.TypeName); AddProperties(tempProperty.ComplexResourceInstance, newComplexType); newResource.Properties.Add(tempProperty.Name, newComplexType); } else if (property is ResourceInstanceNavRefProperty) { ResourceInstanceNavRefProperty tempProperty = (ResourceInstanceNavRefProperty)property; RowEntityType nestedResource = FindRowInstance((KeyedResourceInstance)tempProperty.TreeNode); if (nestedResource == null) nestedResource = AddNewEntity((KeyedResourceInstance)tempProperty.TreeNode, null); newResource.Properties.Add(tempProperty.Name, nestedResource); } else if (property is ResourceInstanceNavColProperty) { ResourceInstanceNavColProperty tempProperty = (ResourceInstanceNavColProperty)property; List<RowEntityType> entityList = new List<RowEntityType>(); foreach (ResourceBodyTree entity in tempProperty.Collection.NodeList) { RowEntityType nestedResource = FindRowInstance((KeyedResourceInstance)entity); if (nestedResource == null) nestedResource = AddNewEntity((KeyedResourceInstance)entity, null); entityList.Add(nestedResource); } newResource.Properties.Add(tempProperty.Name, entityList); } else throw new TestException(TestResult.Failed, "NonClr - Unhandled property type."); } }
public static ComplexResourceInstance CreateComplexResourceInstance(ComplexType type) { List<ResourceInstanceProperty> instanceProperties = new List<ResourceInstanceProperty>(); foreach (ResourceProperty childProperty in type.Properties) { ResourceInstanceProperty resourceInstanceProperty = null; if (childProperty.IsComplexType) { resourceInstanceProperty = new ResourceInstanceComplexProperty(childProperty.Type.Name, childProperty.Name, CreateComplexResourceInstance((ComplexType)childProperty.Type)); } else { NodeValue nodeValue = Resource.CreateValue(childProperty); resourceInstanceProperty = new ResourceInstanceSimpleProperty(childProperty.Name, nodeValue); } instanceProperties.Add(resourceInstanceProperty); } ComplexResourceInstance complexInstance = new ComplexResourceInstance(type.Name, instanceProperties.ToArray()); return complexInstance; }
private void ModifyProperties(ComplexResourceInstance updateTree, RowComplexType newResource, bool replace) { foreach (ResourceInstanceProperty property in updateTree.Properties) { if (property is ResourceInstanceSimpleProperty) { ResourceInstanceSimpleProperty tempProperty = (ResourceInstanceSimpleProperty)property; newResource.Properties[tempProperty.Name] = tempProperty.PropertyValue; } else if (property is ResourceInstanceComplexProperty) { ResourceInstanceComplexProperty tempProperty = (ResourceInstanceComplexProperty)property; RowComplexType newComplexType = new RowComplexType(tempProperty.TypeName); ModifyProperties(tempProperty.ComplexResourceInstance, newComplexType, replace); newResource.Properties[tempProperty.Name] = newComplexType; } } if (replace) { string[] keys = newResource.Properties.Keys.ToArray(); foreach (string key in keys) { if (updateTree.Properties.Where(p => p.Name == key).Count() == 0) { if (updateTree is KeyedResourceInstance) { KeyedResourceInstance keyedResource = (KeyedResourceInstance)updateTree; if (keyedResource.KeyProperties.Where(p => p.Name == key).Count() == 0) newResource.Properties[key] = null; } else newResource.Properties[key] = null; } } } }