Debian Codegraph @

dstream/gen/main.go

raw symbols SCIP
1 // +build ignore
2
3 package main
4
5 import (
6 "bytes"
7 "flag"
8 "go/format"
9 "os"
10 "path"
11 "strings"
12 "text/template"
13 )
14
15 type Dtype struct {
16 Type string
17 Utype string
18 ConvGroup string
19 }
20
21 var (
22 NumTypes = []Dtype{
23 {"uint8", "Uint8", "i"},
24 {"uint16", "Uint16", "i"},
25 {"uint32", "Uint32", "i"},
26 {"uint64", "Uint64", "i"},
27 {"int8", "Int8", "i"},
28 {"int16", "Int16", "i"},
29 {"int32", "Int32", "i"},
30 {"int64", "Int64", "i"},
31 {"float32", "Float32", "f"},
32 {"float64", "Float64", "f"},
33 }
34
35 AllTypes = []Dtype{
36 {"string", "String", "s"},
37 {"time.Time", "Time", "t"},
38 }
39 )
40
41 func main() {
42
43 noformat := flag.Bool("noformat", false, "format code")
44 numeric := flag.Bool("numeric", false, "only use numeric types")
45 templatefile := flag.String("template", "", "template file")
46 flag.Parse()
47
48 if *templatefile == "" {
49 panic("'template' is a required argument")
50 }
51
52 AllTypes = append(AllTypes, NumTypes...)
53
54 tmpl, err := template.ParseFiles(*templatefile)
55 if err != nil {
56 panic(err)
57 }
58
59 var buf bytes.Buffer
60 if *numeric {
61 err = tmpl.Execute(&buf, NumTypes)
62 } else {
63 err = tmpl.Execute(&buf, AllTypes)
64 }
65 if err != nil {
66 panic(err)
67 }
68
69 var p []byte
70 if *noformat {
71 p = buf.Bytes()
72 } else {
73 p, err = format.Source(buf.Bytes())
74 if err != nil {
75 panic(err)
76 }
77 }
78
79 outname := strings.Replace(*templatefile, ".tmpl", "_gen.go", 1)
80 out, err := os.Create(path.Join("..", outname))
81 if err != nil {
82 panic(err)
83 }
84 out.WriteString("// GENERATED CODE, DO NOT EDIT\n\n")
85 _, err = out.Write(p)
86 if err != nil {
87 panic(err)
88 }
89 out.Close()
90 }