common

package
v1.0.0-pre Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TableFormatExcel     = "excel"
	TableFormatCSV       = "csv"
	TableFormatMarkdown  = "markdown"
	TableFormatHTML      = "html"
	TableFormatMediaWiki = "mediawiki"
	TableFormatLatex     = "latex"
	TableFormatJSON      = "json"
	TableFormatXML       = "xml"
	TableFormatASCII     = "ascii"
	TableFormatSQL       = "sql"
	TableFormatTracWiki  = "tracwiki"
	TableFormatTWiki     = "twiki"
)

Variables

View Source
var FormatParamsRegistry = map[string][]FormatParam{
	"ascii": {
		{Name: "style", DefaultValue: "box", AllowedValues: "box, plus(+), dot(·), bubble(◌)", Description: "Table Style"},
	},
	"csv": {
		{Name: "first-column-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Use first column as headers"},
		{Name: "bom", DefaultValue: "false", AllowedValues: "", Description: "Add Byte Order Mark"},
		{Name: "delimiter", DefaultValue: ",", AllowedValues: "COMMA, TAB, SEMICOLON, PIPE, SLASH, HASH", Description: "Value Delimiter"},
	},
	"excel": {
		{Name: "first-column-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Use first column as headers"},
		{Name: "sheet-name", DefaultValue: "Sheet1", AllowedValues: "", Description: "Excel Sheet Name"},
		{Name: "auto-width", DefaultValue: "false", AllowedValues: "true, false", Description: "Auto Width"},
		{Name: "text-format", DefaultValue: "true", AllowedValues: "true, false", Description: "force text format"},
	},
	"xlsx": {
		{Name: "first-column-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Use first column as headers"},
		{Name: "sheet-name", DefaultValue: "Sheet1", AllowedValues: "", Description: "Excel Sheet Name"},
		{Name: "auto-width", DefaultValue: "false", AllowedValues: "true, false", Description: "Auto Width"},
		{Name: "text-format", DefaultValue: "true", AllowedValues: "true, false", Description: "force text format"},
	},
	"html": {
		{Name: "first-column-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Use first column as headers"},
		{Name: "div", DefaultValue: "false", AllowedValues: "true, false", Description: "Convert into div table"},
		{Name: "minify", DefaultValue: "false", AllowedValues: "true, false", Description: "Minify HTML table"},
		{Name: "thead", DefaultValue: "false", AllowedValues: "true, false", Description: "Include thead and tbody tags"},
	},
	"json": {
		{Name: "format", DefaultValue: "object", AllowedValues: "object, 2d, column, keyed", Description: "JSON Format"},
		{Name: "minify", DefaultValue: "false", AllowedValues: "true, false", Description: "Minify JSON"},
		{Name: "parsing-json", DefaultValue: "false", AllowedValues: "true, false", Description: "Parsing JSON"},
	},
	"jsonl": {
		{Name: "parsing-json", DefaultValue: "false", AllowedValues: "true, false", Description: "Parsing JSON"},
	},
	"jsonlines": {
		{Name: "parsing-json", DefaultValue: "false", AllowedValues: "true, false", Description: "Parsing JSON"},
	},
	"latex": {
		{Name: "bold-first-column", DefaultValue: "false", AllowedValues: "true, false", Description: "Bold first column"},
		{Name: "bold-first-row", DefaultValue: "false", AllowedValues: "true, false", Description: "Bold first row"},
		{Name: "borders", DefaultValue: "1111,1111", AllowedValues: "1111,1111, 1101,1101, 0000,1101, 1111,0100, 0000,0100, 0000,0000", Description: "Table Border"},
		{Name: "caption", DefaultValue: "", AllowedValues: "", Description: "Table Caption"},
		{Name: "escape", DefaultValue: "true", AllowedValues: "true, false", Description: "Escape LaTeX table"},
		{Name: "ht", DefaultValue: "false", AllowedValues: "true, false", Description: "Place here or top of page"},
		{Name: "label", DefaultValue: "", AllowedValues: "", Description: "Table Label"},
		{Name: "location", DefaultValue: "above", AllowedValues: "above, below", Description: "Caption Location"},
		{Name: "mwe", DefaultValue: "false", AllowedValues: "true, false", Description: "Minimal working example"},
		{Name: "table-align", DefaultValue: "centering", AllowedValues: "centering, raggedleft, raggedright", Description: "Table Alignment"},
		{Name: "text-align", DefaultValue: "l", AllowedValues: "l, c, r", Description: "Text Alignment"},
	},
	"markdown": {
		{Name: "align", DefaultValue: "l", AllowedValues: "l, c, r", Description: "Text Alignment, columns seperate by comma"},
		{Name: "bold-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Table Header Bold"},
		{Name: "bold-first-column", DefaultValue: "false", AllowedValues: "true, false", Description: "Bold first column"},
		{Name: "escape", DefaultValue: "true", AllowedValues: "true, false", Description: "Escape Markdown table"},
		{Name: "pretty", DefaultValue: "true", AllowedValues: "true, false", Description: "Pretty-print Markdown"},
	},
	"md": {
		{Name: "align", DefaultValue: "l", AllowedValues: "l, c, r", Description: "Text Alignment, columns seperate by comma"},
		{Name: "bold-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Table Header Bold"},
		{Name: "bold-first-column", DefaultValue: "false", AllowedValues: "true, false", Description: "Bold first column"},
		{Name: "escape", DefaultValue: "true", AllowedValues: "true, false", Description: "Escape Markdown table"},
		{Name: "pretty", DefaultValue: "true", AllowedValues: "true, false", Description: "Pretty-print Markdown"},
	},
	"mediawiki": {
		{Name: "first-row-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Use first row as headers"},
		{Name: "minify", DefaultValue: "false", AllowedValues: "true, false", Description: "Minify MediaWiki table"},
		{Name: "sort", DefaultValue: "false", AllowedValues: "true, false", Description: "Make table sortable in Wikipedia"},
	},
	"mysql": {
		{Name: "style", DefaultValue: "box", AllowedValues: "box", Description: "MySQL table style (box format)"},
	},
	"twiki": {
		{Name: "first-row-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Use first row as headers"},
	},
	"tracwiki": {
		{Name: "first-row-header", DefaultValue: "false", AllowedValues: "true, false", Description: "Use first row as headers"},
	},
	"tmpl": {
		{Name: "template", DefaultValue: "", AllowedValues: "", Description: "Template file path"},
	},
	"template": {
		{Name: "template", DefaultValue: "", AllowedValues: "", Description: "Template file path"},
	},
	"sql": {
		{Name: "one-insert", DefaultValue: "false", AllowedValues: "true, false", Description: "Insert multiple rows at once"},
		{Name: "replace", DefaultValue: "false", AllowedValues: "true, false", Description: "Use REPLACE instead of INSERT"},
		{Name: "dialect", DefaultValue: "mysql", AllowedValues: "none, mysql, oracle, mssql, postgresql", Description: "identity escape SQL Dialect, none for no escape"},
		{Name: "table", DefaultValue: "", AllowedValues: "", Description: "Table Name"},
	},
	"xml": {
		{Name: "minify", DefaultValue: "false", AllowedValues: "true, false", Description: "Minify XML"},
		{Name: "root-element", DefaultValue: "dataset", AllowedValues: "string", Description: "Root Element Tag"},
		{Name: "row-element", DefaultValue: "record", AllowedValues: "string", Description: "Row Element Tag"},
		{Name: "declaration", DefaultValue: "true", AllowedValues: "true, false", Description: "Include XML Declaration"},
	},
}

