Templates
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using SzCore;
|
using SzCore;
|
||||||
|
using SzCore.DataObjects;
|
||||||
using SzCore.Defaults;
|
using SzCore.Defaults;
|
||||||
|
|
||||||
namespace SzCli;
|
namespace SzCli;
|
||||||
@@ -13,18 +14,9 @@ public class Program
|
|||||||
{
|
{
|
||||||
SZ.Init(_fileManager, _logger);
|
SZ.Init(_fileManager, _logger);
|
||||||
|
|
||||||
// _logger.Log("Creating test dataset...");
|
Test.CreateTestData();
|
||||||
// var dataset = Test.CreateTestDataset();
|
|
||||||
// _logger.Log($"Successfully created test dataset with id {dataset.Id}");
|
|
||||||
|
|
||||||
// _logger.Log("Attempting to save dataset...");
|
var dataTemplate = SZ.SzDataHandler.LoadTemplate<SzDataObjectTemplate>("szcore-item-basic");
|
||||||
// var saved = parser.SaveDataset(dataset);
|
_logger.Log($"Test - {dataTemplate?.DataObjectType ?? "Template was null"}");
|
||||||
// _logger.Log($"Successfully saved dataset? {saved}");
|
|
||||||
|
|
||||||
// _logger.Log("Attempting to load dataset...");
|
|
||||||
// var loadedDataset = parser.LoadDataset(dataset.Id);
|
|
||||||
// _logger.Log($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset?.Id}");
|
|
||||||
|
|
||||||
// _logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value ?? "Could not get value of test.value");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,16 @@
|
|||||||
|
using SzCore;
|
||||||
using SzCore.DataObjects;
|
using SzCore.DataObjects;
|
||||||
|
|
||||||
namespace SzCli;
|
namespace SzCli;
|
||||||
|
|
||||||
public static class Test
|
public static class Test
|
||||||
{
|
{
|
||||||
public static SzDataset CreateTestDataset()
|
public static void CreateTestData()
|
||||||
|
{
|
||||||
|
|
||||||
|
SZ.Logger.Log(" -- CreateTestData Test --");
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var dt = new SzDataObjectTemplate(){
|
var dt = new SzDataObjectTemplate(){
|
||||||
Name = "SzCore - Basic Item",
|
Name = "SzCore - Basic Item",
|
||||||
@@ -22,6 +28,8 @@ public static class Test
|
|||||||
|
|
||||||
dt.TemplateFields.Add(testTemplateField.Id, testTemplateField);
|
dt.TemplateFields.Add(testTemplateField.Id, testTemplateField);
|
||||||
|
|
||||||
|
SZ.SzDataHandler.SaveTemplate(dt);
|
||||||
|
|
||||||
var ds = new SzDataset()
|
var ds = new SzDataset()
|
||||||
{
|
{
|
||||||
Name = "TestDataset",
|
Name = "TestDataset",
|
||||||
@@ -48,6 +56,13 @@ public static class Test
|
|||||||
testDataObject.Fields.Add(testField.Id, testField);
|
testDataObject.Fields.Add(testField.Id, testField);
|
||||||
ds.DataObjects.Add(testDataObject.Id, testDataObject);
|
ds.DataObjects.Add(testDataObject.Id, testDataObject);
|
||||||
|
|
||||||
return ds;
|
SZ.SzDataHandler.SaveDataset(ds);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogError($"CreateTestData test failed: {e}");
|
||||||
|
}
|
||||||
|
|
||||||
|
SZ.Logger.Log(" -- CreateTestData Test: Success --");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
10
SzCore/DataObjects/ISzTemplate.cs
Normal file
10
SzCore/DataObjects/ISzTemplate.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace SzCore.DataObjects;
|
||||||
|
|
||||||
|
public interface ISzTemplate
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Id { get; set; }
|
||||||
|
public Guid Uuid { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
|
public Dictionary<string, SzTemplateField> TemplateFields { get; set; }
|
||||||
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
namespace SzCore.DataObjects;
|
namespace SzCore.DataObjects;
|
||||||
|
|
||||||
public class SzDataObjectTemplate
|
public class SzDataObjectTemplate : ISzTemplate
|
||||||
{
|
{
|
||||||
public required string Name {get; set;}
|
public required string Name {get; set;}
|
||||||
public required string Id {get; set;}
|
public required string Id {get; set;}
|
||||||
public Guid? Uuid {get; set;}
|
public Guid Uuid {get; set;}
|
||||||
public required string DataObjectType {get; set;}
|
public string Description { get; set; } = "";
|
||||||
public Dictionary<string, SzTemplateField> TemplateFields {get; set;} = [];
|
public Dictionary<string, SzTemplateField> TemplateFields {get; set;} = [];
|
||||||
|
|
||||||
|
public required string DataObjectType {get; set;}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.ComponentModel.Design;
|
||||||
using SzCore.DataObjects;
|
using SzCore.DataObjects;
|
||||||
|
|
||||||
namespace SzCore;
|
namespace SzCore;
|
||||||
@@ -5,6 +6,7 @@ namespace SzCore;
|
|||||||
public class SzDataHandler
|
public class SzDataHandler
|
||||||
{
|
{
|
||||||
private Dictionary<string, SzDataset> _loadedDatasets = [];
|
private Dictionary<string, SzDataset> _loadedDatasets = [];
|
||||||
|
private Dictionary<string, ISzTemplate> _loadedTemplates = [];
|
||||||
|
|
||||||
public SzDataHandler(){}
|
public SzDataHandler(){}
|
||||||
|
|
||||||
@@ -26,7 +28,7 @@ public class SzDataHandler
|
|||||||
{
|
{
|
||||||
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
||||||
{
|
{
|
||||||
if (value is not null) return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||||
@@ -49,19 +51,62 @@ public class SzDataHandler
|
|||||||
return parsedDataset;
|
return parsedDataset;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The client should periodically call this to free memory, probably
|
public bool SaveTemplate(ISzTemplate template)
|
||||||
private void ClearLoadedDatasets()
|
|
||||||
{
|
{
|
||||||
_loadedDatasets.Clear();
|
var templatePath = Path.Combine(SZ.LocalFileManager.TemplatesPath, template.Id, "template.json");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return SZ.LocalFileManager.SaveFile(templatePath, SZ.SzParser.SerializeTemplateToJson(template));
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogError("Error saving template: " + e.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only use this for test purposes, we probably don't want to load all datasets to memory in production
|
public T? LoadTemplate<T>(string templateId) where T : ISzTemplate
|
||||||
|
{
|
||||||
|
if (_loadedTemplates.TryGetValue(templateId, out ISzTemplate? existing))
|
||||||
|
{
|
||||||
|
return (T)existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
var templatePah = Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId, "template.json");
|
||||||
|
|
||||||
|
var json = SZ.LocalFileManager.LoadFile(templatePah);
|
||||||
|
if (json is null)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogWarning($"Could not load tetmplate with ID '{templateId}'");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsedTemplate = SZ.SzParser.DeserializeTemplate<T>(json);
|
||||||
|
if (parsedTemplate is null)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogWarning($"Could not load template with ID '{templateId}'");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
_loadedTemplates.TryAdd(parsedTemplate.Id, parsedTemplate);
|
||||||
|
|
||||||
|
return parsedTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The client should periodically call this to free memory, probably
|
||||||
|
private void ClearCache()
|
||||||
|
{
|
||||||
|
_loadedDatasets.Clear();
|
||||||
|
_loadedTemplates.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only use these two methods for test purposes, we probably don't want to load all datasets and templates to memory in production
|
||||||
private void LoadAllDatasets()
|
private void LoadAllDatasets()
|
||||||
{
|
{
|
||||||
ClearLoadedDatasets();
|
_loadedDatasets.Clear();
|
||||||
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
|
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
|
||||||
{
|
{
|
||||||
throw new Exception("Could not load datasets, the datase directory does not exist.");
|
throw new Exception("Could not load datasets, the dataset directory does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
|
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
|
||||||
@@ -82,4 +127,31 @@ public class SzDataHandler
|
|||||||
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LoadAllTemplates()
|
||||||
|
{
|
||||||
|
_loadedTemplates.Clear();
|
||||||
|
if (!Directory.Exists(SZ.LocalFileManager.TemplatesPath))
|
||||||
|
{
|
||||||
|
throw new Exception("Could not load templates, the template directory does not exist.");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.TemplatesPath))
|
||||||
|
{
|
||||||
|
var templateFilePath = Path.Combine(dir, "template.json");
|
||||||
|
if (!File.Exists(templateFilePath)) continue;
|
||||||
|
|
||||||
|
var templateJson = SZ.LocalFileManager.LoadFile(templateFilePath);
|
||||||
|
if (templateJson == null) continue;
|
||||||
|
|
||||||
|
var parsedTemplate = SZ.SzParser.DeserializeTemplate<ISzTemplate>(templateJson);
|
||||||
|
if (parsedTemplate == null)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogWarning($"Parse error: Could not parse file {templateFilePath} as an ISzTemplate");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
_loadedTemplates.Add(parsedTemplate.Id, parsedTemplate);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ public class SzParser
|
|||||||
}
|
}
|
||||||
catch (JsonException e)
|
catch (JsonException e)
|
||||||
{
|
{
|
||||||
throw new Exception("Parse Error: " + e.Message);
|
throw new Exception("Dataset Serialization Error: " + e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,5 +34,33 @@ public class SzParser
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string SerializeTemplateToJson(ISzTemplate template)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var templateType = template.GetType();
|
||||||
|
var jsonString = JsonSerializer.Serialize(template, templateType, _jsonOptions);
|
||||||
|
return jsonString;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new Exception("Template Serialization Error: " + e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T? DeserializeTemplate<T>(string jsonString)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = JsonSerializer.Deserialize<T>(jsonString, _jsonOptions);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogError($"Could not deserialize JSON to type {typeof(T)}: " + e.Message);
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user