gg

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: Apache-2.0 Imports: 6 Imported by: 5

README

Build Status Go dev License matrix

gg

gg is a General Golang Code Generator: A Good Game to play with Golang.

package main

import (
	"fmt"

	. "github.com/Xuanwo/gg"
)

func main() {
	f := NewGroup()
	f.AddPackage("main")
	f.NewImport().
		AddPath("fmt")
	f.NewFunction("main").AddBody(
		String(`fmt.Println("%s")`, "Hello, World!"),
	)
	fmt.Println(f.String())
}

Output (after go fmt)

package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}

Design

gg is a general golang code generator that designed for resolving problems exists in the following tools:

  • text/template: Additional syntax, Steep learning curve, Complex logic is difficult to maintain
  • dave/jennifer: Overly abstract APIs, user need to take care about (), , everywhere.
  • kubernetes-sigs/kubebuilder: Parse data from struct tags/comments, not a general code generator.

In short, gg will provide near-native golang syntax and helpful API so play a good game with Golang. With gg, we can generate golang code more easily and understandable.

Usage

Package Name
f := Group()
f.AddPackage("main")
// package main
Imports
f := Group()
f.NewImport().
    AddPath("context").
	AddDot("math").
	AddBlank("time").
	AddAlias("x", "testing")
// import (
//      "context"
//      . "math"
//      _ "time"
//      x "testing"
// )
Function
f := Group()
f.NewFunction("hello").
    WithReceiver("v", "*World").
    AddParameter("content", "string").
    AddParameter("times", "int").
    AddResult("v", "string").
    AddBody(gg.String(`return fmt.Sprintf("say %s in %d times", content, times)`))
// func (v *World) hello(content string, times int) (v string) {
//  return fmt.Sprintf("say %s in %d times", content, times)
//}
Struct
f := Group()
f.NewStruct("World").
    AddField("x", "int64").
    AddField("y", "string")
// type World struct {
//    x int64
//    y string
//}

Acknowledgement

  • gg is inspired by dave/jennifer, I borrowed most ideas and some code from it. Nice work!

Documentation

Index

Examples

Constants

This section is empty.

Variables

S is an alias to String. TODO: can we find a new name for this?

Functions

func Call added in v0.1.0

func Call(name string) *icall

Call is used to generate a function call.

func Const

func Const() *iconst

func For

func For(judge interface{}) *ifor

func Function

func Function(name string) *ifunction

Function represent both method and function in Go.

NOTES

If `WithReceiver`, we will generate a method:

func (t test) Test()

If `WithCall`, we will generate a function call:

func Test(){}()

If `AddBody`, we will generate like a function definition without body:

func Test() {
    println("Hello, World!")
}

func If

func If(judge interface{}) *iif

func Import added in v0.1.0

func Import() *iimport

Import will start a new import group.

func Interface added in v0.1.0

func Interface(name string) *iinterface

func LineComment added in v0.0.2

func LineComment(content string, args ...interface{}) *istring

func Lit

func Lit(value interface{}) *lit

func NewGroup added in v0.1.0

func NewGroup() *group
Example
f := NewGroup()
f.AddPackage("main")
f.NewImport().AddPath("fmt")
f.NewFunction("main").AddBody(
	String(`fmt.Println("%s")`, "Hello, World!"),
)
fmt.Println(f.String())
Output:

package main

import "fmt"
func main(){
fmt.Println("Hello, World!")}

func Package

func Package(name string) *ipackage

func Return

func Return(node ...interface{}) *ireturn

func String

func String(format string, args ...interface{}) *istring

String will add a format string in NewGroup, just like fmt.Printf.

func Struct

func Struct(name string) *istruct

Struct will insert a new struct.

func Switch

func Switch(judge interface{}) *iswitch

func Type added in v0.2.0

func Type(name string, typ interface{}) *itype

func TypeAlias added in v0.2.0

func TypeAlias(name string, typ interface{}) *itype

func Value

func Value(typ string) *ivalue

func Var

func Var() *ivar

Types

type Generator added in v0.3.0

type Generator struct {
	// contains filtered or unexported fields
}

func New added in v0.3.0

func New() *Generator

New will create a new generator which hold the group reference.

func (*Generator) AppendFile added in v0.3.0

func (g *Generator) AppendFile(path string) error

AppendFile will append the group after the give path.

func (*Generator) NewGroup added in v0.3.0

func (g *Generator) NewGroup() (ng *group)

func (*Generator) Write added in v0.3.0

func (g *Generator) Write(w io.Writer)

Write will write the group into the given writer.

func (*Generator) WriteFile added in v0.3.0

func (g *Generator) WriteFile(path string) error

WriteFile will write the group into the given path.

type Node

type Node interface {
	// contains filtered or unexported methods
}

func Continue

func Continue() Node

func Defer added in v0.1.0

func Defer(body interface{}) Node

func Embed

func Embed(fn func() Node) Node

Embed accept a close clause to build a node.

func Line

func Line() Node

func Template

func Template(data interface{}, tmpl string) Node

Jump to

Keyboard shortcuts

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