code

package module
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: MIT Imports: 3 Imported by: 1

README

Code Go Report Card Coverage Status Build Status

Code is a small wrapper around jen that allows go-services to generate go code easier.

It has a very friendly api

package main

import (
    "github.com/go-services/code"
    "fmt"
)

func main() {
    structure := code.NewStruct("MyStruct")
    
    fmt.Println(structure) // 	type MyStruct struct{}

    structure.Fields = []code.StructField{
        code.NewStructField("Name", code.NewType("string")),
    }
    fmt.Println(structure)  // 	type MyStruct struct {
                            //  	Name string
                            //  }

}

Documentation

Overview

Package code is a small wrapper for https://github.com/dave/jennifer this package is used to easily generate go code it also has a very straight forward api.

e.x to create a structure:

structure := code.NewStruct("MyStruct")

fmt.Println(structure) // 	type MyStruct struct{}

to add fields to the structure

structure.Fields = []code.StructField{
    code.NewStructField("Name", code.NewType("string")),
}
fmt.Println(structure)  // 	type MyStruct struct {
                        //  		Name string
                        //  	}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Code

type Code interface {
	// Docs returns the documentation comments
	Docs() []Comment

	// String returns the string representation of the code.
	String() string

	// Code returns the jen representation of the code.
	Code() *jen.Statement

	// AddDocs adds documentation comments to the code.
	AddDocs(docs ...Comment)

	ImportAliases() []ImportAlias
}

Code is the interface that all code nodes need to implement.

type Comment

type Comment string

Comment represents a code comment, it implements the Code interface.

func NewComment

func NewComment(s string) Comment

NewComment creates a Comment with the given string.

func (Comment) AddDocs

func (c Comment) AddDocs(_ ...Comment)

AddDocs does nothing for the comment code. We only implement this so we implement the Code interface.

func (Comment) Code

func (c Comment) Code() *jen.Statement

Code returns the jen representation of the comment.

func (Comment) Docs

func (c Comment) Docs() []Comment

Docs does nothing for the comment code.

func (Comment) ImportAliases added in v0.1.7

func (c Comment) ImportAliases() []ImportAlias

func (Comment) String

func (c Comment) String() string

String returns the go code string of the comment.

type Const

type Const Var

Const represents a constant, it has the same attributes as the variable only that it assumes that the value is always set (there can not be any constant without initial value).

func NewConst

func NewConst(name string, tp Type, value interface{}, docs ...Comment) *Const

NewConst creates a 7new constant with the given name type and value.

func (*Const) AddDocs

func (c *Const) AddDocs(docs ...Comment)

AddDocs adds a list of documentation strings to the constant.

func (*Const) Code

func (c *Const) Code() *jen.Statement

Code returns the jen representation of the constant.

func (*Const) Docs

func (c *Const) Docs() []Comment

Docs returns the docs comments of the constant.

func (*Const) ImportAliases added in v0.1.7

func (c *Const) ImportAliases() []ImportAlias

ImportAliases returns the import aliases of the constant.

func (*Const) String

func (c *Const) String() string

String returns the go code string of the constant.

type FieldTags

type FieldTags map[string]string

FieldTags represent the structure field tags (e.x `json:"test"`).

func NewFieldTags

func NewFieldTags(key, value string) *FieldTags

NewFieldTags creates a new field tags map with initial key and value. there is also an optional list of documentation comments that you can add to the variable

func (*FieldTags) Set

func (f *FieldTags) Set(key, value string)

Set is used to set an existing or new field tag.

type File

type File struct {
	Code []Code
	// contains filtered or unexported fields
}

File represents a go source file.

func NewFile

func NewFile(packageName string, code ...Code) *File

NewFile creates a new file with the given package name and optional code nodes.

func (*File) AppendAfter

func (f *File) AppendAfter(c Code, new Code) error

AppendAfter appends a new code node after the given code node.

func (*File) PrependBefore

func (f *File) PrependBefore(c Code, new Code) error

PrependBefore prepends a new code node before the given code node.

func (*File) SetImportAliases

func (f *File) SetImportAliases(ia []ImportAlias)

SetImportAliases sets the files import aliases, if the jenFile representation of the file is nil SetImportAliases will do nothing.

func (*File) String

func (f *File) String() string

