Exemple #1
0
        private void ProcessFooterRow(ICsvRow row)
        {
            var fRowCount = int.Parse(row[_csvDef.Footer.RowCountIndex]);
            var fChecksum = int.Parse(row[_csvDef.Footer.ChecksumIndex]);

            Console.WriteLine($"footer row count: {fRowCount}, checksum: {fChecksum}");
        }
Exemple #2
0
        private void ProcessDataRow(ICsvRow row)
        {
            if (row.AsStringArray().Length != _csvDef.Data.Columns.Length)
            {
                OnRowError(row, $"expected {_csvDef.Data.Columns.Length} fields, found {row.AsStringArray().Length}");
                return;
            }
            var dataRow = _dataRows.NewRow();

            try
            {
                foreach (System.Data.DataColumn col in _dataRows.Columns)
                {
                    var value = row[col.ColumnName];
                    if (value != null)
                    {
                        dataRow[col.ColumnName] = value;
                    }
                    else
                    {
                        dataRow[col.ColumnName] = DBNull.Value;
                    }
                }
                _dataRows.Rows.Add(dataRow);
                Console.WriteLine("name: " + row["Name"]);
            }
            catch (ArgumentException e)
            {
                OnRowError(row, e.Message);
            }
            catch (NoNullAllowedException e)
            {
                OnRowError(row, e.Message);
            }
        }
