dati

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2023 License: GPL-3.0 Imports: 17 Imported by: 1

README

Overview

dati provides a single unified interface for data-serialization and template languages. Ideally it supports any language that you need! If it doesn't, let me know or feel free to add support and submit a patch.

It can be used as a Go library or a binary for excuting data against templates, it was built as a library first but works well in both forms:

  • The Go library allows you to easily parse data-serialization and template languages using a single interface (removing the need to learn a seperate library for each). This allows much better coherence and readability in your own software.
  • The binary was originally intended to showcase what can be done with the library. It works well as its own data-template executioner though.

Why?

Load data files without dati:

package example

import("os";"io";"path/filepath";"encoding/json";"gopkg.in/yaml.v3";"github.com/pelletier/go-toml")

func ParseDataFile(path string) (data map[string]any, err error) {
	var file *os.File
	if file, err = os.Open(path); err != nil {
		return
	}

	var filedata []byte
	if filedata, err = io.ReadAll(file); err != nil {
		return
	}

	switch filepath.Ext(path) {
	case ".yaml":
		err = yaml.Unmarshal(filedata, &data)
	case ".toml":
		err = toml.Unmarshal(filedata, &data)
	case ".json":
		err = json.Unmarshal(filedata, &data)
	default:
		err = fmt.Errorf("unsupported filetype: '%s'", filepath.Ext(path))
	}

	return
}

Load any data file with dati:

package example

import ("notabug.org/gearsix/dati")

func ParseDataFile(path string) (data map[string]any, err error) {
	err = dati.LoadDataFile(path, &data)
	return
}

And this doesn't even begin to demonstrate the relief dati provides when libraries have different interfaces (the template libraries are much worse for this)!

Documentation

Go library documentation

Binary documentation

Supported Languages

Supported Data-Serialization Languages

Supported Template Language

Acknowledgments

All of these libraries do the hard work, dati just combines them together - so thanks to the authors. Also here for reference.

Authors

  • gearsix

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedTemplate = func(format string) error {
		return fmt.Errorf("template language '%s' is not supported", format)
	}
	ErrUnknownTemplateType = func(templateType string) error {
		return fmt.Errorf("unable to infer template type '%s'", templateType)
	}
	ErrRootPathIsDir = func(path string) error {
		return fmt.Errorf("rootPath path must be a file, not a directory (%s)", path)
	}
	ErrNilTemplate = errors.New("template is nil")
)
View Source
var ErrUnsupportedData = func(format string) error {
	return fmt.Errorf("data format '%s' is not supported", format)
}

Functions

func IsDataFormat added in v1.1.0

func IsDataFormat(path string) bool

IsDataFile checks if `path` is one of the known *DatFormat*s.

func IsTemplateLanguage added in v1.1.0

func IsTemplateLanguage(path string) bool

IsTemplateLanguage will return a bool if the file found at `path` is a known *TemplateLanguage*, based upon it's file extension.

func LoadData

func LoadData(format DataFormat, in io.Reader, out interface{}) error

LoadData attempts to load all data from `in` as `format` and writes the result in the pointer `out`.

func LoadDataFile

func LoadDataFile(path string, outp interface{}) error

LoadDataFile loads all the data from the file found at `path` into the the format of that files extension (e.g. "x.json" will be loaded as a json). The result is written to the value pointed at by `outp`.

func SortFileList

func SortFileList(paths []string, order string) (sorted []string, err error)

SortFileList sorts `filepath` (a list of filepaths) in `order`. `order` can be any of the following values: "filename", "filename-asc", "filename-desc", "modified", "modified-asc", "modified-desc"; by default "filename" and "modified" are in ascending direction, if specified an "-asc" suffix will set the direction to ascending and "-desc" will set the direction to descending. This was originally intended to be used before calling LoadDataFiles on a set of "data" files.

func WriteData added in v1.2.1

func WriteData(format DataFormat, data interface{}, w io.Writer) error

WriteData attempts to write `data` as `format` to `outp`.

func WriteDataFile added in v1.2.1

func WriteDataFile(format DataFormat, data interface{}, path string, force bool) (f *os.File, err error)

WriteDataFile attempts to write `data` as `format` to the file at `path`. If `force` is *true*, then any existing files will be overwritten.

Types

type DataFormat

type DataFormat string

DataFormat provides a list of supported languages for data files (lower-case)

const (
	JSON DataFormat = "json"
	YAML DataFormat = "yaml"
	TOML DataFormat = "toml"
)

func ReadDataFormat

func ReadDataFormat(path string) DataFormat

ReadDataFormat returns the *DataFormat* that the file extension of `path` matches. If the file extension of `path` does not match any *DataFormat*, then an "" is returned.

func (DataFormat) String added in v1.2.2

func (df DataFormat) String() string

String returns the typical file extension used to represent `df`

type Template

type Template struct {
	Name string
	T    interface{}
}

Template is a wrapper to interface with any template parsed by dati. Ideally it would have just been an interface{} that defines Execute but the libaries being used aren't that uniform.

func LoadTemplate

func LoadTemplate(lang TemplateLanguage, rootName string, root io.Reader, partials map[string]io.Reader) (t Template, err error)

LoadTemplate loads a Template from `root` of type `lang`, named `name`. `lang` must be an element in `SupportedTemplateLangs`. `name` is optional, if empty the template name will be "template". `root` should be a string of template, with syntax matching that of `lang`. `partials` should be a string of template, with syntax matching that of `lang`.

func LoadTemplateFile

func LoadTemplateFile(rootPath string, partialPaths ...string) (t Template, err error)

LoadTemplateFilepath loads a Template from file `root`. All files in `partials` that have the same template type (identified by file extension) are also parsed and associated with the parsed root template.

func LoadTemplateString

func LoadTemplateString(lang TemplateLanguage, rootName string, root string, partials map[string]string) (t Template, e error)

LoadTemplateString will convert `root` and `partials` data to io.StringReader variables and return a `LoadTemplate` call using them as parameters. The `partials` map should have the template name to assign the partial template to in the string key and the template data in as the value.

func (*Template) Execute

func (t *Template) Execute(data interface{}) (result bytes.Buffer, err error)

Execute executes `t` against `d`. Reflection is used to determine the template type and call it's execution fuction.

func (*Template) ExecuteToFile added in v1.3.0

func (t *Template) ExecuteToFile(data interface{}, path string, force bool) (f *os.File, err error)

ExecuteToFile writes the result of `(*Template).Execute(data)` to the file at `path` (if no errors occurred). If `force` is true, any existing file at `path` will be overwritten.

type TemplateLanguage

type TemplateLanguage string

TemplateLanguage provides a list of supported languages for Template files (lower-case)

const (
	TMPL  TemplateLanguage = "tmpl"
	HTMPL TemplateLanguage = "htmpl"
	MST   TemplateLanguage = "mst"
)

func ReadTemplateLangauge added in v1.0.0

func ReadTemplateLangauge(path string) TemplateLanguage

ReadTemplateLanguage returns the *TemplateLanguage* that the file extension of `path` matches. If the file extension of `path` does not match any *TemplateLanguage*, then an "" is returned.

func (TemplateLanguage) String added in v1.2.2

func (t TemplateLanguage) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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