Datasets now serialize/save and deserialize/load properly. Created a test in the CLI project
This commit is contained in:
34
SzCli/LocalFileManager.cs
Normal file
34
SzCli/LocalFileManager.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using SzLib;
|
||||
|
||||
namespace SzCli;
|
||||
|
||||
public class LocalFileManager : ISzFileManager
|
||||
{
|
||||
public string DataPath
|
||||
{
|
||||
get
|
||||
{
|
||||
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
public bool SaveFile(string path, string fileContent)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.WriteAllText(path, fileContent);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error saving file: {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,26 @@
|
||||
using System;
|
||||
using SzLib;
|
||||
|
||||
namespace SzCli;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var parser = new SzParser(new LocalFileManager());
|
||||
|
||||
Console.WriteLine("Creating test dataset...");
|
||||
var dataset = Test.CreateTestDataset();
|
||||
Console.WriteLine($"Successfully created test dataset with id {dataset.Id}");
|
||||
|
||||
Console.WriteLine("Attempting to save dataset...");
|
||||
var saved = parser.SaveDataset(dataset);
|
||||
Console.WriteLine($"Successfully saved dataset? {saved}");
|
||||
|
||||
Console.WriteLine("Attempting to load dataset...");
|
||||
var loadedDataset = parser.LoadDataset(dataset.Id);
|
||||
Console.WriteLine($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset.Id}");
|
||||
|
||||
Console.WriteLine(loadedDataset.DataObjects["test"].Fields["value"].Value);
|
||||
}
|
||||
}
|
||||
36
SzCli/Test.cs
Normal file
36
SzCli/Test.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using SzLib.DataObjects;
|
||||
|
||||
namespace SzCli;
|
||||
|
||||
public static class Test
|
||||
{
|
||||
public static SzDataset CreateTestDataset()
|
||||
{
|
||||
var ds = new SzDataset()
|
||||
{
|
||||
Name = "TestDataset",
|
||||
Id = "test-dataset",
|
||||
DataObjectTemplateId = "0",
|
||||
DataObjectType = "item",
|
||||
Uuid = Guid.NewGuid(),
|
||||
};
|
||||
|
||||
var testDataObject = new SzDataObject()
|
||||
{
|
||||
Name = "TestObject",
|
||||
Id = "test",
|
||||
};
|
||||
|
||||
var testField = new SzField()
|
||||
{
|
||||
Id = "value",
|
||||
FieldType = SzFieldType.Number,
|
||||
Value = "200"
|
||||
};
|
||||
|
||||
testDataObject.Fields.Add(testField.Id, testField);
|
||||
ds.DataObjects.Add(testDataObject.Id, testDataObject);
|
||||
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzDataObject
|
||||
{
|
||||
public required string Name;
|
||||
public required string Id;
|
||||
public Dictionary<string, SzField> Fields = [];
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public Dictionary<string, SzField> Fields {get; set;} = [];
|
||||
}
|
||||
@@ -2,9 +2,9 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzDataObjectTemplate
|
||||
{
|
||||
public required string Name;
|
||||
public required string Id;
|
||||
public Guid? Uuid;
|
||||
public required string DataObjectType;
|
||||
public Dictionary<string, SzTemplateField> TemplateFields = [];
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public Guid? Uuid {get; set;}
|
||||
public required string DataObjectType {get; set;}
|
||||
public Dictionary<string, SzTemplateField> TemplateFields {get; set;} = [];
|
||||
}
|
||||
@@ -2,11 +2,11 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzDataset
|
||||
{
|
||||
public required string Name;
|
||||
public required string Id;
|
||||
public Guid? Uuid;
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public Guid? Uuid {get; set;}
|
||||
|
||||
public required string DataObjectType;
|
||||
public required string DataObjectTemplateId;
|
||||
public Dictionary<string, SzDataObject> DataObjects = [];
|
||||
public required string DataObjectType {get; set;}
|
||||
public required string DataObjectTemplateId {get; set;}
|
||||
public Dictionary<string, SzDataObject> DataObjects {get; set;} = [];
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzField
|
||||
{
|
||||
public required string Id;
|
||||
public required SzFieldType FieldType;
|
||||
public bool IsList = false;
|
||||
public string Value = "";
|
||||
public required string Id {get; set;}
|
||||
public required SzFieldType FieldType {get; set;}
|
||||
public bool IsList {get; set;} = false;
|
||||
public string Value {get; set;} = "";
|
||||
}
|
||||
@@ -2,10 +2,10 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzTemplateField
|
||||
{
|
||||
public required string Name;
|
||||
public required string Id;
|
||||
public bool IsList = false;
|
||||
public string DefaultValue = "";
|
||||
public bool IsSpecialType = false;
|
||||
public string SpecialTypeValue = "";
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public bool IsList {get; set;} = false;
|
||||
public string DefaultValue {get; set;} = "";
|
||||
public bool IsSpecialType {get; set;} = false;
|
||||
public string SpecialTypeValue {get; set;} = "";
|
||||
}
|
||||
@@ -2,5 +2,7 @@ namespace SzLib;
|
||||
|
||||
public interface ISzFileManager
|
||||
{
|
||||
public string DataPath {get; set;}
|
||||
public string DataPath {get;}
|
||||
|
||||
public bool SaveFile(string path, string fileContent);
|
||||
}
|
||||
@@ -3,37 +3,71 @@ using SzLib.DataObjects;
|
||||
|
||||
namespace SzLib;
|
||||
|
||||
public class SzParser
|
||||
public class SzParser(ISzFileManager szFileManager)
|
||||
{
|
||||
public readonly ISzFileManager FileManager;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
||||
|
||||
private JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
};
|
||||
|
||||
public SzParser(ISzFileManager szFileManager)
|
||||
{
|
||||
FileManager = szFileManager;
|
||||
}
|
||||
|
||||
public (string result, SzParseError error) SerializeDatasetToJson(SzDataset dataset)
|
||||
public string SerializeDatasetToJson(SzDataset dataset)
|
||||
{
|
||||
try
|
||||
{
|
||||
var jsonString = JsonSerializer.Serialize(dataset, _jsonOptions);
|
||||
return (jsonString, SzParseError.None);
|
||||
return jsonString;
|
||||
}
|
||||
catch (JsonException e)
|
||||
{
|
||||
return ("", SzParseError.CouldNotSerialize);
|
||||
throw new Exception("Parse Error: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SzDataset? DeserializeDataset(string jsonString)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = JsonSerializer.Deserialize<SzDataset>(jsonString, _jsonOptions);
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception("Could not deserialize JSON to type SzDataset: " + e.Message, e);
|
||||
}
|
||||
}
|
||||
|
||||
public bool SaveDataset(SzDataset dataset)
|
||||
{
|
||||
var datasetDir = Path.Combine(szFileManager.DataPath, "datasets", dataset.Id);
|
||||
Directory.CreateDirectory(datasetDir);
|
||||
try
|
||||
{
|
||||
var json = SerializeDatasetToJson(dataset);
|
||||
File.WriteAllText(Path.Combine(datasetDir, "dataset.json"), json);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Error saving dataset: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public SzDataset? LoadDataset(string datasetId)
|
||||
{
|
||||
var datasetPath = Path.Combine(szFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||
if (!File.Exists(datasetPath))
|
||||
{
|
||||
Console.WriteLine("Dataset not found: " + datasetId);
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return DeserializeDataset(File.ReadAllText(datasetPath));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error loading dataset: {e.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum SzParseError
|
||||
{
|
||||
None,
|
||||
CouldNotSerialize,
|
||||
GenericError,
|
||||
}
|
||||
Reference in New Issue
Block a user