{"action":"created","comment":{"url":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/comments/238073430","pull_request_review_id":180534630,"id":238073430,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIzODA3MzQzMA==","diff_hunk":"@@ -1,62 +1,155 @@\n ﻿using System;\n+using System.Collections;\n+using System.Collections.Generic;\n using System.Linq;\n using System.Linq.Expressions;\n+using System.Reflection;\n using System.Text;\n \n namespace ObjectPrinting\n {\n     public class PrintingConfig<TOwner>\n     {\n+        private readonly HashSet<Type> excludedPropertiesTypes = new HashSet<Type>();\n+        private readonly HashSet<string> excludedPropertiesNames = new HashSet<string>();\n+\n+        private readonly Dictionary<Type, IPropertyPrintingConfig<TOwner>> propertiesConfigByType =\n+            new Dictionary<Type, IPropertyPrintingConfig<TOwner>>();\n+\n+        private readonly Dictionary<string, IPropertyPrintingConfig<TOwner>> propertiesConfigByName =\n+            new Dictionary<string, IPropertyPrintingConfig<TOwner>>();\n+\n         public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>()\n         {\n-            return new PropertyPrintingConfig<TOwner, TPropType>(this);\n+            var propertyConfig = new PropertyPrintingConfig<TOwner, TPropType>(this);\n+            propertiesConfigByType[typeof(TPropType)] = propertyConfig;\n+            return propertyConfig;\n         }\n \n         public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector)\n         {\n-            return new PropertyPrintingConfig<TOwner, TPropType>(this);\n+            var propertyConfig = new PropertyPrintingConfig<TOwner, TPropType>(this);\n+            var propertyName = ((MemberExpression)memberSelector.Body).Member.Name;\n+            propertiesConfigByName[propertyName] = propertyConfig;\n+            return propertyConfig;\n         }\n \n         public PrintingConfig<TOwner> Excluding<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector)\n         {\n+            var propertyName = ((MemberExpression)memberSelector.Body).Member.Name;\n+            excludedPropertiesNames.Add(propertyName);\n             return this;\n         }\n \n         public PrintingConfig<TOwner> Excluding<TPropType>()\n         {\n+            excludedPropertiesTypes.Add(typeof(TPropType));\n             return this;\n         }\n \n-        public string PrintToString(TOwner obj)\n-        {\n-            return PrintToString(obj, 0);\n-        }\n+        public string PrintToString(TOwner obj) => PrintToString(obj, 0);\n \n         private string PrintToString(object obj, int nestingLevel)\n         {\n-            //TODO apply configurations\n             if (obj == null)\n                 return \"null\" + Environment.NewLine;\n \n-            var finalTypes = new[]\n-            {\n-                typeof(int), typeof(double), typeof(float), typeof(string),\n-                typeof(DateTime), typeof(TimeSpan)\n-            };\n-            if (finalTypes.Contains(obj.GetType()))\n-                return obj + Environment.NewLine;\n-\n-            var identation = new string('\\t', nestingLevel + 1);\n-            var sb = new StringBuilder();\n+            if (nestingLevel > 1000)\n+                return \"[Infinite Recursion]\";\n+\n+            if (TryPrintFinalType(obj, out var result))\n+                return result;\n+\n+            if (TryPrintCollection(obj, out result))\n+                return result;\n+\n+            return PrintAnyType(obj, nestingLevel);\n+        }\n+\n+        private string PrintAnyType(object obj, int nestingLevel)\n+        {\n+            var indentation = new string('\\t', nestingLevel + 1);\n+            var builder = new StringBuilder();\n             var type = obj.GetType();\n-            sb.AppendLine(type.Name);\n-            foreach (var propertyInfo in type.GetProperties())\n+            var includedProperties = GetIncludedProperties(type);\n+            builder.AppendLine(includedProperties.Any() ? type.Name : obj.ToString());\n+            foreach (var propertyInfo in includedProperties)\n             {\n-                sb.Append(identation + propertyInfo.Name + \" = \" +\n-                          PrintToString(propertyInfo.GetValue(obj),\n-                              nestingLevel + 1));\n+                builder.Append(indentation + propertyInfo.Name + \" = \" +\n+                               PrintProperty(obj, nestingLevel, propertyInfo));\n             }\n-            return sb.ToString();\n+            return builder.ToString();\n+        }\n+\n+        private PropertyInfo[] GetIncludedProperties(Type type)\n+        {\n+            return type.GetProperties()\n+                .Where(property => !excludedPropertiesTypes.Contains(property.PropertyType))\n+                .Where(property => !excludedPropertiesNames.Contains(property.Name))\n+                .ToArray();\n+        }\n+\n+        private bool TryPrintCollection(object obj, out string result)\n+        {\n+            var builder = new StringBuilder();\n+            result = null;\n+            if (!(obj is IEnumerable collection))\n+                return false;\n+            builder.AppendLine(obj.GetType().Name + Environment.NewLine + \"{\");\n+            AddCollectionItems(collection, builder);","path":"ObjectPrinting/PrintingConfig.cs","position":119,"original_position":122,"commit_id":"e034198d6c11a25c71232a8eb2c037cf3a0b9659","original_commit_id":"e034198d6c11a25c71232a8eb2c037cf3a0b9659","user":{"login":"xackill","id":9920311,"node_id":"MDQ6VXNlcjk5MjAzMTE=","avatar_url":"https://avatars2.githubusercontent.com/u/9920311?v=4","gravatar_id":"","url":"https://api.github.com/users/xackill","html_url":"https://github.com/xackill","followers_url":"https://api.github.com/users/xackill/followers","following_url":"https://api.github.com/users/xackill/following{/other_user}","gists_url":"https://api.github.com/users/xackill/gists{/gist_id}","starred_url":"https://api.github.com/users/xackill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xackill/subscriptions","organizations_url":"https://api.github.com/users/xackill/orgs","repos_url":"https://api.github.com/users/xackill/repos","events_url":"https://api.github.com/users/xackill/events{/privacy}","received_events_url":"https://api.github.com/users/xackill/received_events","type":"User","site_admin":false},"body":"Почему выбран такой способ заполнить StringBuilder? Не проще ли сделать метод, который возвращает то, что можно добавить в StringBuilder?\r\nДаешь pure-методы!","created_at":"2018-12-01T19:06:49Z","updated_at":"2018-12-01T19:06:57Z","html_url":"https://github.com/kontur-courses/fluent-api/pull/10#discussion_r238073430","pull_request_url":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/10","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/comments/238073430"},"html":{"href":"https://github.com/kontur-courses/fluent-api/pull/10#discussion_r238073430"},"pull_request":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/10"}}},"pull_request":{"url":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/10","id":233272452,"node_id":"MDExOlB1bGxSZXF1ZXN0MjMzMjcyNDUy","html_url":"https://github.com/kontur-courses/fluent-api/pull/10","diff_url":"https://github.com/kontur-courses/fluent-api/pull/10.diff","patch_url":"https://github.com/kontur-courses/fluent-api/pull/10.patch","issue_url":"https://api.github.com/repos/kontur-courses/fluent-api/issues/10","number":10,"state":"open","locked":false,"title":"Изаков Артемий","user":{"login":"CGOptimum","id":38810090,"node_id":"MDQ6VXNlcjM4ODEwMDkw","avatar_url":"https://avatars2.githubusercontent.com/u/38810090?v=4","gravatar_id":"","url":"https://api.github.com/users/CGOptimum","html_url":"https://github.com/CGOptimum","followers_url":"https://api.github.com/users/CGOptimum/followers","following_url":"https://api.github.com/users/CGOptimum/following{/other_user}","gists_url":"https://api.github.com/users/CGOptimum/gists{/gist_id}","starred_url":"https://api.github.com/users/CGOptimum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CGOptimum/subscriptions","organizations_url":"https://api.github.com/users/CGOptimum/orgs","repos_url":"https://api.github.com/users/CGOptimum/repos","events_url":"https://api.github.com/users/CGOptimum/events{/privacy}","received_events_url":"https://api.github.com/users/CGOptimum/received_events","type":"User","site_admin":false},"body":"Наставник: @rx00","created_at":"2018-11-23T19:55:56Z","updated_at":"2018-12-01T19:06:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"177eaf15cc9f6511b0019505663a6d18e18e08a9","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/10/commits","review_comments_url":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/10/comments","review_comment_url":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/comments{/number}","comments_url":"https://api.github.com/repos/kontur-courses/fluent-api/issues/10/comments","statuses_url":"https://api.github.com/repos/kontur-courses/fluent-api/statuses/e034198d6c11a25c71232a8eb2c037cf3a0b9659","head":{"label":"CGOptimum:master","ref":"master","sha":"e034198d6c11a25c71232a8eb2c037cf3a0b9659","user":{"login":"CGOptimum","id":38810090,"node_id":"MDQ6VXNlcjM4ODEwMDkw","avatar_url":"https://avatars2.githubusercontent.com/u/38810090?v=4","gravatar_id":"","url":"https://api.github.com/users/CGOptimum","html_url":"https://github.com/CGOptimum","followers_url":"https://api.github.com/users/CGOptimum/followers","following_url":"https://api.github.com/users/CGOptimum/following{/other_user}","gists_url":"https://api.github.com/users/CGOptimum/gists{/gist_id}","starred_url":"https://api.github.com/users/CGOptimum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CGOptimum/subscriptions","organizations_url":"https://api.github.com/users/CGOptimum/orgs","repos_url":"https://api.github.com/users/CGOptimum/repos","events_url":"https://api.github.com/users/CGOptimum/events{/privacy}","received_events_url":"https://api.github.com/users/CGOptimum/received_events","type":"User","site_admin":false},"repo":{"id":158549199,"node_id":"MDEwOlJlcG9zaXRvcnkxNTg1NDkxOTk=","name":"fluent-api","full_name":"CGOptimum/fluent-api","private":false,"owner":{"login":"CGOptimum","id":38810090,"node_id":"MDQ6VXNlcjM4ODEwMDkw","avatar_url":"https://avatars2.githubusercontent.com/u/38810090?v=4","gravatar_id":"","url":"https://api.github.com/users/CGOptimum","html_url":"https://github.com/CGOptimum","followers_url":"https://api.github.com/users/CGOptimum/followers","following_url":"https://api.github.com/users/CGOptimum/following{/other_user}","gists_url":"https://api.github.com/users/CGOptimum/gists{/gist_id}","starred_url":"https://api.github.com/users/CGOptimum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CGOptimum/subscriptions","organizations_url":"https://api.github.com/users/CGOptimum/orgs","repos_url":"https://api.github.com/users/CGOptimum/repos","events_url":"https://api.github.com/users/CGOptimum/events{/privacy}","received_events_url":"https://api.github.com/users/CGOptimum/received_events","type":"User","site_admin":false},"html_url":"https://github.com/CGOptimum/fluent-api","description":"Разработка Fluent API","fork":true,"url":"https://api.github.com/repos/CGOptimum/fluent-api","forks_url":"https://api.github.com/repos/CGOptimum/fluent-api/forks","keys_url":"https://api.github.com/repos/CGOptimum/fluent-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CGOptimum/fluent-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CGOptimum/fluent-api/teams","hooks_url":"https://api.github.com/repos/CGOptimum/fluent-api/hooks","issue_events_url":"https://api.github.com/repos/CGOptimum/fluent-api/issues/events{/number}","events_url":"https://api.github.com/repos/CGOptimum/fluent-api/events","assignees_url":"https://api.github.com/repos/CGOptimum/fluent-api/assignees{/user}","branches_url":"https://api.github.com/repos/CGOptimum/fluent-api/branches{/branch}","tags_url":"https://api.github.com/repos/CGOptimum/fluent-api/tags","blobs_url":"https://api.github.com/repos/CGOptimum/fluent-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CGOptimum/fluent-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CGOptimum/fluent-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/CGOptimum/fluent-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CGOptimum/fluent-api/statuses/{sha}","languages_url":"https://api.github.com/repos/CGOptimum/fluent-api/languages","stargazers_url":"https://api.github.com/repos/CGOptimum/fluent-api/stargazers","contributors_url":"https://api.github.com/repos/CGOptimum/fluent-api/contributors","subscribers_url":"https://api.github.com/repos/CGOptimum/fluent-api/subscribers","subscription_url":"https://api.github.com/repos/CGOptimum/fluent-api/subscription","commits_url":"https://api.github.com/repos/CGOptimum/fluent-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/CGOptimum/fluent-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/CGOptimum/fluent-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/CGOptimum/fluent-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/CGOptimum/fluent-api/contents/{+path}","compare_url":"https://api.github.com/repos/CGOptimum/fluent-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CGOptimum/fluent-api/merges","archive_url":"https://api.github.com/repos/CGOptimum/fluent-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CGOptimum/fluent-api/downloads","issues_url":"https://api.github.com/repos/CGOptimum/fluent-api/issues{/number}","pulls_url":"https://api.github.com/repos/CGOptimum/fluent-api/pulls{/number}","milestones_url":"https://api.github.com/repos/CGOptimum/fluent-api/milestones{/number}","notifications_url":"https://api.github.com/repos/CGOptimum/fluent-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CGOptimum/fluent-api/labels{/name}","releases_url":"https://api.github.com/repos/CGOptimum/fluent-api/releases{/id}","deployments_url":"https://api.github.com/repos/CGOptimum/fluent-api/deployments","created_at":"2018-11-21T13:05:34Z","updated_at":"2018-11-23T18:05:46Z","pushed_at":"2018-11-23T18:05:45Z","git_url":"git://github.com/CGOptimum/fluent-api.git","ssh_url":"git@github.com:CGOptimum/fluent-api.git","clone_url":"https://github.com/CGOptimum/fluent-api.git","svn_url":"https://github.com/CGOptimum/fluent-api","homepage":null,"size":1496,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"kontur-courses:master","ref":"master","sha":"cbeab99467dff34cd973eb9f44fd4c72dbcf6fe0","user":{"login":"kontur-courses","id":19208073,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE5MjA4MDcz","avatar_url":"https://avatars2.githubusercontent.com/u/19208073?v=4","gravatar_id":"","url":"https://api.github.com/users/kontur-courses","html_url":"https://github.com/kontur-courses","followers_url":"https://api.github.com/users/kontur-courses/followers","following_url":"https://api.github.com/users/kontur-courses/following{/other_user}","gists_url":"https://api.github.com/users/kontur-courses/gists{/gist_id}","starred_url":"https://api.github.com/users/kontur-courses/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kontur-courses/subscriptions","organizations_url":"https://api.github.com/users/kontur-courses/orgs","repos_url":"https://api.github.com/users/kontur-courses/repos","events_url":"https://api.github.com/users/kontur-courses/events{/privacy}","received_events_url":"https://api.github.com/users/kontur-courses/received_events","type":"Organization","site_admin":false},"repo":{"id":62727275,"node_id":"MDEwOlJlcG9zaXRvcnk2MjcyNzI3NQ==","name":"fluent-api","full_name":"kontur-courses/fluent-api","private":false,"owner":{"login":"kontur-courses","id":19208073,"node_id":"MDEyOk9yZ2FuaXphdGlvbjE5MjA4MDcz","avatar_url":"https://avatars2.githubusercontent.com/u/19208073?v=4","gravatar_id":"","url":"https://api.github.com/users/kontur-courses","html_url":"https://github.com/kontur-courses","followers_url":"https://api.github.com/users/kontur-courses/followers","following_url":"https://api.github.com/users/kontur-courses/following{/other_user}","gists_url":"https://api.github.com/users/kontur-courses/gists{/gist_id}","starred_url":"https://api.github.com/users/kontur-courses/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kontur-courses/subscriptions","organizations_url":"https://api.github.com/users/kontur-courses/orgs","repos_url":"https://api.github.com/users/kontur-courses/repos","events_url":"https://api.github.com/users/kontur-courses/events{/privacy}","received_events_url":"https://api.github.com/users/kontur-courses/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/kontur-courses/fluent-api","description":"Разработка Fluent API","fork":false,"url":"https://api.github.com/repos/kontur-courses/fluent-api","forks_url":"https://api.github.com/repos/kontur-courses/fluent-api/forks","keys_url":"https://api.github.com/repos/kontur-courses/fluent-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kontur-courses/fluent-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kontur-courses/fluent-api/teams","hooks_url":"https://api.github.com/repos/kontur-courses/fluent-api/hooks","issue_events_url":"https://api.github.com/repos/kontur-courses/fluent-api/issues/events{/number}","events_url":"https://api.github.com/repos/kontur-courses/fluent-api/events","assignees_url":"https://api.github.com/repos/kontur-courses/fluent-api/assignees{/user}","branches_url":"https://api.github.com/repos/kontur-courses/fluent-api/branches{/branch}","tags_url":"https://api.github.com/repos/kontur-courses/fluent-api/tags","blobs_url":"https://api.github.com/repos/kontur-courses/fluent-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kontur-courses/fluent-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kontur-courses/fluent-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/kontur-courses/fluent-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kontur-courses/fluent-api/statuses/{sha}","languages_url":"https://api.github.com/repos/kontur-courses/fluent-api/languages","stargazers_url":"https://api.github.com/repos/kontur-courses/fluent-api/stargazers","contributors_url":"https://api.github.com/repos/kontur-courses/fluent-api/contributors","subscribers_url":"https://api.github.com/repos/kontur-courses/fluent-api/subscribers","subscription_url":"https://api.github.com/repos/kontur-courses/fluent-api/subscription","commits_url":"https://api.github.com/repos/kontur-courses/fluent-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/kontur-courses/fluent-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/kontur-courses/fluent-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/kontur-courses/fluent-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/kontur-courses/fluent-api/contents/{+path}","compare_url":"https://api.github.com/repos/kontur-courses/fluent-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kontur-courses/fluent-api/merges","archive_url":"https://api.github.com/repos/kontur-courses/fluent-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kontur-courses/fluent-api/downloads","issues_url":"https://api.github.com/repos/kontur-courses/fluent-api/issues{/number}","pulls_url":"https://api.github.com/repos/kontur-courses/fluent-api/pulls{/number}","milestones_url":"https://api.github.com/repos/kontur-courses/fluent-api/milestones{/number}","notifications_url":"https://api.github.com/repos/kontur-courses/fluent-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kontur-courses/fluent-api/labels{/name}","releases_url":"https://api.github.com/repos/kontur-courses/fluent-api/releases{/id}","deployments_url":"https://api.github.com/repos/kontur-courses/fluent-api/deployments","created_at":"2016-07-06T14:27:15Z","updated_at":"2018-11-20T21:52:15Z","pushed_at":"2018-12-01T15:22:03Z","git_url":"git://github.com/kontur-courses/fluent-api.git","ssh_url":"git@github.com:kontur-courses/fluent-api.git","clone_url":"https://github.com/kontur-courses/fluent-api.git","svn_url":"https://github.com/kontur-courses/fluent-api","homepage":null,"size":1484,"stargazers_count":4,"watchers_count":4,"language":"C#","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":84,"mirror_url":null,"archived":false,"open_issues_count":35,"license":null,"forks":84,"open_issues":35,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/10"},"html":{"href":"https://github.com/kontur-courses/fluent-api/pull/10"},"issue":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/issues/10"},"comments":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/issues/10/comments"},"review_comments":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/10/comments"},"review_comment":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/pulls/10/commits"},"statuses":{"href":"https://api.github.com/repos/kontur-courses/fluent-api/statuses/e034198d6c11a25c71232a8eb2c037cf3a0b9659"}},"author_association":"NONE"}}
{"actor":{"display_login":"xackill"}}