transcribe

package module
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 19 Imported by: 17

README

Transcribe for Go

License Go Reference Go Report Card

Go programs often need to output data in a structured representation format, such as JSON or YAML. But why not provide wider compatibility and support all common formats, letting the user choose? This library provides a unified API over transcribing stuctured data to several formats.

It also supports "pretty" printing to terminals with semantic colorization (including "it just works" support for colorizing Windows terminals, which defy common standards).

Supported formats:

  • YAML
  • JSON, including a convention for extending JSON to support additional type differentiation
  • XML via a conventional schema
  • CBOR
  • MessagePack
  • Go

The binary formats (CBOR, MessagePack) can be output as is (incompatible with terminals) or textualized into Base64.

Documentation

Index

Constants

View Source
const DIRECTORY_WRITE_PERMISSIONS = 0700
View Source
const FILE_WRITE_PERMISSIONS = 0600

Variables

View Source
var YAMLColorPrinter = yamlprinter.Printer{
	String: func() *yamlprinter.Property {
		return &yamlprinter.Property{
			Prefix: terminal.BlueCode,
			Suffix: terminal.ResetCode,
		}
	},
	Number: func() *yamlprinter.Property {
		return &yamlprinter.Property{
			Prefix: terminal.MagentaCode,
			Suffix: terminal.ResetCode,
		}
	},
	Bool: func() *yamlprinter.Property {
		return &yamlprinter.Property{
			Prefix: terminal.CyanCode,
			Suffix: terminal.ResetCode,
		}
	},

	MapKey: func() *yamlprinter.Property {
		return &yamlprinter.Property{
			Prefix: terminal.GreenCode,
			Suffix: terminal.ResetCode,
		}
	},

	Anchor: func() *yamlprinter.Property {
		return &yamlprinter.Property{
			Prefix: terminal.RedCode,
			Suffix: terminal.ResetCode,
		}
	},
	Alias: func() *yamlprinter.Property {
		return &yamlprinter.Property{
			Prefix: terminal.YellowCode,
			Suffix: terminal.ResetCode,
		}
	},
}
View Source
var YAMLNodeKinds = map[yaml.Kind]string{
	yaml.DocumentNode: "Document",
	yaml.SequenceNode: "Sequence",
	yaml.MappingNode:  "Mapping",
	yaml.ScalarNode:   "Scalar",
	yaml.AliasNode:    "Alias",
}

Functions

func ColorizeYAML

func ColorizeYAML(code string, writer io.Writer) error

func DumpYAMLNode

func DumpYAMLNode(writer io.Writer, node *yaml.Node, indent int)

func DumpYAMLNodes

func DumpYAMLNodes(writer io.Writer, node *yaml.Node)

func NewJSONColorFormatter

func NewJSONColorFormatter(indent int) *prettyjson.Formatter

func NewUtterConfig

func NewUtterConfig(indent string) *utter.ConfigState

func OpenFileForWrite

func OpenFileForWrite(path string) (*os.File, error)

Types

type Transcriber added in v0.2.0

type Transcriber struct {
	File        string    // if not empty will supersede Writer
	Writer      io.Writer // if nil then os.Stdout will be used
	Format      string    // "yaml", "json", "xjson", "xml", "cbor", "messagepack", or "go"
	ForTerminal bool

	Indent    string         // used by YAML, JSON, XML, and Go
	Strict    bool           // used by YAML
	Base64    bool           // used by CBOR and MessagePack
	InPlace   bool           // used by XJSON and XML
	Reflector *ard.Reflector // used by XJSON and XML
}

func NewTranscriber added in v0.2.0

func NewTranscriber() *Transcriber

func (*Transcriber) Clone added in v0.3.0

func (self *Transcriber) Clone() *Transcriber

func (*Transcriber) SetBase64 added in v0.3.4

func (self *Transcriber) SetBase64(base64 bool) *Transcriber

func (*Transcriber) SetFile added in v0.3.4

func (self *Transcriber) SetFile(file string) *Transcriber

func (*Transcriber) SetForTerminal added in v0.3.4

func (self *Transcriber) SetForTerminal(forTerminal bool) *Transcriber

func (*Transcriber) SetFormat added in v0.3.4

func (self *Transcriber) SetFormat(format string) *Transcriber

func (*Transcriber) SetInPlace added in v0.3.4

func (self *Transcriber) SetInPlace(inPlace bool) *Transcriber

func (*Transcriber) SetIndent added in v0.3.4

func (self *Transcriber) SetIndent(indent string) *Transcriber

func (*Transcriber) SetIndentSpaces added in v0.3.4

func (self *Transcriber) SetIndentSpaces(spaces int) *Transcriber

