formats

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidFormat indicates the format is invalid or unsupported
	ErrInvalidFormat = errors.New("invalid format")

	// ErrInvalidKey indicates an invalid key name
	ErrInvalidKey = errors.New("invalid key")

	// ErrInvalidValue indicates an invalid value
	ErrInvalidValue = errors.New("invalid value")

	// ErrDuplicateKey indicates a duplicate key was found
	ErrDuplicateKey = errors.New("duplicate key")

	// ErrUnclosedQuote indicates an unclosed quote in the content
	ErrUnclosedQuote = errors.New("unclosed quote")

	// ErrCircularReference indicates a circular variable reference
	ErrCircularReference = errors.New("circular variable reference")

	// ErrVariableNotFound indicates a required variable was not found
	ErrVariableNotFound = errors.New("variable not found")

	// ErrMaxDepthExceeded indicates maximum interpolation depth was exceeded
	ErrMaxDepthExceeded = errors.New("maximum interpolation depth exceeded")
)

Common errors

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the global format registry

Functions

This section is empty.

Types

type Detector

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

Detector detects file formats

func NewDetector

func NewDetector() *Detector

NewDetector creates a new format detector

func (*Detector) DetectFormat

func (d *Detector) DetectFormat(content []byte) (Format, error)

DetectFormat detects the format of the content

func (*Detector) DetectFormatFromFile

func (d *Detector) DetectFormatFromFile(filename string) (Format, error)

DetectFormatFromFile detects format from filename

type Format

type Format string

Format represents a file format type

const (
	FormatENV    Format = "env"
	FormatJSON   Format = "json"
	FormatYAML   Format = "yaml"
	FormatShell  Format = "shell"
	FormatDocker Format = "dockerfile"
)

type Formatter

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

Formatter provides formatting options for output

func NewFormatter

func NewFormatter(opts *FormatterOptions) *Formatter

NewFormatter creates a new formatter

func (*Formatter) Format

func (f *Formatter) Format(w io.Writer, data map[string]string) error

Format formats the key-value data according to options

type FormatterOptions

type FormatterOptions struct {
	// Display options
	ShowHeader      bool     // Show header with metadata
	ShowComments    bool     // Show comments for each key
	ShowLineNumbers bool     // Show line numbers
	ColorOutput     bool     // Use ANSI colors
	MaxValueLength  int      // Truncate values longer than this
	MaskSecrets     bool     // Mask sensitive values
	SecretPatterns  []string // Patterns to identify secrets

	// Sorting options
	SortBy          SortOption // How to sort keys
	GroupByPrefix   bool       // Group keys by prefix
	PrefixSeparator string     // Separator for prefix grouping

	// Output format
	Format        OutputFormat // Output format style
	Indent        string       // Indentation string
	KeyValueSep   string       // Separator between key and value
	LineSeparator string       // Line separator
}

FormatterOptions configures the formatter

func DefaultFormatterOptions

func DefaultFormatterOptions() *FormatterOptions

DefaultFormatterOptions returns default formatter options

type GenerateOptions

type GenerateOptions struct {
	*Options
	Header    string            // File header comment
	KeyOrder  []string          // Specific key order
	KeyFilter func(string) bool // Filter keys
}

GenerateOptions provides fine-grained control over generation

type Generator

type Generator interface {
	// Generate writes key-value pairs to the writer
	Generate(w io.Writer, data map[string]string) error

	// GenerateString returns the formatted string
	GenerateString(data map[string]string) (string, error)

	// GenerateFile writes to a file
	GenerateFile(filename string, data map[string]string) error
}

Generator defines the interface for generating different formats

type Handler

type Handler interface {
	Parser
	Generator
	Validator

	// Format returns the format type
	Format() Format

	// Extensions returns file extensions for this format
	Extensions() []string
}

Handler combines parser, generator, and validator

type InterpolationError

type InterpolationError struct {
	Variable string
	Message  string
	Err      error
}

InterpolationError represents an error during variable interpolation

func (*InterpolationError) Error

func (e *InterpolationError) Error() string

Error implements the error interface

func (*InterpolationError) Unwrap

func (e *InterpolationError) Unwrap() error

Unwrap returns the underlying error

type Options

type Options struct {
	// Parsing options
	ExpandVariables  bool              // Expand ${VAR} references
	StrictMode       bool              // Strict parsing mode
	PreserveComments bool              // Preserve comments (ENV only)
	TrimSpace        bool              // Trim whitespace from values
	Variables        map[string]string // Variables for interpolation

	// Generation options
	SortKeys        bool   // Sort keys alphabetically
	QuoteValues     bool   // Always quote values
	IndentSize      int    // Indent size for structured formats
	LineEnding      string // Line ending style (\n or \r\n)
	IncludeComments bool   // Include instructional comments
}

Options for parsing and generation

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns default options

type OutputFormat

type OutputFormat int

OutputFormat defines the output format style

const (
	OutputDefault OutputFormat = iota
	OutputTable
	OutputExport
	OutputShell
	OutputDockerfile
	OutputCompact
)

type ParseError

type ParseError struct {
	Line    int
	Column  int
	Message string
	Err     error
}

ParseError represents a parsing error with line information

func NewParseError

func NewParseError(line int, message string, err error) *ParseError

NewParseError creates a new parse error

func (*ParseError) Error

func (e *ParseError) Error() string

Error implements the error interface

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

Unwrap returns the underlying error

type ParseResult

type ParseResult struct {
	Data     map[string]string
	Comments map[string]string // Key -> comment mapping
	Order    []string          // Original key order
	Metadata map[string]interface{}
}

ParseResult contains parsed data with metadata

type Parser

type Parser interface {
	// Parse reads from the reader and returns key-value pairs
	Parse(r io.Reader) (map[string]string, error)

	// ParseString parses a string
	ParseString(content string) (map[string]string, error)

	// ParseFile parses a file
	ParseFile(filename string) (map[string]string, error)
}

Parser defines the interface for parsing different formats

type Registry

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

Registry manages format handlers

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new format registry

func (*Registry) Generate

func (r *Registry) Generate(format Format, data map[string]string) (string, error)

Generate generates content with the appropriate handler

func (*Registry) Get

func (r *Registry) Get(format Format) (Handler, error)

Get returns a handler for the format

func (*Registry) List

func (r *Registry) List() []Format

List returns all registered formats

func (*Registry) Parse

func (r *Registry) Parse(format Format, content string) (map[string]string, error)

Parse parses content with the appropriate handler

func (*Registry) Register

func (r *Registry) Register(format Format, handler Handler) error

Register registers a format handler

type SortOption

type SortOption int

SortOption defines how to sort keys

const (
	SortNone SortOption = iota
	SortAlphabetical
	SortAlphabeticalReverse
	SortByLength
	SortByCategory
)

type ValidationError

type ValidationError struct {
	Field   string
	Value   string
	Message string
}

ValidationError represents a validation error

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface

type Validator

type Validator interface {
	// Validate checks if the content is valid for the format
	Validate(content []byte) error

	// ValidateKey checks if a key is valid
	ValidateKey(key string) error

	// ValidateValue checks if a value is valid
	ValidateValue(value string) error
}

Validator defines the interface for format validation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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