diagnostics

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package diagnostics provides rich diagnostic information for db-catalyst.

Package diagnostics provides rich diagnostic information for db-catalyst.

Package diagnostics provides rich diagnostic information for db-catalyst. It captures file locations, contextual code snippets, suggestions for fixes, and severity levels to help users understand and resolve issues.

Package diagnostics provides rich diagnostic information for db-catalyst.

Index

Constants

View Source
const (
	// Schema parsing errors (E1xx)
	ErrSchemaParseError     = "E101"
	ErrSchemaDuplicateTable = "E102"
	ErrSchemaDuplicateView  = "E103"
	ErrSchemaDuplicateCol   = "E104"
	ErrSchemaInvalidType    = "E105"
	ErrSchemaUnknownTable   = "E106"
	ErrSchemaUnknownColumn  = "E107"
	ErrSchemaInvalidFK      = "E108"
	ErrSchemaInvalidPK      = "E109"
	ErrSchemaInvalidIndex   = "E110"

	// Query parsing errors (E2xx)
	ErrQueryParseError    = "E201"
	ErrQueryInvalidVerb   = "E202"
	ErrQueryInvalidParam  = "E203"
	ErrQueryInvalidCTE    = "E204"
	ErrQueryMissingAlias  = "E205"
	ErrQueryAmbiguousCol  = "E206"
	ErrQueryUnknownTable  = "E207"
	ErrQueryUnknownColumn = "E208"
	ErrQueryTypeMismatch  = "E209"
	ErrQueryInvalidSyntax = "E210"

	// Configuration errors (E3xx)
	ErrConfigInvalid        = "E301"
	ErrConfigMissingPackage = "E302"
	ErrConfigMissingOut     = "E303"
	ErrConfigInvalidPath    = "E304"
	ErrConfigUnknownKey     = "E305"
	ErrConfigInvalidDriver  = "E306"
	ErrConfigInvalidLang    = "E307"
	ErrConfigInvalidDB      = "E308"

	// Code generation errors (E4xx)
	ErrCodeGenFailed      = "E401"
	ErrCodeGenWriteFailed = "E402"
	ErrCodeGenTypeError   = "E403"

	// Warnings (W1xx)
	WarnDeprecatedFeature = "W101"
	WarnUnnecessaryAlias  = "W102"
	WarnTypeInference     = "W103"
	WarnUnusedParam       = "W104"
	WarnSchemaMismatch    = "W105"
)

ErrorCodes provides standardized error codes for diagnostics. These codes help users identify and search for specific issues.

Variables

This section is empty.

Functions

func BatchEnrich

func BatchEnrich(c *Collection, extractor *ContextExtractor, contextLines int)

BatchEnrich enriches all diagnostics in a collection with context and suggestions. This is a convenience function that combines EnrichWithContext and EnrichWithSuggestions.

func CodeDescription

func CodeDescription(code string) string

CodeDescription returns a human-readable description for an error code.

func CollectionToQueryAnalyzer

func CollectionToQueryAnalyzer(c *Collection) []queryanalyzer.Diagnostic

CollectionToQueryAnalyzer converts a collection to a slice of query analyzer diagnostics.

func EnrichWithContext

func EnrichWithContext(c *Collection, extractor *ContextExtractor, contextLines int)

EnrichWithContext adds code context to diagnostics that have file locations.

func EnrichWithSuggestions

func EnrichWithSuggestions(c *Collection)

EnrichWithSuggestions adds suggestions to common error patterns.

func ExtractLine

func ExtractLine(content []byte, lineNum int) (string, error)

ExtractLine extracts a specific line from content.

func ExtractLines

func ExtractLines(content []byte, startLine, endLine int) ([]string, error)

ExtractLines extracts a range of lines from content.

func FormatForTerminal

func FormatForTerminal(c *Collection, verbose bool) string

FormatForTerminal formats diagnostics for terminal output with colors.

func PrintToWriter

func PrintToWriter(w io.Writer, c *Collection, verbose bool) error

PrintToWriter prints formatted diagnostics to a writer.

func ToQueryAnalyzer

func ToQueryAnalyzer(d Diagnostic) queryanalyzer.Diagnostic

ToQueryAnalyzer converts a rich diagnostic to a query analyzer diagnostic. This is useful for backward compatibility.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder provides a fluent API for constructing diagnostics.

func Error

func Error(message string) *Builder

Error creates a builder for an error-level diagnostic.

func Info

func Info(message string) *Builder

