transform

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: MIT Imports: 7 Imported by: 20

Documentation

Overview

Package transform defines some useful transformations on TOML documents.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindTable added in v0.0.3

func FindTable(doc *tomledit.Document, name ...string) *tomledit.Entry

FindTable returns the entry the first table with the given name in doc, or nil if no such table exists.

func WithLogWriter

func WithLogWriter(ctx context.Context, w io.Writer) context.Context

WithLogWriter attaches w to ctx as a logging target.

Types

type Applier

type Applier interface {
	Apply(context.Context, *tomledit.Document) error
}

An Applier applies one or more transformations to a document. The Transform, Step, and Plan types implement this interface.

type Plan

type Plan []Step

A Plan is a sequence of transformations to be applied in order.

func (Plan) Apply

func (p Plan) Apply(ctx context.Context, doc *tomledit.Document) error

Apply applies each step of p to the document in order, and reports the error from the first step that fails, or nil. An empty Plan always succeeds.

type Step

type Step struct {
	Desc    string    // human-readable description (for logging)
	T       Transform // the transformation itself
	ErrorOK bool      // if true, ignore errors from evaluating T
}

A Step is a single transformation in a plan.

func (Step) Apply

func (s Step) Apply(ctx context.Context, doc *tomledit.Document) error

Apply applies the transformation step and reports its error. If ErrorOK is true, a non-nil error from the transformation is ignored.

type Transform

type Transform func(context.Context, *tomledit.Document) error

A Transform performs an arbitrary transformation on a document and reports whether the transformation was successful.

func MoveKey

func MoveKey(oldKey, rootKey, newKey parser.Key) Transform

MoveKey moves the mapping at oldKey from its current location to be a child of rootKey with the new name newKey. It reports whether the key was moved.

func Remove

func Remove(key parser.Key) Transform

Remove removes the section or mapping at key, and reports whether the removal was successful.

func Rename

func Rename(oldKey, newKey parser.Key) Transform

Rename renames the section or mapping at oldKey to newKey, and reports whether the rename was successful. The mapping is not moved within the document, only its label is changed.

func SnakeToKebab

func SnakeToKebab() Transform

SnakeToKebab transforms all the key names in doc from snake_case to kebab-case. This transformation cannot fail.

func (Transform) Apply

func (t Transform) Apply(ctx context.Context, doc *tomledit.Document) error

Apply applies t to doc. It is syntactic sugar for calling the function.

Jump to

Keyboard shortcuts

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