Processors
Compute values at build time with member, record and whole-dataset processors, and report warnings and errors with file and line.
On this page
public sealed class WordCounter : IProcessor<string, int>
{
public int Process(string text) => text.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
}
public sealed record BlogPost(
string Slug, string RawMarkdown,
[Process<WordCounter>(nameof(RawMarkdown))] int WordCount = 0);
A processor computes a value at build time. Bevel ships none. You write them, so you choose the library. There are three kinds:
| Form | Declared as | Implements |
|---|---|---|
| Member | [Process<T>(nameof(Source))] on a property or constructor parameter |
IProcessor<TSource, TResult> |
| Record | [Process<T>] on the item type. Optional Order (lower first). Repeatable. |
IProcessor<TItem> |
| Whole dataset Since 0.3 | [PostProcess<T>] on the item type. Optional Order. Repeatable. |
IDatasetProcessor<TItem> |
public interface IProcessor<in TSource, TResult> { TResult Process(TSource source); }
public interface IProcessor<TItem> { ProcessResult<TItem> Process(TItem item); }
Process several members at once
Put [Process<T>] on the record and write an IProcessor<TItem>. It gets the item and returns a copy (usually from a with expression, so it can set init members too), or ProcessResult.Remove to drop it. This one gets the word count and reading time from one parse:
[DataSource("Posts", "./*.md", OrderBy = "Date desc, Title")]
[Process<PostStats>]
public sealed record BlogPost(
string Slug, string Title, DateOnly Date, string RawMarkdown,
int WordCount = 0, int ReadingMinutes = 0);
public sealed class PostStats(IBlogMarkdown markdown, IReadingTimeCalculator time) : IProcessor<BlogPost>
{
public ProcessResult<BlogPost> Process(BlogPost post)
{
var words = markdown.CountWords(post.RawMarkdown);
return post with { WordCount = words, ReadingMinutes = time.Minutes(words) };
}
}
TItem and ProcessResult.Remove (a RemoveMarker) both convert implicitly to ProcessResult<TItem>. IsRemoved tells the two apart. Item throws when the item was removed. For dropping items, see Drafts and removing items.
The order things run
- Bevel binds the file.
- Member processors run.
- Record processors run (lowest
Orderfirst, then the order you wrote them). - Since 0.3 Dataset processors (
[PostProcess<T>]) run, once per dataset. OrderBy,[Unique], references, groups and later datasets run. All of them see the processed values.
Rules
- A processed value replaces whatever the file or front matter says. There is no error.
- A processor can take constructor dependencies from your Build-time services. Its type must live under
Data/.IDisposableandIAsyncDisposableprocessors are disposed after the dataset loads. - Processors are synchronous. One that throws, or a record processor that returns
null(or a result holdingnull), fails the build (BEVEL085). - Processors work for JSON, YAML, XML and Markdown datasets, lists and single objects (except removal, BEVEL093).
- A member processor cannot read another processed member, and cannot read itself. A member cannot have both
[Reference]and[Process]. - After all processors ran, a non-nullable reference member that is still
nullfails with BEVEL092. - Output is embedded in the generated code, like any dataset. A processor cannot use
ISiteLinks. If a value needs links, or you would rather not store it, compute it in your component.
Processor diagnostics are BEVEL081 to BEVEL088 and BEVEL093.
Report problems with file and line Since 0.3
Take BevelLoadContext in your constructor. It is registered in the build-time services. Report warnings and errors with your own code:
public sealed class DescriptionCheck(BevelLoadContext ctx) : IProcessor<string, string>
{
public string Process(string text)
{
if (text.Length > 160)
ctx.ReportWarning("DOC001", "Description is longer than 160 characters.", "Data/Docs/intro.md", line: 3);
return text;
}
}
ReportWarning, ReportError and Report(BevelSeverity, ...) take a code, a message, and an optional file, line and column.
- Messages print as
path(line,col): warning CODE: message, sodotnet buildand your IDE show them. - An error fails the build after all datasets have loaded. One run lists every problem.
- Codes use letters, digits,
_and-. Use your own prefix so they never clash withBEVELnnn. fileis relative to the content root, or starts with@/for the repo root.- Warnings appear when the data step runs. An up-to-date incremental build skips the step and does not repeat them.
Read files from the repo Since 0.3
A processor can read files outside its content root. Bevel tracks them as build inputs.
var sample = ctx.ReadAllText("@/samples/App/Program.cs");
ctx.RepoRootPathis the nearest parent directory with.git, else with a.sln/.slnx, else the content root. SetBevel:RepoRootin configuration to override it.ctx.ResolvePath("@/samples/App/Program.cs")resolves against the repo root. Other relative paths use the content root.ctx.ResolveRepoPath(path)is the repo-only form.ctx.ReadAllText(path),ctx.ReadTextAsync(path)andctx.FileExists(path)read through the context and register the file as a build input. Changing it (or creating a missing one) reruns the data step.- If you read a file yourself, call
ctx.RegisterInputFile(path).
Process a whole dataset Since 0.3
[PostProcess<T>] runs an IDatasetProcessor<TItem> once, after every item is bound and processed. Use it for previous and next links, navigation, cross-references and link checks. It returns the items, possibly replaced, filtered or reordered. It runs before OrderBy, [Unique] and groups.
[DataSource("Pages", "Data/Docs/**/*.md")]
[DependsOn("Releases")] // loaded first
[PostProcess<PageNav>]
public sealed record Page(string Slug, string Title, string? Prev = null, string? Next = null);
public sealed class PageNav(BevelLoadContext ctx) : IDatasetProcessor<Page>
{
public IReadOnlyList<Page> Process(IReadOnlyList<Page> items)
{
var releases = ctx.Get<IReadOnlyList<Release>>("Releases");
if (releases.Count == 0)
ctx.ReportWarning("DOC010", "No releases found.");
return items
.Select((p, i) => p with
{
Prev = i > 0 ? items[i - 1].Slug : null,
Next = i + 1 < items.Count ? items[i + 1].Slug : null,
})
.ToList();
}
}
[PostProcess<T>]is repeatable.Orderruns lower first.[DependsOn]works on[DataSource]item types. Read a dependency withctx.Get<T>("Name").- A single-object dataset fails with BEVEL097. A processor that throws or returns
nullfails with BEVEL097 too. An invalid processor type fails with BEVEL096.