genconstructor

package
v0.0.0-...-43fbcb5 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

genconstructor is constructor generator for Go.

```go

// genconstructor
type Foo struct {
    key string `required:"[constValue]"`
}

```

with `go generate` command

```go

//go:generate go-genconstructor

```

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(targetDir string, newWriter func(pkg *ast.Package) io.Writer, opts ...Option) error
Example
package main

import (
	"go/ast"
	"io"
	"log"
	"os"
	"strings"

	"github.com/aruga-inc/go-genconstructor/genconstructor"
)

func main() {
	targetDir := "../_example"
	if err := genconstructor.Run(
		targetDir,
		func(pkg *ast.Package) io.Writer {
			return os.Stdout
		},
		genconstructor.WithFileFilter(
			func(finfo os.FileInfo) bool {
				return !strings.HasSuffix(finfo.Name(), "_test.go")
			},
		),
	); err != nil {
		log.Fatal(err)
	}
}
Output:

// Code generated by go-genconstructor; DO NOT EDIT.

package example

import (
	"time"
)

func NewPerson(
	id string,
	name string,
) Person {
	return Person{
		id:        id,
		name:      name,
		createdAt: time.Now(),
	}
}

func NewPersonService(
	id string,
) *PersonService {
	return &PersonService{
		id: id,
	}
}

Types

type FieldInfo

type FieldInfo struct {
	Type       string
	Name       string
	ConstValue string
}

type Option

type Option func(o *option)

func WithFileFilter

func WithFileFilter(fileFilter func(finfo os.FileInfo) bool) Option

func WithGeneratorName

func WithGeneratorName(generatorName string) Option

Jump to

Keyboard shortcuts

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