genrules

command
v0.0.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2025 License: BSD-3-Clause Imports: 13 Imported by: 0

README

genrules - EN 16931 Business Rule Generator

Auto-generates Go code for EN 16931 electronic invoicing business rules from official schematron specifications.

Overview

genrules parses EN 16931 schematron XML files and generates type-safe Go constants for business rule validation. It extracts rule codes, BT-/BG- field references, and descriptions from the official CEN/TC 434 specifications.

Installation

go install github.com/speedata/einvoice/cmd/genrules@latest

Or build from source:

go build -o genrules cmd/genrules/main.go

Usage

Basic Usage

Generate rules from a URL:

genrules \
  --source https://raw.githubusercontent.com/ConnectingEurope/eInvoicing-EN16931/master/cii/schematron/abstract/EN16931-CII-model.sch \
  --version v1.3.14.1 \
  --package rules \
  --output rules/en16931.go

Generate rules from a local file:

genrules \
  --source path/to/EN16931-CII-model.sch \
  --version v1.3.14.1 \
  --package rules \
  --output rules/en16931.go
Using go generate

The recommended workflow uses go generate for reproducible builds:

# Generate from rules/ directory
cd rules && go generate

# Or generate all packages
go generate ./...

Flags

  • --source (required): Path or URL to schematron XML file
  • --version (required): Source file version (e.g., "v1.3.14.1")
  • --package (default: "rules"): Target Go package name
  • --output (default: "rules/en16931.go"): Output file path

Output Format

Generates Go code with:

  • Package declaration and Rule struct definition
  • Generation metadata (source, version, timestamp)
  • 203 business rule constants from EN 16931 specification
  • Sorted by rule code (BR-1, BR-2, ..., BR-CO-10, ..., BR-S-1, ...)

Example output:

// Code generated by genrules from EN16931-CII-model.sch; DO NOT EDIT.
// Source: https://github.com/ConnectingEurope/eInvoicing-EN16931
// Version: v1.3.14.1
// Generated: 2025-10-07T12:31:26Z

package rules

type Rule struct {
    Code        string
    Fields      []string
    Description string
}

var (
    BR1 = Rule{
        Code:        "BR-01",
        Fields:      []string{"BT-24"},
        Description: `An Invoice shall have a Specification identifier (BT-24).`,
    }
    // ... 202 more rules
)

Rule Naming Conventions

The generator converts schematron rule codes to Go identifiers:

  • BR-01BR1 (remove leading zeros)
  • BR-S-08BRS8 (remove dashes and leading zeros)
  • BR-CO-14BRCO14 (remove all dashes)
  • BR-AE-1BRAE1 (standard format)

Architecture

Parsing
  1. Fetch schematron XML from URL or file
  2. Parse <pattern>, <rule>, and <assert> elements
  3. Extract rule code from assert @id attribute
  4. Extract description from assert test text
  5. Extract BT-/BG- field references using regex \(B[TG]-\d+\)
Code Generation
  1. Clean descriptions (remove [BR-XX]- prefix, normalize whitespace)
  2. Deduplicate and sort field references
  3. Convert rule codes to Go identifiers
  4. Sort rules alphabetically by code
  5. Generate Go code using text/template
  6. Format output with gofmt

Custom Rules

The generated rules/en16931.go file includes a custom rules section that must be manually preserved:

  • Check: Line total calculation validation
  • BR34-40: Non-negative amount validations (not in official spec)
  • BRIG1-10: IGIC aliases (BR-AF-* in spec)
  • BRIP1-10: IPSI aliases (BR-AG-* in spec)

⚠️ Important: When regenerating rules, manually add back the custom rules section marked by the comment banner.

Testing

The generator includes built-in validation:

  1. Verifies 200+ rules are extracted
  2. Checks rule code format
  3. Validates field references (BT-/BG- format)
  4. Ensures descriptions are non-empty
  5. Reports generation statistics

Sources

Maintenance

To update rules to a new EN 16931 version:

  1. Check for new releases at ConnectingEurope/eInvoicing-EN16931
  2. Update --version flag in rules/en16931.go go:generate directive
  3. Run cd rules && go generate
  4. Manually re-add custom rules section
  5. Run tests: go test ./...
  6. Commit changes with version number in message

License

Generated rules are derived from the EN 16931 specification, which is maintained by CEN/TC 434.

Documentation

Overview

genrules generates Go code from EN 16931 schematron specifications.

This tool parses official schematron files and generates Rule definitions for the einvoice/rules package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL