Exemple #1
0
        private CodeFileConverter GetClrTypeCodeConverter(ClrTypeInfo clrTypeInfo)
        {
            var codeType = GetCodeType(clrTypeInfo);

            if (codeType != "enum")
            {
                codeType = $"partial {codeType}";
            }
            var declaration = $"public {codeType} {clrTypeInfo.CSharpName}";

            if (!string.IsNullOrEmpty(clrTypeInfo.BaseTypeName))
            {
                declaration += $" : {clrTypeInfo.BaseTypeName}";
                if (clrTypeInfo.Interfaces.Any())
                {
                    declaration += $", {string.Join(", ", clrTypeInfo.Interfaces)}";
                }
            }
            else if (clrTypeInfo.Interfaces.Any())
            {
                declaration += $" : {string.Join(", ", clrTypeInfo.Interfaces)}";
            }
            var codeFile = new CodeFile(
                fileName: clrTypeInfo.CSharpName,
                relativePath: clrTypeInfo.InitialGeneratedNamespace,
                relativeNamespace: clrTypeInfo.GeneratedNamespace ?? string.Empty,
                declaration: declaration);

            foreach (var usingNamespace in clrTypeInfo.RequiredNamespaces)
            {
                codeFile.UsingNamespaces.Add(usingNamespace);
            }
            TranslateClrTypeToCodeFile(clrTypeInfo, codeFile);
            return(new CodeFileConverter(codeFile));
        }
 public static void AddRequiredNamespaces(this ClrTypeInfo clrTypeInfo, IEnumerable <string> requiredNamespaces)
 {
     foreach (var requiredNamespace in requiredNamespaces)
     {
         clrTypeInfo.RequiredNamespaces.Add(requiredNamespace);
     }
 }
Exemple #3
0
        void DeserializeMissmatchingVersionNumber()
        {
            var info = new ClrTypeInfo();

            info.ClrTypeName = "Lucile.Signed.Test.Model.ClrTypeSample, Lucile.Signed.Test.Model, Version=9.9.9.9, Culture=neutral, PublicKeyToken=d9998064f7e191e3";


            Assert.Equal(typeof(ClrTypeSample), info.ClrType);
        }
 public void AddInterfaceConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
 {
     codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));
     // Api Root has properties and no functions or events
     foreach (var property in clrTypeInfo.Properties)
     {
         codeFile.Properties.Add(new ApiRootInterfacePropertyCodeConverter(property));
     }
 }
Exemple #5
0
        void DeserializeMissmatchingPublicKeyToken()
        {
            var info = new ClrTypeInfo();

            info.ClrTypeName = "Lucile.Signed.Test.Model.ClrTypeSample, Lucile.Signed.Test.Model, PublicKeyToken=b77a5c561934e089";


            Assert.Equal(typeof(ClrTypeSample), info.ClrType);
        }
        public void AddConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            codeFile.Comments.Add(new CommentCodeConverter("Empty Class"));
            codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));

            if (clrTypeInfo.IsObsolete)
            {
                codeFile.Attributes.Add(new AttributeObsoleteCodeConverter(clrTypeInfo.ObsoleteMessage));
            }
        }
        public static ClrTypeInfo MakeNullable(this ClrTypeInfo clrTypeInfo)
        {
            if (clrTypeInfo.IsNullable)
            {
                throw new InvalidOperationException($"Type '{clrTypeInfo.FullName}' is already nullable.");
            }
            var genericClrType = (ClrTypeInfo)clrTypeInfo.Clone();

            genericClrType.CSharpName += "?";
            genericClrType.IsNullable  = true;
            return(genericClrType);
        }
Exemple #8
0
 private static string GetCodeType(ClrTypeInfo clrTypeInfo)
 {
     if (clrTypeInfo.IsInterface)
     {
         return("interface");
     }
     if (clrTypeInfo.IsEnum)
     {
         return("enum");
     }
     return("class");
 }
        public static ClrTypeInfo MakeJsonElement(this ClrTypeInfo clrTypeInfo)
        {
            var type = typeof(JsonElement);
            var jsonElementClrType = (ClrTypeInfo)clrTypeInfo.Clone();

            jsonElementClrType.CSharpName = type.Name;
#pragma warning disable CS8601, CS8604 // Type should not have these properties as null
            jsonElementClrType.Id        = type.FullName;
            jsonElementClrType.Namespace = type.Namespace;
            jsonElementClrType.ReferenceNamespaces.Add(type.Namespace);
#pragma warning restore CS8601, CS8604
            return(jsonElementClrType);
        }
