text

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package text parses entities from textual input like CSV or TSV files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldsExample

func FieldsExample(typ seed.Entity) string

FieldsExample returns an example value for the web interface's "fields" input.

func InputExample

func InputExample(typ seed.Entity, format Format) string

InputExample returns an example value for the web interface's "input" textarea.

func ListFields

func ListFields(typ seed.Entity, html bool) map[string]string

ListFields returns a map from the names of fields that can be passed to Read for typ to human-readable descriptions. If html is true, links in descriptions are rewritten to HTML links.

func ParseSetCommands

func ParseSetCommands(cmds []string, typ seed.Entity) ([][2]string, error)

ParseSetCommands parses "field=val" commands into pairs and validates that they can be used to set fields on a seed.Edit of the supplied type.

func Read

func Read(ctx context.Context, r io.Reader, format Format, typ seed.Entity,
	fields []string, rawSetCmds []string, db *mbdb.DB, opts ...Option) ([]seed.Edit, error)

Read reads one or more edits of the specified type from r in the specified format. fields specifies the field associated with each column (unused for the KeyVal format). Multiple fields can be associated with a single column by separating their names with slashes, and empty field names indicate that the column should be ignored. rawSets contains "field=value" directives describing values to set for all edits.

func SetExample

func SetExample(typ seed.Entity) string

SetExample returns an example value for the web interface's "set" textarea.

func SetField

func SetField(edit seed.Edit, field, val string) error

SetField sets the named field in edit. The field must be appropriate for the edit's type (see ListFields).

Types

type Format

type Format string

Format represents a textual format for supplying seed data.

const (
	// CSV corresponds to lines of comma-separated values as described in RFC 4180.
	// See https://pkg.go.dev/encoding/csv.
	CSV Format = "csv"
	// KeyVal corresponds to an individual "field=value" pair on each line.
	// Unlike the other formats, this is used to specify a single edit.
	KeyVal Format = "keyval"
	// TSV corresponds to lines of tab-separated values. No escaping is supported.
	TSV Format = "tsv"
)

type Option

type Option func(*config)

Option can be passed to Read to configure its behavior.

func MaxEdits

func MaxEdits(max int) Option

MaxEdits returns an Option that limits the maximum number of edits to be read. If more edits are supplied, an error will be returned.

func MaxFields

func MaxFields(max int) Option

MaxEdits returns an Option that limits the maximum number of fields that can be set. "field=value" directives are included in the count.

Jump to

Keyboard shortcuts

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