String returns the go source string of the file, if the jen representation of the file is nil it will return a basic file with package

type Function

type Function struct {
	// Name is the name of the function.
	Name string

	// Recv is the receiver of the function (e.x func (rcv *MyStruct) name() {}).
	Recv *Parameter

	// Params are the functions parameters.
	Params []Parameter

	// Results are the functions results.
	Results []Parameter

	// Body is the function body.
	Body []jen.Code
	// contains filtered or unexported fields
}

Function represents a function.

func NewFunction

func NewFunction(name string, options ...FunctionOptions) *Function

NewFunction creates a new function with the given name and options.

func (*Function) AddDocs

func (f *Function) AddDocs(docs ...Comment)

AddDocs adds a list of documentation strings to the function.

func (*Function) AddParameter

func (f *Function) AddParameter(p Parameter)

AddParameter adds a new parameter to the function.

func (*Function) AddResult

func (f *Function) AddResult(p Parameter)

AddResult adds a new result to the function.

func (*Function) AddStringBody

func (f *Function) AddStringBody(s string)

AddStringBody adds raw string code to the body of the function.

func (*Function) Code

func (f *Function) Code() *jen.Statement

Code returns the jen representation of the function.

func (*Function) Docs

func (f *Function) Docs() []Comment

Docs returns the docs comments of the function.

func (*Function) ImportAliases added in v0.1.7

func (f *Function) ImportAliases() []ImportAlias

ImportAliases returns the import aliases of the function.

func (*Function) String

func (f *Function) String() string

String returns the go code string of the function.

type FunctionOptions

type FunctionOptions func(m *Function)

FunctionOptions is used when you call NewFunction, it is a handy way to allow multiple configurations for a function.

Code has some preconfigured option functions you can use to add parameter, results, receiver, body, docs to the function.

func BodyFunctionOption

func BodyFunctionOption(body ...jen.Code) FunctionOptions

BodyFunctionOption adds given body code to the function.

func DocsFunctionOption

func DocsFunctionOption(docs ...Comment) FunctionOptions

DocsFunctionOption adds given docs to the function.

func FunctionTypeName added in v0.1.10

func FunctionTypeName(name string) FunctionOptions

func ParamsFunctionOption

func ParamsFunctionOption(params ...Parameter) FunctionOptions

ParamsFunctionOption adds given parameters to the function.

func RecvFunctionOption

func RecvFunctionOption(recv *Parameter) FunctionOptions

RecvFunctionOption sets the function receiver.

func ResultsFunctionOption

func ResultsFunctionOption(results ...Parameter) FunctionOptions

ResultsFunctionOption adds given results to the function.

type FunctionType

type FunctionType Function

FunctionType is used in Type to specify a method type (e.x func(sting) int).

func NewFunctionType

func NewFunctionType(options ...FunctionOptions) *FunctionType

NewFunctionType creates a new function type with the given options.

func (*FunctionType) AddDocs

func (m *FunctionType) AddDocs(_ ...Comment)

AddDocs does nothing for the function type code. We only implement this so we implement the Code interface.

func (*FunctionType) Code

func (m *FunctionType) Code() *jen.Statement

Code returns the jen representation of the function type.

func (*FunctionType) Docs

func (m *FunctionType) Docs() []Comment

Docs does nothing for the function type code.

func (*FunctionType) String

func (m *FunctionType) String() string

String returns the go code string of the function type. because the renderer does not render only function types we create a dummy structure to add the function type field to than we remove everything besides the function type.

type Import

type Import struct {
	// FilePath is the path of the package. (e.x /User/Kujtim/go/src/github.com/go-services/code).
	FilePath string

	// Alias is the alias of the import (e.x import my_alias "fmt").
	Alias string

	// Path is the path of the import (e.x import "github.com/go-services/code").
	Path string
}

Import represents an import this is mostly used for parameters and field types so we know what the package of the type is.

func NewImport

func NewImport(alias, path string) *Import

NewImport creates a new Import with the given alias and path.

alias is the alias you want to give a package (e.x import my_alias "fmt") path is the import path (e.x "github.com/go-services/code")

func NewImportWithFilePath

func NewImportWithFilePath(alias, path, filePath string) *Import

NewImportWithFilePath creates a new Import with the given alias and path and filepath.

filePath is the path in the filesystem of the source code.

type ImportAlias

