codec

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 32 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Codecs = map[EncodingType]Encoding{
	JSON:       {json.Unmarshal, jsonCodec.Marshal, []string{"json"}},
	YAML:       {yamlCodec.Unmarshal, yamlCodec.Marshal, []string{"yaml", "yml"}},
	TOML:       {toml.Unmarshal, toml.Marshal, []string{"toml"}},
	HCL:        {hclCodec.Unmarshal, hclCodec.Marshal, []string{"hcl", "tf"}},
	CSV:        {csvCodec.Unmarshal, csvCodec.Marshal, []string{"csv"}},
	TSV:        {tsvCodec.Unmarshal, tsvCodec.Marshal, []string{"tsv"}},
	XML:        {xmlCodec.Unmarshal, xmlCodec.Marshal, []string{"xml"}},
	INI:        {iniCodec.Unmarshal, iniCodec.Marshal, []string{"ini"}},
	GRON:       {gronCodec.Unmarshal, gronCodec.Marshal, []string{"gron"}},
	HTML:       {htmlCodec.Unmarshal, xmlCodec.Marshal, []string{"html"}},
	LINE:       {lineCodec.Unmarshal, jsonCodec.Marshal, []string{"line"}},
	TXT:        {lineCodec.Unmarshal, jsonCodec.Marshal, []string{"txt", "text"}},
	PROTO:      {protoCodec.Unmarshal, jsonCodec.Marshal, []string{"proto"}},
	ENV:        {envCodec.Unmarshal, envCodec.Marshal, []string{"env"}},
	PARQUET:    {parquetCodec.Unmarshal, parquetCodec.Marshal, []string{"parquet"}},
	MSGPACK:    {msgpackCodec.Unmarshal, msgpackCodec.Marshal, []string{"msgpack", "mpk"}},
	PROPERTIES: {propertiesCodec.Unmarshal, propertiesCodec.Marshal, []string{"properties"}},
	JSONL:      {jsonlCodec.Unmarshal, jsonlCodec.Marshal, []string{"jsonl", "ndjson", "jsonlines"}},
	JSONC:      {jsoncCodec.Unmarshal, jsoncCodec.Marshal, []string{"jsonc"}},
	BASE64:     {base64Codec.Unmarshal, base64Codec.Marshal, []string{"base64", "b64"}},
	CBOR:       {cborCodec.Unmarshal, cborCodec.Marshal, []string{"cbor"}},
	AVRO:       {avroCodec.Unmarshal, avroCodec.Marshal, []string{"avro"}},
}

Functions

func GetExtensionMap added in v0.3.4

func GetExtensionMap() map[string]EncodingType

GetExtensionMap returns a map of file extensions to their encoding types

func GetSupportedExtensions added in v0.3.4

func GetSupportedExtensions() []string

GetSupportedExtensions returns a sorted list of unique supported extensions

func IsBinaryFormat added in v0.3.1

func IsBinaryFormat(fileType EncodingType) bool

func Marshal

func Marshal(v any, outputFileType EncodingType) ([]byte, error)

func PrettyFormat

func PrettyFormat(s string, fileType EncodingType, raw bool, monochrome bool) (string, error)

func StreamParser added in v0.3.4

func StreamParser(reader io.Reader, inputType EncodingType) (<-chan any, <-chan error)

StreamParser parses input in streaming mode, emitting path-value pairs via a channel For JSON: matches jq's --stream behavior exactly For other formats: converts each record/document to path-value pairs

func StreamParserCollect added in v0.3.4

func StreamParserCollect(reader io.Reader, inputType EncodingType) ([]any, error)

Legacy function that collects all stream elements (for backward compatibility)

func Unmarshal

func Unmarshal(input []byte, inputFileType EncodingType, data any) error

Types

type Encoding

type Encoding struct {
	Unmarshal  func([]byte, any) error
	Marshal    func(any) ([]byte, error)
	Extensions []string
}

General Encoding struct to hold unmarshal/marshal functions and associated file extensions for each encoding type This allows for a clean separation of concerns and makes it easy to add new encodings in the future by simply implementing the Codec interface and adding an entry to the Codecs map. The Extensions field is used for file type detection and mapping to the appropriate encoding type.

type EncodingType

type EncodingType int

EncodingType represents the supported encoding types as an enum with a string representation

const (
	JSON EncodingType = iota
	YAML
	TOML
	HCL
	CSV
	TSV
	XML
	INI
	GRON
	HTML
	LINE
	TXT
	PROTO
	ENV
	PARQUET
	MSGPACK
	PROPERTIES
	JSONL
	JSONC
	BASE64
	CBOR
	AVRO
)

func GetEncodingType

func GetEncodingType(fileType string) (EncodingType, error)

func (EncodingType) String

func (e EncodingType) String() string

String implements the Stringer interface, converting the enum to its canonical string name. This is used for:

  • Human-readable error messages and logs (e.g., "json" instead of "0")
  • Lexer lookup for syntax highlighting
  • Test output and debugging

Note: These names are duplicated in the Extensions field of Codecs map. This duplication is intentional for performance - O(1) array lookup here vs O(n) map iteration. The array indices must match the iota order in the const block above.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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