Skip to content

Commit 1268084

Browse files
dmealingclaude
andcommitted
feat(csharp): MetaDataLoader.FromDirectory/FromUris/FromString static factories
Cross-language consistent factory entry points for the 99% case. Each factory builds the appropriate Source(s) and delegates to Load(). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent cce3538 commit 1268084

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.IO;
2+
using MetaObjects.Loader;
3+
using Xunit;
4+
5+
namespace MetaObjects.Conformance.Tests;
6+
7+
public class MetaDataLoaderFactoryTests
8+
{
9+
[Fact]
10+
public void FromDirectory_LoadsAndReturnsRoot()
11+
{
12+
string dir = Path.Combine(Path.GetTempPath(), "fl_" + Path.GetRandomFileName());
13+
Directory.CreateDirectory(dir);
14+
try
15+
{
16+
File.WriteAllText(Path.Combine(dir, "meta.tiny.json"),
17+
"{\"metadata.root\":{\"package\":\"x\",\"children\":[]}}");
18+
var result = MetaDataLoader.FromDirectory(dir);
19+
Assert.Empty(result.Errors);
20+
Assert.NotNull(result.Root);
21+
}
22+
finally
23+
{
24+
Directory.Delete(dir, recursive: true);
25+
}
26+
}
27+
28+
[Fact]
29+
public void FromString_LoadsInlineJson()
30+
{
31+
var result = MetaDataLoader.FromString(
32+
"{\"metadata.root\":{\"package\":\"x\",\"children\":[]}}",
33+
MetaDataFormat.Json);
34+
Assert.Empty(result.Errors);
35+
}
36+
}

server/csharp/MetaObjects/Loader/MetaDataLoader.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,40 @@ public MetaDataLoader(TypeRegistry registry, bool freeze = true, bool strict = f
7474
private static TypeRegistry DefaultRegistry() =>
7575
Provider.ComposeRegistry([CoreTypes.CoreTypesProvider]);
7676

77+
// -------------------------------------------------------------------------
78+
// Static factories (the 99% case, cross-language consistent)
79+
// -------------------------------------------------------------------------
80+
81+
/// <summary>
82+
/// Convenience: build a <see cref="DirectorySource"/> for <paramref name="directory"/>
83+
/// and load all discovered files in deterministic order.
84+
/// </summary>
85+
public static LoadResult FromDirectory(string directory, DirectorySource.Options? opts = null)
86+
{
87+
var src = new DirectorySource(directory, opts);
88+
var loader = new MetaDataLoader();
89+
return loader.Load(src.Expand().Cast<IMetaDataSource>().ToList());
90+
}
91+
92+
/// <summary>
93+
/// Convenience: wrap each URI in a <see cref="UriSource"/> and load in order.
94+
/// </summary>
95+
public static LoadResult FromUris(IReadOnlyList<Uri> uris)
96+
{
97+
var loader = new MetaDataLoader();
98+
var sources = uris.Select(u => (IMetaDataSource)new UriSource(u)).ToList();
99+
return loader.Load(sources);
100+
}
101+
102+
/// <summary>
103+
/// Convenience: load a single in-memory string of the given format.
104+
/// </summary>
105+
public static LoadResult FromString(string content, MetaDataFormat format)
106+
{
107+
var loader = new MetaDataLoader();
108+
return loader.Load(new IMetaDataSource[] { new InMemoryStringSource(content, format: format) });
109+
}
110+
77111
// -------------------------------------------------------------------------
78112
// Public properties
79113
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)