formats

package module
v0.0.0-...-82e0b4c Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2018 License: MIT Imports: 8 Imported by: 3

README

formats

Build Status

This package provides:

  • command-line tool which converts stdin from format A to format B
  • library with consistent API to transform data from format A to format B

Supported formats:

  • JSON
  • YAML
  • TOML

Command-line tool example

$ go get -u github.com/corpix/formats/...
...

$ echo '{"hello": ["world", {"of": "foo", "bar": true}]}' | formats --from json --to yaml
hello:
- world
- bar: true
  of: foo

$ echo '{"hello": ["world", {"of": "foo", "bar": true}]}' | formats --from json --to yaml | formats --from yaml --to json
{"hello":["world",{"bar":"true","of":"foo"}]}

$ echo -n 'hello' | formats --to hex
68656c6c6f

$ echo -n '68656c6c6f' | formats --from hex
hello

$ echo -n '68656c6c6f' | formats --from hex --to json
"hello"

Library usage example

This will convert JSON to YAML:

package main

import (
	"fmt"

	"github.com/corpix/formats"
)

var (
	json = `
        {
            "name": "Danny",
            "roles": ["warrior", "worker"]
        }
    `
)

func main() {
	v := new(interface{})

	j := formats.NewJSON()
	err := j.Unmarshal(
		[]byte(json),
		v,
	)
	if err != nil {
		panic(err)
	}

	y := formats.NewYAML()
	yaml, err := y.Marshal(v)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(yaml))
}
$ go run ./example/json-to-yaml/json-to-yaml.go
name: Danny
roles:
- name: warrior
- name: worker

$ go run ./example/json-to-toml/json-to-toml.go
name = "Danny"

[[roles]]
name = "warrior"

[[roles]]
name = "worker"

Limitations

There is a compatibility layer for:

  • JSON, which helps to mitigate non string keys in maps before they will be marshaled into JSON

YAML

go-yaml handles struct keys without tags in non-standard way, they are lowercased.

There is no good workaround for this at the time of writing. Make sure you have tags for your struct field.

Actualy I'd like to switch to more configurable yaml marshaler in the future, but at this time there is nothing better :(

TOML

toml can not marshal interface{} values at this time(panics, requires struct or map).

Which is strange, probably some reflection misuse.

License

MIT

Documentation

Index

Constants

View Source
const (
	// JSON is a JSON format name.
	JSON = "json"

	// YAML is a YAML format name.
	YAML = "yaml"

	// TOML is a TOML format name.
	TOML = "toml"

	// HEX is a HEX format name.
	HEX = "hex"
)

Variables

View Source
var (
	// Names lists supported set of formats
	// which could be used as argument to Name() and
	// also available as constants exported from the package.
	Names = []string{
		JSON,
		YAML,
		TOML,
		HEX,
	}
)

Functions

func NewErrFormatNameIsEmpty

func NewErrFormatNameIsEmpty() error

NewErrFormatNameIsEmpty wraps format name with ErrFormatNameIsEmpty.

func NewErrNotSupported

func NewErrNotSupported(format string) error

NewErrNotSupported wraps format name with ErrNotSupported.

Types

type ErrFormatNameIsEmpty

type ErrFormatNameIsEmpty struct{}

ErrFormatNameIsEmpty indicates that format name is an empty string.

func (*ErrFormatNameIsEmpty) Error

func (e *ErrFormatNameIsEmpty) Error() string

type ErrNotSupported

type ErrNotSupported struct {
	Format string
}

ErrNotSupported indicates that format with specified name is not supported.

func (*ErrNotSupported) Error

func (e *ErrNotSupported) Error() string

type Format

type Format interface {
	Marshal(v interface{}) ([]byte, error)
	Unmarshal(data []byte, v interface{}) error
	Name() string
}

Format is a iterator that provides a data from source.

func New

func New(name string) (Format, error)

New create a new format marshaler/unmarshaler from name.

func NewFromPath

func NewFromPath(path string) (Format, error)

NewFromPath returns a new Format from file extension in the path argument. It respects the synonyms for format names, for example: yaml format files could have extensions yaml or yml.

type HEXFormat

type HEXFormat struct{}

HEXFormat is a HEX marshaler.

func NewHEX

func NewHEX() *HEXFormat

NewHEX constructs a new HEX format marshaler.

func (*HEXFormat) Marshal

func (f *HEXFormat) Marshal(v interface{}) ([]byte, error)

Marshal serializes data represented by v into slice of bytes.

func (*HEXFormat) Name

func (f *HEXFormat) Name() string

Name returns a format name which is used to identify this format in the package.

func (*HEXFormat) Unmarshal

func (f *HEXFormat) Unmarshal(data []byte, v interface{}) error

Unmarshal deserializes data represented by data byte slice into v.

type JSONFormat

type JSONFormat struct{}

JSONFormat is a JSON marshaler.

func NewJSON

func NewJSON() *JSONFormat

NewJSON constructs a new JSON format marshaler.

func (*JSONFormat) Marshal

func (j *JSONFormat) Marshal(v interface{}) ([]byte, error)

Marshal serializes data represented by v into slice of bytes.

func (*JSONFormat) Name

func (j *JSONFormat) Name() string

Name returns a format name which is used to identify this format in the package.

func (*JSONFormat) Unmarshal

func (j *JSONFormat) Unmarshal(data []byte, v interface{}) error

Unmarshal deserializes data represented by data byte slice into v.

type Stringer

type Stringer string

func NewStringer

func NewStringer(s string) *Stringer

func (*Stringer) String

func (s *Stringer) String() string

type TOMLFormat

type TOMLFormat struct{}

TOMLFormat is a TOML marshaler.

func NewTOML

func NewTOML() *TOMLFormat

NewTOML constructs a new TOML format marshaler.

func (*TOMLFormat) Marshal

func (y *TOMLFormat) Marshal(v interface{}) ([]byte, error)

Marshal serializes data represented by v into slice of bytes.

func (*TOMLFormat) Name

func (y *TOMLFormat) Name() string

Name returns a format name which is used to identify this format in the package.

func (*TOMLFormat) Unmarshal

func (y *TOMLFormat) Unmarshal(data []byte, v interface{}) error

Unmarshal deserializes data represented by data byte slice into v.

type YAMLFormat

type YAMLFormat struct{}

YAMLFormat is a YAML marshaler.

func NewYAML

func NewYAML() *YAMLFormat

NewYAML constructs a new YAML format marshaler.

func (*YAMLFormat) Marshal

func (y *YAMLFormat) Marshal(v interface{}) ([]byte, error)

Marshal serializes data represented by v into slice of bytes.

func (*YAMLFormat) Name

func (y *YAMLFormat) Name() string

Name returns a format name which is used to identify this format in the package.

func (*YAMLFormat) Unmarshal

func (y *YAMLFormat) Unmarshal(data []byte, v interface{}) error

Unmarshal deserializes data represented by data byte slice into v.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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