go-genial

Go Reference

A prototype code-generator library for golang.

Install

go get github.com/karitham/go-genial

Example

	t := &genial.StructBuilder{}
	t.Comment("Baz is a implementation of Iface").
		Name("Baz").
		Fields(genial.Field{
			Name: "Foo",
			Type: "*string",
			Tag: []genial.StructTag{
				{Type: "json", Value: "foo", Omitempty: true},
			},
		},
		)

	f := &genial.FuncBuilder{}
	f.Comment("FooBar is a new example function").
		Name("FooBar").
		Receiver(genial.Parameter{
			Name: "b",
			Type: "*Baz",
		}).
		Parameters(
			genial.Parameter{
				Name: "foo",
				Type: "int",
			}, genial.Parameter{
				Name: "bar",
				Type: "string",
			},
		).
		ReturnTypes(
			genial.Parameter{
				Type: "int",
			},
			genial.Parameter{
				Type: "error",
			},
		).
		Write([]byte("\tpanic(\"not implemented\")\n"))

	i := &genial.IfaceBuilder{}
	i.Comment("Iface is an example interface").
		Functions(f).Name("Iface")

	p := &genial.PackageBuilder{}

	p.Comment("example is an example package").
		Blocks(t, i, f).
		Name("example")

	fmt.Println(p.String())

generates

// example is an example package
package example

// Baz is a implementation of Iface
type Baz struct {
	Foo *string `json:"foo,omitempty"`
}

// Iface is an example interface
type Iface interface {
	// FooBar is a new example function
	FooBar(foo int, bar string) (int, error)
}

// FooBar is a new example function
func (b *Baz) FooBar(foo int, bar string) (int, error) {
	panic("not implemented")
}

GitHub

View Github