Exemple #10
0
        private void TranslateClrTypeToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            var classType = (ClassType)clrTypeInfo.Metadata[Constants.TypeMetadata.ClassType];
            var factory   = GetFactory(classType);

            if (clrTypeInfo.IsInterface)
            {
                factory.AddInterfaceConvertersToCodeFile(clrTypeInfo, codeFile);
            }
            else
            {
                factory.AddConvertersToCodeFile(clrTypeInfo, codeFile);
            }
        }
Exemple #11
0
        public void AddInterfaceConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));

            foreach (var property in clrTypeInfo.Properties)
            {
                codeFile.Properties.Add(new ApiInterfacePropertyCodeConverter(property));
            }

            foreach (var method in clrTypeInfo.Methods)
            {
                codeFile.Methods.Add(new ApiInterfaceMethodCodeConverter(method));
            }
        }
        public static ClrTypeInfo MakeEnumerableType(this ClrTypeInfo clrTypeInfo)
        {
            var enumerableClrType = (ClrTypeInfo)clrTypeInfo.Clone();

            enumerableClrType.IsGenericType        = true;
            enumerableClrType.IsInterface          = true;
            enumerableClrType.IsNullable           = true;
            enumerableClrType.GenericTypeArguments = new[] { clrTypeInfo };
#pragma warning disable CS8601 // Possible null reference assignment.
            enumerableClrType.FullName = typeof(IEnumerable <>).FullName;
#pragma warning restore CS8601 // Possible null reference assignment.
            enumerableClrType.CSharpName = $"IEnumerable<{clrTypeInfo.CSharpName}>";
            enumerableClrType.ReferenceNamespaces.Add("System.Collections.Generic");
            return(enumerableClrType);
        }
Exemple #13
0
        public void AddConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            codeFile.Comments.Add(new CommentInheritDocCodeConverter());
            codeFile.Constructors.Add(new ApiConstructorCodeConverter(clrTypeInfo.CSharpName, (string)clrTypeInfo.Metadata[Constants.TypeMetadata.ApiNamespace]));

            foreach (var property in clrTypeInfo.Properties)
            {
                codeFile.Properties.Add(new ApiPropertyCodeConverter(property));
            }

            foreach (var method in clrTypeInfo.Methods)
            {
                codeFile.Methods.Add(new ApiMethodCodeConverter(method));
            }
        }
Exemple #14
0
        public void AddConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            if (clrTypeInfo.TypeChoices is null)
            {
                throw new InvalidOperationException("Multitype type must have type choices.");
            }

            codeFile.UsingNamespaces.Add("System.Text.Json.Serialization");
            codeFile.Comments.Add(new CommentCodeConverter("Multitype Class"));
            codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));

            if (clrTypeInfo.IsObsolete)
            {
                codeFile.Attributes.Add(new AttributeObsoleteCodeConverter(clrTypeInfo.ObsoleteMessage));
            }
            codeFile.Attributes.Add(new AttributeCodeConverter($"JsonConverter(typeof(MultiTypeJsonConverter<{clrTypeInfo.CSharpName}>))"));

            codeFile.Constructors.Add(new MultitypeConstructorCodeConverter(clrTypeInfo.CSharpName, clrTypeInfo.TypeChoices));
        }
        public void AddConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            codeFile.UsingNamespaces.Add("System.Text.Json.Serialization");
            codeFile.Comments.Add(new CommentCodeConverter("Combined Callback Parameter Class"));
            codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));

            if (clrTypeInfo.IsObsolete)
            {
                codeFile.Attributes.Add(new AttributeObsoleteCodeConverter(clrTypeInfo.ObsoleteMessage));
            }
            codeFile.Attributes.Add(new AttributeCodeConverter($"JsonConverter(typeof(CombinedCallbackParameterJsonConverter<{clrTypeInfo.CSharpName}>))"));

            codeFile.Constructors.Add(new CombinedCallbackParameterConstructorCodeConverter(clrTypeInfo.CSharpName, clrTypeInfo.Properties));

            foreach (var property in clrTypeInfo.Properties)
            {
                codeFile.Properties.Add(new TypePropertyCodeConverter(property));
            }
        }
        public void AddConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            codeFile.UsingNamespaces.Add("System.Text.Json.Serialization");
            codeFile.Comments.Add(new CommentCodeConverter("String Format Class"));
            codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));

            if (clrTypeInfo.IsObsolete)
            {
                codeFile.Attributes.Add(new AttributeObsoleteCodeConverter(clrTypeInfo.ObsoleteMessage));
            }

            codeFile.Attributes.Add(new AttributeCodeConverter($"JsonConverter(typeof(StringFormatJsonConverter<{clrTypeInfo.CSharpName}>))"));

            var stringFormat  = clrTypeInfo.Metadata.ContainsKey(Constants.TypeMetadata.StringFormat) ? (string)clrTypeInfo.Metadata[Constants.TypeMetadata.StringFormat] : null;
            var stringPattern = clrTypeInfo.Metadata.ContainsKey(Constants.TypeMetadata.StringPattern) ? (string)clrTypeInfo.Metadata[Constants.TypeMetadata.StringPattern] : null;

            codeFile.Constructors.Add(new StringFormatConstructorCodeConverter(clrTypeInfo.CSharpName, stringFormat, stringPattern));

            codeFile.Methods.Add(new StringFormatCastOperatorCodeConverter(clrTypeInfo.CSharpName));
        }
