Documentation
¶
Overview ¶
Package fixer provides the fix engine for cfv.
A Fixer analyzes source bytes (optionally with a JSON Schema) and produces a list of available fixes. Each fix is a targeted byte-range replacement that corrects a single issue (syntax error, schema violation, etc.).
Design principles:
- Fixes are byte-range edits, not AST rewrites. This keeps the engine format-agnostic and avoids comment/whitespace loss.
- Each fix is independently applicable. The engine handles conflicts by applying fixes left-to-right and dropping any that overlap with an already-applied fix.
- Safe fixes (--fix) never change semantic meaning beyond correcting the specific violation. Unsafe fixes (--fix --unsafe) may alter meaning (e.g., dropping duplicate keys).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Fix ¶
type Fix struct {
// RuleID identifies the fix rule (e.g., "json-trailing-comma",
// "schema-string-to-int").
RuleID string
// Message is a human-readable description of what the fix does.
Message string
// Category classifies the fix (syntax, schema).
Category FixCategory
// Safety indicates whether the fix is safe for automated application.
Safety FixSafety
// Line is the 1-based line number where the issue occurs.
Line int
// Start is the byte offset of the beginning of the region to replace.
Start int
// End is the byte offset of the end of the region to replace (exclusive).
End int
// Replacement is the bytes to substitute for src[Start:End].
Replacement []byte
}
Fix describes a single correctable issue and its replacement.
type FixCategory ¶
type FixCategory int
FixCategory classifies what kind of issue a fix addresses.
const ( // FixSyntax corrects a syntax error (e.g., trailing comma in JSON). FixSyntax FixCategory = iota // FixSchema corrects a schema violation (e.g., string→integer coercion). FixSchema )
type FixSafety ¶
type FixSafety int
FixSafety indicates whether a fix is safe to apply without human review.
type Fixer ¶
type Fixer struct {
// contains filtered or unexported fields
}
Fixer applies fix rules to source bytes.
func (*Fixer) Fix ¶
Fix analyzes src and applies all applicable fixes. schema is the raw JSON Schema bytes (nil if no schema is available). format is the file format name (e.g., "json", "yaml").
Fixes are applied left-to-right by byte offset. If two fixes overlap, the earlier one wins and the later one is dropped.
func (*Fixer) WithUnsafe ¶
WithUnsafe returns a copy of the Fixer that also applies unsafe fixes.
type JSONStringToBool ¶
type JSONStringToBool struct{}
JSONStringToBool detects JSON string values that should be booleans according to the provided JSON Schema, and produces fixes to coerce them.
type JSONStringToInt ¶
type JSONStringToInt struct{}
JSONStringToInt detects JSON string values that should be integers according to the provided JSON Schema, and produces fixes to coerce them.
type JSONTrailingComma ¶
type JSONTrailingComma struct{}
JSONTrailingComma removes trailing commas before ] and } in JSON. This is a safe syntax fix — trailing commas are never valid in JSON.
type Result ¶
type Result struct {
// Fixed is the corrected source content.
Fixed []byte
// Applied is the list of fixes that were successfully applied.
Applied []Fix
// Dropped is the list of fixes that were skipped due to conflicts
// (overlapping byte ranges with a previously applied fix).
Dropped []Fix
}
Result holds the outcome of applying fixes to a file.
type Rule ¶
type Rule interface {
// ID returns the unique identifier for this rule (e.g., "json-trailing-comma").
ID() string
// Detect analyzes src and returns fixes for issues this rule handles.
// schema is the raw JSON Schema bytes (nil if no schema available).
// format is the file format name (e.g., "json", "yaml", "toml").
Detect(src []byte, schema []byte, format string) []Fix
}
Rule is a single fix rule that can detect and produce fixes for a specific class of issue.