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); }
private ApiObject HandleTypeDuplication(Guid id, ApiObject apiObj, string key, IDictionary <Guid, string> linkedType) { var match = UniquenessHelper.FirstOrDefaultWithSameProperties(apiObj, existingObjects, key, newObjects, emptyDic); if (match != null) { if (!linkedType.ContainsKey(id)) { linkedType.Add(id, match.Type); } } else { apiObj.Type = UniquenessHelper.GetUniqueName(existingObjects, apiObj.Type, newObjects, emptyDic); } return(match); }
private ApiObject HandleNameDuplication(Guid id, ApiObject apiObj, string key, IDictionary <Guid, string> linkedType) { var match = UniquenessHelper.FirstOrDefaultWithSameProperties(apiObj, existingObjects, key, newObjects, emptyDic); if (match != null) { linkedType.Add(id, match.Type); } else { apiObj.Name = UniquenessHelper.GetUniqueName(existingObjects, apiObj.Name, newObjects, emptyDic); foreach (var prop in apiObj.Properties) { prop.ParentClassName = apiObj.Name; } } return(match); }
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 static KeyValuePair <string, ApiObject> HandleDuplicatedObjectName(KeyValuePair <string, ApiObject> newElement, IDictionary <string, ApiObject> objects) { var apiObject = newElement.Value; 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.TypeId == apiObject.Id) { prop.Type = apiObject.Name; } } } return(newElement); }
private Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> > ParseEnum(Guid id, IDictionary <string, string> warnings, IDictionary <string, ApiEnum> existingEnums, ScalarShape scalar) { if (existingEnums.ContainsKey(scalar.Id)) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, new Dictionary <Guid, string>())); } if (newEnums.ContainsKey(scalar.Id)) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, new Dictionary <Guid, string>())); } var apiEnum = CreateEnum(scalar, existingEnums, warnings, newEnums); apiEnum.Id = id; apiEnum.AmfId = scalar.Id; if (newEnums.ContainsKey(apiEnum.AmfId) || existingEnums.ContainsKey(apiEnum.AmfId)) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, new Dictionary <Guid, string>())); } if (existingEnums.Values.Any(o => o.Name == apiEnum.Name) || newEnums.Values.Any(o => o.Name == apiEnum.Name)) { if (UniquenessHelper.HasSamevalues(apiEnum, existingEnums, newEnums)) { return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, new Dictionary <Guid, string>())); } apiEnum.Name = UniquenessHelper.GetUniqueName(apiEnum.Name, existingEnums, newEnums); } newEnums.Add(apiEnum.AmfId, apiEnum); return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, new Dictionary <Guid, string>())); }
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 Property MapProperty(PropertyShape p, string parentClassName) { var name = GetPropertyName(p); var prop = new Property(parentClassName) { Name = NetNamingMapper.GetPropertyName(name), Required = p.Required, Type = NetNamingMapper.GetObjectName(name), InheritanceProvenance = p.InheritanceProvenance }; if (p.Range == null) { return(prop); } prop.OriginalName = p.Range.Name; prop.AmfId = p.Range.Id; prop.Description = p.Range.Description; prop.Example = MapExample(p.Range); prop.Type = NewNetTypeMapper.GetNetType(p.Range, existingObjects, newObjects, existingEnums, newEnums); if (p.Range is ScalarShape scalar) { prop.Pattern = scalar.Pattern; prop.MaxLength = scalar.MaxLength; prop.MinLength = scalar.MinLength; prop.Maximum = string.IsNullOrWhiteSpace(scalar.Maximum) ? (double?)null : Convert.ToDouble(scalar.Maximum); prop.Minimum = string.IsNullOrWhiteSpace(scalar.Minimum) ? (double?)null : Convert.ToDouble(scalar.Minimum); if (scalar.Values != null && scalar.Values.Any()) { prop.IsEnum = true; var apiEnum = CreateEnum(scalar, existingEnums, warnings, newEnums); string amfId = apiEnum.AmfId; if (string.IsNullOrWhiteSpace(amfId)) { amfId = prop.AmfId ?? prop.Name; } prop.Type = apiEnum.Name; if (newEnums.ContainsKey(amfId)) { return(prop); } if (existingEnums.Values.Any(o => o.Name == apiEnum.Name) || newEnums.Values.Any(o => o.Name == apiEnum.Name)) { if (UniquenessHelper.HasSamevalues(apiEnum, existingEnums, newEnums)) { return(prop); } apiEnum.Name = UniquenessHelper.GetUniqueName(apiEnum.Name, existingEnums, newEnums); prop.Type = apiEnum.Name; } newEnums.Add(amfId, apiEnum); } return(prop); } //if (existingObjects.ContainsKey(prop.Type) || newObjects.ContainsKey(prop.Type)) // return prop; if (p.Range is NodeShape) { if (existingObjects.ContainsKey(p.Range.Id)) { prop.Type = existingObjects[p.Range.Id].Type; return(prop); } if (newObjects.ContainsKey(p.Range.Id)) { prop.Type = newObjects[p.Range.Id].Type; return(prop); } var id = Guid.NewGuid(); prop.TypeId = id; var tuple = ParseObject(id, prop.Name, p.Range, existingObjects, warnings, existingEnums); prop.Type = NetNamingMapper.GetObjectName(prop.Name); } if (p.Range is ArrayShape array) { if (array.Items is ScalarShape) { return(prop); } if (!string.IsNullOrWhiteSpace(array.Items.LinkTargetName)) { return(prop); } var itemType = NewNetTypeMapper.GetNetType(array.Items, existingObjects, newObjects, existingEnums, newEnums); GetCollectionType(prop, itemType); if (array.Items is NodeShape && itemType == "Items") { itemType = NetNamingMapper.GetObjectName(array.Name); prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(array.Name)); } if (existingObjects.ContainsKey(array.Id)) { return(prop); } if (array.Items is ScalarShape || array.Items.GetType() == typeof(AnyShape)) { return(prop); } var newId = Guid.NewGuid(); prop.TypeId = newId; ParseObject(newId, array.Name, array.Items, existingObjects, warnings, existingEnums); } foreach (var parent in p.Range.Inherits) { if (!(parent is ScalarShape) && !NewNetTypeMapper.IsPrimitiveType(prop.Type) && !(CollectionTypeHelper.IsCollection(prop.Type) && NewNetTypeMapper.IsPrimitiveType(CollectionTypeHelper.GetBaseType(prop.Type))) && string.IsNullOrWhiteSpace(parent.LinkTargetName)) { var newId = Guid.NewGuid(); ParseObject(newId, prop.Name, parent, existingObjects, warnings, existingEnums); } } return(prop); }
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)); }