gnfmt

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: MIT Imports: 11 Imported by: 41

README

Package gnfmt

This is a helper package for setting output formats, and making coversions of data from go structures to JSON, CSV, of gob.

Documentation

Overview

Example
var enc Encoder
var err error
enc = GNjson{Pretty: true}
ary1 := []int{1, 2, 3}
jsonRes, err := enc.Encode(ary1)
if err != nil {
	panic(err)
}
var ary2 []int
err = enc.Decode(jsonRes, &ary2)
if err != nil {
	panic(err)
}
fmt.Println(ary1[0] == ary2[0])
Output:

true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ppr added in v0.3.0

func Ppr(obj interface{}) string

Ppr is a pretty print of an object

func ReadHeaderCSV

func ReadHeaderCSV(path string, sep rune) (map[string]int, error)

ReadHeaderCSV only reads the first line of a CSV/TSV input. It takes a path to a CSV/TSV-encoded file and a separator character. It returns back a map with name of a field, and its index. It also returns an error, if the header could not be read.

func TimeString added in v0.4.0

func TimeString(secs float64) string

func ToCSV

func ToCSV(record []string, sep rune) string

ToCSV takes a slice of strings and converts them to a CSV/TSV row with '"' as a field quote. On Windows machine the row ends with '\r\n', in all other cases with '\n'

Types

type Encoder

type Encoder interface {
	//Encode takes a Go object and converts it into bytes
	Encode(input interface{}) ([]byte, error)
	// Decode takes an input of bytes and decodes it into Go object.
	Decode(input []byte, output interface{}) error
}

Encoder interface allows to switch between different encoding types.

type Format

type Format int

Format sets available output formats

const (
	// FormatNone is for cases when format is not set yet.
	FormatNone Format = iota

	// CSV sets output to comma-separated values.
	CSV

	// CompactJSON sets output into one-liner JSON.
	CompactJSON

	// PrettyJSON sets output into easier to read JSON with new lines and
	// indentations.
	PrettyJSON

	// TSV sets output to tab-separated values.
	TSV
)

func NewFormat

func NewFormat(s string) (Format, error)

NewFormat is a constructor that converts a string into a corresponding format. If string cannot be converted, the constructor returns an error and and FormatNone format.

func (Format) String

func (f Format) String() string

String representation of a format.

type GNgob

type GNgob struct{}

GNgob is for serializing data into gob format.

func (GNgob) Decode

func (e GNgob) Decode(input []byte, output interface{}) error

Decode deserializes gob bytes into a go object. It returns an error if decoding fails.

func (GNgob) Encode

func (e GNgob) Encode(input interface{}) ([]byte, error)

Encode takes an object and serializes it into gob blob. It returns an error in case of encoding problems.

type GNjson

type GNjson struct {
	Pretty bool
}

GNjson allows to decode and encode JSON format.

func (GNjson) Decode

func (e GNjson) Decode(input []byte, output interface{}) error

Decode converts JSON into a go object. If decoding breaks, it returns an error.

func (GNjson) Encode

func (e GNjson) Encode(input interface{}) ([]byte, error)

Encode takes an object and coverts it into JSON. It returns an error if the encoding fails.

func (GNjson) Output

func (e GNjson) Output(input interface{}, f Format) string

Output converts an object into a JSON string. It takes an object and a format and returns the corresponding JSON string. In case of a problem it returns an empty string.

type Outputter

type Outputter interface {
	// FormattedOutput takes a record and returns a string representation of
	// the record accourding to supplied format.
	Output(record interface{}, f Format) string
}

Outputter interface us a uniform way to create an output of a datum

Jump to

Keyboard shortcuts

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