• Stars
    star
    162
  • Rank 232,214 (Top 5 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

General Golang Code Generator

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!

More Repositories

1

WebsitesForStudents

本库旨在建设一个对大学生有益有用的集合,欢迎参与。
Python
345
star
2

backon

Retry with backoff without effort.
Rust
336
star
3

reqsign

Signing HTTP requests without heavy SDKs.
Rust
93
star
4

go-locale

Cross platform locale detection for Golang
Go
90
star
5

remote-work-spectrum

Remote work spectrum
54
star
6

blog

All my thinking land down here.
JavaScript
43
star
7

beancollect

beancollect helps your collect beans so that you can count them.
Go
25
star
8

serfig

Layered configuration system built upon serde
Rust
24
star
9

keyboard-coming

我陈平安,唯有一键,可搬山,倒海,降妖,镇魔,敕神,摘星,断江,摧城,开天!
22
star
10

hdrs

HDFS Native Client in Rust via HDFS C API libhdfs
Rust
20
star
11

xuanzang

A fulltext search backend, especially for static website having sitemap.
Go
17
star
12

serde-bridge

A bridge between different serde implementations.
Rust
16
star
13

github-saved-replies

Building github saved replies
JavaScript
14
star
14

formwork

formwork is my favourite rust template
Rust
11
star
15

serde-go

Serialize and Deserialize Golang data structures efficiently and generically
Go
10
star
16

learn-data-lake-from-storage

Learn Data Lake From Storage Layer.
9
star
17

tiresias

A tool to help you access your servers.
Go
9
star
18

globiter

Turn glob range and set into Iterator
Rust
8
star
19

qingchat

Python
8
star
20

serde-env

env support for serde
Rust
7
star
21

hdfs-sys

Bindings to HDFS Native C API.
C
7
star
22

SublimeForWiz

听说你们都喜欢Sublime?
JavaScript
7
star
23

hdfs-rpc

Rust Native Client for HDFS RPC
Rust
6
star
24

Learning

Open Learning, Growing Together.
6
star
25

jade-ims

Jade inventory management system , 玉石进销存管理系统
HTML
6
star
26

auto-add-to-project

Add issues and PRs into project automatically!
Go
5
star
27

gitqus

disqus alternative via git
Go
4
star
28

go-mod-redirect

Go
4
star
29

when-i-find-rust-is-slow

It's crazy, I found a case where Rust is slower than Python! But, really?
Python
4
star
30

syringa

A bridge to lilac for archlinuxcn.
Rust
3
star
31

bard

A pastebin service
Go
3
star
32

streamlit-store

Python
3
star
33

onobot

Rust
3
star
34

hcl2-example

Go
2
star
35

CodeforcesGrabber

A Crabber for Codeforces Contest
C++
2
star
36

atomdns

Go
2
star
37

go-bufferpool

buffer pool for Golang.
Go
2
star
38

qi

qi(炁) is a RESTful client and server generator for human
Rust
1
star
39

navvy

Go
1
star
40

reqwest-hickory-resolver

The hickory-dns resolver for reqwest
Rust
1
star
41

tokio-tcpstream-with-busy-estab

Python
1
star
42

CLIWiki

A wiki for all command
CSS
1
star
43

serde-msgpack-go

serde-go + vmihailenco/msgpack = serde-msgpack-go
Go
1
star
44

Xuanwo

1
star
45

go-record-jar

Record jar (https://tools.ietf.org/html/draft-phillips-record-jar-01) support for the Go language.
Go
1
star
46

serde_header

Strongly typed HTTP Header library for Rust, built upon serde
Rust
1
star