type ImportAlias struct {
	Name string
	Path string
}

ImportAlias is used to specify the import alias in a file

func NewImportAlias

func NewImportAlias(name, path string) ImportAlias

NewImportAlias creates a new import alias with the given name and path.

type Interface

type Interface struct {
	// Name is the name of the interface.
	Name string

	// Methods are the interface methods, the interface can also have no methods.
	Methods []InterfaceMethod
	// contains filtered or unexported fields
}

Interface is the representation of an interface.

func NewInterface

func NewInterface(name string, methods []InterfaceMethod, docs ...Comment) *Interface

NewInterface creates a new interface with the given name and methods, there is also an optional list of documentation comments that you can add to the variable

func (*Interface) AddDocs

func (i *Interface) AddDocs(docs ...Comment)

AddDocs adds a list of documentation strings to the interface.

func (*Interface) AddMethod

func (i *Interface) AddMethod(m InterfaceMethod)

AddMethod a method to the method list

func (*Interface) Code

func (i *Interface) Code() *jen.Statement

Code returns the jen representation of the interface.

func (*Interface) Docs

func (i *Interface) Docs() []Comment

Docs returns the docs comments of the interface.

func (*Interface) ImportAliases added in v0.1.7

func (i *Interface) ImportAliases() []ImportAlias

ImportAliases returns the import aliases of the interface.

func (*Interface) String

func (i *Interface) String() string

String returns the go code string of the interface.

type InterfaceMethod

type InterfaceMethod Function

InterfaceMethod is the representation of interface methods (e.x String() string).

func NewInterfaceMethod

func NewInterfaceMethod(name string, options ...FunctionOptions) InterfaceMethod

NewInterfaceMethod creates a new interface method with the given name and options.

func (*InterfaceMethod) AddDocs

func (m *InterfaceMethod) AddDocs(docs ...Comment)

AddDocs adds a list of documentation strings to the interface method.

func (*InterfaceMethod) Code

func (m *InterfaceMethod) Code() *jen.Statement

Code returns the jen representation of the interface method.

func (*InterfaceMethod) Docs

func (m *InterfaceMethod) Docs() []Comment

Docs returns the docs comments of the interface method.

func (*InterfaceMethod) ImportAliases added in v0.1.7

func (m *InterfaceMethod) ImportAliases() []ImportAlias

ImportAliases returns the import aliases of the interface method.

func (*InterfaceMethod) String

func (m *InterfaceMethod) String() string

String returns the go code string of the interface method. because the renderer does not render only interface methods we create a dummy interface to add the interface method to than we remove everything besides the interface method.

type Parameter

type Parameter struct {
	Name string
	Type Type
}

Parameter represents a parameter, it is used in function parameters, function return parameters.

func NewParameter

func NewParameter(name string, tp Type) *Parameter

NewParameter creates a new parameter with the given name and type.

func (*Parameter) AddDocs

func (p *Parameter) AddDocs(_ ...Comment)

AddDocs does nothing for the parameter code. We only implement this so we implement the Code interface.

func (*Parameter) Code

func (p *Parameter) Code() *jen.Statement

Code returns the jen representation of the parameter.

func (*Parameter) Docs

func (p *Parameter) Docs() []Comment

Docs does nothing for the parameter code.

func (*Parameter) String

func (p *Parameter) String() string

String returns the go code string of the parameter. because the renderer does not render only parameters we create a dummy function to add the parameters to than we remove everything besides the parameters.

type RawCode

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

RawCode represents raw lines of code.

func NewRawCode

func NewRawCode(code *jen.Statement) *RawCode

func (*RawCode) AddDocs

func (c *RawCode) AddDocs(_ ...Comment)

AddDocs does nothing for the code. We only implement this so we implement the Code interface.

func (*RawCode) Code

func (c *RawCode) Code() *jen.Statement

Code returns the jen representation of code.

func (*RawCode) Docs

func (c *RawCode) Docs() []Comment

Docs does nothing for the code.

func (*RawCode) ImportAliases added in v0.1.7

func (c *RawCode) ImportAliases() []ImportAlias

func (*RawCode) String

func (c *RawCode) String() string

String returns the go code string of the comment.

type Struct

type Struct struct {

	// Name represents the name of the structure.
	Name string

	// Fields represents the structure fields.
	Fields []StructField
	// contains filtered or unexported fields
}

