/// <inheritdoc /> public byte[] Serialize <TPrimaryKeyType, TModelType>(GGDBFTable <TPrimaryKeyType, TModelType> table) { RegisterTypeIfNotRegistered <TPrimaryKeyType, TModelType>(); //This allocates like HELL and is not efficient but this process is //offline for running time of the applications (only generation) //so ignore perf. using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize(ms, table); ms.Position = 0; return(ms.ToArray()); } }
/// <inheritdoc /> public async Task WriteAsync <TPrimaryKeyType, TModelType>(GGDBFTable <TPrimaryKeyType, TModelType> table, CancellationToken token = default) { if (table == null) { throw new ArgumentNullException(nameof(table)); } var bytes = Serializer.Serialize(table); //TODO: Any reason to support async?? Generator performance doesn't really matter tbh if (WriteToCurrentDirectory) { File.WriteAllBytes(Path.Combine(Directory.GetCurrentDirectory(), $"{table.TableName}.{GGDBFConstants.FILE_EXTENSION_SUFFIX}"), bytes); } else { File.WriteAllBytes(Path.Combine(OutputPath, $"{table.TableName}.{GGDBFConstants.FILE_EXTENSION_SUFFIX}"), bytes); } }
/// <summary> /// Converts a <see cref="GGDBFTable{TPrimaryKeyType,TModelType}"/> to another <see cref="GGDBFTable{TPrimaryKeyType,TModelType}"/> /// with a less derived type. /// </summary> /// <typeparam name="TPrimaryKeyType">Primary table key.</typeparam> /// <typeparam name="TModelType">Model type.</typeparam> /// <typeparam name="TSerializableModelType">The derived model type.</typeparam> /// <param name="table">The table to convert.</param> /// <returns></returns> public static GGDBFTable <TPrimaryKeyType, TModelType> ConvertFrom <TPrimaryKeyType, TModelType, TSerializableModelType>(this GGDBFTable <TPrimaryKeyType, TSerializableModelType> table) where TSerializableModelType : TModelType { if (table == null) { throw new ArgumentNullException(nameof(table)); } return(new GGDBFTable <TPrimaryKeyType, TModelType>() { Version = table.Version, TableName = table.TableName, TableData = table.TableData.ToDictionary(kvp => kvp.Key, kvp => (TModelType)kvp.Value) }); }