1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using System;
- using System.Collections.Generic;
- using InABox.Core;
- using Scriban;
- using Scriban.Runtime;
- namespace PRSDesktop;
- public static class DataModelUtils
- {
- public static string ParseTemplate(DataModel model, string data)
- {
- if (string.IsNullOrWhiteSpace(data))
- return "";
- var template = Template.Parse(data);
- if (template.HasErrors) throw new Exception(string.Join("\n", template.Messages));
- var templatecontext = new TemplateContext
- {
- MemberRenamer = member => member.Name
- };
- var so = new ScriptObject();
- ScriptObjectExtensions.Import(so, "string.to_base64", new Func<byte[], string>(b => Convert.ToBase64String(b)));
- foreach (var (key, table) in model.ModelTables)
- {
- var objects = new List<object>();
- if (table.Type is not null)
- {
- foreach (var tablerow in table.Table.Rows)
- objects.Add(tablerow.ToObject(table.Type));
- }
- so.Add(key, objects);
- }
- templatecontext.PushGlobal(so);
- return template.Render(templatecontext);
- }
- }
|