Skip to content

Feature/bitfield change detector factory#2

Open
matt05d wants to merge 4 commits intomainfrom
feature/bitfield-change-detector-factory
Open

Feature/bitfield change detector factory#2
matt05d wants to merge 4 commits intomainfrom
feature/bitfield-change-detector-factory

Conversation

@matt05d
Copy link
Collaborator

@matt05d matt05d commented Mar 25, 2026

Generate change detection factory for schemas with leveled bitfields

Summary

  • The C# source generator can now emit a static {SchemaName}ChangeDetection factory class that creates a BitfieldChangeDetector pre-configured with all bitfield points that have level annotations
    (Warning/Error/Emergency). This eliminates the need for consumers to manually wire up tracking for each point — the factory stays in sync with the schema automatically.
  • Factory generation is opt-in via an MSBuild property, since it requires the consuming project to provide a BitfieldChangeDetector implementation. This keeps the Modspec schema language-agnostic —
    the JSON schema itself is unchanged, and generators for other languages (e.g. Python) can implement their own equivalent using the same level annotations. Modspec itself remains self-contained and does not
    depend on any specific change detection runtime.

Usage

Consuming C# projects opt in by adding the following to their .csproj:

  <PropertyGroup>
      <ModspecGenerateChangeDetectionFactory>true</ModspecGenerateChangeDetectionFactory>
  </PropertyGroup>

  <ItemGroup>
      <CompilerVisibleProperty Include="ModspecGenerateChangeDetectionFactory" />
  </ItemGroup>

The consuming project must provide a BitfieldChangeDetector class in the Modspec.Model namespace with a Track method matching the following signature:

  BitfieldChangeDetector<TClient> Track<T>(Func<TClient, T> getter, Func<T, Level> getLevel)
      where T : struct, Enum

Generated output example

For a schema named SomeBms with bitfield points StringErrors1 and StringErrors2 that have level annotations:

  public static class SomeBmsChangeDetection
  {
      public static BitfieldChangeDetector<SomeBmsClient> CreateDetector()
      {
          return new BitfieldChangeDetector<SomeBmsClient>()
              .Track(c => c.StringErrors1, v => v.GetLevel())
              .Track(c => c.StringErrors2, v => v.GetLevel());
      }
  }

Test plan

  • Modspec builds with factory generation disabled (default) — no BitfieldChangeDetector reference in generated output
  • Modspec builds with factory generation enabled — factory class generated for schemas with leveled bitfields
  • Three new test cases verify factory creation, change detection, and highest-level reporting
  • All 13 tests pass

For each schema with bitfield points that have level annotations, emit
a static factory method that returns a BitfieldChangeDetector configured
with the appropriate property getters and GetLevel calls. The runtime
change detection logic lives in the consuming project.
The source generator now emits a change detection factory that
references BitfieldChangeDetector<T>, which lives in the consuming
project. The test project needs a minimal stub so the generated
code compiles.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants