genial

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 6 Imported by: 0

README

go-genial

Go Reference

A golang code-generation library

Install

go get github.com/karitham/go-genial

Example

	p := &genial.PackageB{}
	p.Comment("example is an example package").
		Name("example").
		Imports("encoding/json")

	t := &genial.StructB{}
	t.Comment("Baz is a implementation of Iface").
		Name("Baz").
		Field("Foo", "*string", genial.StructTag{Type: "json", Value: "foo,omitempty"}).
		Field("rest", "json.Raw")

	f := &genial.FuncB{}
	f.Comment("FooBar is a new example function").
		Name("FooBar").
		Receiver("b", "*Baz").
		Parameter("foo", "int").
		Parameter("bar", "string").
		ReturnTypes("int", "error").
		WriteString(`panic("not implemented")`)

	i := &genial.InterfaceB{}
	i.Comment("Iface is an example interface").
		Members(f).
		Name("Iface")

	p.Declarations(t, i, f).WriteTo(os.Stdout)

generates

// example is an example package
package example

import "encoding/json"

// Baz is a implementation of Iface
type Baz struct {
	Foo  *string `json:"foo,omitempty"`
	rest json.Raw
}

// Iface is an example interface
type Iface interface {
	// FooBar is a new example function
	FooBar(foo int, bar string) (int, error)
}

