gowrtr is a library that supports golang code generation
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
generator
internal Support `caused at` on composite literal generator Jan 21, 2019
.gitignore
LICENSE
Makefile
README.md
go.mod

README.md

gowrtr CircleCI codecov GoDoc Go Report Card

gowrtr (pronunciation:go writer) is a library that supports golang code generation.

This library is inspired by square/javapoet.

Synopsis

Here is a simple example:

package main

import (
	"fmt"

	"github.com/moznion/gowrtr/generator"
)

func main() {
	generator := generator.NewRoot(
		generator.NewComment(" THIS CODE WAS AUTO GENERATED"),
		generator.NewPackage("main"),
		generator.NewNewline(),
	).AddStatements(
		generator.NewFunc(
			nil,
			generator.NewFuncSignature("main"),
		).AddStatements(
			generator.NewRawStatement(`fmt.Println("hello, world!")`),
		),
	).
		Gofmt("-s").
		Goimports()

	generated, err := generator.Generate(0)
	if err != nil {
		panic(err)
	}
	fmt.Println(generated)
}

then it generates the golang code like so:

// THIS CODE WAS AUTO GENERATED
package main

import "fmt"

func main() {
        fmt.Println("hello, world!")
}

And GoDoc shows you a greater number of examples.

Description

Please refer to the godoc: GoDoc

Root

  • Root is an entry point to generate the go code.
  • Root supports following code formatting on code generating phase. It applies such formatters to generated code.
    • gofmt: with Gofmt(gofmtOptions ...string)
    • goimports: with Goimports()

Immutability

Methods of this library act as immutable. It means it doesn't change any internal state implicitly, so you can take a snapshot of the code generator. That is useful to reuse and derive the code generator instance.

Debug friendly

This library shows "where is a cause of the error" when code generator raises an error. This means each error message contains a pointer for the error source (i.e. file name and the line number). This should be helpful for debugging.

Error messages example:

[GOWRTR-14] condition of case must not be empty, but it gets empty (caused at /tmp/main.go:22)

Supported syntax

  • package
  • import
  • struct
  • interface
  • composite literal
  • if
    • else if
    • else
  • switch
    • case
    • default
  • for
  • code block
  • func
  • anonymous func
    • immediately invoking
  • one line statement
    • raw
    • newline
    • return
    • comment

For developers of this library

Setup development environment

$ make bootstrap

How to define and generate error messages

Please edit internal/errmsg/errmsg.go and execute make errgen.

See also: moznion/go-errgen

Blog posts

License

The MIT License (MIT)
Copyright © 2019 moznion, http://moznion.net/ <moznion@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.