go-simple-serializer: github.com/spatialcurrent/go-simple-serializer/gss Index | Files

package gss

import "github.com/spatialcurrent/go-simple-serializer/gss"

Package gss provides simple functions for serializing/deserializing objects into common formats.

Usage

The simplest usage of gss is to call the DeserializeBytes, DeserializeString, SerializeBytes, and SerializeString functions.

inputObject, err := gss.DeserializeString(string(inputBytesPlain), inputFormat, inputHeader, inputComment, inputLazyQuotes, inputLimit, inputType, verbose)
if err != nil {
  fmt.Println(errors.Wrap(err, "error deserializing input using format "+inputFormat))
  os.Exit(1)
}
...
str, err := gss.SerializeString(object, "json", header, -1)
if err != nil {
  return "", errors.Wrap(err, "error serializing object")
}

Usage with options

You can also call [Serialize|Deserialize][Bytes|String] using an options object.

options := gss.Options{
  Header: inputHeader,
  Comment: inputComment,
  LazyQuotes: inputLazyQuotes,
  Limit: 1,
  Type: reflect.TypeOf(map[string]interface{}{}),
}

if inputFormat == "jsonl" {
  options.Format = "json"
} else {
  options.Format = inputFormat
}

for inputLine := range inputLines {
  inputObject, err := options.DeserializeBytes(inputLine, verbose)
  if err != nil {
    errorsChannel <- errors.Wrap(err, "error deserializing input using format "+objectFormat)
    continue
  }
  ...
}

Formats

GSS supports the following formats:

- bson
- csv
- tsv
- hcl
- hcl2
- json
- jsonl
- properties
- toml
- yaml

Index

Package Files

Convert.go ConvertInput.go DeserializeBytes.go DeserializeInput.go DeserializeJSON.go DeserializeJSONL.go DeserializeProperties.go DeserializeSV.go DeserializeString.go DeserializeTOML.go DeserializeYAML.go ErrEmptyRow.go ErrIncompatibleFormats.go ErrInvalidComment.go ErrInvalidKind.go ErrInvalidLimit.go ErrUnknownFormat.go Formats.go GetKeys.go GetKeysFromValue.go GetType.go GetTypeJSON.go MustSerializeString.go Options.go SerializeBytes.go SerializeInput.go SerializeJSON.go SerializeString.go TestCase.go ToRowI.go ToRowS.go ToStringSlice.go Version.go gss.go types.go

Variables

var Formats = []string{"bson", "csv", "tsv", "hcl", "hcl2", "json", "jsonl", "properties", "toml", "yaml"}

Formats is the formats supported by go-simple-serializer (GSS).

var NoComment = ""
var NoHeader = []string{}
var NoLimit = -1
var NoSkip = 0
var Version = "0.0.7"

Version is the version of this package and updated during a release.

func Convert Uses

func Convert(input *ConvertInput) (string, error)

