<commit_before>﻿using Newtonsoft.Json;
using Serenity.Data;
using Serenity.Services;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Serenity.Reflection
{
    public class CodeWriter
    {
        private StringBuilder sb;
        private string space;
        private string indent;

        public CodeWriter(StringBuilder sb, int indentSize)
        {
            this.space = new String(' ', indentSize);
            this.indent = "";
            this.sb = sb;
        }

        private void IncreaseIndent()
        {
            indent += space;
        }

        private void DecreaseIndent()
        {
            if (indent.Length >= space.Length)
                indent = indent.Substring(0, indent.Length - space.Length);
        }

        public void Block(Action insideBlock)
        {
            IncreaseIndent();
            insideBlock();
            DecreaseIndent();
        }

        public void InBrace(Action insideBlock)
        {
            sb.Append(indent);
            sb.AppendLine("{");
            IncreaseIndent();
            insideBlock();
            DecreaseIndent();
            sb.Append(indent);
            sb.AppendLine("}");
        }

        public void Indent()
        {
            sb.Append(indent);
        }

        public void Indented(string s)
        {
            sb.Append(indent);
            sb.Append(s);
        }

        public void IndentedLine(string s)
        {
            sb.Append(indent);
            sb.AppendLine(s);
        }
    }

    public class DtoGenerator
    {
        private StringBuilder sb;
        private CodeWriter cw;
        private HashSet<Type> visited;
        private Queue<Type> generateQueue;

        public DtoGenerator()
        {
        }

        private bool EnqueueType(Type type)
        {
            if (visited.Contains(type))
                return false;

            visited.Add(type);
            generateQueue.Enqueue(type);
            return true;
        }

        public string GenerateCode(string anamespace, params Type[] fromTypes)
        {
            this.sb = new StringBuilder(4096);
            this.cw = new CodeWriter(sb, 4);
            this.generateQueue = new Queue<Type>();
            this.visited = new HashSet<Type>();

            sb.Append("namespace ");
            sb.AppendLine(anamespace);
            cw.InBrace(delegate
            {
                foreach (var ns in
                    new string[] {
                        "Newtonsoft.Json",
                        "Newtonsoft.Json.Converters",
                        "System",
                        "System.Collections.Generic"
                    })
                {
                    cw.Indented("using ");
                    sb.Append(ns);
                    sb.AppendLine(";");
                }

                sb.AppendLine();

                foreach (var fromType in fromTypes)
                    EnqueueType(fromType);

                int i = 0;
                while (generateQueue.Count > 0)
                {
                    if (i++ > 0)
                        sb.AppendLine();

                    GenerateCodeFor(generateQueue.Dequeue());
                }
            });

            return sb.ToString();
        }

        private static bool IsSimpleType(Type type)
        {
            if (type == typeof(String) ||
                type == typeof(Int32) ||
                type == typeof(Int64) ||
                type == typeof(Int16) ||
                type == typeof(Double) ||
                type == typeof(Decimal) ||
                type == typeof(DateTime) ||
                type == typeof(Boolean))
                return true;

            return false;
        }

        private void HandleMemberType(Type memberType)
        {
            HandleMemberType(sb, memberType, t => EnqueueType(t));
        }

        public static void HandleMemberType(StringBuilder code, Type memberType, Action<Type> enqueueType = null)
        {
            if (IsSimpleType(memberType))
            {
                code.Append(memberType.Name);
                return;
            }

            var nullableType = Nullable.GetUnderlyingType(memberType);
            if (nullableType != null &&
                IsSimpleType(nullableType))
            {
                code.Append(nullableType.Name);
                code.Append("?");
                return;
            }

            if (nullableType != null)
            {
                HandleMemberType(code, nullableType, enqueueType);
                code.Append("?");
                return;
            }

            // TODO: Bunlar özel durumlar, attribute la daha sonra halledelim!
            if (memberType == typeof(SortBy[]))
            {
                code.Append("List<String>");
                return;
            }

            if (memberType == typeof(HashSet<string>))
            {
                code.Append("List<String>");
                return;
            }

            if (memberType == typeof(Object))
            {
                code.Append("Object");
                return;
            }

            if (memberType.IsGenericType &&
                memberType.GetGenericTypeDefinition() == typeof(List<>))
            {
                code.Append("List<");
                HandleMemberType(code, memberType.GenericTypeArguments[0], enqueueType);
                code.Append(">");
                return;
            }
<commit_msg>dictionary string string temporary fix<commit_after>﻿using Newtonsoft.Json;
using Serenity.Data;
using Serenity.Services;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Serenity.Reflection
{
    public class CodeWriter
    {
        private StringBuilder sb;
        private string space;
        private string indent;

        public CodeWriter(StringBuilder sb, int indentSize)
        {
            this.space = new String(' ', indentSize);
            this.indent = "";
            this.sb = sb;
        }

        private void IncreaseIndent()
        {
            indent += space;
        }

        private void DecreaseIndent()
        {
            if (indent.Length >= space.Length)
                indent = indent.Substring(0, indent.Length - space.Length);
        }

        public void Block(Action insideBlock)
        {
            IncreaseIndent();
            insideBlock();
            DecreaseIndent();
        }

        public void InBrace(Action insideBlock)
        {
            sb.Append(indent);
            sb.AppendLine("{");
            IncreaseIndent();
            insideBlock();
            DecreaseIndent();
            sb.Append(indent);
            sb.AppendLine("}");
        }

        public void Indent()
        {
            sb.Append(indent);
        }

        public void Indented(string s)
        {
            sb.Append(indent);
            sb.Append(s);
        }

        public void IndentedLine(string s)
        {
            sb.Append(indent);
            sb.AppendLine(s);
        }
    }

    public class DtoGenerator
    {
        private StringBuilder sb;
        private CodeWriter cw;
        private HashSet<Type> visited;
        private Queue<Type> generateQueue;

        public DtoGenerator()
        {
        }

        private bool EnqueueType(Type type)
        {
            if (visited.Contains(type))
                return false;

            visited.Add(type);
            generateQueue.Enqueue(type);
            return true;
        }

        public string GenerateCode(string anamespace, params Type[] fromTypes)
        {
            this.sb = new StringBuilder(4096);
            this.cw = new CodeWriter(sb, 4);
            this.generateQueue = new Queue<Type>();
            this.visited = new HashSet<Type>();

            sb.Append("namespace ");
            sb.AppendLine(anamespace);
            cw.InBrace(delegate
            {
                foreach (var ns in
                    new string[] {
                        "Newtonsoft.Json",
                        "Newtonsoft.Json.Converters",
                        "System",
                        "System.Collections.Generic"
                    })
                {
                    cw.Indented("using ");
                    sb.Append(ns);
                    sb.AppendLine(";");
                }

                sb.AppendLine();

                foreach (var fromType in fromTypes)
                    EnqueueType(fromType);

                int i = 0;
                while (generateQueue.Count > 0)
                {
                    if (i++ > 0)
                        sb.AppendLine();

                    GenerateCodeFor(generateQueue.Dequeue());
                }
            });

            return sb.ToString();
        }

        private static bool IsSimpleType(Type type)
        {
            if (type == typeof(String) ||
                type == typeof(Int32) ||
                type == typeof(Int64) ||
                type == typeof(Int16) ||
                type == typeof(Double) ||
                type == typeof(Decimal) ||
                type == typeof(DateTime) ||
                type == typeof(Boolean))
                return true;

            return false;
        }

        private void HandleMemberType(Type memberType)
        {
            HandleMemberType(sb, memberType, t => EnqueueType(t));
        }

        public static void HandleMemberType(StringBuilder code, Type memberType, Action<Type> enqueueType = null)
        {
            if (IsSimpleType(memberType))
            {
                code.Append(memberType.Name);
                return;
            }

            var nullableType = Nullable.GetUnderlyingType(memberType);
            if (nullableType != null &&
                IsSimpleType(nullableType))
            {
                code.Append(nullableType.Name);
                code.Append("?");
                return;
            }

            if (nullableType != null)
            {
                HandleMemberType(code, nullableType, enqueueType);
                code.Append("?");
                return;
            }

            // TODO: Bunlar özel durumlar, attribute la daha sonra halledelim!
            if (memberType == typeof(SortBy[]))
            {
                code.Append("List<String>");
                return;
            }

            if (memberType == typeof(HashSet<string>))
            {
                code.Append("List<String>");
                return;
            }

            if (memberType == typeof(Dictionary<string, string>))
            {
                code.Append("Dictionary<String, String>");
                return;
            }

            if (memberType == typeof(Object))
            {
                code.Append("Object");
                return;
            }

            if (memberType.IsGenericType &&
                memberType.GetGenericTypeDefinition() == typeof(List<>))
            {
                code.Append("List<");
                HandleMemberType(code, memberType.GenericTypeArguments[0], enqueueType);
                code.Append(">");
                return;
            }

{'fraction_non_alphanumeric': 0.09779626515620851, 'fraction_numerical': 0.002832109036197894, 'mean_word_length': 1.223096596498131, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2479595', 'n_tokens_mistral': 2870, 'n_tokens_neox': 2685, 'n_words': 563}