コード例 #1
0
ファイル: ObjectParser.cs プロジェクト: fmj/raml-dotnet-tools
        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>()));
        }
コード例 #2
0
ファイル: ObjectParser.cs プロジェクト: fmj/raml-dotnet-tools
        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);
        }