goembed

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2022 License: BSD-3-Clause Imports: 14 Imported by: 2

README

goembed

goembed is Golang go:embed parse package

Go1.16 Go1.17 Go1.18 Go1.19

demo
package main

import (
	"fmt"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"path/filepath"

	"github.com/visualfc/goembed"
)

func main() {
	pkg, err := build.Import("github.com/visualfc/goembed", "", 0)
	if err != nil {
		panic(err)
	}
	fset := token.NewFileSet()
	var files []*ast.File
	for _, file := range pkg.TestGoFiles {
		f, err := parser.ParseFile(fset, filepath.Join(pkg.Dir, file), nil, 0)
		if err != nil {
			panic(err)
		}
		files = append(files, f)
	}
	ems,err := goembed.CheckEmbed(pkg.TestEmbedPatternPos, fset, files)
	if err != nil {
		panic(err)
	}
	r := goembed.NewResolve()
	for _, em := range ems {
		files, err := r.Load(pkg.Dir, fset, em)
		if err != nil {
			panic(err)
		}
		for _, f := range files {
			fmt.Println(f.Name, f.Data, f.Hash)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToHex

func BytesToHex(data []byte) string

func BytesToList

func BytesToList(data []byte) string

func WriteToHex

func WriteToHex(data []byte, w io.Writer) (n int, err error)

Types

type Embed

type Embed struct {
	Name     string
	Kind     Kind
	Patterns []string
	Pos      token.Position
	Spec     *ast.ValueSpec
}

Embed describes go:embed variable

func CheckEmbed

func CheckEmbed(embedPatternPos map[string][]token.Position, fset *token.FileSet, files []*ast.File) ([]*Embed, error)

CheckEmbed lookup go:embed vars for embedPatternPos

type File

type File struct {
	Name string
	Data []byte
	Hash [16]byte // truncated SHA256 hash
}

File is embed data info

func BuildFS

func BuildFS(files []*File) []*File

BuildFS is build files to new files list with directory

type Kind added in v0.3.2

type Kind int

Kind is embed var type kind

const (
	EmbedUnknown Kind = iota
	EmbedBytes
	EmbedString
	EmbedFiles
	EmbedMaybeAlias // may be alias string or []byte
)

type Resolve

type Resolve interface {
	Load(dir string, fset *token.FileSet, em *Embed) ([]*File, error)
	Files() []*File
}

Resolve is load embed data interface

func NewResolve

func NewResolve() Resolve

NewResolve create load embed data interface

Directories

Path Synopsis
Package fsys is an abstraction for reading files that allows for virtual overlays on top of the files on disk.
Package fsys is an abstraction for reading files that allows for virtual overlays on top of the files on disk.

Jump to

Keyboard shortcuts

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