Info creates a builder for an info-level diagnostic.

func NewBuilder

func NewBuilder(severity Severity, message string) *Builder

NewBuilder creates a new diagnostic builder with the given severity and message.

func Warning

func Warning(message string) *Builder

Warning creates a builder for a warning-level diagnostic.

func (*Builder) At

func (b *Builder) At(path string, line, column int) *Builder

At sets the location.

func (*Builder) AtLocation

func (b *Builder) AtLocation(loc Location) *Builder

AtLocation sets the location from a Location struct.

func (*Builder) Build

func (b *Builder) Build() Diagnostic

Build returns the constructed diagnostic.

func (*Builder) WithCode

func (b *Builder) WithCode(code string) *Builder

WithCode sets the error code.

func (*Builder) WithContext

func (b *Builder) WithContext(context string) *Builder

WithContext sets the code context.

func (*Builder) WithNote

func (b *Builder) WithNote(note string) *Builder

WithNote adds a note.

func (*Builder) WithRelated

func (b *Builder) WithRelated(path string, line, column int, message string) *Builder

WithRelated adds related information.

func (*Builder) WithSource

func (b *Builder) WithSource(source string) *Builder

WithSource sets the source component.

func (*Builder) WithSpan

func (b *Builder) WithSpan(start, end Location) *Builder

WithSpan sets the span.

func (*Builder) WithSuggestion

func (b *Builder) WithSuggestion(message, replacement string) *Builder

WithSuggestion adds a suggestion.

type CategorizedSummary

type CategorizedSummary struct {
	SchemaErrors  []Diagnostic
	QueryErrors   []Diagnostic
	ConfigErrors  []Diagnostic
	CodegenErrors []Diagnostic
	Warnings      []Diagnostic
	Infos         []Diagnostic
	Uncategorized []Diagnostic
}

CategorizedSummary groups diagnostics by category based on error codes.

func (CategorizedSummary) ErrorCount

func (cs CategorizedSummary) ErrorCount() int

ErrorCount returns the total number of errors (excluding warnings and infos).

func (CategorizedSummary) HasDiagnostics

func (cs CategorizedSummary) HasDiagnostics() bool

HasDiagnostics returns true if any category has diagnostics.

func (CategorizedSummary) Total

func (cs CategorizedSummary) Total() int

Total returns the total number of diagnostics across all categories.

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

Collection holds a set of diagnostics.

func CollectionFromQueryAnalyzer

func CollectionFromQueryAnalyzer(diags []queryanalyzer.Diagnostic) *Collection

CollectionFromQueryAnalyzer converts a slice of query analyzer diagnostics to a collection.

func NewCollection

func NewCollection() *Collection

NewCollection creates a new empty diagnostic collection.

func (*Collection) Add

func (c *Collection) Add(d Diagnostic)

Add adds a diagnostic to the collection.

func (*Collection) AddAll

func (c *Collection) AddAll(other *Collection)

AddAll adds all diagnostics from another collection.

func (*Collection) All

func (c *Collection) All() []Diagnostic

All returns all diagnostics.

func (*Collection) ByCode

func (c *Collection) ByCode(code string) []Diagnostic

ByCode returns diagnostics with a specific error code.

func (*Collection) BySeverity

func (c *Collection) BySeverity(severity Severity) []Diagnostic

BySeverity returns diagnostics of a specific severity.

func (*Collection) BySource

func (c *Collection) BySource(source string) []Diagnostic

BySource returns diagnostics from a specific source.

func (*Collection) Categorize

func (c *Collection) Categorize() CategorizedSummary

Categorize groups diagnostics by their error code category.

func (*Collection) Errors

func (c *Collection) Errors() []Diagnostic

Errors returns all error-level diagnostics.

func (*Collection) Filter

func (c *Collection) Filter(predicate func(Diagnostic) bool) []Diagnostic

Filter returns diagnostics matching the given predicate.

func (*Collection) HasErrors

func (c *Collection) HasErrors() bool

HasErrors returns true if the collection contains any errors.

func (*Collection) Len

func (c *Collection) Len() int

Len returns the number of diagnostics.

func (*Collection) SortByLocation

func (c *Collection) SortByLocation()

SortByLocation sorts diagnostics by file path and line number.

func (*Collection) Summary

func (c *Collection) Summary() Summary

Summary returns a summary of the diagnostics collection.

func (*Collection) Warnings

func (c *Collection) Warnings() []Diagnostic

Warnings returns all warning-level diagnostics.

