Example #1
0
        private void ConvertJson2CS(string code, CSharpBuilder builder, string cname, bool isExpression)
        {
            var x = new Json2CSharp(builder, code, isExpression);

            x.Generate(cname);

            builder.AddUsingRange(base.Usings);
            PrintOutput(builder, cname);
        }
Example #2
0
 public void AddOptionalUsing()
 {
     builder.AddUsingRange(base.Usings);
 }
Example #3
0
        private void ExportConstant(DataTable dt)
        {
            //command: export /c# /type:const /field:col1,col2 /value:col3,col4
            string[] optionColumns   = cmd.GetStringArray("field");
            string[] optionConstants = cmd.GetStringArray("value");

            if (optionColumns.Length == 0)
            {
                cerr.WriteLine("missing parameter /field:col1,col2");
                return;
            }

            if (optionConstants.Length == 0)
            {
                optionConstants = optionColumns;
            }
            else if (optionColumns.Length != optionConstants.Length)
            {
                cerr.WriteLine($"invalid parameter /value:{string.Join(",", optionConstants)}");
                return;
            }

            CSharpBuilder builder = new CSharpBuilder()
            {
                Namespace = NamespaceName
            };

            builder.AddUsingRange(base.Usings);

            string cname = ClassName;
            Class  clss  = new Class(cname)
            {
                Modifier = Modifier.Public | Modifier.Static
            };

            builder.AddClass(clss);

            SortedDictionary <string, object> dict = new SortedDictionary <string, object>();
            Type type = null;

            int i = 0;

            foreach (string column in optionColumns)
            {
                string constant = optionConstants[i++];

                Type _type = dt.Columns[constant].DataType;
                if (type == null)
                {
                    type = _type;
                }
                else if (type != _type)
                {
                    cerr.WriteLine($"column [{constant}] data type is imcompatible");
                    continue;
                }

                foreach (DataRow row in dt.Rows)
                {
                    if (row[column] == DBNull.Value)
                    {
                        continue;
                    }

                    string key = row.Field <string>(column);
                    if (!dict.ContainsKey(key))
                    {
                        dict.Add(key, row[constant]);
                    }
                }
            }

            foreach (var kvp in dict)
            {
                string fieldName = Sys.ident.Identifier(kvp.Key);

                Field field = new Field(new TypeInfo(type), fieldName, new Value(kvp.Value))
                {
                    Modifier = Modifier.Public | Modifier.Const,
                    Comment  = new Comment(kvp.Key),
                };

                clss.Add(field);
            }

            PrintOutput(builder, cname);
        }
Example #4
0
        private void ExportEnum(DataTable dt)
        {
            int count = dt.Columns.Count;

            if (count < 2)
            {
                cerr.WriteLine("cannot generate enum class because table is < 2 columns");
                return;
            }

            CSharpBuilder builder = new CSharpBuilder()
            {
                Namespace = NamespaceName
            };

            builder.AddUsingRange(base.Usings);

            string cname = ClassName;

            if (count > 2)
            {
                builder.AddUsing("System.ComponentModel");
            }

            DataColumn _feature  = null;    //1st string column as property name
            DataColumn _value    = null;    //1st int column as property value
            DataColumn _label    = null;    //2nd string column as attribute [DataEnum("label")]
            DataColumn _category = null;    //3rd string column as category to generate multiple enum types

            foreach (DataColumn column in dt.Columns)
            {
                if (column.DataType == typeof(string))
                {
                    if (_feature == null)
                    {
                        _feature = column;
                    }
                    else if (_label == null)
                    {
                        _label = column;
                    }
                    else if (_category == null)
                    {
                        _category = column;
                    }
                }

                if (_value == null && column.DataType == typeof(int))
                {
                    _value = column;
                }
            }

            if (_feature == null)
            {
                cerr.WriteLine("invalid enum property name");
                return;
            }

            if (_value == null)
            {
                cerr.WriteLine("invalid enum property value");
                return;
            }

            var rows = dt
                       .AsEnumerable()
                       .Select(row => new
            {
                Feature  = row.Field <string>(_feature),
                Value    = row.Field <int>(_value),
                Category = _category != null ? row.Field <string>(_category) : null,
                Label    = _label != null ? row.Field <string>(_label) : null
            });

            if (_category != null)
            {
                var groups = rows.GroupBy(row => row.Category);

                foreach (var group in groups)
                {
                    var _enum = new EnumType(group.First().Category);
                    foreach (var row in group)
                    {
                        _enum.Add(row.Feature, row.Value, $"\"{row.Label}\"");
                    }

                    builder.AddEnum(_enum);
                }
            }
            else
            {
                var _enum = new EnumType(cname);
                foreach (var row in rows)
                {
                    _enum.Add(row.Feature, row.Value, $"\"{row.Label}\"");
                }

                builder.AddEnum(_enum);
            }

            PrintOutput(builder, cname);
        }
