Add sqldb and set packages

This commit is contained in:
2018-06-23 23:46:35 +02:00
parent 0df8b1ec84
commit b06910f223
31 changed files with 2983 additions and 0 deletions
+119
View File
@@ -0,0 +1,119 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"reflect"
"strings"
)
type genericType struct {
name string
testVals []string
}
var types = []genericType{
// Int
{name: reflect.Int.String(), testVals: []string{"1", "2", "3", "4", "5"}},
{name: reflect.Int8.String(), testVals: []string{"1", "2", "3", "4", "5"}},
{name: reflect.Int16.String(), testVals: []string{"1", "2", "3", "4", "5"}},
{name: reflect.Int32.String(), testVals: []string{"1", "2", "3", "4", "5"}},
{name: reflect.Int64.String(), testVals: []string{"1", "2", "3", "4", "5"}},
// Uint
{name: reflect.Uint.String(), testVals: []string{"1", "2", "3", "4", "5"}},
{name: reflect.Uint8.String(), testVals: []string{"1", "2", "3", "4", "5"}},
{name: reflect.Uint16.String(), testVals: []string{"1", "2", "3", "4", "5"}},
{name: reflect.Uint32.String(), testVals: []string{"1", "2", "3", "4", "5"}},
{name: reflect.Uint64.String(), testVals: []string{"1", "2", "3", "4", "5"}},
// String
{name: reflect.String.String(), testVals: []string{`"1"`, `"2"`, `"3"`, `"4"`, `"5"`}},
}
func main() {
tplDir := flag.String("tpl", "", "Path to template directory")
destDir := flag.String("dest", "", "Path to destination directory")
flag.Parse()
if *tplDir == "" {
log.Println("Template directory is not specified")
flag.Usage()
os.Exit(1)
}
if *destDir == "" {
log.Println("Destination directory is not specified")
flag.Usage()
os.Exit(1)
}
implSource, err := ioutil.ReadFile(path.Join(*tplDir, "set.go"))
if err != nil {
log.Fatalf("Failed to read source file at %s: %v", path.Join(*tplDir, "set.go"), err)
}
testSource, err := ioutil.ReadFile(path.Join(*tplDir, "set_test.go"))
if err != nil {
log.Fatalf("Failed to read test file at %s: %v", path.Join(*tplDir, "set_test.go"), err)
}
for _, typ := range types {
log.Printf("Generating package for type %s\n", typ.name)
err := generate(*destDir, implSource, testSource, typ)
if err != nil {
log.Fatalf("Failed to generate a package for type %s: %v", typ.name, err)
}
}
err = gofmt(*destDir)
if err != nil {
log.Fatalf("Formatting failed: %v", err)
}
log.Println("Set packages were successfully generated")
}
func generate(destDir string, implSource, testSource []byte, typ genericType) error {
pkgDir := path.Join(destDir, fmt.Sprintf("set%s", typ.name))
err := os.RemoveAll(pkgDir)
if err != nil && !os.IsNotExist(err) {
return err
}
err = os.Mkdir(pkgDir, 0777)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(pkgDir, "set.go"), renderBytes(implSource, typ), 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(pkgDir, "set_test.go"), renderBytes(testSource, typ), 0755)
return err
}
func renderBytes(src []byte, typ genericType) []byte {
return []byte(render(string(src), typ))
}
func render(src string, typ genericType) string {
const genericTypeName = "TypeName"
// Replace test constants
src = strings.Replace(src, "One TypeName = 1", "One TypeName = "+typ.testVals[0], 1)
src = strings.Replace(src, "Two TypeName = 2", "Two TypeName = "+typ.testVals[1], 1)
src = strings.Replace(src, "Three TypeName = 3", "Three TypeName = "+typ.testVals[2], 1)
src = strings.Replace(src, "Four TypeName = 4", "Four TypeName = "+typ.testVals[3], 1)
src = strings.Replace(src, "Five TypeName = 5", "Five TypeName = "+typ.testVals[4], 1)
// Replace the type name
src = strings.Replace(src, genericTypeName, typ.name, -1)
return src
}
func gofmt(dir string) error {
out, err := exec.Command("gofmt", "-w", "-l", dir).CombinedOutput()
if err != nil {
log.Println("gofmt returned:", string(out))
}
return err
}
+74
View File
@@ -0,0 +1,74 @@
package impl
import (
"fmt"
"sort"
)
// Set is a set of TypeName.
type Set struct {
items map[TypeName]struct{}
}
// New creates a new TypeName set.
func New(items ...TypeName) *Set {
s := &Set{items: make(map[TypeName]struct{}, len(items))}
s.Add(items...)
return s
}
// Add adds given items to the set.
func (s *Set) Add(items ...TypeName) *Set {
for _, item := range items {
s.items[item] = struct{}{}
}
return s
}
// Remove delete given items from the set.
func (s *Set) Remove(items ...TypeName) *Set {
for _, item := range items {
delete(s.items, item)
}
return s
}
// Has returns true if all the given items are included in the set.
func (s *Set) Has(items ...TypeName) bool {
for _, item := range items {
if _, ok := s.items[item]; !ok {
return false
}
}
return true
}
// Len returns the size of the set.
func (s *Set) Len() int {
return len(s.items)
}
// Slice returns items of the set as a slice.
func (s *Set) Slice() []TypeName {
sl := make([]TypeName, len(s.items))
i := 0
for item := range s.items {
sl[i] = item
i++
}
return sl
}
// SortedSlice returns items of the set as a slice sorted ascending.
func (s *Set) SortedSlice() []TypeName {
ss := s.Slice()
sort.Slice(ss, func(i, j int) bool {
return ss[i] < ss[j]
})
return ss
}
// String implements fmt.Stringer interface.
func (s *Set) String() string {
return fmt.Sprintf("[%v]", s.Slice())
}
+114
View File
@@ -0,0 +1,114 @@
package impl
import (
"testing"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/go-cmp/cmp"
)
const (
One TypeName = 1
Two TypeName = 2
Three TypeName = 3
Four TypeName = 4
Five TypeName = 5
)
func TestNew(t *testing.T) {
s := New(One, Two, Three)
if s == nil {
t.Fatal("Set is nil")
}
if s.Len() != 3 {
t.Errorf("Expected set to contain 3 items, got %d", s.Len())
}
for _, item := range []TypeName{One, Two, Three} {
if ok := s.Has(item); !ok {
t.Errorf("Set is expected to contain item %q", item)
}
}
}
func TestAdd(t *testing.T) {
s := New()
if s.Len() != 0 {
t.Errorf("Expected set to be empty, got %d items", s.Len())
}
s.Add(One)
s.Add(Two, Three)
for _, item := range []TypeName{One, Two, Three} {
if ok := s.Has(item); !ok {
t.Errorf("Set is expected to contain item %q", item)
}
}
}
func TestRemove(t *testing.T) {
s := New(One, Two, Three, Four, Five)
s.Remove(One, Two)
s.Remove(Three)
if s.Len() != 2 {
t.Errorf("Expected set to contain 2 items, got %d", s.Len())
}
for _, item := range []TypeName{One, Two, Three} {
if ok := s.Has(item); ok {
t.Errorf("Set is expected to not contain item %q", item)
}
}
for _, item := range []TypeName{Four, Five} {
if ok := s.Has(item); !ok {
t.Errorf("Set is expected to contain item %q", item)
}
}
}
func TestHas(t *testing.T) {
s := New(One, Two)
table := map[TypeName]bool{
One: true,
Two: true,
Three: false,
Four: false,
Five: false,
}
for v, exp := range table {
if res := s.Has(v); res != exp {
t.Errorf("Item: %v, In: %v, Expected: %v", v, res, exp)
}
}
}
func TestLen(t *testing.T) {
table := map[*Set]int{
New(): 0,
New(One): 1,
New(Two, Three): 2,
New(One, Two, Three, Four, Five, Five): 5,
}
for s, exp := range table {
if res := s.Len(); res != exp {
t.Errorf("Expected set %s to have length %d, got %d", s, exp, res)
}
}
}
func TestSlice(t *testing.T) {
s := New(One, Two, Three)
out := s.Slice()
exp := []TypeName{One, Two, Three}
ignoreOrder := cmpopts.SortSlices(func(a, b TypeName) bool { return a < b })
if !cmp.Equal(exp, out, ignoreOrder) {
t.Errorf("Retured slice does not match: %s", cmp.Diff(exp, out))
}
}
func TestSortedSlice(t *testing.T) {
s := New(One, Two, Three)
out := s.SortedSlice()
exp := []TypeName{One, Two, Three}
if !cmp.Equal(exp, out) {
t.Errorf("Retured slice does not match: %s", cmp.Diff(exp, out))
}
}
+4
View File
@@ -0,0 +1,4 @@
package impl
// TypeName is a type placeholder.
type TypeName int