type Context

type Context struct {
	Lines       []string
	StartLine   int
	ErrorLine   int
	ErrorColumn int
	EndLine     int
	EndColumn   int
	IsSpan      bool
}

Context represents extracted code context.

func (Context) Format

func (c Context) Format() string

Format formats the context for display with line numbers.

func (Context) GetErrorLine

func (c Context) GetErrorLine() string

GetErrorLine returns the line containing the error.

func (Context) IsEmpty

func (c Context) IsEmpty() bool

IsEmpty returns true if the context has no lines.

func (Context) String

func (c Context) String() string

String returns a simple string representation of the context.

type ContextExtractor

type ContextExtractor struct {
	// contains filtered or unexported fields
}

ContextExtractor extracts code context from source files.

func NewContextExtractor

func NewContextExtractor() *ContextExtractor

NewContextExtractor creates a new context extractor.

func (*ContextExtractor) ExtractContext

func (e *ContextExtractor) ExtractContext(path string, line, column int, contextLines int) (Context, error)

ExtractContext extracts lines around a specific location. Returns the context lines and the position within those lines.

func (*ContextExtractor) ExtractSpan

func (e *ContextExtractor) ExtractSpan(path string, startLine, endLine, startCol, endCol int) (Context, error)

ExtractSpan extracts context for a span of lines.

type Diagnostic

type Diagnostic struct {
	// Core diagnostic information
	Severity Severity
	Message  string
	Code     string // Optional error code (e.g., "E001", "W003")

	// Location information
	Location Location
	Span     *Span // Optional span for multi-line diagnostics

	// Context
	Context string // Code snippet showing the problematic area

	// Help information
	Suggestions []Suggestion  // Suggested fixes
	Notes       []string      // Additional notes explaining the issue
	Related     []RelatedInfo // Related locations (e.g., previous definition)

	// Source information
	Source string // Component that produced the diagnostic (e.g., "schema-parser", "query-analyzer")
}

Diagnostic represents a rich diagnostic message with context.

func CreateConfigError

func CreateConfigError(path string, line, column int, message string) Diagnostic

CreateConfigError creates a rich diagnostic for configuration errors.

func CreateQueryError

func CreateQueryError(path string, line, column int, message string) Diagnostic

CreateQueryError creates a rich diagnostic for query analysis errors.

func CreateSchemaError

func CreateSchemaError(path string, line, column int, message string) Diagnostic

CreateSchemaError creates a rich diagnostic for schema parsing errors.

func CreateWarning

func CreateWarning(path string, line, column int, message string) Diagnostic

CreateWarning creates a rich diagnostic for warnings.

func EnrichDiagnostic

func EnrichDiagnostic(d Diagnostic, extractor *ContextExtractor, contextLines int) Diagnostic

EnrichDiagnostic adds context and suggestions to a single diagnostic. This is useful when you want to enrich diagnostics one at a time.

func FromQueryAnalyzer

func FromQueryAnalyzer(d queryanalyzer.Diagnostic) Diagnostic

FromQueryAnalyzer converts a query analyzer diagnostic to a rich diagnostic.

func FromQueryParser

func FromQueryParser(d queryparser.Diagnostic) Diagnostic

FromQueryParser converts a query parser diagnostic to a rich diagnostic.

func FromSchemaParser

func FromSchemaParser(d schemaparser.Diagnostic) Diagnostic

FromSchemaParser converts a schema parser diagnostic to a rich diagnostic.

func (Diagnostic) Error

func (d Diagnostic) Error() string

Error implements the error interface for error-level diagnostics.

func (Diagnostic) HasLocation

func (d Diagnostic) HasLocation() bool

HasLocation returns true if the diagnostic has a valid location.

func (Diagnostic) HasSpan

func (d Diagnostic) HasSpan() bool

HasSpan returns true if the diagnostic has a valid span.

func (Diagnostic) IsError

func (d Diagnostic) IsError() bool

IsError returns true if the diagnostic is an error.

func (Diagnostic) IsInfo

func (d Diagnostic) IsInfo() bool

IsInfo returns true if the diagnostic is informational.

func (Diagnostic) IsWarning

func (d Diagnostic) IsWarning() bool

IsWarning returns true if the diagnostic is a warning.

func (Diagnostic) String

func (d Diagnostic) String() string

String returns a human-readable string representation of the diagnostic.

type Formatter

