https://github.com/openacid/genr
generator for generic types
https://github.com/openacid/genr
Last synced: about 1 month ago
JSON representation
generator for generic types
- Host: GitHub
- URL: https://github.com/openacid/genr
- Owner: openacid
- License: mit
- Created: 2020-11-23T04:08:15.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-11-23T09:34:16.000Z (over 5 years ago)
- Last Synced: 2025-03-04T09:43:52.400Z (over 1 year ago)
- Language: Go
- Size: 21.5 KB
- Stars: 0
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# genr
[](https://travis-ci.com/openacid/genr)

[](https://goreportcard.com/report/github.com/openacid/genr)
[](https://coveralls.io/github/openacid/genr?branch=main&service=github)
[](http://godoc.org/github.com/openacid/genr)
[](https://pkg.go.dev/github.com/openacid/genr)
[](https://sourcegraph.com/github.com/openacid/genr?badge)
genr generates source code to emulate `generic type`.
- [Synopsis](#synopsis)
- [Generate two types `U16` and `I64` with a template:](#generate-two-types-u16-and-i64-with-a-template)
# Synopsis
## Generate two types `U16` and `I64` with a template:
```go
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:
```go
// 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
}
```