Struct represent a structure.

func NewStruct

func NewStruct(name string, docs ...Comment) *Struct

NewStruct creates a new structure with the given name, there is also an optional list of documentation comments that you can add to the variable

func NewStructWithFields

func NewStructWithFields(name string, fields []StructField, docs ...Comment) *Struct

NewStructWithFields creates a new structure with the given name and fields, there is also an optional list of documentation comments that you can add to the variable

func (*Struct) AddDocs

func (s *Struct) AddDocs(docs ...Comment)

AddDocs adds a list of documentation strings to the structure.

func (*Struct) Code

func (s *Struct) Code() *jen.Statement

Code returns the jen representation of the structure.

func (*Struct) Docs

func (s *Struct) Docs() []Comment

Docs returns the docs comments of the structure.

func (*Struct) ImportAliases added in v0.1.7

func (s *Struct) ImportAliases() []ImportAlias

ImportAliases returns the import aliases of the structure.

func (*Struct) String

func (s *Struct) String() string

String returns the go code string of the structure.

type StructField

type StructField struct {
	// Parameter is used for representing the name and type because the go code is the same.
	Parameter

	// Tags represent the field tags.
	Tags *FieldTags
	// contains filtered or unexported fields
}

StructField represent a structure field, it uses the parameter representation to represent the name and the type the difference between parameter and struct field is that struct fields can have docs and tags.

func NewStructField

func NewStructField(name string, tp Type, docs ...Comment) *StructField

NewStructField creates a new structure field with the given name and type, there is also an optional list of documentation comments that you can add to the variable

func NewStructFieldWithTag

func NewStructFieldWithTag(name string, tp Type, tags *FieldTags, docs ...Comment) *StructField

NewStructFieldWithTag creates a new structure field with the given name type and tags, there is also an optional list of documentation comments that you can add to the variable

func (*StructField) Code

func (s *StructField) Code() *jen.Statement

Code returns the jen representation of the structure field.

func (*StructField) Docs

func (s *StructField) Docs() []Comment

Docs returns the docs comments of the structure field.

func (*StructField) ImportAliases added in v0.1.8

func (s *StructField) ImportAliases() []ImportAlias

ImportAliases returns the import aliases of the structure field.

func (*StructField) String

func (s *StructField) String() string

String returns the go code string of the struct field. because the renderer does not render only struct fields we create a dummy structure to add the struct field to than we remove everything besides the struct field.

type StructType added in v0.1.6

type StructType Struct

StructType is used in Type to specify a struct type (e.x struct{}).

func NewStructType added in v0.1.6

func NewStructType(fields ...StructField) *StructType

NewStructType creates a new struct type with the given fields

func (*StructType) AddDocs added in v0.1.6

func (s *StructType) AddDocs(_ ...Comment)

AddDocs does nothing for the struct type code. We only implement this so we implement the Code interface.

func (*StructType) Code added in v0.1.6

func (s *StructType) Code() *jen.Statement

Code returns the jen representation of the struct type.

func (*StructType) Docs added in v0.1.6

func (s *StructType) Docs() []Comment

Docs does nothing for the struct type code.

func (*StructType) String added in v0.1.6

func (s *StructType) String() string

String returns the go code string of the struct type. because the renderer does not render only struct types we create a dummy structure to add the struct type field to than we remove everything besides the struct type.

type Type

type Type struct {
	// Import specifies the import of the type, it is used so we know how to call jen.Qual.
	// e.x if you want to specify the type to be `context.Context`.
	// the Import would be
	//   Import{
	//		Path:"context"
	// 	}
	// and the Qualifier = Context
	Import *Import

	// Function is used in Type to specify a method type (e.x func(sting) int)
	// if method is set all other type parameters besides the pointer are ignored.
	Function *FunctionType

	// MapType is used if the type is a map.
	MapType *struct {
		Key   Type
		Value Type
	}

	// Struct is used for inline struct types
	// e.x
	//  var a struct{}
	Struct *StructType

	// RawType is used to specify complex types (e.x map[string]*test.SomeStruct)
	// if the raw type is not nil all the other parameters will be ignored.
	RawType *jen.Statement

	// Pointer tells if the type is a pointer.
	Pointer bool

	// ArrayType tells if the type is an array.
	ArrayType *Type

	// Variadic tells if the type is used for variadic functions
	Variadic bool

	// Qualifier specifies the qualifier, for simple types like `string` it is the only
	// parameter set on the type.
	Qualifier string
}