type Formatter struct {
	// ShowContext controls whether to display code snippets.
	ShowContext bool
	// ShowSuggestions controls whether to display suggestions.
	ShowSuggestions bool
	// ShowNotes controls whether to display notes.
	ShowNotes bool
	// ShowRelated controls whether to display related information.
	ShowRelated bool
	// ShowSource controls whether to display the source component.
	ShowSource bool
	// ShowCode controls whether to display error codes.
	ShowCode bool
	// ShowCodeDescription controls whether to display error code descriptions.
	ShowCodeDescription bool
	// Colorize controls whether to use ANSI color codes.
	Colorize bool
	// ContextLines is the number of context lines to show around the error.
	ContextLines int
}

Formatter formats diagnostics for display.

func NewFormatter

func NewFormatter() *Formatter

NewFormatter creates a new formatter with default settings.

func NewSimpleFormatter

func NewSimpleFormatter() *Formatter

NewSimpleFormatter creates a formatter with minimal output.

func NewVerboseFormatter

func NewVerboseFormatter() *Formatter

NewVerboseFormatter creates a formatter with all options enabled.

func (*Formatter) Format

func (f *Formatter) Format(d Diagnostic) string

Format formats a single diagnostic as a string.

func (*Formatter) FormatAll

func (f *Formatter) FormatAll(c *Collection) string

FormatAll formats all diagnostics in a collection.

func (*Formatter) PrintCategorizedSummary

func (f *Formatter) PrintCategorizedSummary(w io.Writer, c *Collection)

PrintCategorizedSummary prints a categorized summary of diagnostics.

func (*Formatter) PrintSummary

func (f *Formatter) PrintSummary(w io.Writer, c *Collection)

PrintSummary prints a summary of diagnostics.

func (*Formatter) Write

func (f *Formatter) Write(w io.Writer, d Diagnostic) error

Write writes a single diagnostic to the writer.

func (*Formatter) WriteAll

func (f *Formatter) WriteAll(w io.Writer, c *Collection) error

WriteAll writes all diagnostics in a collection to the writer.

type JSONFormatter

type JSONFormatter struct {
	Indent bool
}

JSONFormatter formats diagnostics as JSON.

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(d Diagnostic) string

Format formats a diagnostic as JSON.

func (*JSONFormatter) FormatCollection

func (f *JSONFormatter) FormatCollection(c *Collection) string

FormatCollection formats an entire collection as a JSON array.

type Location

type Location struct {
	Path   string
	Line   int
	Column int
	Offset int
}

Location represents a position in a source file.

type RelatedInfo

type RelatedInfo struct {
	Location Location
	Message  string
}

RelatedInfo represents related context for a diagnostic.

type Severity

type Severity int

Severity indicates the seriousness of a diagnostic.

const (
	// SeverityInfo indicates an informational message.
	SeverityInfo Severity = iota
	// SeverityWarning indicates a potential issue that doesn't prevent code generation.
	SeverityWarning
	// SeverityError indicates a fatal issue that prevents code generation.
	SeverityError
)

func SeverityFromString

func SeverityFromString(s string) Severity

SeverityFromString parses a severity level from a string.

func (Severity) String

func (s Severity) String() string

String returns the string representation of the severity level.

type SimpleFormatter

type SimpleFormatter struct{}

SimpleFormatter provides a simple one-line format for diagnostics.

func (*SimpleFormatter) Format

func (f *SimpleFormatter) Format(d Diagnostic) string

Format formats a diagnostic in simple format.

type SnippetExtractor

type SnippetExtractor struct {
	// contains filtered or unexported fields
}

SnippetExtractor extracts short code snippets from SQL/content.

func NewSnippetExtractor

func NewSnippetExtractor() *SnippetExtractor

NewSnippetExtractor creates a new snippet extractor.

func (*SnippetExtractor) Extract

func (e *SnippetExtractor) Extract(content []byte, offset int) string

Extract extracts a snippet around a position in content.

func (*SnippetExtractor) WithMaxLength

func (e *SnippetExtractor) WithMaxLength(maxLen int) *SnippetExtractor

WithMaxLength sets the maximum snippet length.

type Span

type Span struct {
	Start Location
	End   Location
}

Span represents a range of locations in a source file.

type Suggestion

type Suggestion struct {
	Message     string
	Replacement string
	Span        Span
}

Suggestion represents a suggested fix for a diagnostic.

type Summary

type Summary struct {
	Total    int
	Errors   int
	Warnings int
	Infos    int
}

Summary provides a quick overview of diagnostics.

Jump to

Keyboard shortcuts

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