filehelper

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2020 License: MIT Imports: 25 Imported by: 1

README

File Helper

Documentation Go Report Card Build Status

See (limited) tests for example usage. This package is used by some other projects, you might find it useful as well, but this package is a borderline anti-pattern.

Main responsibilities:

  • read and write csv
  • unified file read for csv, xml or json with extendible parsing
  • template parsing with handy functions - see tests

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindInTar

func FindInTar(tarfile, search string) map[string]string

FindInTar looks for search string in tarball, returns list of filenames and matches

func JsonEscape

func JsonEscape(i string) string

JsonEscape escapes a string for JSON

func ListTar

func ListTar(filename string) []string

ListTar will return file list from given tar file

func MustProcessTemplateFile

func MustProcessTemplateFile(template string, bundle interface{}) string

MustProcessTemplateFile processes golang template file

func MustTemplate

func MustTemplate(str string, data interface{}) string

MustTemplate parses string as Go template, using data as scope

func OnlyWriteCSV

func OnlyWriteCSV(w csv.Writer, columns []string, rows []map[string]interface{}) error

OnlyWriteCSV writes headers and rows into a given file handle

func ProcessTemplateFile

func ProcessTemplateFile(template string, bundle interface{}) ([]byte, error)

ProcessTemplateFile processes golang template file

func ReadCSV

func ReadCSV(filename string) ([]map[string]string, []string, error)

ReadCSV reads csv into []map[string]string + []string for headers

func ReadTar

func ReadTar(tarfile, filename string) interface{}

ReadTar reads filename from given tarball and returns content

func RegisterFS

func RegisterFS(filesystem afero.Fs)

RegisterFS (afero) virtual filesystem for ProcessTemplateFile

func SplitKeys

func SplitKeys(v interface{}) ([]string, []map[string]interface{}, error)

SplitKeys creates a map for CSV header

func Template

func Template(str string, data interface{}) (string, error)

Template parses string as Go template, using data as scope

func TemplateDelim

func TemplateDelim(str string, data interface{}, begin, end string) (string, error)

TemplateDelim parses string with custom delimiters as Go template, using data as scope

func WriteCSV

func WriteCSV(file io.ReadWriter, columns []string, rows []map[string]interface{}) ([]byte, error)

WriteCSV writes headers and rows into a given file handle and reads it back as []byte

func WriteTar

func WriteTar(datafile, filename string, buf []byte)

WriteTar will append to datafile with filename using buf data

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is the main type

func NewParser

func NewParser() *Parser

NewParser defines a new parser

func (*Parser) ParseStruct

func (l *Parser) ParseStruct(content io.Reader, format string) (interface{}, error)

ParseStruct parses byte slice into map or slice

func (*Parser) ReadStruct

func (l *Parser) ReadStruct(filename, format string) (interface{}, error)

ReadStruct reads from given file, parsing into structure

func (*Parser) RegisterParser

func (l *Parser) RegisterParser(format string, parser ParserFunc)

RegisterParser registers or overrides a format parser func. Indices are lower case.

type ParserFunc

type ParserFunc func(io.Reader) (interface{}, error)

ParserFunc is to parse a []byte into an interface{}

Jump to

Keyboard shortcuts

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