Exemple #17
0
        public void AddConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            if (clrTypeInfo.EnumValues is null)
            {
                throw new InvalidOperationException("Enum type must have enum values.");
            }

            codeFile.UsingNamespaces.Add("System.Text.Json.Serialization");
            codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));

            if (clrTypeInfo.IsObsolete)
            {
                codeFile.Attributes.Add(new AttributeObsoleteCodeConverter(clrTypeInfo.ObsoleteMessage));
            }
            codeFile.Attributes.Add(new AttributeCodeConverter($"JsonConverter(typeof(EnumStringConverter<{clrTypeInfo.CSharpName}>))"));

            foreach (var enumValue in clrTypeInfo.EnumValues)
            {
                codeFile.Properties.Add(new EnumPropertyCodeConverter(enumValue));
            }
        }
        private ClrTypeInfo GetClrTypeFromSystemType(Type type)
        {
#pragma warning disable CS8601, CS8604 // Type should not have these properties as null
            var clrTypeInfo = new ClrTypeInfo()
            {
                Id                        = type.FullName,
                Namespace                 = type.Namespace,
                Name                      = type.Name,
                FullName                  = type.FullName,
                CSharpName                = type.Name,
                IsEnum                    = type.IsEnum,
                EnumValues                = null,
                IsNullable                = type.IsClass,
                IsInterface               = type.IsInterface,
                IsNullType                = type == typeof(void),
                IsGenericType             = type.IsGenericType,
                GenericTypeArguments      = type.GenericTypeArguments.Select(GetClrTypeFromSystemType).ToArray(),
                IsObsolete                = false,
                ObsoleteMessage           = null,
                IsGenerated               = false,
                GeneratedNamespace        = null,
                InitialGeneratedNamespace = null,
                RequiredNamespaces        = new HashSet <string>(),
                ReferenceNamespaces       = new HashSet <string>()
                {
                    type.Namespace
                },
                Interfaces   = new HashSet <string>(),
                BaseTypeName = null,
                Description  = null,
                Metadata     = new Dictionary <string, object>(),
                Methods      = Enumerable.Empty <ClrMethodInfo>(),
                Properties   = Enumerable.Empty <ClrPropertyInfo>(),
                TypeChoices  = null
            };
#pragma warning restore CS8601, CS8604

            if (type.IsGenericType)
            {
                clrTypeInfo.CSharpName = clrTypeInfo.CSharpName[..clrTypeInfo.CSharpName.IndexOf('`')];
        public void AddConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
        {
            codeFile.UsingNamespaces.Add("JsBind.Net");

            codeFile.Comments.Add(new CommentCodeConverter("Type Class"));
            codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));
            codeFile.Comments.Add(new AttributeCodeConverter("BindAllProperties"));

            if (clrTypeInfo.IsObsolete)
            {
                codeFile.Attributes.Add(new AttributeObsoleteCodeConverter(clrTypeInfo.ObsoleteMessage));
            }

            foreach (var property in clrTypeInfo.Properties)
            {
                codeFile.Properties.Add(new TypePropertyCodeConverter(property));
            }

            foreach (var method in clrTypeInfo.Methods)
            {
                codeFile.Methods.Add(new TypeMethodCodeConverter(method));
            }
        }
        public ClrMethodInfo TranslateFunctionDefinition(FunctionDefinition functionDefinition, NamespaceEntity namespaceEntity, ClrTypeInfo clrTypeInfo)
        {
            if (functionDefinition.Name is null)
            {
                throw new InvalidOperationException("Function definition should have a name.");
            }

            var parameterDefinitions = functionDefinition.FunctionParameters?.ToList() ?? new List <ParameterDefinition>();
            var returnDefinition     = GetReturnDefinition(functionDefinition, parameterDefinitions);

            var methodParameters = parameterDefinitions.Select(parameterDefinition =>
            {
                var clrParameterInfo = TranslateParameterDefinition(parameterDefinition, namespaceEntity);
                clrTypeInfo.AddRequiredNamespaces(clrParameterInfo.ParameterType.ReferenceNamespaces);
                return(clrParameterInfo);
            }).ToArray();
            var methodReturnType = GetReturnType(returnDefinition, namespaceEntity);

            if (methodReturnType is not null)
            {
                clrTypeInfo.AddRequiredNamespaces(methodReturnType.ReferenceNamespaces);
            }

            var methodInfo = new ClrMethodInfo()
            {
                Name          = functionDefinition.Name,
                PublicName    = functionDefinition.Name.ToCapitalCase(),
                Description   = functionDefinition.Description,
                DeclaringType = clrTypeInfo,
                Parameters    = methodParameters,
                Return        = new ClrMethodReturnInfo()
                {
                    Description   = returnDefinition?.Description,
                    HasReturnType = methodReturnType is not null,
                    ReturnType    = methodReturnType
                },
        internal void Initialize()
        {
            if (!IsInitialized)
            {
                var config       = HttpConfiguration;
                var asmResolver  = config.Services.GetAssembliesResolver();
                var typeResolver = config.Services.GetHttpControllerTypeResolver();
                var selector     = config.Services.GetHttpControllerSelector();
                var mapping      = selector.GetControllerMapping();

                var keyresolver = config.Services.GetClrTypeKeyResolver();

                var items = new Dictionary <string, ControllerInfo>();
                foreach (var kv in registerClrType)
                {
                    if (mapping.TryGetValue(kv.Key, out HttpControllerDescriptor controller))
                    {
                        var info = new ClrTypeInfo(kv.Value, keyresolver);
                        var con  = new ControllerInfo(info, controller.ControllerType, controller.ControllerName);
                    }
                }
                Controllers = items.AsReadOnly();
            }
        }
 public void AddClrType(ClrTypeInfo clrTypeInfo)
 {
     clrTypeStore.Add(clrTypeInfo.Id, clrTypeInfo);
 }
        public IEnumerable <ClrTypeInfo> ShallowTranslate(ClassEntity classEntity)
        {
            var classEntityName = classEntity.FormattedName;

            if (classTranslationOptions.Aliases.ContainsKey(classEntityName))
            {
                classEntityName = classTranslationOptions.Aliases[classEntityName];
            }

            var namespaceName = classEntity.NamespaceEntity.FullFormattedName;

            if (namespaceName is not null && classTranslationOptions.NamespaceAliases.ContainsKey(namespaceName))
            {
                namespaceName = classTranslationOptions.NamespaceAliases[namespaceName];
            }
            var @namespace = FullyQualifyNamespace(namespaceName);

            var clrTypeInfo = new ClrTypeInfo()
            {
                Id                        = $"{FullyQualifyNamespace(classEntity.NamespaceEntity.FullFormattedName)}.{classEntity.FormattedName}",
                Namespace                 = @namespace,
                Name                      = classEntity.Name,
                FullName                  = $"{@namespace}.{classEntity.FormattedName}",
                CSharpName                = classEntityName,
                IsNullable                = classEntity.Type != ClassType.EnumClass,
                IsEnum                    = classEntity.Type == ClassType.EnumClass,
                EnumValues                = classEntity.Type == ClassType.EnumClass ? classEntity.Properties.Select(EnumPropertyDefinitionTranslator.TranslatePropertyDefinition).ToArray() : null,
                IsInterface               = false,
                IsNullType                = false,
                IsGenericType             = false,
                GenericTypeArguments      = Enumerable.Empty <ClrTypeInfo>(),
                IsObsolete                = classEntity.TypeDefinition.IsDeprecated,
                ObsoleteMessage           = classEntity.TypeDefinition.Deprecated,
                IsGenerated               = true,
                GeneratedNamespace        = namespaceName,
                InitialGeneratedNamespace = classEntity.NamespaceEntity.FullFormattedName,
                RequiredNamespaces        = new HashSet <string>(),
                ReferenceNamespaces       = new HashSet <string>()
                {
                    @namespace
                },
                Interfaces   = new HashSet <string>(),
                BaseTypeName = classEntity.BaseClassName,
                Description  = classEntity.Description,
                Metadata     = new Dictionary <string, object>()
                {
                    { Constants.TypeMetadata.ClassType, classEntity.Type },
                    { Constants.TypeMetadata.ApiNamespace, classEntity.NamespaceEntity.Name }
                }
            };

            if (classEntity.ImplementInterface)
            {
                var interfaceTypeName = $"I{classEntity.FormattedName}";
                clrTypeInfo.Interfaces.Add(interfaceTypeName);
                var interfaceClrTypeInfo = (ClrTypeInfo)clrTypeInfo.Clone();
                interfaceClrTypeInfo.Id           = $"{@namespace}.{interfaceTypeName}";
                interfaceClrTypeInfo.Name         = interfaceTypeName;
                interfaceClrTypeInfo.FullName     = $"{@namespace}.{interfaceTypeName}";
                interfaceClrTypeInfo.CSharpName   = $"I{classEntityName}";
                interfaceClrTypeInfo.IsInterface  = true;
                interfaceClrTypeInfo.Interfaces   = new HashSet <string>();
                interfaceClrTypeInfo.BaseTypeName = null;

                clrTypeStore.AddClrType(interfaceClrTypeInfo);
                yield return(interfaceClrTypeInfo);
            }

            clrTypeStore.AddClrType(clrTypeInfo);
            yield return(clrTypeInfo);
        }
 public void AddInterfaceConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
 {
     throw new NotImplementedException();
 }
Exemple #25
0
 public void TestMehtod2()
 {
     var resolver = new DefalutClrTypeKeyResolver();
     var info     = new ClrTypeInfo(typeof(Customer), resolver);
     var con      = new ControllerInfo(info, typeof(CustomersController), "Customers");
 }
 public void DeepTranslate(ClassEntity classEntity, ClrTypeInfo clrTypeInfo)
 {
     if (clrTypeInfo.BaseTypeName is not null && clrTypeInfo.BaseTypeName.Contains('.'))
     {
         var namespaceSeparatorIndex = clrTypeInfo.BaseTypeName.LastIndexOf('.');
         clrTypeInfo.RequiredNamespaces.Add(clrTypeInfo.BaseTypeName[..namespaceSeparatorIndex]);
Exemple #27
0
        public ClrPropertyInfo TranslatePropertyDefinition(string propertyName, PropertyDefinition propertyDefinition, NamespaceEntity namespaceEntity, ClrTypeInfo clrTypeInfo)
        {
            var propertyType = clrTypeStore.GetClrType(propertyDefinition, namespaceEntity);

            if (clrTypeInfo.Metadata.TryGetValue(Constants.TypeMetadata.ClassType, out var classType) &&
                (ClassType)classType == ClassType.CombinedCallbackParameterClass &&
                propertyType.FullName == typeof(object).FullName)
            {
                propertyType = propertyType.MakeJsonElement();
            }

            if (propertyDefinition.IsOptional && !propertyType.IsNullable)
            {
                propertyType = propertyType.MakeNullable();
            }

            clrTypeInfo.AddRequiredNamespaces(propertyType.ReferenceNamespaces);

            if (propertyName.Equals(clrTypeInfo.CSharpName, StringComparison.OrdinalIgnoreCase))
            {
                // Property name cannot be the same as declaring type, prefer to change the type name instead of the property name
                clrTypeInfo.CSharpName = $"{clrTypeInfo.CSharpName}Type";
            }

            return(new ClrPropertyInfo()
            {
                Name = propertyName,
                PrivateName = propertyName.ToCamelCase(),
                PublicName = propertyName.ToCapitalCase(),
                Description = propertyDefinition.Description,
                DeclaringType = clrTypeInfo,
                PropertyType = propertyType,
                IsConstant = propertyDefinition.IsConstant,
                ConstantValue = propertyDefinition.ConstantValue,
                IsObsolete = propertyDefinition.IsDeprecated,
                ObsoleteMessage = propertyDefinition.Deprecated
            });
        }
Exemple #28
0
        void SerializeName()
        {
            var info = new ClrTypeInfo(typeof(ClrTypeSample));

            Assert.Equal("Lucile.Signed.Test.Model.ClrTypeSample, Lucile.Signed.Test.Model", info.ClrTypeName);
        }