Example #1
0
        private Dictionary <string, Metadata.Property> CreateEntityProperties(Type entityType, HashSet <Assembly> assemblies)
        {
            var propMetas = new Dictionary <string, Metadata.Property>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var prop in entityType
                     .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)

                     // Virtual properties are skipped, they are used for lazy loading which is not supported for now
                     // Ignore-properties are skipped, they are meant for internal/database usage
                     .Where(it => !it.GetGetMethod().IsVirtual&& !it.IsDefined(typeof(IgnoreAttribute)))
                     )
            {
                string propName = ExternalNameAttribute.GetName(prop);
                if (ServiceVersionAttribute.CheckVersion(prop, version, ref propName))
                {
                    var propMeta = new Metadata.Property
                    {
                        Name         = propName.ToCamelCase(),
                        PropertyType = ConvertType(prop.PropertyType, assemblies),
                        PropertyInfo = prop,
                        IsRequired   = prop.GetCustomAttribute <RequiredAttribute>() != null,
                        IsOptional   = prop.GetCustomAttribute <OptionalAttribute>() != null,
                        MaxLength    = prop.GetCustomAttribute <StringLengthAttribute>()?.MaximumLength
                    };
                    propMetas.Add(propMeta.Name, propMeta);
                }
            }
            return(propMetas);
        }
Example #2
0
        private Metadata.Command BuildCommand(string prefix, Metadata.Service service, MethodInfo command, HashSet <Assembly> assemblies)
        {
            string commandName = ExternalNameAttribute.GetName(command);

            if (ServiceVersionAttribute.CheckVersion(command, version, ref commandName))
            {
                var  parMetas  = new List <Metadata.Parameter>();
                bool isHttpRaw = false;
                foreach (var par in command.GetParameters())
                {
                    if (par.ParameterType == typeof(Microsoft.AspNetCore.Http.HttpContext))
                    {
                        isHttpRaw = true;
                        var parMeta = new Metadata.Parameter
                        {
                            IsOptional         = par.IsOptional,
                            IsPlatformSpecific = true,
                            ParameterType      = new Metadata.TypeInfo {
                                Type = par.ParameterType
                            },
                            ParameterInfo = par
                        };
                        parMetas.Add(parMeta);
                    }
                    else
                    {
                        string parName = ExternalNameAttribute.GetName(par);
                        var    parMeta = new Metadata.Parameter
                        {
                            Name          = parName,
                            IsOptional    = par.IsOptional,
                            ParameterType = ConvertType(par.ParameterType, assemblies),
                            ParameterInfo = par
                        };
                        parMetas.Add(parMeta);
                    }
                }
                var commandMeta = new Metadata.Command
                {
                    Name       = commandName.ToCamelCase(),
                    Fullname   = prefix + commandName.ToLower(),
                    Parameters = parMetas,
                    ReturnType = ConvertType(command.ReturnType, assemblies),
                    MethodInfo = command,
                    Service    = service,
                    IsQuery    = command.IsDefined(typeof(QueryAttribute)),
                    IsHttpRaw  = isHttpRaw
                };
                return(commandMeta);
            }
            return(null);
        }
Example #3
0
        private Metadata.Entity CreateEntityMeta(Type entityType, HashSet <Assembly> assemblies)
        {
            string entityName = ExternalNameAttribute.GetName(entityType);

            if (ServiceVersionAttribute.CheckVersion(entityType, version, ref entityName))
            {
                Metadata.Entity parentEntity = null;
                if (entityType.BaseType != null && entityType.BaseType.IsClass && entityType.BaseType != typeof(Object))
                {
                    parentEntity = GetOrAddEntityMeta(entityType.BaseType, assemblies);
                    if (entities.TryGetValue(entityType, out Metadata.Entity existingEntity))
                    {
                        return(existingEntity);
                    }
                }
                var entity = new Metadata.Entity
                {
                    Name         = entityName,
                    Properties   = CreateEntityProperties(entityType, assemblies),
                    Parent       = parentEntity,
                    ParentEntity = parentEntity?.Name,
                    Type         = entityType
                };
                entities.Add(entityType, entity);

                var derivedTypes = assemblies.SelectMany(it => it.GetTypes().Where(tp => entityType.IsAssignableFrom(tp) && !tp.IsAbstract && tp.IsPublic));
                foreach (var derivedType in derivedTypes)
                {
                    GetOrAddEntityMeta(derivedType, assemblies);
                }

                return(entity);
            }
            else
            {
                throw new MetadataBuilderException($"Entity {entityName} not available to version {version}, but still refered to");
            }
        }
Example #4
0
        private Metadata.Service BuildService(Type service, string prefix, HashSet <Assembly> assemblies)
        {
            string serviceName = ExternalNameAttribute.GetName(service);

            if (ServiceVersionAttribute.CheckVersion(service, version, ref serviceName))
            {
                var commandMetas = new Dictionary <string, Metadata.Command>(StringComparer.InvariantCultureIgnoreCase);
                var serviceMeta  = new Metadata.Service {
                    Name = serviceName, Type = service
                };
                foreach (var command in service.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
                {
                    var commandMeta = BuildCommand($"{prefix}.v{version}.{serviceName}.".ToLower(), serviceMeta, command, assemblies);
                    if (commandMeta != null)
                    {
                        commandMetas.Add(commandMeta.Name, commandMeta);
                    }
                }
                serviceMeta.Commands = commandMetas;
                return(serviceMeta);
            }
            return(null);
        }
Example #5
0
 private Metadata.Enum GetOrAddEnumMeta(Type enumType)
 {
     if (!enums.TryGetValue(enumType, out Metadata.Enum metaEnum))
     {
         string enumName = ExternalNameAttribute.GetName(enumType);
         if (ServiceVersionAttribute.CheckVersion(enumType, version, ref enumName))
         {
             var enumValues = new Dictionary <string, Metadata.EnumValue>(StringComparer.InvariantCultureIgnoreCase);
             foreach (var field in enumType.GetFields())
             {
                 if (field.IsLiteral)
                 {
                     int    value     = (int)field.GetValue(null);
                     string fieldName = ExternalNameAttribute.GetName(field);
                     if (ServiceVersionAttribute.CheckVersion(field, version, ref fieldName))
                     {
                         var enumValue = new Metadata.EnumValue
                         {
                             Name      = fieldName,
                             Value     = value,
                             FieldInfo = field
                         };
                         enumValues.Add(enumValue.Name, enumValue);
                     }
                 }
             }
             metaEnum = new Metadata.Enum
             {
                 Name   = enumName,
                 Values = enumValues,
                 Type   = enumType
             };
             enums.Add(enumType, metaEnum);
         }
     }
     return(metaEnum);
 }