// FooBar is a new example function
func (b *Baz) FooBar(foo int, bar string) (int, error) {
	panic("not implemented")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Byter added in v0.4.0

type Byter interface {
	Bytes() []byte
}

type Field

type Field struct {
	Name    string
	Type    string
	Comment string
	Tag     []StructTag
}

Field is a struct field

type FuncB

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

FuncB is a function builder

func (*FuncB) Bytes added in v0.4.0

func (f *FuncB) Bytes() []byte

Bytes returns the bytes of the function

func (*FuncB) Comment

func (f *FuncB) Comment(c string) *FuncB

Comment sets the comment of the function

func (*FuncB) Commentf added in v0.3.0

func (f *FuncB) Commentf(format string, a ...interface{}) *FuncB

Commentf sets the comment using fmt.Sprintf

func (*FuncB) Description

func (f *FuncB) Description() []byte

Description returns the description of the function, (as in, the top level comment)

func (*FuncB) Name

func (f *FuncB) Name(n string) *FuncB

Name sets the name of the function

func (*FuncB) Namef added in v0.3.0

func (f *FuncB) Namef(format string, a ...interface{}) *FuncB

Namef sets the name of the function using fmt.Sprintf

func (*FuncB) Parameter added in v0.3.1

func (f *FuncB) Parameter(name string, typ string) *FuncB

Parameter adds a single parameter to the function

func (*FuncB) Parameters

func (f *FuncB) Parameters(p ...Parameter) *FuncB

Parameters sets the parameters of the function

func (*FuncB) Receiver

func (f *FuncB) Receiver(name string, typ string) *FuncB

Receiver sets the receiver of the function

func (*FuncB) ReturnTypes

func (f *FuncB) ReturnTypes(p ...string) *FuncB

ReturnTypes sets the return types of the function

func (*FuncB) Signature

func (f *FuncB) Signature() []byte

Signature returns the signature of the function Useful for the interface builder

func (*FuncB) String

func (f *FuncB) String() string

String returns a string representation of the function

func (*FuncB) Write

func (f *FuncB) Write(b []byte) (n int, err error)

Write directly into the body of the function

func (*FuncB) WriteString added in v0.3.0

func (f *FuncB) WriteString(s string) (n int, err error)

WriteString writes a string into the body of the function

func (*FuncB) Writef added in v0.3.0

func (f *FuncB) Writef(format string, a ...interface{}) (n int, err error)

Writef writes directly to the body of the function using fmt.Sprintf

type InterfaceB

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

InterfaceB is an interface builder

func (*InterfaceB) Bytes added in v0.4.0

func (i *InterfaceB) Bytes() []byte

Bytes returns a byte representation of the iface

func (*InterfaceB) Comment

func (i *InterfaceB) Comment(comment string) *InterfaceB

Comment sets the comment on the interface

func (*InterfaceB) Commentf added in v0.3.0

func (i *InterfaceB) Commentf(format string, args ...interface{}) *InterfaceB

Commentf sets the comment on the interface using fmt.Sprintf

func (*InterfaceB) Members

func (i *InterfaceB) Members(s ...Signaturer) *InterfaceB

Members adds members to the interface

func (*InterfaceB) Name

func (i *InterfaceB) Name(n string) *InterfaceB

Name sets the name of the interface

func (*InterfaceB) Namef added in v0.3.0

func (i *InterfaceB) Namef(format string, args ...interface{}) *InterfaceB

Namef sets the name of the interface using fmt.Sprintf

func (*InterfaceB) String

func (i *InterfaceB) String() string

String returns a string representation of the iface

type PackageB

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

PackageB is a package builder

Do not edit once `Bytes`, `String`, `WriteTo`, `Write` or `Read` are called.

func (*PackageB) Bytes added in v0.4.0

func (p *PackageB) Bytes() []byte

Bytes returns the bytes representation of the package.

func (*PackageB) Comment

func (p *PackageB) Comment(c string) *PackageB

Comment sets the comment for the package

func (*PackageB) Declarations

func (p *PackageB) Declarations(b ...Byter) *PackageB

Declarations sets the declarations for the package

func (*PackageB) Imports

func (p *PackageB) Imports(i ...string) *PackageB

Imports appends to imports

func (*PackageB) License

func (p *PackageB) License(s string) *PackageB

License sets the license header for the generated code

func (*PackageB) Name

func (p *PackageB) Name(n string) *PackageB

Name sets the name of the package

func (*PackageB) Namef added in v0.3.0

func (p *PackageB) Namef(format string, args ...interface{}) *PackageB

Namef sets the name of the package using fmt.Sprintf

func (*PackageB) Read added in v0.4.0

func (p *PackageB) Read(b []byte) (int, error)

Read the package source

func (*PackageB) String

func (p *PackageB) String() string

String returns the string representation of the package.

func (*PackageB) WriteTo added in v0.4.0

func (p *PackageB) WriteTo(w io.Writer) (int64, error)

WriteTo writes the package to the given writer.

type Parameter

type Parameter struct {
	Name     string
	Type     string
	Variadic bool
}

Parameter is a go function parameter

type Signaturer

type Signaturer interface {
	Description() []byte
	Signature() []byte
}

Signaturer is implemented by functions, which enables us to pass them to the interface builder

type StructB

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

StructB is a struct builder

func (*StructB) Bytes added in v0.4.0

func (s *StructB) Bytes() []byte

Bytes returns the byte representation of the struct

func (*StructB) Comment

func (s *StructB) Comment(comment string) *StructB

Comment sets the comment

func (*StructB) Commentf added in v0.3.0

func (s *StructB) Commentf(format string, args ...interface{}) *StructB

Commentf sets the comment using fmt.Sprintf

func (*StructB) Field added in v0.3.0

func (s *StructB) Field(name string, typeName string, tags ...StructTag) *StructB

Field appends a basic field

func (*StructB) Fields

func (s *StructB) Fields(fields ...Field) *StructB

Fields sets the fields

func (*StructB) Name

func (s *StructB) Name(name string) *StructB

Name sets the name

func (*StructB) Namef added in v0.3.0

func (s *StructB) Namef(format string, args ...interface{}) *StructB

Namef sets the name using fmt.Sprintf

func (*StructB) String

func (s *StructB) String() string

String returns the string representation of the struct

type StructTag

type StructTag struct {
	Type  string // json:
	Value string // "value"
}

StructTag is a struct tag

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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