go_constructor

package
v0.0.0-...-9c8aaae Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Copyright © 2019 hori-ryota <hori.ryota@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Example
package main

import (
	cmd "github.com/hori-ryota/go-codegen/codegen"
	"github.com/mattn/go-shellwords"
	"github.com/spf13/cobra"
)

func main() {
	input := "go_constructor -t internal/testdata"

	cmd := prepareCmdWithStdout(input)
	err := cmd.Execute()
	if err != nil {
		panic(err)
	}
}

func prepareCmdWithStdout(input string) *cobra.Command {
	input += " --useStdOut"
	args, err := shellwords.Parse(input)
	if err != nil {
		panic(err)
	}
	cmd := cmd.NewRootCmd()
	cmd.SetArgs(args)
	return cmd
}
Output:

// Code generated by go-codegen go_constructor; 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,
	}
}

func NewArmedWarrior(
	person Person,
	armament string,
) ArmedWarrior {
	return ArmedWarrior{
		Person:   person,
		armament: armament,
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(pkgInfo *loader.PackageInfo) (string, error)

func NewGoConstructorCmd

func NewGoConstructorCmd() *cobra.Command

func Run

func Run(targetDir string, useStdOut bool) error

Types

type Field

type Field struct {
	Type       types.Type
	Name       string
	ConstValue string
}

Jump to

Keyboard shortcuts

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