djent

package module
v0.0.0-...-3f0b436 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2019 License: MIT Imports: 21 Imported by: 0

README

Djent Go Report Card Build Status

Code generation helper/wrapper for https://github.com/dave/jennifer

Easily generate golang code.

Installation

go get github.com/iamgoroot/djent

Basics

Types: List of types. Use when you see def.Types parameter (interface def, returns etc.)

       		T("")
		T(reflect.TypeOf(""))
		T(Q("","string"))
		T("", 1, Q("pkg", "Name")) // (string, int, pkg.Name)

See .test/func_test.go

Variables: Pair (name, type) use when you see def.Var parameter

        	V("Name", "") 
		V("Lastname", reflect.TypeOf("")) 
		V("Info", Q("", "string"))      //Q - qualifier pair (pkg,name)

See .test/struct_test.go

Usage example

See test package for more examples

	j := New("test")
	//create struct
	j.Struct("User", V("Name", ""))


	//implement each method of interface by embedding functionToEnbed body and code generated by jen
	bodyGen := func(method reflect.Type) *jen.Statement {
		return j.Embeddable(functionToEmbed).Line().Return(jen.Lit(0), jen.Nil())
	}
	//make writer imlementation with embedded body
	j.ImplInterface(reflect.TypeOf((*io.Writer)(nil)), "NewWriter", bodyGen)

	//create interface code
	j.Interface(
		"NewInterfaceName",
		MethodDef(
			"FirstMethodName",
			T("", "", 3),
			T("", reflect.StructField{}),
		),
		MethodDef("SecondMethodName", nil, nil),
	)
	//save
	j.Save("filename.go")

This results in code like this:

	package test

        import (
        	"fmt"
        	"reflect"
        )

        type User struct {
        	Name string
        }

        type NewWriter struct {
        }

        //  fmt.fmt
        func (r NewWriter) Write(param0 []uint8) (int, error) {

        	fmt.Println(p)
        	return 0, nil
        }

        type NewInterfaceName interface {
        	FirstMethodName(string, string, int) (string, reflect.StructField)
        	SecondMethodName()
        }

Contributing

Feel free to contribure with code or feedback

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JenFunc

func JenFunc(name string, in def.Vars, out def.Types, body *jen.Statement) *jen.Statement

Returns function code as *Statement Does not support named returns

func MethodDef

func MethodDef(name string, in def.Types, out def.Types) def.Method

Create function definition use when defining interface of a function

func Q

func Q(pkg string, name string) def.Q

Q is a pair Q(name, pkg) where it represents T.

func RemoveSelfImport

func RemoveSelfImport(file string)

func StmtChan

func StmtChan() stmtChan

Make sure size is enough. managing size is up to you

func StmtChanSized

func StmtChanSized(size int) stmtChan

func T

func T(prototypes ...interface{}) def.Types

T Makes List of Types for given prototypes, Q (Qualifier) or reflect.Type Think of interface method parameters and return parameters Use where method has def.Types parameter

T("", (*error)(nil)) 		// (string, error)
T(Address{}, (*error)(nil)) // (Address, error)
T(false) 					// (bool)

T(reflect.TypeOf("stringval")) // (string)
T(reflect.TypeOf(reflect.TypeOf("stringval")))  // (reflect.Type)

T(Q("reflect","Type"))							// (reflect.Type)

TODO: make warning when something like T("reflect","Type") gets called

func V

func V(name string, prototype interface{}) def.Var

V is anything that has name and type. prototype can be value of some type, reflect.T value or pair Q(name, pkg)

V("name","") // name string
V("age", int8(0)) // age int8
V("location", Location{}) //location Location
V("c", Q("context","Context") // pair Q(name, pkg) when you don't have type in runtime.
V("reflected", reflect.TypeOf(Location{})) // When you have reflection of type
V("location", Location{}).Ptr() //Now it's a pointer

func Vars

func Vars(vars ...def.Var) def.Vars

Types

type Djent

type Djent struct {
	File *jen.File
}

func NewDjent

func NewDjent(packagepath string) Djent

func NewOf

func NewOf(jen *jen.File) Djent

func (Djent) Bytes

func (j Djent) Bytes() []byte

Get source but exit on fail

func (Djent) Embeddable

func (j Djent) Embeddable(function interface{}) *jen.Statement

Embeddable use to embed body of the method to generated code. tries it's best to import stuff. !!!!! Don't use unnamed functions (for now) !!!!!

func (Djent) ExternalGenerator

func (j Djent) ExternalGenerator(dir string, injects def.Vars, body *jen.Statement, params ...string)

ExternalGenerator runs generated generation code in separate process. TODO: find better way See: TestExternalGenerator as example of usage

func (Djent) ImplFunc

func (j Djent) ImplFunc(name string, in def.Vars, out def.Types, body *jen.Statement)

ImplFunc Implements function

func (Djent) ImplFuncOf

func (j Djent) ImplFuncOf(name string, function interface{}, body *jen.Statement)

ImplFuncOf implements function with given name, signature type or prototype

f := func() string {}
j.ImplFuncOf("returnHello", f, jen.Return().Lit("Hello!"))

func (Djent) ImplInterface

func (j Djent) ImplInterface(iface reflect.Type, name string, bodyGenerator func(method reflect.Method) *jen.Statement)

Implements given interface

Usage:
j.ImplInterface(reflect.TypeOf((*Writer)(nil)), "emptyWriter", jen.Empty())

func (Djent) ImplMethod

func (j Djent) ImplMethod(receiver def.Var, name string, in def.Vars, out def.Types, body *jen.Statement)

ImplMethod Implements method

func (Djent) ImplTest

func (j Djent) ImplTest(name string, body *jen.Statement)

func (Djent) Interface

func (j Djent) Interface(name string, methods ...def.Method)

Interface creates interface

func (Djent) Iota

func (j Djent) Iota(name string, values []string)

Create Iota

d.Iota(Direction, []string{"North","East","South","West"})

const (

North Direction = iota
East
South
West

)

func (Djent) Jen

func (j Djent) Jen(code ...jen.Code) *jen.Statement

Jen Bridge to jennifer

func (Djent) PostGen

func (j Djent) PostGen(dir string, qualifier def.Q, postGenHook PostGenHook, params ...string)

PostGen Executes in separate process

func (Djent) Render

func (j Djent) Render(writer io.Writer) error

func (Djent) Save

func (j Djent) Save(file string) error

func (Djent) Struct

func (j Djent) Struct(name string, fields ...def.Var)

Struct makes struct with fields

type PostGenHook

type PostGenHook interface {
	PostGenHook(valType reflect.Type, params ...string)
}

PostGenHook receives new type that was generated has metadata needed for further generation. also string parameters

	var val *grpc.TestServiceServer
	valType := reflect.TypeOf(val).Elem()
	params := []string{
	"param1",
}

type StatementChanOpts

type StatementChanOpts uint
const (
	IndentLine StatementChanOpts = iota
	IndentComma
	IndentSpace
)

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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