Commits

Hosein Yeganloo  committed f160383 Draft

some change on csv provider & work on overloads
create some test on csv provider

  • Participants
  • Parent commits b74b844

Comments (0)

Files changed (4)

File HYLogger/HYLogger/HYLogger.csproj

     <ConsolePause>false</ConsolePause>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Microsoft.CSharp" />
     <Reference Include="System" />
   </ItemGroup>
   <ItemGroup>

File HYLogger/HYLogger/SerializationProviders/CSVProvider.cs

 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 namespace HYLogger.SerializationProviders
 {
-    public static class CSVProvider
+    public class CSVProvider
     {
-        public static string ConvertToCSV(object obj, char seprator)
+        public string ConvertToCSV<T>(IEnumerable<T> obj, string seprator = ",", Model.RunTimeInterface filter = null)
+        {
+            StringBuilder str = new StringBuilder();
+            foreach (var o in obj)
+            {
+                str.Append(ConvertToCSV<T>(o, seprator, filter) + "\n");
+            }
+            return str.ToString();
+        }
+
+        public string ConvertToCSV<T>(dynamic obj, string seprator = ",", Model.RunTimeInterface filter = null)
+        {
+            return ConvertToCSV(typeof(T), obj, seprator, filter);
+        }
+
+        public string ConvertToCSV(IEnumerable obj, string seprator = ",", Model.RunTimeInterface filter = null)
+        {
+            StringBuilder str = new StringBuilder();
+            foreach (var o in obj)
+            {
+                str.Append(ConvertToCSV(o, seprator, filter) + "\n");
+            }
+            return str.ToString();
+        }
+
+        public string ConvertToCSV(dynamic obj, string seprator = ",", Model.RunTimeInterface filter = null)
+        {
+            return ConvertToCSV(obj.GetType(), obj, seprator, filter);
+        }
+
+        public string ConvertToCSV(Type type, dynamic obj, string seprator = ",", Model.RunTimeInterface filter = null)
         {
             StringBuilder str = new StringBuilder();
             bool hasValue = false;
             foreach (var i in obj.GetType().GetProperties())
             {
-                string tmp = i.GetValue(obj, null).ToString();
-                if (tmp.Contains("\""))
+                if (filter == null || filter.MustLog(i))
                 {
-                    int k = 0;
-                    foreach (var t in tmp.Split('"'))
-                        tmp = k++ == 0 ? t : "\"\"";
+                    string tmp = i.GetValue(obj, null).ToString();
+                    if (tmp.Contains('"'))
+                    {
+                        int k = 0;
+                        string tp = "";
+                        foreach (var t in tmp.Split('"'))
+                            tp += k++ == 0 ? t : "\"\"" + t;
+                        tmp = tp;
+                    }
+                    if (tmp.Contains("\n") || tmp.Contains(seprator))
+                        tmp = "\"" + tmp + "\"";
+                    if (!hasValue)
+                    {
+                        str.Append(tmp);
+                        hasValue = true;
+                    }
+                    else
+                    {
+                        str.Append(seprator + tmp);
+                    }
                 }
-                if (tmp.Contains("\n") || tmp.Contains(seprator))
-                    tmp = "\"" + tmp + "\"";
-                if (!hasValue)
-                    str.Append(tmp);
-
             }
             return str.ToString();
         }
-
-        public static string ConvertTo(object obj, Model.RunTimeInterface filter)
-        {
-            StringBuilder str = new StringBuilder();
-            return str.ToString();
-        }
     }
 }

File HYLogger/HYLoggerTest/HYLoggerTest.csproj

-<?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <PropertyGroup>
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
-    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
-    <ProductVersion>10.0.0</ProductVersion>
-    <SchemaVersion>2.0</SchemaVersion>
-    <ProjectGuid>{CA9DE579-D0B7-446A-85E0-90CD7EEF810A}</ProjectGuid>
-    <OutputType>Exe</OutputType>
-    <RootNamespace>HYLoggerTest</RootNamespace>
-    <AssemblyName>HYLoggerTest</AssemblyName>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
-    <DebugSymbols>true</DebugSymbols>
-    <DebugType>full</DebugType>
-    <Optimize>false</Optimize>
-    <OutputPath>bin\Debug</OutputPath>
-    <DefineConstants>DEBUG;</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-    <PlatformTarget>x86</PlatformTarget>
-    <Externalconsole>true</Externalconsole>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
-    <DebugType>none</DebugType>
-    <Optimize>false</Optimize>
-    <OutputPath>bin\Release</OutputPath>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-    <PlatformTarget>x86</PlatformTarget>
-    <Externalconsole>true</Externalconsole>
-  </PropertyGroup>
-  <ItemGroup>
-    <Reference Include="System" />
-  </ItemGroup>
-  <ItemGroup>
-    <Compile Include="Main.cs" />
-    <Compile Include="AssemblyInfo.cs" />
-  </ItemGroup>
-  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>10.0.0</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{CA9DE579-D0B7-446A-85E0-90CD7EEF810A}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>HYLoggerTest</RootNamespace>
+    <AssemblyName>HYLoggerTest</AssemblyName>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <DebugType>none</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Main.cs" />
+    <Compile Include="AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\HYLogger\HYLogger.csproj">
+      <Project>{24D0DFE2-7B3B-4722-B09A-4CAB174B169E}</Project>
+      <Name>HYLogger</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 </Project>

File HYLogger/HYLoggerTest/Main.cs

-using System;
-
-namespace HYLoggerTest
-{
-	class MainClass
-	{
-		public static void Main (string[] args)
-		{
-			Console.WriteLine ("Hello World!");
-		}
-	}
-}
+using System;
+using System.Collections;
+using System.Collections.Generic;
+
+namespace HYLoggerTest
+{
+    class MainClass
+    {
+        public static void Main(string[] args)
+        {
+            HYLogger.Model.RunTimeInterface filter = HYLogger.Model.RunTimeInterface.Create();
+            
+            HYLogger.SerializationProviders.CSVProvider csv = new HYLogger.SerializationProviders.CSVProvider();
+            var obj1 = new { Name = "Hos\"ein", LastNme = "Yega\nnloo" };
+            var obj2 = new { Name = "Hos\"ein", LastNme = "Yega\nnloo", tst = "Test" };
+            
+            Queue l = new Queue();
+            l.Enqueue(obj1);
+            l.Enqueue(obj2);
+            List<object> ll = new List<object>();
+            ll.Add(obj1);
+            ll.Add(obj2);
+            Console.Write(csv.ConvertToCSV(l, ","));
+            Console.Write(csv.ConvertToCSV<object>(ll, ","));
+            Console.ReadKey();
+        }
+    }
+}