gen

package module
v0.0.0-...-574e917 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2019 License: MIT Imports: 13 Imported by: 0

README

go-genutils

GoDoc Build Status

Package gen provides some utility functions for go code generation.

SYNOPSIS

see example_test.go.

Documentation

Overview

Package gen provides some utility functions for generating go code using io.Writer or fmt.Fprint things.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Comments []string
	Name     string
	Type     string
	Tags     map[string]string
}

Field represents a field of struct.

func (Field) String

func (field Field) String() string

String returns a struct field line as string.

type Fields

type Fields []Field

Fields is a list of Fields.

func (Fields) String

func (fields Fields) String() string

String returns a struct field lines as string.

type Import

type Import struct {
	PackageName string
	ImportPath  string
}

Import represents an `import` declaration in go struct.

func (Import) String

func (imp Import) String() string

String returns an `import` declaration state.

type Imports

type Imports struct {
	BasePath string
	// contains filtered or unexported fields
}

Imports represents a list of imports.

Example
package main

import (
	"fmt"

	gen "github.com/nasa9084/go-genutils"
)

func main() {
	pkgs := []string{
		"fmt",
		"net/http",
	}
	imports := gen.NewImports(pkgs)
	fmt.Print(imports.String())
}
Output:


import (
"fmt"
"net/http"
)
Example (SingleImport)
package main

import (
	"fmt"

	gen "github.com/nasa9084/go-genutils"
)

func main() {
	pkgs := []string{"fmt"}
	imports := gen.NewImports(pkgs)
	fmt.Print(imports.String())
}
Output:


import "fmt"

func NewImports

func NewImports(pkgs []string) Imports

NewImports returns a new import list from import path string slice.

func (Imports) Import

func (imps Imports) Import(pkgName, importPath string)

func (Imports) String

func (imps Imports) String() string

String generates an `import` declaration states. If the number of imports is 1, this is same as Import.String(). Returned string is not formatted. If you need, use format.Source() to format returned code..

type Interface

type Interface struct {
	DocComment string
	Name       string
	Interfaces []Interface
	Methods    Methods
}

func LoadInterfaces

func LoadInterfaces(f *ast.File) ([]Interface, error)

func LoadInterfacesFromFile

func LoadInterfacesFromFile(filename string) ([]Interface, error)

func NewInterface

func NewInterface(comment, name string, iface *ast.InterfaceType) Interface

func (Interface) String

func (iface Interface) String() string

type Method

type Method struct {
	Name    string
	Params  Params
	Results Results
}

func (Method) String

func (method Method) String() string

type Methods

type Methods []Method

type Package

type Package struct {
	Name       string
	Structs    []Struct
	Interfaces []Interface
}

func LoadPackage

func LoadPackage(astpkg *ast.Package) (Package, error)

func LoadPackages

func LoadPackages(pkgs map[string]*ast.Package) ([]Package, error)

func LoadPackagesFromPath

func LoadPackagesFromPath(path string) ([]Package, error)

type Param

type Param struct {
	Name string
	Type string
}

func (Param) String

func (param Param) String() string

type Params

type Params []Param

func (Params) String

func (params Params) String() string

type Result

type Result struct {
	Name string
	Type string
}

func (Result) String

func (result Result) String() string

type Results

type Results []Result

func (Results) String

func (results Results) String() string

type Struct

type Struct struct {
	DocComments []string
	Name        string
	Fields      Fields
}

Struct represents a struct type.

func LoadStructs

func LoadStructs(f *ast.File) ([]Struct, error)

LoadStructs load struct types from *ast.File.

Example
package main

import (
	"fmt"

	gen "github.com/nasa9084/go-genutils"
)

func main() {
	structs, _ := gen.LoadStructsFromFile("./internal/tests/test_structs.go")
	for _, s := range structs {
		fmt.Println(s.Name)
	}
}
Output:

Something
Nested
Parent
Child

func LoadStructsFromFile

func LoadStructsFromFile(filename string) ([]Struct, error)

LoadStructsFromFile load struct types from file.

func NewStruct

func NewStruct(comment string, name string, st *ast.StructType) Struct

NewStruct creates a new Struct object from *ast.StructType.

func (Struct) String

func (s Struct) String() string

String returns formatted source-form struct type definition.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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