public DefaultTransform(IContext context, IEnumerable <Field> fields) : base(context, null) { var expanded = fields.ToArray(); var defaults = Constants.TypeDefaults(); foreach (var field in expanded) { var hasDefault = field.Default != Constants.DefaultSetting; var fieldDefault = new FieldDefault(field.Name, field.Alias, field.Index, field.MasterIndex, field.KeyIndex, field.Type) { Value = hasDefault ? field.Convert(field.Default) : defaults[field.Type], StringValue = hasDefault ? field.Default : string.Empty, DefaultWhiteSpace = field.DefaultWhiteSpace }; if (field.IsCalculated) { CalculatedFieldDefaults.Add(fieldDefault); } else { FieldDefaults.Add(fieldDefault); } } }
public DefaultTransform(IContext context, IEnumerable <Field> fields) : base(context, null) { var expanded = fields.ToArray(); var defaults = Constants.TypeDefaults(); var canConvert = Constants.CanConvert(); foreach (var field in expanded) { var hasDefault = field.Default != Constants.DefaultSetting; var value = defaults[field.Type]; if (hasDefault) { switch (field.Type) { case "char" when field.Default.Length > 1: field.Default = field.Default[0].ToString(); break; case "string" when field.Length != "max" && int.TryParse(field.Length, out var length): field.Default = field.Default.Substring(0, Math.Min(field.Default.Length, length)); break; } if (canConvert[field.Type](field.Default)) { value = field.Convert(field.Default); } else { Context.Warn($"The default value {field.Default} can not be converted to {field.Type}. Setting default value to {value} instead."); } } var fieldDefault = new FieldDefault(field.Name, field.Alias, field.Index, field.MasterIndex, field.KeyIndex, field.Type) { Value = value, StringValue = hasDefault ? field.Default : string.Empty, DefaultWhiteSpace = field.DefaultWhiteSpace, DefaultEmpty = field.DefaultEmpty, OnlyDefaultNulls = !field.DefaultWhiteSpace && !field.DefaultEmpty }; if (field.IsCalculated) { CalculatedFieldDefaults.Add(fieldDefault); } else { FieldDefaults.Add(fieldDefault); } } }
public DefaultTransform(IContext context, IEnumerable <Field> fields) : base(context, null) { var expanded = fields.ToArray(); var defaults = Constants.TypeDefaults(); foreach (var field in expanded) { var hasDefault = field.Default != Constants.DefaultSetting; if (hasDefault) { switch (field.Type) { case "char" when field.Default.Length > 1: field.Default = field.Default[0].ToString(); break; case "string" when field.Length != "max" && int.TryParse(field.Length, out var length): field.Default = field.Default.Substring(0, Math.Min(field.Default.Length, length)); break; } } var fieldDefault = new FieldDefault(field.Name, field.Alias, field.Index, field.MasterIndex, field.KeyIndex, field.Type) { Value = hasDefault ? field.Convert(field.Default) : defaults[field.Type], StringValue = hasDefault ? field.Default : string.Empty, DefaultWhiteSpace = field.DefaultWhiteSpace, DefaultEmpty = field.DefaultEmpty }; if (field.IsCalculated) { CalculatedFieldDefaults.Add(fieldDefault); } else { FieldDefaults.Add(fieldDefault); } } }