generator_helpers

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 13 Imported by: 0

README

generator_helpers

Go Reference

Overview

generator_helpers is a Go package designed to provide utility functions for common operations such as template generation, YAML processing, and error handling in Go applications.

Installation

To use generator_helpers in your Go project, you need to install it as a dependency:

go get github.com/point-c/generator_helpers

Usage

Below are some examples of how to use the generator_helpers package:

Unmarshalling YAML Data

To unmarshal YAML data from a file:

var config MyConfig
err := generator_helpers.UnmarshalYAML("config.yaml", &config)
if err != nil {
    // Handle error
}
Template Generation

Generating text or HTML from templates:

tmpl, err := generator_helpers.NewTemplate[*text_template.Template](templateFS, funcs)
if err != nil {
    // Handle error
}

var data MyData
generator_helpers.Generate(tmpl, data, "templateName", "output.txt")
Error Handling

Simplifying error handling with Must and Check:

getValue := func() (int, error) { return 1, errors.New("error") }
value := generator_helpers.Must(getValue())
generator_helpers.Check(err)
Go Code Formatting

The generator_helpers package offers functionalities to format Go source code. Below is an example of how to use these functions:

Formatting Go Source Code

To format a Go source code byte slice:

sourceCode := []byte("package main\nimport \"fmt\"\nfunc main() {fmt.Println(\"hello world\")}")
formattedCode, err := generator_helpers.GoFmt(sourceCode)
if err != nil {
    // Handle formatting error
}
// Use formattedCode...
Formatting Source Code from an io.Reader

If you have Go source code in an io.Reader, you can format it as follows:

var reader io.Reader = ... // your io.Reader with Go source code
formattedCode, err := generator_helpers.GoFmtReader(reader)
if err != nil {
    // Handle formatting error
}
// Use formattedCode...

Testing

The package includes tests that demonstrate its functionality. Use Go's testing tools to run the tests:

go test

Godocs

To regenerate godocs:

go generate -tags docs ./...

Documentation

Index

Constants

View Source
const (
	EnvKeyGoArch    = "GOARCH"
	EnvKeyGoOS      = "GOOS"
	EnvKeyGoFile    = "GOFILE"
	EnvKeyGoLine    = "GOLINE"
	EnvKeyGoPackage = "GOPACKAGE"
	EnvKeyGoRoot    = "GOROOT"
	EnvKeyDollar    = "DOLLAR"
	EnvKeyPath      = "PATH"
)

Variables

View Source
var (
	EnvDefaultGoPackage = "generator_helpers"
	EnvDefaultGoFile    = ""
	EnvDefaultGoArch    = runtime.GOARCH
	EnvDefaultGoOS      = runtime.GOOS
	EnvDefaultGoLine    = 0
	EnvDefaultDollar    = "$"
	EnvDefaultPath      = os.Getenv(EnvKeyPath)
	EnvDefaultGoRoot    = os.Getenv(EnvKeyGoRoot)
)

Functions

func Check

func Check(err error)

Check takes an error as an argument. If the error is not nil, it logs the error and exits the program.

func EnvDollar

func EnvDollar() string

EnvDollar returns the dollar sign from environment or default.

func EnvGoArch

func EnvGoArch() string

EnvGoArch returns the Go architecture from environment or default.

func EnvGoFile

func EnvGoFile() string

EnvGoFile returns the Go file name from environment or default.

func EnvGoLine

func EnvGoLine() int

EnvGoLine returns the Go file line number from environment or default.

func EnvGoOS

func EnvGoOS() string

EnvGoOS returns the Go operating system from environment or default.

func EnvGoPackage

func EnvGoPackage() string

EnvGoPackage returns the Go package name from environment or default.

func EnvGoRoot

func EnvGoRoot() string

EnvGoRoot returns the Go file root path from environment or default.

func EnvOrDefault

func EnvOrDefault(key, def string) string

EnvOrDefault returns the environment variable's value or default if empty.

func EnvOrDefaultInt

func EnvOrDefaultInt(key string, def int) int

EnvOrDefaultInt returns the environment variable's integer value or default if empty. If there is an error parsing the environment variable. The default will be returned.

func EnvPath

func EnvPath() string

EnvPath returns the system path from environment or default.

func Exec

func Exec[T Template, D any](tmpl T, d D, startTemplate string) ([]byte, error)

Exec executes a template with the provided data and returns the result as a byte slice.

func ExecTemplate

func ExecTemplate[T Template, D any](tmpl T, dot D, name string) []byte

ExecTemplate executes a template and returns the generated content as a byte slice. It assumes the output is go source code and formats the output.

func Generate

func Generate[T Template, D any](tmpl T, dot D, name, out string)

Generate creates and writes a file using a template and data.

func GoFmt

func GoFmt(src []byte) (dst []byte, err error)

GoFmt formats Go source code, returning the formatted code or the original and an error.

func GoFmtReader

func GoFmtReader(r io.Reader) ([]byte, error)

GoFmtReader functions similarly to GoFmt but accepts an io.Reader as input. It reads the source code from the reader, formats it, and returns the formatted code and any error encountered.

func IfStringEmptyUseDefault

func IfStringEmptyUseDefault(s, def string) string

IfStringEmptyUseDefault returns the default value if the string is empty.

func Must

func Must[T any](t T, err error) T

Must takes a value and an error. If the error is not nil, it logs the error and exits the program. Otherwise, it returns the value.

func NewTemplate

func NewTemplate[T Template](templates fs.FS, funcs map[string]any) T

NewTemplate initializes a new template, either text or HTML, with provided functions and file system.

func OutputFilename

func OutputFilename(original string) string

OutputFilename generates a filename for the output file by appending "_generated" to the original file name.

func TestOutputFilename

func TestOutputFilename(original string) string

TestOutputFilename generates a filename for the test output file by appending "_test" to the original file name.

func UnmarshalYAML

func UnmarshalYAML[T any](filename string) (v T, err error)

UnmarshalYAML reads YAML data from a file and unmarshals it into a value. It returns the unmarshaled value and any error encountered.

Types

type Template

type Template interface {
	ExecuteTemplate(wr io.Writer, name string, data any) error
}

Template interface abstracts the common functionality for text and HTML templates.

Jump to

Keyboard shortcuts

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