foptgen

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

foptgen

foptgen is a binary tool that generates functional options for Go structs.

Install

go install github.com/200sc/foptgen/main/foptgen

Usage

$ foptgen --dir=internal/components/titlebar --struct=Constructor --overwrite=false
Output target internal/components/titlebar/opts_gen.go already exists; overwrite? (y/N)
y

Input struct:

type Constructor struct {
	Color        color.Color
	Height       float64
	Layers       []int
	Fire         bool
	StringArrays [][][]string
}

Output (to file internal\components\titlebar\opts_gen.go):

// Code generated by foptgen; DO NOT EDIT.

package titlebar

import "image/color"

type Option func(Constructor) Constructor

func WithColor(v color.Color) Option {
	return func(s Constructor) Constructor {
		s.Color = v
		return s
	}
}

func WithHeight(v float64) Option {
	return func(s Constructor) Constructor {
		s.Height = v
		return s
	}
}

func WithLayers(v []int) Option {
	return func(s Constructor) Constructor {
		s.Layers = v
		return s
	}
}

func WithFire(v bool) Option {
	return func(s Constructor) Constructor {
		s.Fire = v
		return s
	}
}

func WithStringArrays(v [][][]string) Option {
	return func(s Constructor) Constructor {
		s.StringArrays = v
		return s
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStructNotFoundInFile = fmt.Errorf("struct not found in file")
View Source
var TemplateFile string

Functions

func CheckIfOutputExists

func CheckIfOutputExists(filename string) error

CheckIfOutputExists will prompt to overwrite a file if it already exists. If not confirmed, it will error.

func FindStructInDirectory

func FindStructInDirectory(directory, structName string) (*ast.StructType, *token.FileSet, string, error)

FindStructInDirectory reads a directory, without recursing to subdirectories. It searches all Go files within the directory for a struct named 'structName'. It will return that struct's definition, the token file set used to parse that file, and the name of the package that struct was found in, or an error.

func FindStructInFile

func FindStructInFile(file, structName string) (structDef *ast.StructType, fset *token.FileSet, pkg string, err error)

FindStructInFile searches a fully qualified file path for a struct named 'structName'. If found, it will return that struct definition, the token file set used to parse the file, and the package name of the file. Otherwise, if not found, it will return ErrStructNotFoundInFile.

func WriteTemplate

func WriteTemplate(w io.Writer, tplInput *TemplateInput) error

Types

type TemplateInput

type TemplateInput struct {
	PackageName    string
	Options        []TemplateOption
	StructName     string
	OptionTypeName string
}

func NewTemplateInput

func NewTemplateInput(structDef *ast.StructType, fset *token.FileSet, structName, pkg, optionTypeName string) *TemplateInput

type TemplateOption

type TemplateOption struct {
	// struct fields can have multiple names but we're ignoring that for now
	FieldName string
	FieldType string
}

Directories

Path Synopsis
main

Jump to

Keyboard shortcuts

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