Example #5
0
        /// <summary>
        /// create C# data from data table
        /// </summary>
        /// <param name="cmd"></param>
        public void ExportCSData(DataTable dt)
        {
            string dataclass = cmd.GetValue("dataclass") ?? "DbReadOnly";

            CSharpBuilder builder = new CSharpBuilder
            {
                Namespace = NamespaceName
            };

            builder.AddUsingRange(base.Usings);

            string cname = ClassName;

            Dictionary <string, TypeInfo> codeColumns = CodeColumnDef();
            var clss = new Class(cname)
            {
                Modifier = Modifier.Public | Modifier.Partial
            };

            if (!cmd.Has("dataonly"))
            {
                builder.AddClass(clss);
            }

            Property prop;

            foreach (DataColumn column in dt.Columns)
            {
                bool     nullable = dt.AsEnumerable().Any(row => row[column] is DBNull);
                TypeInfo ty       = new TypeInfo(column.DataType)
                {
                    Nullable = nullable
                };
                if (codeColumns.ContainsKey(column.ColumnName))
                {
                    ty = codeColumns[column.ColumnName];
                }

                prop = new Property(ty, column.ColumnName.ToFieldName())
                {
                    Modifier = Modifier.Public
                };
                clss.Add(prop);
            }

            clss = new Class(dataclass)
            {
                Modifier = Modifier.Public | Modifier.Partial
            };

            if (!cmd.Has("classonly"))
            {
                builder.AddClass(clss);
            }


            string[] columns = dt.Columns.Cast <DataColumn>().Select(col => col.ColumnName).ToArray();

            string fieldName = cmd.GetValue("dataname") ?? $"{cname}Data";

            if (dataType == DataClassType.List || dataType == DataClassType.Array)
            {
                Field field = CreateListOrArrayField(fieldName, dataType, dt, cname, columns, codeColumns);
                clss.Add(field);
            }
            else
            {
                if (dt.Columns.Count < 2)
                {
                    cerr.WriteLine("cannot generate dictionary class, column# > 2");
                    return;
                }

                Field field = CreateDictionaryField(fieldName, dt, cname, columns, codeColumns);
                clss.Add(field);
            }

            PrintOutput(builder, cname);
        }
Example #6
0
        public void ExportCSharpData()
        {
            string code = LoadCode();

            if (code == null)
            {
                return;
            }

            ClassType ctype = getClassType();

            string _GetValueMethodName    = cmd.GetValue("method");
            string _ConstKeyClassName     = cmd.GetValue("kc");
            string _DefaultValueClassName = cmd.GetValue("dc");

            var builder = new CSharpBuilder {
                Namespace = NamespaceName
            };

            builder.AddUsing("System");
            builder.AddUsing("System.Collections.Generic");
            string cname = ClassName;

            if (ctype == ClassType.TieDataContract || ctype == ClassType.JsonDataContract)
            {
                bool isExpression = ctype == ClassType.JsonDataContract;

                string inputPath = cmd.InputPath();
                if (inputPath != null && Path.GetExtension(inputPath).ToLower() == ".json")
                {
                    isExpression = true;
                }

                ConvertJson2CS(code, builder, cname, isExpression);
                return;
            }

            var maker = new ConfigScript(code);

            if ((ctype & ClassType.HierarchicalProperty) == ClassType.HierarchicalProperty)
            {
                maker.HierarchicalMemberType = CodeMemberType.Property;
            }
            else if ((ctype & ClassType.HierarchicalMethod) == ClassType.HierarchicalMethod)
            {
                maker.HierarchicalMemberType = CodeMemberType.Method;
            }
            else
            {
                maker.HierarchicalMemberType = CodeMemberType.Field;
            }

            if (_GetValueMethodName != null)
            {
                maker.GetValueMethodName = _GetValueMethodName;
            }

            if (_ConstKeyClassName != null)
            {
                maker.ConstKeyClassName = _ConstKeyClassName;
            }

            if (_DefaultValueClassName != null)
            {
                maker.DefaultValueClassName = _DefaultValueClassName;
            }

            var clss = maker.Generate(cname);

            builder.AddClass(clss);

            if (ctype == ClassType.ConstKey)
            {
                builder = CreateClass(maker.ConstKeyFields);
            }
            else if (ctype == ClassType.DefaultValue)
            {
                builder = CreateClass(maker.DefaultValueFields);
            }
            else if (ctype == ClassType.StaticField)
            {
                builder = CreateClass(maker.StaticFields);
            }
            else if (ctype == ClassType.StaticPropery)
            {
                builder = CreateClass(maker.StaticProperties);
            }
            else if (ctype == ClassType.StaticMethod)
            {
                builder = CreateClass(maker.StaticMethods);
            }
            else if (ctype == ClassType.HierarchicalField || ctype == ClassType.HierarchicalProperty || ctype == ClassType.HierarchicalMethod)
            {
                //skip, because clss has created class already
            }
            else
            {
                if ((ctype & ClassType.HierarchicalField) != ClassType.HierarchicalField &&
                    (ctype & ClassType.HierarchicalProperty) != ClassType.HierarchicalProperty &&
                    (ctype & ClassType.HierarchicalMethod) != ClassType.HierarchicalMethod
                    )
                {
                    clss.Clear();
                }

                if ((ctype & ClassType.StaticField) == ClassType.StaticField)
                {
                    clss.AddRange(maker.StaticFields);
                }

                if ((ctype & ClassType.StaticPropery) == ClassType.StaticPropery)
                {
                    clss.AddRange(maker.StaticProperties);
                }

                if ((ctype & ClassType.StaticMethod) == ClassType.StaticMethod)
                {
                    clss.AddRange(maker.StaticMethods);
                }

                if ((ctype & ClassType.ConstKey) == ClassType.ConstKey)
                {
                    clss.AddRange(maker.ConstKeyFields);
                }

                if ((ctype & ClassType.DefaultValue) == ClassType.DefaultValue)
                {
                    clss.AddRange(maker.DefaultValueFields);
                }
            }

            builder.AddUsingRange(base.Usings);
            PrintOutput(builder, cname);
        }