Exemple #3
0
        private static JToken Generate(KeyValuePair <string, JSchema> property, ICsvRow row)
        {
            var schema = property.Value;

            if (schema.Type == null)
            {
                if (schema.Const == null)
                {
                    schema.Type = JSchemaType.Object;
                }
            }
            JToken output = null;

            switch (schema.Type)
            {
            case JSchemaType.Object:
                JObject jObject = null;
                if (schema.Properties != null)
                {
                    jObject = new JObject();

                    foreach (var prop in schema.Properties)
                    {
                        jObject.Add(prop.Key, Generate(prop, row));
                    }
                    output = jObject;
                }
                if (schema.OneOf.Count > 0)
                {
                    if (jObject == null)
                    {
                        jObject = new JObject();
                    }

                    bool found = false;

                    foreach (var oneOf in schema.OneOf)
                    {
                        foreach (var prop in oneOf.Properties)
                        {
                            if (prop.Value.Const?.ToString() == row.DataType)
                            {
                                found = true;
                                var first = Generate(oneOf, row);

                                if (first is JObject)
                                {
                                    var firstObj = first as JObject;
                                    foreach (var p in firstObj)
                                    {
                                        jObject.Add(p.Key, p.Value);
                                    }
                                }
                            }
                        }
                    }

                    if (!found)
                    {
                        var item = schema.OneOf.FirstOrDefault();
                        if (item != null)
                        {
                            // Add first option
                            var first = Generate(item, row);

                            if (first is JObject)
                            {
                                var firstObj = first as JObject;
                                foreach (var prop in firstObj)
                                {
                                    jObject.Add(prop.Key, prop.Value);
                                }
                            }
                        }
                    }

                    output = jObject;
                }
                break;

            case JSchemaType.String:
            {
                string val = "";
                if (property.Key.EndsWith("-field-type"))
                {
                    val = "ignored";
                }
                if (schema.Pattern == "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
                {
                    val = Guid.Empty.ToString().ToLower();
                }
                else if (schema.Format == "ipv4")
                {
                    val = "127.0.0.1";
                }
                else if (schema.Enum.Count > 0)
                {
                    val = schema.Enum.First().ToString();
                }
                else if (property.Key == "command-id" || property.Key == "command-order")
                {
                    val = DefaultCommandId;
                }

                output = new JValue(val);
            }
            break;

            case JSchemaType.Number:
            {
                double num = 0.0;
                if (property.Key == "scale")
                {
                    num = 1;
                }
                output = new JValue(num);
            }
            break;

            case JSchemaType.Integer:
            {
                int num = 0;
                if (property.Key == "index" || property.Key == "lower_index")
                {
                    num = Convert.ToInt32(row.Index);
                }
                else if (property.Key == "upper_index")
                {
                    var modbus = row as ModbusCsvRow;
                    if (modbus != null)
                    {
                        num = Convert.ToInt32(modbus.UpperIndex);
                    }
                }
                output = new JValue(num);
            }
            break;

            case JSchemaType.Boolean:
                output = new JValue(false);
                break;

            case JSchemaType.Null:
                output = JValue.CreateNull();
                break;

            default:
                if (schema.Const != null)
                {
                    output = new JValue(schema.Const.ToString());
                }
                else
                {
                    output = Generate(schema);
                }
                break;
            }

            return(output);
        }
Exemple #4
0
        public static JToken Generate(JSchema schema, ICsvRow row)
        {
            if (schema.Type == null)
            {
                if (schema.Const == null)
                {
                    schema.Type = JSchemaType.Object;
                }
            }
            JToken output = null;

            switch (schema.Type)
            {
            case JSchemaType.Object:
                JObject jObject = null;
                if (schema.Properties != null)
                {
                    jObject = new JObject();

                    foreach (var prop in schema.Properties)
                    {
                        jObject.Add(prop.Key, Generate(prop, row));
                    }
                    output = jObject;
                }
                if (schema.OneOf.Count > 0)
                {
                    if (jObject == null)
                    {
                        jObject = new JObject();
                    }

                    bool found = false;

                    foreach (var oneOf in schema.OneOf)
                    {
                        foreach (var prop in oneOf.Properties)
                        {
                            if (prop.Value.Const?.ToString() == row.DataType)
                            {
                                found = true;
                                var first = Generate(oneOf, row);

                                if (first is JObject)
                                {
                                    var firstObj = first as JObject;
                                    foreach (var property in firstObj)
                                    {
                                        jObject.Add(property.Key, property.Value);
                                    }
                                }
                            }
                        }
                    }

                    if (!found)
                    {
                        var item = schema.OneOf.FirstOrDefault();
                        if (item != null)
                        {
                            // Add first option
                            var first = Generate(item, row);

                            if (first is JObject)
                            {
                                var firstObj = first as JObject;
                                foreach (var prop in firstObj)
                                {
                                    jObject.Add(prop.Key, prop.Value);
                                }
                            }
                        }
                    }

                    output = jObject;
                }
                break;

            case JSchemaType.Array:
                var jArray = new JArray();

                foreach (var item in schema.Items)
                {
                    jArray.Add(Generate(item, row));
                }
                output = jArray;
                break;

            case JSchemaType.String:
            {
                string val = "ignored";
                if (schema.Pattern == "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
                {
                    val = Guid.Empty.ToString().ToLower();
                }
                else if (schema.Format == "ipv4")
                {
                    val = "127.0.0.1";
                }
                else if (schema.Enum.Count > 0)
                {
                    val = schema.Enum.First().ToString();
                }
                output = new JValue(val);
            }
            break;

            case JSchemaType.Number:
            {
                double num = 0.0;
                output = new JValue(num);
            }
            break;

            case JSchemaType.Integer:
            {
                int num = 0;
                output = new JValue(num);
            }
            break;

            case JSchemaType.Boolean:
                output = new JValue(false);
                break;

            case JSchemaType.Null:
                output = JValue.CreateNull();
                break;

            default:
                if (schema.Const != null)
                {
                    try
                    {
                        if (schema.Const is JValue)
                        {
                            output = new JValue(schema.Const.ToString());
                        }
                        else
                        {
                            output = null;
                        }
                    }
                    catch
                    {
                        output = null;
                    }
                }
                else
                {
                    Console.WriteLine("SchemaType is null!!!!!");
                    output = null;
                }
                break;
            }

            return(output);
        }
Exemple #5
0
 private void OnRowError(ICsvRow row, string msg)
 {
     Console.WriteLine($"Error reading row {row.RowNum}:");
     Console.WriteLine(string.Join(_csvDef.Options.Delimiter, row.AsStringArray()));
     Console.WriteLine(msg);
 }
Exemple #6
0
 public DynamicCsvRow(ICsvRow row)
 {
     _row = row;
 }