File: main.go

package info (click to toggle)
golang-github-kshedden-dstream 0.0~git20190512.c4c4106-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: makefile: 30
file content (90 lines) | stat: -rw-r--r-- 1,576 bytes parent folder | download
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
// +build ignore

package main

import (
	"bytes"
	"flag"
	"go/format"
	"os"
	"path"
	"strings"
	"text/template"
)

type Dtype struct {
	Type      string
	Utype     string
	ConvGroup string
}

var (
	NumTypes = []Dtype{
		{"uint8", "Uint8", "i"},
		{"uint16", "Uint16", "i"},
		{"uint32", "Uint32", "i"},
		{"uint64", "Uint64", "i"},
		{"int8", "Int8", "i"},
		{"int16", "Int16", "i"},
		{"int32", "Int32", "i"},
		{"int64", "Int64", "i"},
		{"float32", "Float32", "f"},
		{"float64", "Float64", "f"},
	}

	AllTypes = []Dtype{
		{"string", "String", "s"},
		{"time.Time", "Time", "t"},
	}
)

func main() {

	noformat := flag.Bool("noformat", false, "format code")
	numeric := flag.Bool("numeric", false, "only use numeric types")
	templatefile := flag.String("template", "", "template file")
	flag.Parse()

	if *templatefile == "" {
		panic("'template' is a required argument")
	}

	AllTypes = append(AllTypes, NumTypes...)

	tmpl, err := template.ParseFiles(*templatefile)
	if err != nil {
		panic(err)
	}

	var buf bytes.Buffer
	if *numeric {
		err = tmpl.Execute(&buf, NumTypes)
	} else {
		err = tmpl.Execute(&buf, AllTypes)
	}
	if err != nil {
		panic(err)
	}

	var p []byte
	if *noformat {
		p = buf.Bytes()
	} else {
		p, err = format.Source(buf.Bytes())
		if err != nil {
			panic(err)
		}
	}

	outname := strings.Replace(*templatefile, ".tmpl", "_gen.go", 1)
	out, err := os.Create(path.Join("..", outname))
	if err != nil {
		panic(err)
	}
	out.WriteString("// GENERATED CODE, DO NOT EDIT\n\n")
	_, err = out.Write(p)
	if err != nil {
		panic(err)
	}
	out.Close()
}