packagen

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 29, 2019 License: BSD-3-Clause Imports: 12 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 a source package. 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 Single

func Single(out io.Writer, o SingleOption) error

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

Types

type SingleOption

type SingleOption struct {
	Log        *log.Logger
	Patterns   []string            // Packages to be processed
	NewPkgName 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]struct{} // Named types to be removed
	Const      map[string]int      // Values for const to be updated
	RmConst    map[string]struct{} // Constants to be removed
}

SingleOption defines the options for the Single processor.

Directories

Path Synopsis
cmd
examples
set
DO NOT EDIT Code automatically generated.
DO NOT EDIT Code automatically generated.
slice
This file is the reference implementation of the Slice type.
This file is the reference implementation of the Slice type.

Jump to

Keyboard shortcuts

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