private void AddObjectToObjectCollectionOrLink(ApiObject obj, string key, IDictionary <string, ApiObject> objects, IDictionary <string, ApiObject> otherObjects) { if (obj == null || (!obj.Properties.Any() && obj.Type == null)) { return; } if (schemaObjects.All(o => o.Value.Name != obj.Name) && objects.All(o => o.Value.Name != obj.Name) && otherObjects.All(o => o.Value.Name != obj.Name)) { objects.Add(key, obj); } else { if (UniquenessHelper.HasSameProperties(obj, objects, key, otherObjects, schemaObjects)) { if (string.IsNullOrWhiteSpace(obj.GeneratedCode) && !linkKeysWithObjectNames.ContainsKey(key)) { linkKeysWithObjectNames.Add(key, obj.Name); } } else if (!objects.ContainsKey(key)) { obj.Name = UniquenessHelper.GetUniqueName(objects, obj.Name, schemaObjects, schemaObjects); objects.Add(key, obj); } } }
protected void AddElement(KeyValuePair <string, ApiObject> newElement, IDictionary <string, ApiObject> objects) { if (objects.ContainsKey(newElement.Key)) { if (UniquenessHelper.HasSameProperties(objects[newElement.Key], objects, newElement.Key, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>())) { return; } var apiObject = objects[newElement.Key]; var oldName = apiObject.Name; apiObject.Name = UniquenessHelper.GetUniqueName(objects, apiObject.Name, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>()); var newKey = UniquenessHelper.GetUniqueKey(objects, newElement.Key, new Dictionary <string, ApiObject>()); objects.Add(newKey, apiObject); objects.Remove(objects.First(o => o.Key == newElement.Key)); foreach (var apiObj in objects) { foreach (var prop in apiObj.Value.Properties) { if (prop.Type == oldName) { prop.Type = apiObject.Name; } } } } objects.Add(newElement.Key, newElement.Value); }
protected void AddElement(KeyValuePair <string, ApiObject> newElement, IDictionary <string, ApiObject> objects) { if (objects.ContainsKey(newElement.Key)) { return; } if (objects.Values.Any(o => o.Name == newElement.Value.Name)) { if (UniquenessHelper.HasSameProperties(newElement.Value, objects, newElement.Key, emptyDic, emptyDic)) { return; } newElement = HandleDuplicatedObjectName(newElement, objects); } objects.Add(newElement.Key, newElement.Value); }
private Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> > ParseObject(Guid id, string key, Shape shape, IDictionary <string, ApiObject> existingObjects, bool isRootType) { if (AlreadyExists(existingObjects, existingEnums, shape.Id)) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> > (newObjects, newEnums, new Dictionary <Guid, string>())); } // if is array of scalar or array of object there's no new object, except when is a root type if (!isRootType && shape is ArrayShape array && (array.Items is ScalarShape || array.Items is AnyShape)) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> > (newObjects, newEnums, new Dictionary <Guid, string>())); } var linkedType = new Dictionary <Guid, string>(); var apiObj = new ApiObject { Id = id, AmfId = shape.Id, BaseClass = GetBaseClass(shape), IsArray = shape is ArrayShape, IsScalar = shape is ScalarShape, IsUnionType = shape is UnionShape, Name = NetNamingMapper.GetObjectName(key), Type = NetNamingMapper.GetObjectName(key), Description = shape.Description, Example = MapExample(shape) }; if (isRootType && apiObj.IsScalar) { apiObj.Type = NewNetTypeMapper.GetNetType(shape, existingObjects); } if (isRootType && apiObj.IsArray && shape is ArrayShape arrShape) { var itemShape = arrShape.Items; if (itemShape is NodeShape) { var itemId = Guid.NewGuid(); ParseObject(itemId, itemShape.Name, itemShape, existingObjects, false); apiObj.Type = NewNetTypeMapper.GetNetType(shape, existingObjects); } else { //if (newObjects.ContainsKey(itemShape.Id)) // apiObj.Type = CollectionTypeHelper.GetCollectionType(newObjects[itemShape.Id].Type); //else if(existingObjects.ContainsKey(itemShape.Id)) // apiObj.Type = CollectionTypeHelper.GetCollectionType(existingObjects[itemShape.Id].Type); //else apiObj.Type = NewNetTypeMapper.GetNetType(shape, existingObjects); } } if (shape is NodeShape node && node.Properties.Count() == 1 && node.Properties.First().Path != null && node.Properties.First().Path.StartsWith("/") && node.Properties.First().Path.EndsWith("/")) { apiObj.IsMap = true; var valueType = "object"; if (node.Properties.First().Range != null) { valueType = NewNetTypeMapper.GetNetType(node.Properties.First().Range, existingObjects); } apiObj.Type = $"Dictionary<string, {valueType}>"; AddNewObject(shape, apiObj); return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType)); } apiObj.Properties = MapProperties(shape, apiObj.Name).ToList(); ApiObject match = null; if (existingObjects.Values.Any(o => o.Name == apiObj.Name) || newObjects.Values.Any(o => o.Name == apiObj.Name)) { if (UniquenessHelper.HasSameProperties(apiObj, existingObjects, key, newObjects, emptyDic)) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType)); } match = HandleNameDuplication(id, apiObj, key, linkedType); } if (existingObjects.Values.Any(o => o.Type == apiObj.Type) || newObjects.Values.Any(o => o.Type == apiObj.Type)) { if (UniquenessHelper.HasSameProperties(apiObj, existingObjects, key, newObjects, emptyDic)) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType)); } match = HandleTypeDuplication(id, apiObj, key, linkedType); } if (match != null) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType)); } //if (match == null) //{ // match = UniquenessHelper.FirstOrDefaultWithSameProperties(apiObj, existingObjects, key, newObjects, emptyDic); // if (match != null) // { // if (!linkedType.ContainsKey(id)) // linkedType.Add(id, match.Type); // } //} if (shape.Inherits != null && shape.Inherits.Count() == 1) { var baseClass = NewNetTypeMapper.GetNetType(shape.Inherits.First(), existingObjects, newObjects, existingEnums, newEnums); if (!string.IsNullOrWhiteSpace(baseClass)) { apiObj.BaseClass = CollectionTypeHelper.GetConcreteType(baseClass); } } AddNewObject(shape, apiObj); return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType)); }
private Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> > ParseObject(string key, Shape shape, IDictionary <string, ApiObject> existingObjects, bool isRootType) { var apiObj = new ApiObject { BaseClass = GetBaseClass(shape), IsArray = shape is ArrayShape, IsScalar = shape is ScalarShape, IsUnionType = shape is UnionShape, Name = NetNamingMapper.GetObjectName(key), Type = NetNamingMapper.GetObjectName(key), Description = shape.Description, Example = MapExample(shape) }; if (isRootType && (apiObj.IsArray || apiObj.IsScalar)) { apiObj.Type = NewNetTypeMapper.GetNetType(shape, existingObjects); } if (shape is NodeShape node && node.Properties.Count() == 1 && node.Properties.First().Path.StartsWith("/") && node.Properties.First().Path.EndsWith("/")) { apiObj.IsMap = true; var valueType = "object"; if (node.Properties.First().Range != null) { valueType = NewNetTypeMapper.GetNetType(node.Properties.First().Range, existingObjects); } apiObj.Type = $"Dictionary<string, {valueType}>"; new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> >(newObjects, newEnums); } apiObj.Properties = MapProperties(shape, apiObj.Name).ToList(); if (existingObjects.Values.Any(o => o.Name == apiObj.Name)) { if (UniquenessHelper.HasSameProperties(apiObj, existingObjects, key, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>())) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> >(newObjects, newEnums)); } apiObj.Name = UniquenessHelper.GetUniqueName(existingObjects, apiObj.Name, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>()); foreach (var prop in apiObj.Properties) { prop.ParentClassName = apiObj.Name; } } if (existingObjects.Values.Any(o => o.Type == apiObj.Type)) { if (UniquenessHelper.HasSameProperties(apiObj, existingObjects, key, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>())) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> >(newObjects, newEnums)); } apiObj.Type = UniquenessHelper.GetUniqueName(existingObjects, apiObj.Type, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>()); } if (!newObjects.ContainsKey(apiObj.Type)) { newObjects.Add(apiObj.Type, apiObj); } return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> >(newObjects, newEnums)); }