func (*Transcriber) SetReflector added in v0.3.4

func (self *Transcriber) SetReflector(reflector *ard.Reflector) *Transcriber

func (*Transcriber) SetStrict added in v0.3.4

func (self *Transcriber) SetStrict(strict bool) *Transcriber

func (*Transcriber) SetWriter added in v0.3.4

func (self *Transcriber) SetWriter(writer io.Writer) *Transcriber

func (*Transcriber) Stringify added in v0.2.0

func (self *Transcriber) Stringify(value any) (string, error)

Converts the value to a string according to [Transcriber.Format]. If the format is any empty string will default to YAML.

The binary formats (CBOR and MessagePack) will be converted to base64, ensuring that the returned value is always a valid string.

func (*Transcriber) StringifyCBOR added in v0.2.0

func (self *Transcriber) StringifyCBOR(value any) (string, error)

Will always use base64.

func (*Transcriber) StringifyGo added in v0.2.0

func (self *Transcriber) StringifyGo(value any) (string, error)

func (*Transcriber) StringifyJSON added in v0.2.0

func (self *Transcriber) StringifyJSON(value any) (string, error)

func (*Transcriber) StringifyMessagePack added in v0.2.0

func (self *Transcriber) StringifyMessagePack(value any) (string, error)

Will always use base64.

func (*Transcriber) StringifyXJSON added in v0.2.0

func (self *Transcriber) StringifyXJSON(value any) (string, error)

func (*Transcriber) StringifyXML added in v0.2.0

func (self *Transcriber) StringifyXML(value any) (string, error)

func (*Transcriber) StringifyYAML added in v0.2.0

func (self *Transcriber) StringifyYAML(value any) (string, error)

Note special handling when value is []any. In this case, instead of writing a YAML seq, it will treat the value as a list of documents separated by "---".

func (*Transcriber) Write added in v0.2.0

func (self *Transcriber) Write(value any) error

Writes the value to a writer according to [Transcriber.Format]. If the format is any empty string will default to YAML.

If [Transcriber.File] is not empty will create the file and write to it. Sub-directories in the path will be created if they don't exist. Otherwise will write to [Transcriber.Writer], and if that is nil will write to stdout.

When [Transcriber.ForTerminal] is true will optimize for terminals, such as ensuring a newline at the end and colorization if supported by the format and the terminal. In this mode [Transcriber.Indent] is ignored and terminal.Indent will be used instead.

If value is a string then will ignore the format and use Transcriber.WriteString. If value is a *etree.Document will ignore the format and use Transcriber.WriteXMLDocument.

Note special handling for YAML when value is []any. In this case, instead of writing a YAML seq, it will treat the value as a list of documents separated by "---".

func (*Transcriber) WriteCBOR added in v0.2.0

func (self *Transcriber) WriteCBOR(value any) error

When [Transcriber.Base64] is true will first convert to base64 and will also add a trailing newline if [Self.ForTerminal] is true.

func (*Transcriber) WriteGo added in v0.2.0

func (self *Transcriber) WriteGo(value any) error

func (*Transcriber) WriteJSON added in v0.2.0

func (self *Transcriber) WriteJSON(value any) error

func (*Transcriber) WriteMessagePack added in v0.2.0

func (self *Transcriber) WriteMessagePack(value any) error

When [Transcriber.Base64] is true will first convert to base64 and will also add a trailing newline if [Self.ForTerminal] is true.

func (*Transcriber) WriteString added in v0.3.0

func (self *Transcriber) WriteString(value any) error

Converts the value to string (using util.ToString) and writes it, adding a newline if the string doesn't already have it.

func (*Transcriber) WriteXJSON added in v0.2.0

func (self *Transcriber) WriteXJSON(value any) error

If inPlace is false then the function is non-destructive: the written data structure is a ard.ValidCopy of the value argument. Otherwise, the value may be changed during preparing it for writing.

func (*Transcriber) WriteXML added in v0.2.0

func (self *Transcriber) WriteXML(value any) error

If inPlace is false then the function is non-destructive: the written data structure is a ard.ValidCopy of the value argument. Otherwise, the value may be changed during preparing it for writing.

func (*Transcriber) WriteXMLDocument added in v0.2.0

func (self *Transcriber) WriteXMLDocument(xmlDocument *etree.Document) error

Will change the indentation of the document.

func (*Transcriber) WriteYAML added in v0.2.0

func (self *Transcriber) WriteYAML(value any) error

Note special handling when value is []any. In this case, instead of writing a YAML seq, it will treat the value as a list of documents separated by "---".

Jump to

Keyboard shortcuts

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