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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user