Ejemplo n.º 1
0
        private string GetTypeName(Type type)
        {
            if (type.IsEnum)
            {
                return(type.Name.Pascal());
            }

            var primitive = PrimitiveType.GetPrimitiveType(type);

            if (primitive != null)
            {
                return(primitive.Name);
            }

            var attr = type.GetCustomAttribute <GenerateMessageAttribute>(true);

            return(attr?.Name ?? (type.Name.Pascal() + "Dto"));
        }
Ejemplo n.º 2
0
        private void GenerateMessage(Type type, CodeWriter codeWriter, HashSet <Type> generated, HashSet <Type> visited)
        {
            var attr = type.GetCustomAttribute <GenerateMessageAttribute>(true);

            if (attr == null || generated.Contains(type))
            {
                return;
            }

            generated.Add(type);
            visited.Add(type);

            codeWriter.Append($"message { GetTypeName(type) } {{");
            codeWriter.AppendLine();

            bool hasMessageOptions = false;

            if (type.GetCustomAttribute <DescriptionAttribute>() != null)
            {
                var description = type.GetCustomAttribute <DescriptionAttribute>();
                codeWriter.Append('\t', 1).Append($"option description = \"{description.Description}\";").AppendLine();
                hasMessageOptions = true;
            }

            if (type.GetCustomAttribute <ObsoleteAttribute>() != null)
            {
                codeWriter.Append('\t', 1).Append($"option deprecated = true;").AppendLine();
                hasMessageOptions = true;
            }

            if (hasMessageOptions)
            {
                codeWriter.AppendLine();
            }

            var         props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var         counter = 1;
            List <Type> types   = new List <Type>();

            foreach (var p in props)
            {
                if (p.GetCustomAttribute <MessageExcludedAttribute>() != null ||
                    p.DeclaringType.FullName.StartsWith("Cybtans.Entities.DomainTenantEntity") ||
                    p.DeclaringType.FullName.StartsWith("Cybtans.Entities.TenantEntity"))
                {
                    continue;
                }

                if (p.DeclaringType.FullName.StartsWith("Cybtans.Entities.DomainAuditableEntity") ||
                    p.DeclaringType.FullName.StartsWith("Cybtans.Entities.AuditableEntity"))
                {
                    if (p.Name == "Creator")
                    {
                        continue;
                    }
                }

                Type propertyType = p.PropertyType;
                bool repeated     = false;

                if (propertyType.IsArray && propertyType != typeof(byte[]))
                {
                    propertyType = propertyType.GetElementType();
                    repeated     = true;
                }
                else if (propertyType.IsGenericType && typeof(ICollection <>).IsAssignableFrom(propertyType.GetGenericTypeDefinition()))
                {
                    propertyType = propertyType.GetGenericArguments()[0];
                    repeated     = true;
                }

                bool optional = propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>);
                if (optional)
                {
                    propertyType = propertyType.GetGenericArguments()[0];
                }

                var isPrimitive      = PrimitiveType.GetPrimitiveType(propertyType) != null;
                var propertyTypeAttr = propertyType.GetCustomAttribute <GenerateMessageAttribute>(true);

                if (visited.Contains(propertyType) || (!isPrimitive && !propertyType.IsEnum && propertyTypeAttr == null))
                {
                    continue;
                }

                codeWriter.Append('\t', 1);

                if (repeated)
                {
                    codeWriter.Append("repeated ");
                }

                codeWriter.Append($"{GetTypeName(propertyType)} {p.Name.Camel()} = {counter++}");

                AppendOptions(codeWriter, p, optional);

                codeWriter.Append(";");
                codeWriter.AppendLine();

                if (!isPrimitive && !generated.Contains(propertyType) && (propertyTypeAttr != null || propertyType.IsEnum))
                {
                    types.Add(propertyType);
                }
            }

            codeWriter.Append("}");
            codeWriter.AppendLine(2);

            foreach (var t in types)
            {
                if (t.IsClass)
                {
                    GenerateMessage(t, codeWriter, generated, visited);
                }
                else if (t.IsEnum)
                {
                    GenerateEnum(t, codeWriter, generated);
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// static constructor
 /// </summary>
 static @Function()
 {
     // build the build-in functions
     _buildInFunctions.Add(new @Function(Token.BEEP, CreateSignature(PrimitiveType.GetPrimitiveType(otDataType.Null)), PrimitiveType.GetPrimitiveType(otDataType.Bool)));
 }