This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
/
base.go
98 lines (86 loc) · 3.75 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"text/template"
)
type typeBucket struct {
BaseType string
PackageType string
ZeroValue string
BaseFile string
TestFile string
}
func main() {
dest, err := filepath.Abs("../../")
if err != nil {
panic(err)
}
buckets := []typeBucket{
{"int", "Int", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"int8", "Int8", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"int16", "Int16", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"int32", "Int32", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"int64", "Int64", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"uint", "UInt", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"uint8", "UInt8", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"uint16", "UInt16", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"uint32", "UInt32", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"uint64", "UInt64", "0", "./tmpl/base.go.tpl", "./tmpl/integertype_test.go.tpl"},
{"float64", "Float64", "0", "./tmpl/base.go.tpl", "./tmpl/floattype_test.go.tpl"},
{"float32", "Float32", "0", "./tmpl/base.go.tpl", "./tmpl/floattype_test.go.tpl"},
{"string", "String", `""`, "./tmpl/base.go.tpl", "./tmpl/stringtype_test.go.tpl"},
}
for _, bucket := range buckets {
baseFileBytes, readBaseErr := ioutil.ReadFile(bucket.BaseFile)
if readBaseErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "Opening integertype base file failed with error: %v", readBaseErr)
os.Exit(1)
}
testFileBytes, readTestErr := ioutil.ReadFile(bucket.TestFile)
if readTestErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "Opening integertype test file failed with error: %v", readTestErr)
os.Exit(1)
}
baseTmpl := template.Must(template.New("base").Parse(string(baseFileBytes)))
testTmpl := template.Must(template.New("test").Parse(string(testFileBytes)))
var execErr, writeErr, sourceErr error
var baseBytes, testBytes, baseSourceBytes, testSourceBytes []byte
baseBuff := bytes.NewBuffer(baseBytes)
testBuff := bytes.NewBuffer(testBytes)
if execErr = baseTmpl.Execute(baseBuff, bucket); execErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "executing template failed for %s/%s: %v", bucket.BaseType, bucket.PackageType, execErr)
os.Exit(1)
}
if execErr = testTmpl.Execute(testBuff, bucket); execErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "executing template failed for %s/%s: %v", bucket.BaseType, bucket.PackageType, execErr)
os.Exit(1)
}
baseSourceBytes, sourceErr = format.Source(baseBuff.Bytes())
if sourceErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "canonical source formatting failed for %s/%s: %v", bucket.BaseType, bucket.PackageType, sourceErr)
os.Exit(1)
}
baseBuff = bytes.NewBuffer(baseSourceBytes)
testSourceBytes, sourceErr = format.Source(testBuff.Bytes())
if sourceErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "canonical source formatting failed for %s/%s: %v", bucket.BaseType, bucket.PackageType, sourceErr)
os.Exit(1)
}
testBuff = bytes.NewBuffer(testSourceBytes)
fileName := filepath.Join(dest, fmt.Sprintf("%s.go", bucket.BaseType))
if writeErr = ioutil.WriteFile(fileName, baseBuff.Bytes(), 0644); writeErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "writing base file failed for %s/%s: %v", bucket.BaseType, bucket.PackageType, writeErr)
os.Exit(1)
}
testName := filepath.Join(dest, fmt.Sprintf("%s_test.go", bucket.BaseType))
if writeErr = ioutil.WriteFile(testName, testBuff.Bytes(), 0644); writeErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "writing test file failed for %s/%s: %v", bucket.BaseType, bucket.PackageType, writeErr)
os.Exit(1)
}
}
}