multiparser

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2023 License: Apache-2.0 Imports: 3 Imported by: 1

README

multiparser

Parse raw data into concrete types unburdened by serialization standards.

Example usage

Current multiparser example uses JSON and YAML parsers.

package main

import (
    "github.com/ramizpolic/multiparser"
    "github.com/ramizpolic/multiparser/parser"
)

type object struct {
    Data string `json:"data" yaml:"data"`
}

func main() {
    parser, _ := multiparser.New(parser.JSON, parser.YAML)

    // Parse JSON
    var jsonObj object
    _ = parser.Parse([]byte(`{"data": "data"}`), &jsonObj)
	
    // Parse YAML
    var yamlObj object
    _ = parser.Parse([]byte(`data: data`), &yamlObj)
}
Supported parsers
  • JSON - encoding.json
  • YAML - gopkg.in/yaml.v3
Bring your own parser

All you have to do is implement multiparser.Parser interface, e.g.

type parser struct {}

// Parse converts raw to object
func (p *parser) Parse(from []byte, to interface{}) error {
    panic("implement me")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyParsers  = errors.New("no Parser passed, at least one required")
	ErrInvalidObject = errors.New("object must be non-nil pointer")
)

Functions

This section is empty.

Types

type Parser

type Parser interface {
	Parse(from []byte, to interface{}) error
}

Parser implements raw data to object deserialization.

func New

func New(parsers ...Parser) (Parser, error)

New creates a new Parser which can be used to serialize data from different formats.

type ParserError added in v1.0.1

type ParserError struct {
	Parser string
	Err    error
}

func (*ParserError) Error added in v1.0.1

func (e *ParserError) Error() string

func (*ParserError) Unwrap added in v1.0.1

func (e *ParserError) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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