Giter Club home page Giter Club logo

genr's Introduction

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
}

genr's People

Contributors

drmingdrmer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.