Convert converts an input_string from the inputFormat to the outputFormat. Returns the output string and error, if any. func Convert(inputBytes []byte, inputFormat string, inputHeader []string, inputComment string, inputLazyQuotes bool, inputSkipLines int, inputLimit int, outputFormat string, outputHeader []string, outputLimit int, async bool, verbose bool) (string, error) {

func DeserializeBytes Uses

func DeserializeBytes(input *DeserializeInput) (interface{}, error)

DeserializeBytes reads in an object as string bytes and returns the representative Go instance.

func DeserializeJSON Uses

func DeserializeJSON(inputBytes []byte, outputType reflect.Type) (interface{}, error)

DeserializeJSON deserializes the input bytes into a Go object.

- https://golang.org/pkg/encoding/json/

func DeserializeJSONL Uses

func DeserializeJSONL(input io.Reader, inputComment string, inputSkipLines int, inputLimit int, outputType reflect.Type, async bool) (interface{}, error)

DeserializeJSONL deserializes the input JSON lines bytes into a Go object.

- https://golang.org/pkg/encoding/json/

func DeserializeProperties Uses

func DeserializeProperties(input string, inputComment string, outputType reflect.Type) (interface{}, error)

DeserializeProperties deserializes a properties string into a Go instance.

- https://en.wikipedia.org/wiki/.properties

func DeserializeSV Uses

func DeserializeSV(input io.Reader, format string, inputHeader []string, inputComment string, inputLazyQuotes bool, inputSkipLines int, inputLimit int, outputType reflect.Type) (interface{}, error)

DeserializeSV deserializes a CSV or TSV string into a Go instance.

- https://golang.org/pkg/encoding/csv/

func DeserializeString Uses

func DeserializeString(input string, inputFormat string, inputHeader []string, inputComment string, inputLazyQuotes bool, inputSkipLines int, inputLimit int, outputType reflect.Type, async bool, verbose bool) (interface{}, error)

DeserializeString reads in an object as a string and returns the representative Go instance.

func DeserializeTOML Uses

func DeserializeTOML(input string, outputType reflect.Type) (interface{}, error)

DeserializeTOML deserializes an input TOML string into a Go object

- https://godoc.org/pkg/github.com/BurntSushi/toml

func DeserializeYAML Uses

func DeserializeYAML(inputBytes []byte, outputType reflect.Type) (interface{}, error)

DeserializeYAML deserializes the YAML input bytes into a Go object

func GetKeys Uses

func GetKeys(object interface{}, sorted bool) []interface{}

GetKeys returns the keys for a map as an []interface{}. If you want the keys to be sorted in alphabetical order, pass sorted equal to true.

func GetKeysFromValue Uses

func GetKeysFromValue(m reflect.Value, sorted bool) []interface{}

GetKeysFromValue returns the keys for a map as an []interface{}. If you want the keys to be sorted in alphabetical order, pass sorted equal to true.

func GetType Uses

func GetType(content []byte, format string) (reflect.Type, error)

GetType takes in the content of an object as a string and the serialization format. Returns the type using reflection. This type is fixed and can be passed through functions without losing type information (unlike an empty object).

func GetTypeJSON Uses

func GetTypeJSON(str string) reflect.Type

func MustSerializeString Uses

func MustSerializeString(input *SerializeInput) string

MustSerializeString serializes an object to its representation given by format and panics if there is any error.

func SerializeBytes Uses

func SerializeBytes(input *SerializeInput) ([]byte, error)

SerializeBytes serializes an object to its representation given by format.

func SerializeJSON Uses

func SerializeJSON(obj interface{}, pretty bool) (string, error)

SerializeJSON serializes an object to its JSON representation.

func SerializeString Uses

func SerializeString(input *SerializeInput) (string, error)

SerializeString serializes an object to its representation given by format.

func ToRowI Uses

func ToRowI(keys []interface{}, m reflect.Value, valueSerializer func(object interface{}) (string, error)) ([]string, error)

ToRowI converts a map to a row of strings using a slice of interface{} keys.

func ToRowS Uses

func ToRowS(keys []string, m reflect.Value, valueSerializer func(object interface{}) (string, error)) ([]string, error)

ToRowS converts a map to a row of strings using a slice of string keys.

func ToStringSlice Uses

func ToStringSlice(keys []interface{}) []string

ToStringSlice converts a slice of interface{} to a slice of strings using fmt.Sprint.

type ConvertInput Uses

type ConvertInput struct {
    InputBytes            []byte
    InputFormat           string
    InputHeader           []string
    InputComment          string
    InputLazyQuotes       bool
    InputSkipLines        int
    InputLimit            int
    OutputFormat          string
    OutputHeader          []string
    OutputLimit           int
    OutputPretty          bool
    OutputSorted          bool
    OutputValueSerializer func(object interface{}) (string, error)
    Async                 bool
    Verbose               bool
}

ConvertInput provides the input for the Convert function.

func NewConvertInput Uses

func NewConvertInput(bytes []byte, inputFormat string, outputFormat string) *ConvertInput

type DeserializeInput Uses

type DeserializeInput struct {
    Bytes      []byte
    Format     string
    Header     []string
    Comment    string
    LazyQuotes bool
    SkipLines  int
    Limit      int
    Type       reflect.Type
    Async      bool
    Verbose    bool
}

DeserializeInput provides the input for the DeserializeBytes function.

type ErrEmptyRow Uses

type ErrEmptyRow struct{}

func (ErrEmptyRow) Error Uses

func (e ErrEmptyRow) Error() string

type ErrIncompatibleFormats Uses

type ErrIncompatibleFormats struct {
    Input  string // the name of the input format
    Output string // the name of the output format
}

ErrIncompatibleFormats is used when an input format and output format are incompatible.

func (ErrIncompatibleFormats) Error Uses

func (e ErrIncompatibleFormats) Error() string

Error returns the error as a string.

type ErrInvalidComment Uses

type ErrInvalidComment struct {
    Value string // the value of the comment
}

ErrInvalidComment is used when an invalid comment string is given.

func (ErrInvalidComment) Error Uses

func (e ErrInvalidComment) Error() string

Error returns the error as a string.

type ErrInvalidKind Uses

type ErrInvalidKind struct {
    Value reflect.Kind
    Valid []reflect.Kind
}

func (ErrInvalidKind) Error Uses

func (e ErrInvalidKind) Error() string

type ErrInvalidLimit Uses

type ErrInvalidLimit struct {
    Value int // the value of the limit
}

ErrInvalidLimit is used when an invalid limit int is given.

func (ErrInvalidLimit) Error Uses

func (e ErrInvalidLimit) Error() string

Error returns the error as a string.

type ErrUnknownFormat Uses

type ErrUnknownFormat struct {
    Name string // the name of the unknown format
}

ErrIncompatibleFormats is used when an unknown format is provided.

func (ErrUnknownFormat) Error Uses

func (e ErrUnknownFormat) Error() string

Error returns the error as a string.

type Options Uses

type Options struct {
    Format     string       // one of gss.Formats
    Header     []string     // if formt as csv or tsv, the column names
    Comment    string       // the line comment prefix
    LazyQuotes bool         // if format is csv or tsv, allow LazyQuotes.
    SkipLines  int          // if format is csv, tsv, or jsonl, the number of lines to skip before processing.
    Limit      int          // if format is a csv, tsv, or jsonl, then limit the number of items processed.
    Type       reflect.Type // the type of the output object
    Async      bool         // async processing
    Pretty     bool         // pretty output
}

Options is a struct that includes option values used during iterative processing.

func (Options) DeserializeBytes Uses

func (o Options) DeserializeBytes(content []byte, verbose bool) (interface{}, error)

Deserialize the input bytes using the values in the options object.

func (Options) DeserializeString Uses

func (o Options) DeserializeString(content string, verbose bool) (interface{}, error)

Deserialize the input string using the values in the options object.

func (Options) SerializeString Uses

func (o Options) SerializeString(object interface{}) (string, error)

type SerializeInput Uses

type SerializeInput struct {
    Object          interface{}
    Format          string
    Header          []string
    Limit           int
    Pretty          bool
    Sorted          bool
    ValueSerializer func(object interface{}) (string, error)
}

SerializeInput provides the input for the SerializeString and SerializeBytes function.

type TestCase Uses

type TestCase struct {
    String     string       // the object in serialized form
    Object     interface{}  // the object as a go object
    Format     string       // the serialization format
    Header     []string     // if format is a csv or tsv, the names of the columns
    Comment    string       // the line comment prefix
    LazyQuotes bool         // if format is csv or tsv, allow LazyQuotes.
    SkipLines  int          // if format is csv, tsv, or jsonl, then the number of lines to skip before processing.
    Limit      int          // if format is a csv, tsv, or jsonl, then limit the number of items processed.
    Type       reflect.Type // // the type of the object when in Go
}

TestCase contains a case for unit tests.

Package gss imports 23 packages (graph) and is imported by 3 packages. Updated 2019-05-23. Refresh now. Tools for package owners.