FormatParamsRegistry maps format names to their supported extension parameters

View Source
var GlobalTransformParams = []FormatParam{
	{Name: "transpose", DefaultValue: "false", AllowedValues: "true, false", Description: "Transpose the table (swap rows and columns)"},
	{Name: "delete-empty", DefaultValue: "false", AllowedValues: "true, false", Description: "Remove empty rows from the table"},
	{Name: "deduplicate", DefaultValue: "false", AllowedValues: "true, false", Description: "Remove duplicate rows"},
	{Name: "uppercase", DefaultValue: "false", AllowedValues: "true, false", Description: "Convert all text to UPPERCASE"},
	{Name: "lowercase", DefaultValue: "false", AllowedValues: "true, false", Description: "Convert all text to lowercase"},
	{Name: "capitalize", DefaultValue: "false", AllowedValues: "true, false", Description: "Capitalize the first letter of each cell"},
}

GlobalTransformParams are transformation parameters available for all formats

Functions

func CSVForceQuote

func CSVForceQuote(field string) string

CSVForceQuote returns a string with quotes

func CSVQuoteEscape

func CSVQuoteEscape(field string) string

CSVQuoteEscape function is used to escape quotes in CSV fields

func Capitalize

func Capitalize(table *Table)

Capitalize converts the first letter of each cell value to uppercase

func CopyReaderToWriter

func CopyReaderToWriter(reader io.Reader, writer io.Writer) error

CopyReaderToWriter is a utility function that copies data from reader to writer Useful for passthrough scenarios or testing

func CreateMCPServer

func CreateMCPServer(registry *FormatRegistry) *mcp.Server

