enki

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: MIT Imports: 9 Imported by: 2

README

enki

MIT license Go Reference CircleCI codecov Go Report Card

enki is a package for easy go files generation

Example

package main

import (
	"os"
	"github.com/ricdeau/enki"
)

const (
	Int     = "int"
	Str     = "string"
	Float32 = "float32"
	Float64 = "float64"
)

func main() {
    f := enki.NewFile()
    f.Package("enki")
    f.GeneratedBy("enki")
    f.Import(".", "fmt")
    f.Import("", "sync")
    
    f.Consts(enki.Field(`DEF = "default"`))
    
    f.Vars(enki.Field(`wg sync.WaitGroup`))
    
    f.Line("// Number type redefined")
    
    f.Add(enki.T("Number").Is(Int))
    f.NewLine()
    
    f.Add(enki.T("NumStruct").Struct(
		enki.Field("a, b " + Int),
    ))
    f.NewLine()
    
    f.Add(enki.T("Num").Interface(
        enki.Def("AsNumber").Returns(Str),
        enki.Def("Sum").Params(Int).Returns(Int),
    ))
    f.NewLine()
    
    f.Add(enki.M("AsNumber").Receiver("n Number").Body(
        enki.Stmt().Line("wg.Add(1)"),
        enki.Stmt().Line("defer wg.Done()"),
        enki.Stmt().Line("return Sprint(n)"),
    ).Returns(Str))
    f.NewLine()
    
    f.Add(enki.M("Sum").Receiver("n Number").Params("x int").Body(
		enki.Stmt().Line("return int(n) + x"),
    ).Returns(Int))
    f.NewLine()
    
    f.Add(enki.M("Sum").Receiver("ns NumStruct").Body(
		enki.Stmt().Line("return ns.a + ns.b"),
    ).Returns(Int))
    f.NewLine()
    
    f.Add(enki.F("sum").Params("a, b " + Float32).Body(
		enki.Stmt().Line("return @1(a + b)", Float64),
    ).Returns("s " + Float64))
    
    _ = f.GoFmt(true).Write(os.Stdout)
}

Output:

// Code generated by enki. DO NOT EDIT.
package enki

import (
	. "fmt"
	"sync"
)

const (
	DEF = "default"
)

var (
	wg sync.WaitGroup
)

// Number type redefined
type Number int

type NumStruct struct {
	a, b int
}

type Num interface {
	AsNumber() string
	Sum(int) int
}

func (n Number) AsNumber() string {
	wg.Add(1)
	defer wg.Done()
	return Sprint(n)
}

func (n Number) Sum(x int) int {
	return int(n) + x
}

func (ns NumStruct) Sum() int {
	return ns.a + ns.b
}

func sum(a, b float32) (s float64) {
	return float64(a + b)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFile

func NewFile() *file

NewFile creates new file.

func Stmt

func Stmt() *statement

Stmt creates new statement builder.

func T

func T(name string) *typeBuilder

T creates new type builder

Types

type Block

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

Block builder that can be materialized

type File

type File interface {
	Statement
	// GoFmt - manage go-fmt before write file. By default, it is enabled.
	GoFmt(enabled bool) File
	// GoImports - formats and adjusts imports for the provided file. By default, it is disabled.
	GoImports(enabled bool) File
	// GeneratedBy - add special comment to identify generated file.
	GeneratedBy(tool string)
	// Package - add this file package name.
	Package(pkg string)
	// Import - add import with optional alias.
	Import(alias, path string)
	// Add - add a code block.
	Add(b Block)
	// Vars - add vars to `var` block.
	Vars(s ...Statement)
	// Consts - add consts to `const` block.
	Consts(s ...Statement)
	// Output - file as raw bytes.
	Output() ([]byte, error)
	// Write - write output or dest io.Writer.
	Write(dest io.Writer) error
	// Create - create new file or rewrite existing.
	Create(fileName string) error
}

File builder for files

type Function

type Function interface {
	Block
	// Name function name.
	Name(name string) Function
	// Params function params.
	Params(params ...string) Function
	// Returns function return values.
	Returns(results ...string) Function
	// Body function body.
	Body(body ...Statement) Function
}

Function builder for free functions

func Def

func Def(name string) Function

Def creates interface method definition.

func F

func F(name string) Function

F creates new function.

type Method

type Method interface {
	Function
	// Receiver method receiver.
	Receiver(def string) Method
}

Method builder for method

func M

func M(name string) Method

M creates new method.

type Statement

type Statement interface {
	fmt.Stringer
	Block
	// Print prints statement to inner buffer.
	Print(s string, args ...interface{}) Statement
	// Line add line with args.
	Line(s string, args ...interface{}) Statement
	NewLine() Statement
}

Statement builder, that can build any statement.

func Field

func Field(line string, args ...interface{}) Statement

type Type

type Type interface {
	Block
	// Name type name.
	Name(name string) Type
	// Is type redeclaration.
	Is(anotherType string) Type
	// Struct type as struct.
	Struct(fields ...Statement) Type
	// Interface type as interface.
	Interface(methods ...Function) Type
}

Type builder for types and interfaces

Jump to

Keyboard shortcuts

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