goembed

package module
v0.0.0-...-6e25e9e Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2015 License: Apache-2.0 Imports: 8 Imported by: 0

README

goembed

goembed is a static asset embedder to use with go generate, introduced in Go 1.4 (https://blog.golang.org/go1.4).

For more information about the goembed command, see http://godoc.org/github.com/jeanfric/goembed/cmd/goembed.

See Also

Assets generated by goembed can be used directly with package github.com/jeanfric/embedfs, which provides an http.FileSystem implementation backed by a map[string]string.

For more information, see https://github.com/jeanfric/embedfs.

Documentation

Overview

Package goembed implements common asset embedder patterns that can be used by concrete asset embedder implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Asset

type Asset struct {
	io.Reader
	Key string
}

An Asset represents a named piece of data, typically the contents of a file identified by its path (beginning with a "/", and using forward slashes ("/") as path separators).

func FindAssets

func FindAssets(rootPath string) ([]*Asset, error)

FindAssets walks a directory recursively and generates a list of embeddable assets that can be embedded using an AssetEmbedder. The Key of each asset will start with a forward slash ("/"), and use slashes as path separators.

type AssetEmbedder

type AssetEmbedder interface {
	AssetEmbed(dst io.Writer, assets []*Asset, packageName, fnName string) (bytes int, err error)
}

AssetEmbedder is an interface that wraps the basic AssetEmbed method.

AssetEmbed is a method that can produce a Go source file that embeds assets as encoded strings, in package packageName, and with fnName as the method to call to load and retrieve the embedded assets. The fnName function must have the following signature:

func fnName() (map[string]string, error)

The returned map is keyed by the asset key, and the value is the decoded contents of the asset (that is, an exact replica of the original contents of the piece of data that was embedded). Typically, the key is a file path, and the value is the contents of the file.

type ConcurrentEmbedder

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

A concurrent embedder is an embedder that concurrently encodes assets, with up to runtime.NumCPU() concurrent embedders.

func NewConcurrentEmbedder

func NewConcurrentEmbedder(encodeFunc func(io.Reader) (string, error), decodeFunc string, imports []string) *ConcurrentEmbedder

NewConcurrentEmbedder creates a new concurrent embedder that encodes assets using encodeFunc, with up to runtime.NumCPU() concurrent embedders. The encoded assets are written to the generated Go source file, together with the decodeFunc and package imports. The decodeFunc will wrap each string representation produced by the encodeFunc.

The encodeFunc should return a string enclosed in its delimiters (double quotes or backticks). The embedder implementation can thus choose if it wants to return a quoted string (with double quotes) or a raw string (with backticks).

The decodeFunc must be written in this form:

func(s string) (string, error) {
	// ...
}

The argument is the encoded string, and the returned string is the decoded data (matching the original asset data).

The imports array should match the compilation requirements of the decodeFunc.

func (*ConcurrentEmbedder) AssetEmbed

func (a *ConcurrentEmbedder) AssetEmbed(dst io.Writer, assets []*Asset, packageName, funcName string) (int, error)

AssetEmbed outputs a Go source file containing the assets. The source file will be in package packageName, and the function that returns the assets will be named funcName. This function will have the following signature:

func funcName() (map[string]string, error)

type SequentialEmbedder

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

A sequential embedder is an embedder that encodes assets one by one.

func NewSequentialEmbedder

func NewSequentialEmbedder(encodeFunc func(io.Reader) (string, error), decodeFunc string, imports []string) *SequentialEmbedder

NewSequentialEmbedder creates a new sequential embedder that encodes assets using encodeFunc. The encoded assets are written to the generated Go source file, together with the decodeFunc and package imports. The decodeFunc will wrap each string representation produced by the encodeFunc.

The encodeFunc should return a string enclosed in its delimiters (double quotes or backticks). The embedder implementation can thus choose if it wants to return a quoted string (with double quotes) or a raw string (with backticks).

The decodeFunc must be written in this form:

func(s string) (string, error) {
	// ...
}

The argument is the encoded string, and the returned string is the decoded data (matching the original asset data).

The imports array should match the compilation requirements of the decodeFunc.

func (*SequentialEmbedder) AssetEmbed

func (e *SequentialEmbedder) AssetEmbed(dst io.Writer, assets []*Asset, packageName, funcName string) (int, error)

AssetEmbed outputs a Go source file containing the assets. The source file will be in package packageName, and the function that returns the assets will be named funcName. This function will have the following signature:

func funcName() (map[string]string, error)

Directories

Path Synopsis
Package base64embedder implements an asset embedder that encodes assets as base64 strings.
Package base64embedder implements an asset embedder that encodes assets as base64 strings.
cmd
goembed
Goembed generates a file named "assets.generated.go" containing an encoded version of the contents of a directory.
Goembed generates a file named "assets.generated.go" containing an encoded version of the contents of a directory.
goembedtest
The goembedtest command tests that goembed-embedded assets match with the original source files that were embedded.
The goembedtest command tests that goembed-embedded assets match with the original source files that were embedded.
Package countingwriter implements an io.Writer that wraps another writer and keeps track of the number of bytes that are written by that wrapped writer.
Package countingwriter implements an io.Writer that wraps another writer and keeps track of the number of bytes that are written by that wrapped writer.
Package hexembedder implements an asset embedder that encodes assets as hexadecimal strings.
Package hexembedder implements an asset embedder that encodes assets as hexadecimal strings.
Package quoteembedder implements an asset embedder that encodes assets as quoted strings.
Package quoteembedder implements an asset embedder that encodes assets as quoted strings.
Package zbase64embedder implements an asset embedder that compresses assets using zlib, then encodes the resulting data as base64 strings.
Package zbase64embedder implements an asset embedder that compresses assets using zlib, then encodes the resulting data as base64 strings.
Package zhexembedder implements an asset embedder that compresses assets using zlib, then encodes the resulting data as hexadecimal strings.
Package zhexembedder implements an asset embedder that compresses assets using zlib, then encodes the resulting data as hexadecimal strings.

Jump to

Keyboard shortcuts

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