genr

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: MIT Imports: 4 Imported by: 3

README

genr

Travis test

Report card Coverage Status

GoDoc PkgGoDev Sourcegraph

genr generates source code to emulate generic type.

Synopsis

Generate two types U16 and I64 with a template:

package main

import (
	"github.com/openacid/genr"
)

var implHead = `package intarray
import (
	"encoding/binary"
)
`

var implTemplate = `
type {{.Name}} struct {
	vals []{{.ValType}}
	eltSize int
	first []byte
}

func New{{.Name}}(elts []{{.ValType}}) (a *{{.Name}}, err error) {
	a = &{{.Name}}{
		vals: make([]{{.ValType}}, 10),
		eltSize: {{.ValLen}},
		first: make([]byte, 0),
	}

	binary.LittleEndian.Put{{.Codec}}(a.first, {{.EncodeCast}}(elts[0]))
	return a, nil
}
`

func main() {

	implfn := "../intarray.go"

	impls := []interface{}{
		genr.NewIntConfig("U16", "uint16"),
		genr.NewIntConfig("I64", "int64"),
	}

	genr.Render(implfn, implHead, implTemplate, impls, []string{"gofmt", "unconvert"})

}

The generated codes looks like the following:

// Code generated 'by go generate ./...'; DO NOT EDIT.

package intarray

import (
	"encoding/binary"
)

type U16 struct {
	vals    []uint16
	eltSize int
	first   []byte
}

func NewU16(elts []uint16) (a *U16, err error) {
	a = &U16{
		vals:    make([]uint16, 10),
		eltSize: 2,
		first:   make([]byte, 0),
	}

	binary.LittleEndian.PutUint16(a.first, elts[0])
	return a, nil
}

type I64 struct {
	vals    []int64
	eltSize int
	first   []byte
}

func NewI64(elts []int64) (a *I64, err error) {
	a = &I64{
		vals:    make([]int64, 10),
		eltSize: 8,
		first:   make([]byte, 0),
	}

	binary.LittleEndian.PutUint64(a.first, uint64(elts[0]))
	return a, nil
}

Documentation

Overview

Package genr provides with utilities to generate codes It builds a user defined type `typeName` with type argument `valueType`, to emulate a generic type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(fn string, header string, tmpl string, datas []interface{}, linters []string)

Render generate a file "fn". File content is defined by a "header", a repeated body template "tmpl" and slice of "data" to render the body template. Additianlly some linters can be specified to run after generating. Supported linters are "gofmt" and "unconvert".

Types

type IntConfig

type IntConfig struct {
	// Name of the data type.
	Name string
	// ValType is the actual underlying int type, such as int32.
	ValType string
	// ValLen specifies the length of ValType
	ValLen int
	// Codec defines the name of function to decode raw bytes into ValType.
	Codec string
	// EncodeCast defines a cast type/function to convert values before encode.
	// Because sometimes encoder does not provides a exact type.
	EncodeCast string
}

IntConfig defines a integer type based template redner config.

func NewIntConfig

func NewIntConfig(typeName, valueType string) *IntConfig

NewIntConfig build a IntConfig for a user defined type `typeName` with type argument valueType, to emulate a generic type.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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