Documentation ¶
Overview ¶
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, hcl, hcl2, json, jsonl, properties, tags, toml, yaml.
Index ¶
- Constants
- Variables
- func CanStream(inputFormat string, outputFormat string, outputSorted bool) bool
- func Convert(input *ConvertInput) ([]byte, error)
- func DeserializeBytes(input *DeserializeBytesInput) (interface{}, error)
- func DeserializeReader(input *DeserializeReaderInput) (interface{}, error)
- func GetType(content []byte, format string) (reflect.Type, error)
- func GetTypeJSON(str string) reflect.Type
- func MustSerializeString(input *SerializeBytesInput) string
- func SerializeBytes(input *SerializeBytesInput) ([]byte, error)
- type ConvertInput
- type DeserializeBytesInput
- type DeserializeReaderInput
- type ErrIncompatibleFormats
- type ErrInvalidComment
- type ErrInvalidKind
- type ErrInvalidLimit
- type ErrUnknownFormat
- type SerializeBytesInput
Constants ¶
const ( NoSkip = 0 // used as SkipLines parameter to indicate no skipping when reading NoLimit = -1 // used to indicate that there is no limit on reading or writing, depending on context. NoComment = "" // used to indicate that there is no comment prefix to consider. )
Variables ¶
var ( // NoHeader is used to indicate that no defined header is given. // Derive the header from the input data. NoHeader = []interface{}{} // Formats is a list of all the formats supported by GSS Formats = serializer.Formats )
var (
ErrEmptyRow = errors.New("empty row")
)
Functions ¶
func CanStream ¶ added in v0.0.9
CanStream returns true if you can process the data as a stream from the given input format to the output format. There are a few logical rules for deciding if streaming is possible. If you are sorting the output, then you cannot stream the data. If the output format has a header, then the input format must also have a header to stream.
func Convert ¶
func Convert(input *ConvertInput) ([]byte, error)
func DeserializeBytes ¶
func DeserializeBytes(input *DeserializeBytesInput) (interface{}, error)
DeserializeBytes reads in an object as string bytes and returns the representative Go instance.
func DeserializeReader ¶
func DeserializeReader(input *DeserializeReaderInput) (interface{}, error)
DeserializeReader reads the serialized object from an io.Reader and returns the representative Go instance.
func GetType ¶
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 ¶
func MustSerializeString ¶
func MustSerializeString(input *SerializeBytesInput) string
MustSerializeString serializes an object to its representation given by format and panics if there is any error.
func SerializeBytes ¶
func SerializeBytes(input *SerializeBytesInput) ([]byte, error)
SerializeBytes serializes an object to its representation given by format.
Types ¶
type ConvertInput ¶
type ConvertInput struct { InputBytes []byte InputFormat string InputHeader []interface{} InputComment string InputLazyQuotes bool InputScannerBufferSize int InputSkipLines int InputLimit int InputLineSeparator string InputDropCR bool InputTrim bool InputEscapePrefix string InputUnescapeSpace bool InputUnescapeNewLine bool InputUnescapeEqual bool InputType reflect.Type OutputFormat string OutputFormatSpecifier string OutputFit bool OutputHeader []interface{} OutputLimit int OutputPretty bool OutputSorted bool OutputReversed bool OutputKeySerializer stringify.Stringer OutputValueSerializer stringify.Stringer OutputLineSeparator string OutputKeyValueSeparator string OutputEscapePrefix string OutputEscapeSpace bool OutputEscapeNewLine bool OutputEscapeEqual bool }
ConvertInput provides the input for the Convert function.
func NewConvertInput ¶
func NewConvertInput(bytes []byte, inputFormat string, outputFormat string) *ConvertInput
type DeserializeBytesInput ¶
type DeserializeBytesInput struct { Bytes []byte Format string Header []interface{} Comment string LazyQuotes bool ScannerBufferSize int SkipLines int SkipBlanks bool SkipComments bool Trim bool Limit int LineSeparator string DropCR bool Type reflect.Type EscapePrefix string UnescapeSpace bool UnescapeNewLine bool UnescapeColon bool UnescapeEqual bool }
DeserializeBytesInput provides the input for the DeserializeBytes function.
type DeserializeReaderInput ¶
type DeserializeReaderInput struct { Reader io.Reader Format string Header []interface{} Comment string LazyQuotes bool SkipLines int SkipBlanks bool SkipComments bool Trim bool Limit int LineSeparator string DropCR bool Type reflect.Type EscapePrefix string UnescapeSpace bool UnescapeNewLine bool UnescapeColon bool UnescapeEqual bool }
DeserializeReaderInput provides the input for the DeserializeReader function.
type ErrIncompatibleFormats ¶
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 ¶
func (e ErrIncompatibleFormats) Error() string
Error returns the error as a string.
type ErrInvalidComment ¶
type ErrInvalidComment struct {
Value string // the value of the comment
}
ErrInvalidComment is used when an invalid comment string is given.
func (ErrInvalidComment) Error ¶
func (e ErrInvalidComment) Error() string
Error returns the error as a string.
type ErrInvalidKind ¶
func (ErrInvalidKind) Error ¶
func (e ErrInvalidKind) Error() string
type ErrInvalidLimit ¶
type ErrInvalidLimit struct {
Value int // the value of the limit
}
ErrInvalidLimit is used when an invalid limit int is given.
func (ErrInvalidLimit) Error ¶
func (e ErrInvalidLimit) Error() string
Error returns the error as a string.
type ErrUnknownFormat ¶
type ErrUnknownFormat struct {
Name string // the name of the unknown format
}
ErrIncompatibleFormats is used when an unknown format is provided.
func (ErrUnknownFormat) Error ¶
func (e ErrUnknownFormat) Error() string
Error returns the error as a string.
type SerializeBytesInput ¶
type SerializeBytesInput struct { Object interface{} Format string FormatSpecifier string Fit bool Header []interface{} Limit int Pretty bool Sorted bool Reversed bool LineSeparator string KeyValueSeparator string KeySerializer func(object interface{}) (string, error) ValueSerializer func(object interface{}) (string, error) EscapePrefix string EscapeSpace bool EscapeNewLine bool EscapeEqual bool EscapeColon bool ExpandHeader bool }
SerializeBytesInput provides the input for the SerializeString and SerializeBytes function.