Type represents a type e.x `string`, `context.Context`... the type is represented by 2 main parameters.

  1. The Import e.x `context`

    Import = &Import { Path: "context" }

  2. The Qualifier e.x `Context` Qualifier = "Context"

this would give you the representation of `context.Context`.

func NewRawType

func NewRawType(tp *jen.Statement) Type

NewRawType creates a new type with raw jen statement.

func NewType

func NewType(qualifier string, options ...TypeOptions) Type

NewType creates the type with the qualifier and options given.

Options are used so we can create a simple type like `string` and complex types

Code has some build in type options that can be used

  • ImportTypeOption(i *Import) TypeOptions
  • FunctionTypeOption(m *FunctionType) TypeOptions
  • PointerTypeOption() TypeOptions

func (Type) AddDocs

func (t Type) AddDocs(_ ...Comment)

AddDocs does nothing for the type code. We only implement this so we implement the Code interface.

func (Type) Code

func (t Type) Code() *jen.Statement

Code returns the jen representation of the the type.

func (Type) Docs

func (t Type) Docs() []Comment

Docs does nothing for the Type code.

func (Type) ImportAliases added in v0.1.7

func (t Type) ImportAliases() []ImportAlias

ImportAliases returns the import aliases of the type.

func (Type) String

func (t Type) String() string

String returns the go code string of the type, if the type is a function type the function string tis used.

type TypeOptions

type TypeOptions func(t *Type)

TypeOptions is used when you call NewType, it is a handy way to allow multiple configurations for a type.

tp:= NewType("string", PointerTypeOption())

This would give you a type of pointer string. You can use many build in type option functions like:

  • ImportTypeOption(i *Import) TypeOptions
  • FunctionTypeOption(m *FunctionType) TypeOptions
  • PointerTypeOption() TypeOptions

func ArrayTypeOption added in v0.1.6

func ArrayTypeOption(tp Type) TypeOptions

ArrayTypeOption marks the type as variadic.

func FunctionTypeOption

func FunctionTypeOption(m *FunctionType) TypeOptions

FunctionTypeOption adds the given function type to the type.

func ImportTypeOption

func ImportTypeOption(i Import) TypeOptions

ImportTypeOption adds the given import to the type.

func MapTypeOption added in v0.1.6

func MapTypeOption(key Type, value Type) TypeOptions

MapTypeOption sets the map type.

func PointerTypeOption

func PointerTypeOption() TypeOptions

PointerTypeOption marks the type as a pointer type.

func StructTypeOption added in v0.1.6

func StructTypeOption(st StructType) TypeOptions

StructTypeOption sets the map type.

func VariadicTypeOption

func VariadicTypeOption() TypeOptions

VariadicTypeOption marks the type as variadic.

type Var

type Var struct {
	// Name is the name of the variable (e.x var {name} string)
	Name string

	// Type is the type of the variable.
	Type Type

	// The value of the variable (e.x 2, 2.9, "Some string").
	// currently only supports literal values, the plan is to expend this.
	Value interface{}
	// contains filtered or unexported fields
}

Var represents a variable.

func NewVar

func NewVar(name string, tp Type, docs ...Comment) *Var

NewVar creates a new var with the given name and type, there is also an optional list of documentation comments that you can add to the variable

func NewVarWithValue

func NewVarWithValue(name string, tp Type, value interface{}, docs ...Comment) *Var

NewVarWithValue creates a new var with the given name type and value.

func (*Var) AddDocs

func (v *Var) AddDocs(docs ...Comment)

AddDocs adds a list of documentation strings to the variable.

func (*Var) Code

func (v *Var) Code() *jen.Statement

Code returns the jen representation of the variable.

func (*Var) Docs

func (v *Var) Docs() []Comment

Docs returns the docs comments of the variable.

func (*Var) ImportAliases added in v0.1.7

func (v *Var) ImportAliases() []ImportAlias

ImportAliases returns the import aliases of the variable.

func (*Var) String

func (v *Var) String() string

String returns the go code string of the variable.

Jump to

Keyboard shortcuts

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