iof

package module
v0.0.0-...-53ef23c Latest Latest
Warning

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

Go to latest
Published: May 10, 2020 License: MIT Imports: 10 Imported by: 0

README

iof GoDoc

iof simplifies i/o operations with files of different data-serialization formats.

features

  • designed to facilitate the process of reading/writing files with simple API:
// Reader is the interface that decodes content of a file
// to the given value.
type Reader interface {
	Read(name string, v interface{}) error
}

// Writer is the interface that creates a new file
// with the content of encoded value, regardless
// to file existence. If file already exists -
// it will be overrided with a new content.
type Writer interface {
	Write(name string, v interface{}) error
}
  • decreases amount of mechanical code like:
f, err := os.Create("file.json")
if err != nil {
    return err
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetEscapeHTML(false)
enc.SetIndent("", "	")
return enc.Encode(f)
  • allows performing i/o operations according to file's extension.

There are few other methods which provide more accurately reading/writing functionality:

  • Insert - writes content only to a new file; returns error if file already exists;
  • Update - writes content only to existing file; returns error if file not exists;
  • Upsert - does same as Write method.

installation

Standard go get:

go get github.com/unqnown/iof

usage & example

package iof_test

import (
	"fmt"
	"log"

	"github.com/unqnown/iof"
)

type Data struct {
	V interface{}
}

func Example() {
	if err := iof.JSON.Write("test.json", Data{V: "ʕ•ᴥ•ʔ"}); err != nil {
		log.Fatal(err)
	}
	var read Data
	if err := iof.JSON.Read("test.json", &read); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%v", read.V)
	// output: ʕ•ᴥ•ʔ
}

func Example_Global() {
	if err := iof.Write("test.yaml", Data{V: "ʕ•ᴥ•ʔ"}); err != nil {
		log.Fatal(err)
	}
	var read Data
	if err := iof.Read("test.yaml", &read); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s", read.V)
	// output: ʕ•ᴥ•ʔ
}

Documentation

Overview

Example
package main

import (
	"fmt"
	"log"

	"github.com/unqnown/iof"
)

type Data struct {
	V interface{}
}

func main() {
	if err := iof.JSON.Write("test.json", Data{V: "ʕ•ᴥ•ʔ"}); err != nil {
		log.Fatal(err)
	}
	var read Data
	if err := iof.JSON.Read("test.json", &read); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%v", read.V)
}
Output:

ʕ•ᴥ•ʔ

Index

Examples

Constants

View Source
const CSV csv = ".csv"

CSV is Encoding implementation for "csv" data format.

View Source
const GOB gob = ".gob"

GOB is Encoding implementation for "gob" data format.

View Source
const JSON json = ".json"

JSON is Encoding implementation for "json" data format.

View Source
const XML xml = ".xml"

XML is Encoding implementation for "xml" data format.

View Source
const YAML yaml = ".yaml"

YAML is Encoding implementation for "yaml" data format.

Variables

View Source
var (
	// ErrAlreadyExists means that that resource of given name already exists.
	ErrAlreadyExists = errors.New("iof: already exists")
	// ErrNotExists means that that resource of given name not exists.
	ErrNotExists = errors.New("iof: not exists")
	// ErrNotFile means that resource of given name is not a file.
	ErrNotFile = errors.New("iof: not file")
)

Functions

func Insert

func Insert(name string, v interface{}) error

Insert encodes given value to a new file via Encoding resolved from file's extension.

func Read

func Read(name string, v interface{}) error

Read decodes content of a file to the given value via Encoding resolved from file's extension.

func RegisterEncoding

func RegisterEncoding(ext string, enc Encoding)

RegisterEncoding makes new Encoding available for resolving by file extension.

func Update

func Update(name string, v interface{}) error

Update encodes given value to the existing file via Encoding resolved from file's extension.

func Upsert

func Upsert(name string, v interface{}) error

Upsert encodes given value to a file via Encoding resolved from file's extension.

func Write

func Write(name string, v interface{}) error

Write encodes given value to a file via Encoding resolved from file's extension.

Types

type Encoding

type Encoding interface {
	fmt.Stringer
	Reader
	Writer

	// Insert creates a new file with the content of encoded value.
	// If file already exists returns ErrAlreadyExists.
	Insert(name string, v interface{}) error
	// Update updates existing file with content of encoded value.
	// If file not exists returns ErrNotExists.
	Update(name string, v interface{}) error
	// Upsert is an alias for Write method.
	Upsert(name string, v interface{}) error
}

Encoding represents interface for file i/o workflow.

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

type Reader

type Reader interface {
	Read(name string, v interface{}) error
}

Reader is the interface that decodes content of a file to the given value.

type Writer

type Writer interface {
	Write(name string, v interface{}) error
}

Writer is the interface that creates a new file with the content of encoded value, regardless to file existence. If file already exists - it will be overrided with a new content.

Jump to

Keyboard shortcuts

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