CreateMCPServer creates the MCP server with tableconvert tools

func DeduplicateRows

func DeduplicateRows(table *Table)

DeduplicateRows removes duplicate rows (rows with identical values in all columns)

func DeleteEmptyRows

func DeleteEmptyRows(table *Table)

DeleteEmptyRows removes rows that are completely empty (all cells are empty strings)

func DetectTableFormatByData

func DetectTableFormatByData(reader io.Reader) (string, error)

func DetectTableFormatByExtension

func DetectTableFormatByExtension(filename string) string

DetectTableFormatByExtension detects the table format by the file extension.

func FormatExists

func FormatExists(format string) bool

FormatExists checks if a format is supported

func GetAllFormats

func GetAllFormats() []string

GetAllFormats returns a list of all supported formats

func GetProjectRootPath

func GetProjectRootPath() (string, error)

func HtmlEscape

func HtmlEscape(s string) string

func InferPrintType

func InferPrintType(s string) string

func InferType

func InferType(value string) interface{}

InferType attempts to convert a string value to a more specific type (bool, int64, float64, nil) If no conversion is successful, it returns the original string.

func LaTeXEscape

func LaTeXEscape(s string) string

LaTeXEscape escapes special LaTeX characters in the content

func LaTeXUnescape

func LaTeXUnescape(s string) string

func Lowercase

func Lowercase(table *Table)

Lowercase converts all cell values to lowercase

func MarkdownEscape

func MarkdownEscape(s string) string

func MarkdownUnescape

func MarkdownUnescape(s string) string

func MssqlIdentifierEscape

func MssqlIdentifierEscape(s string) string

func OracleIdentifierEscape

func OracleIdentifierEscape(s string) string

func PerformConversionWithRegistry

func PerformConversionWithRegistry(registry *FormatRegistry, cfg *Config) error

PerformConversionWithRegistry performs conversion using a registry of format functions

func PostgreSQLIdentifierEscape

func PostgreSQLIdentifierEscape(s string) string

func SQLIdentifierEscape

func SQLIdentifierEscape(s string) string

SQLIdentifierEscape mysql identifier escape

func SQLValueEscape

func SQLValueEscape(s string) string

SQLValueEscape values for SQL insertion (handle quotes, NULLs, etc.)

func ShowFormatHelp

func ShowFormatHelp(format string)

ShowFormatHelp displays help for a specific format

func ShowFormatsHelp

func ShowFormatsHelp()

ShowFormatsHelp displays help for all supported formats and their parameters

func Transpose

func Transpose(table *Table)

Transpose converts columns to rows and rows to columns. The first column of the new table contains the original headers. The first row of the new table contains "" and then "Row_1", "Row_2", etc.

func Uppercase

func Uppercase(table *Table)

Uppercase converts all cell values to uppercase

func Usage

func Usage()

func ValidateIO

func ValidateIO(cfg *Config) error

ValidateIO validates that reader and writer are properly set

Types

type BatchFile

type BatchFile struct {
	InputPath  string
	OutputPath string
	FromFormat string
	ToFormat   string
}

BatchFile represents a single file in batch processing

type Config

type Config struct {
	From      string
	To        string
	File      string
	Result    string
	Verbose   bool
	MCPMode   bool
	Batch     string // Batch mode pattern (e.g., "*.csv")
	Recursive bool   // Recursive directory traversal
	OutputDir string // Output directory for batch mode
	Reader    io.Reader
	Writer    io.Writer
	Extension map[string]string
}

Config holds configuration, reader/writer, and extension parameters

func ParseConfig

func ParseConfig(args []string) (Config, error)

ParseConfig parses arguments in format "--key=value" or "--key value" and returns a key-value map

func (*Config) ApplyTransformations

func (c *Config) ApplyTransformations(table *Table)

ApplyTransformations applies data transformations to the table based on configuration. Transformations are applied in a specific order: transpose -> delete-empty -> deduplicate -> case transformations

func (*Config) GetBatchFiles

func (c *Config) GetBatchFiles() ([]BatchFile, error)

GetBatchFiles expands the batch pattern and returns a list of files to process

func (*Config) GetExtensionBool

func (c *Config) GetExtensionBool(key string, defaultValue bool) bool

GetExtensionBool gets a boolean value from Extension with default

func (*Config) GetExtensionInt

func (c *Config) GetExtensionInt(key string, defaultValue int) int

GetExtensionInt gets an int value from Extension with default

func (*Config) GetExtensionString

func (c *Config) GetExtensionString(key string, defaultValue string) string

GetExtensionString gets a string value from Extension with default

type ConversionError

type ConversionError struct {
	Stage  string // "unmarshal" or "marshal"
	Format string
	Err    error
}

