packagen

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2019 License: BSD-3-Clause Imports: 16 Imported by: 0

README

packagen : Generate Go code from packages or files

Overview GoDoc Go Report Card

Before Go adopts generics in one form or another (if it ever does!), duplicating code for different types is the current way of dealing with it. The goal of this package is to be able to generate that code from a well written and tested package, and incidentally from any go file.

Install

go get github.com/pierrec/packagen/cmd/packagen

Usage

The main idea is to define a pivot type, eventually with its own set of methods, that works for the implemented algorithm. That type is then replaced when generating the code with a custom one that works in the same way, implementing the same methods if necessary.

The generated code contains the same types, functions etc than the source, but prefixed so that they do not collide with the code in the same package.

The available command line options are:

  • single
    • -const list of integer constants to be updated (constname=integer[, ...])
    • -mvtype list of named types to be renamed (old=new[, ...])
    • -newpkg new package name (default=current working dir package)
    • -nogen - do not add the generate directive
    • -o file - write output to file (default=standard output)
    • -prefix prefix used to rename declarations (default=packageName_)
    • -rmtype list of named types to be removed (typename[, ...])

Example

Given a sorting algorithm implemented in the package domain/user/sort, generate the code for another integer type with the following command:

packagen single -o int32s_gen.go -prefix Int32 -mvtype Numbers=Int32s -rmtype Numbers domain/user/sort

Source package:

package sort

type Numbers []int

func (s Numbers) Len() int { return len(s) }
func (s Numbers) Less(i, j int) bool { return s[i] < s[j] }
func (s Numbers) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func Sort(s Numbers) {
    // ... sorting algorithm
}

In the new package domain/user/myapp, there is an existing int32s.go file:

package myapp

type Int32s []int32

// Int32s implements the methods required by the sort algorithm.
func (s Int32s) Len() int { return len(s) }
func (s Int32s) Less(i, j int) bool { return s[i] < s[j] }
func (s Int32s) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

The generated file int32s_gen.go contains:

//go:generate go run github/pierrec/packagen/cmd/packagen single -o int32s_gen.go -prefix Int32 -mvtype Numbers=Int32s -rmtype Numbers domain/user/sort

package myapp

// Note that the package name has been set automatically.
// Also, the go:generate directive is added by default, but this can be disabled.

// All the code from the source is preserved and top level declarations are prefixed, except for the pivot type and its 
// methods which have been removed.
func Int32Sort(s Int32s) {
    // ... sorting algorithm
}

Contributing

Contributions welcome via pull requests. Please provide tests.

Documentation

Overview

Package packagen generate Go source code from source package(s). It is largely inspired by https://github.com/golang/tools/cmd/bundle.

Transformations can be applied to the resulting AST before outputting it.

Limitations: - no asm, no cgo, no build tags

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bundle added in v0.0.10

func Bundle(out io.Writer, o BundleOption) error

Bundle packs the package identified by o.PkgName into a bundle file and writes it to the given io.Writer.

func ExtendStruct added in v0.0.10

func ExtendStruct(out, methods io.Writer, o ExtendOption) (string, error)

ExtendStruct adds fields and methods from one struct to another. It returns the name of the file where the destination struct is located and writes the new destination content to out and its new methods (if any) to methods.

Types

type BundleOption added in v0.0.10

type BundleOption struct {
	Log     *log.Logger
	Pkg     string            // Package to be processed
	NewPkg  string            // Name of the resulting package (default=current working dir package)
	Prefix  string            // Prefix for the global identifiers (default=packageName_)
	Types   map[string]string // Map the names of the types to be renamed to their new one
	RmTypes map[string]bool   // Named types to be removed
	Const   map[string]int    // Values for const to be updated
	RmConst map[string]bool   // Constants to be removed
}

BundleOption defines the options for the Bundle processor.

type ExtendOption added in v0.0.10

type ExtendOption struct {
	Log          *log.Logger
	SrcPkg       string            // Package of the source type
	Src          string            // Name of the struct type to be used as source
	DstPkg       string            // Package of the destination type
	Dst          string            // Name of the struct type to be extended
	Fields       map[string]string // Map field name to the new type
	FieldPrefix  string            // Prefix to be used for the added fields
	MethodPrefix string            // Prefix to be used for method names
}

ExtendOption defines the options in use when extending a type.

Directories

Path Synopsis
cmd
examples
set
slice
This file is the reference implementation of the Slice type.
This file is the reference implementation of the Slice type.
internal
par
Straight copy from https://github.com/golang/go/tree/master/src/cmd/go/internal/par.
Straight copy from https://github.com/golang/go/tree/master/src/cmd/go/internal/par.

Jump to

Keyboard shortcuts

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