Path: redb-route/redb.Route.Xml/BeanModel.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Xml/BeanModel.cs
----
using System.Xml.Linq;
namespace redb.Route.Xml;
///
/// The parsed form of a <bean> declaration: one grammar for the loader, the C#
/// generator and the package gate, so none of them re-reads the markup by its own rules.
///
internal sealed record BeanDeclaration(
string? Name,
string TypeName,
string? FactoryMethod,
IReadOnlyList ConstructorArgs,
IReadOnlyList Properties,
XElement At)
{
///
/// Whether the arguments name their parameter types — then the constructor or factory method
/// is the one with exactly that signature. All arguments do or none do (the parser enforces it).
///
public bool TypedArguments => ConstructorArgs.Count > 0 && ConstructorArgs[0].TypeName is not null;
}
///
/// One <constructorArg>: its value and, when written, the parameter type it binds to
/// (type="System.String", Spring's constructor-arg type) — how a constructor or a
/// factory method is chosen among overloads the values alone do not tell apart.
///
internal sealed record BeanArgument(BeanValue Value, string? TypeName);
/// One <property key=…> of a bean with its parsed value.
internal sealed record BeanProperty(string Key, BeanValue Value, XElement At);
/// A value slot of a bean: a property, a constructor argument or a list item.
internal abstract record BeanValue(XElement At);
/// A literal text, {{key}} placeholders included; converted to the target type.
internal sealed record ScalarBeanValue(string Text, XElement At) : BeanValue(At);
/// A registered bean, by name: ref="name" or <ref bean="name"/>.
internal sealed record RefBeanValue(string Bean, XElement At) : BeanValue(At);
/// A nested anonymous bean, built in place and never registered.
internal sealed record NestedBeanValue(BeanDeclaration Bean) : BeanValue(Bean.At);
///
/// A <list>: its items build into the target's collection shape. of= names
/// the element type where the target does not (a constructor argument).
///
internal sealed record ListBeanValue(string? ElementTypeName, IReadOnlyList Items, XElement At) : BeanValue(At);
///
/// The <bean> grammar. A slot (<property>, <constructorArg>)
/// holds exactly one of: value=, ref=, a nested <bean>, a
/// <list>. A list item is exactly one of: <value>text</value>,
/// <ref bean=…/>, <bean>, <list>. Every violation is
/// reported at its element; the parse returns null when anything was reported.
///
internal static class BeanModel
{
private const string SlotShape =
"takes exactly one of: value=, ref=, one nested anonymous , one ";
/// Parses a declaration; receives every problem with its element.
internal static BeanDeclaration? Parse(XElement bean, Action error)
{
var failed = false;
void Fail(XElement at, string message)
{
failed = true;
error(at, message);
}
var declaration = ParseBean(bean, Fail);
return failed ? null : declaration;
}
/// Every ref of a declaration, nested beans and lists included, in document order.
internal static IEnumerable References(BeanDeclaration bean)
=> bean.ConstructorArgs.Select(a => a.Value).Concat(bean.Properties.Select(p => p.Value)).SelectMany(References);
/// Every nested anonymous bean of a declaration, at any depth.
internal static IEnumerable NestedBeans(BeanDeclaration bean)
=> bean.ConstructorArgs.Select(a => a.Value).Concat(bean.Properties.Select(p => p.Value)).SelectMany(NestedBeans);
private static IEnumerable References(BeanValue value) => value switch
{
RefBeanValue reference => [reference],
NestedBeanValue nested => References(nested.Bean),
ListBeanValue list => list.Items.SelectMany(References),
_ => [],
};
private static IEnumerable NestedBeans(BeanValue value) => value switch
{
NestedBeanValue nested => NestedBeans(nested.Bean).Prepend(nested.Bean),
ListBeanValue list => list.Items.SelectMany(NestedBeans),
_ => [],
};
private static BeanDeclaration ParseBean(XElement bean, Action fail)
{
var typeName = bean.Attribute("type")?.Value;
if (string.IsNullOrWhiteSpace(typeName))
fail(bean, " requires the 'type' attribute.");
var args = new List();
var properties = new List();
foreach (var child in bean.Elements())
{
switch (child.Name.LocalName)
{
case "constructorArg":
var argType = child.Attribute("type")?.Value;
if (argType is not null && string.IsNullOrWhiteSpace(argType))
{
fail(child, " names no type.");
break;
}
if (ParseSlot(child, fail) is { } arg)
args.Add(new BeanArgument(arg, argType));
break;
case "property":
var key = child.Attribute("key")?.Value;
if (string.IsNullOrWhiteSpace(key))
{
fail(child, " requires the 'key' attribute.");
break;
}
if (ParseSlot(child, fail) is { } value)
properties.Add(new BeanProperty(key, value, child));
break;
default:
fail(child, $" does not take <{child.Name.LocalName}> (valid: , ).");
break;
}
}
// One contract per bean: the signature is named in full or chosen from the values alone.
if (args.Count > 0 && args.Any(a => a.TypeName is null) && args.Any(a => a.TypeName is not null))
fail(bean, " is written on some arguments only — name every parameter type, or none.");
return new BeanDeclaration(
bean.Attribute("name")?.Value, typeName ?? "", bean.Attribute("factoryMethod")?.Value is { Length: > 0 } factory ? factory : null,
args, properties, bean);
}
private static BeanValue? ParseSlot(XElement slot, Action fail)
{
var value = slot.Attribute("value");
var reference = slot.Attribute("ref");
var children = slot.Elements().ToList();
var forms = (value is null ? 0 : 1) + (reference is null ? 0 : 1) + children.Count;
if (forms != 1)
{
fail(slot, $"<{slot.Name.LocalName}> {SlotShape}.");
return null;
}
if (value is not null)
return new ScalarBeanValue(value.Value, slot);
if (reference is not null)
return ParseReference(reference.Value, slot, fail);
return children[0].Name.LocalName switch
{
"bean" => ParseNested(children[0], fail),
"list" => ParseList(children[0], fail),
_ => Refuse(children[0], $"<{slot.Name.LocalName}> {SlotShape}; <{children[0].Name.LocalName}> belongs inside a .", fail),
};
}
private static BeanValue? ParseItem(XElement item, Action fail)
{
switch (item.Name.LocalName)
{
case "value":
if (item.HasElements)
return Refuse(item, " holds text only.", fail);
return new ScalarBeanValue(item.Value, item);
case "ref":
return item.Attribute("bean")?.Value is { } name
? ParseReference(name, item, fail)
: Refuse(item, "[ requires the 'bean' attribute.", fail);
case "bean":
return ParseNested(item, fail);
case "list":
return ParseList(item, fail);
default:
return Refuse(item, $"] does not take <{item.Name.LocalName}> (valid: , [, , ]).", fail);
}
}
private static BeanValue? ParseNested(XElement bean, Action fail)
{
if (bean.Attribute("name") is not null)
return Refuse(bean, "a nested is anonymous — drop name=, or declare it at the top level and use ref=.", fail);
return new NestedBeanValue(ParseBean(bean, fail));
}
private static BeanValue ParseList(XElement list, Action fail)
{
var items = new List();
foreach (var item in list.Elements())
{
if (ParseItem(item, fail) is { } parsed)
items.Add(parsed);
}
return new ListBeanValue(list.Attribute("of")?.Value is { Length: > 0 } of ? of : null, items, list);
}
private static BeanValue? ParseReference(string name, XElement at, Action fail)
{
// One spelling: the attribute already says it is a reference, so the URI form '#name'
// is not repeated here.
if (name.Length == 0)
return Refuse(at, "a bean reference needs a name.", fail);
if (name.StartsWith('#'))
return Refuse(at, $"a bean reference is the bare name: '{name[1..]}', not '{name}'.", fail);
return new RefBeanValue(name, at);
}
private static BeanValue? Refuse(XElement at, string message, Action fail)
{
fail(at, message);
return null;
}
}