ConversionError represents a conversion error with context

func (*ConversionError) Error

func (e *ConversionError) Error() string

func (*ConversionError) Unwrap

func (e *ConversionError) Unwrap() error

type ConvertTableArgs

type ConvertTableArgs struct {
	From            string            `json:"from" jsonschema:"source format (e.g., csv, mysql, markdown)"`
	To              string            `json:"to" jsonschema:"target format (e.g., json, csv, markdown)"`
	Input           string            `json:"input" jsonschema:"table data as a string"`
	Options         map[string]string `json:"options,omitempty" jsonschema:"format-specific options as key-value pairs"`
	Transformations map[string]bool   `` /* 141-byte string literal not displayed */
}

ConvertTableArgs represents the arguments for the convert_table tool

type ConvertTableResult

type ConvertTableResult struct {
	Output string `json:"output"`
	Error  string `json:"error,omitempty"`
}

ConvertTableResult represents the result of a conversion

type FormatParam

type FormatParam struct {
	Name          string
	DefaultValue  string
	AllowedValues string
	Description   string
}

FormatParam represents a single format-specific parameter

func GetFormatParams

func GetFormatParams(format string) []FormatParam

GetFormatParams returns the parameters for a specific format

type FormatRegistry

type FormatRegistry struct {
	UnmarshalMap map[string]UnmarshalFunc
	MarshalMap   map[string]MarshalFunc
}

FormatRegistry holds format-specific unmarshal and marshal functions

func NewFormatRegistry

func NewFormatRegistry() *FormatRegistry

NewFormatRegistry creates a new format registry

func (*FormatRegistry) GetMarshalFunc

func (fr *FormatRegistry) GetMarshalFunc(format string) (MarshalFunc, bool)

GetMarshalFunc returns the marshal function for a format

func (*FormatRegistry) GetUnmarshalFunc

func (fr *FormatRegistry) GetUnmarshalFunc(format string) (UnmarshalFunc, bool)

GetUnmarshalFunc returns the unmarshal function for a format

func (*FormatRegistry) RegisterFormat

func (fr *FormatRegistry) RegisterFormat(name string, unmarshal UnmarshalFunc, marshal MarshalFunc)

RegisterFormat adds a format's unmarshal and marshal functions to the registry

func (*FormatRegistry) RegisterFormatAlias

func (fr *FormatRegistry) RegisterFormatAlias(alias, format string)

RegisterFormatAlias adds an alias for an existing format

func (*FormatRegistry) RegisterWriteOnlyFormat

func (fr *FormatRegistry) RegisterWriteOnlyFormat(name string, marshal MarshalFunc)

RegisterWriteOnlyFormat registers a format that only supports writing (no Unmarshal)

type GetFormatsArgs

type GetFormatsArgs struct {
	Format string `json:"format,omitempty" jsonschema:"specific format to get help for"`
}

GetFormatsArgs represents the arguments for the get_formats tool

type GetFormatsResult

type GetFormatsResult struct {
	Formats    map[string]string        `json:"formats"`
	Parameters map[string][]FormatParam `json:"parameters,omitempty"`
}

GetFormatsResult represents the result of getting format information

type MCPFormatParam

type MCPFormatParam struct {
	Name          string `json:"name"`
	DefaultValue  string `json:"default_value"`
	AllowedValues string `json:"allowed_values"`
	Description   string `json:"description"`
}

MCPFormatParam represents a format-specific parameter for MCP (compatible with FormatParam)

type MCPServerContext

type MCPServerContext struct {
	Registry *FormatRegistry
}

MCPServerContext holds the registry for MCP handlers

func NewMCPServerContext

func NewMCPServerContext(registry *FormatRegistry) *MCPServerContext

NewMCPServerContext creates a new MCP server context with a format registry

func (*MCPServerContext) HandleConvertTable

HandleConvertTable handles the convert_table tool call

func (*MCPServerContext) HandleGetFormats

HandleGetFormats handles the get_formats tool call (static version)

type MarshalFunc

type MarshalFunc func(cfg *Config, table *Table) error

MarshalFunc is a function type that converts a Table to output format

type ParseError

type ParseError struct {
	LineNumber int
	Message    string
	Line       string
}

ParseError represents an error during parsing.

func (*ParseError) Error

func (e *ParseError) Error() string

type Table

type Table struct {
	Headers []string
	Rows    [][]string
}

Table represents the parsed data from the MySQL output.

type UnmarshalFunc

type UnmarshalFunc func(cfg *Config, table *Table) error

UnmarshalFunc is a function type that parses input data into a Table

Jump to

Keyboard shortcuts

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