This repository has been archived by the owner on May 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goalrescha.go
103 lines (95 loc) · 2.86 KB
/
goalrescha.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
99
100
101
102
103
//go:generate go get -u github.com/valyala/quicktemplate/qtc
//go:generate go run github.com/valyala/quicktemplate/qtc -dir=goqtpl
package goalrescha
import (
"bytes"
"fmt"
"os/exec"
"strconv"
"github.com/unsafe-risk/go-alrescha/goqtpl"
"github.com/unsafe-risk/go-alrescha/parser"
)
func MakeGoCode(data []byte, PackageName string) ([]byte, error) {
var buffer *bytes.Buffer = bytes.NewBuffer(nil)
info, err := parser.ParseGenerateInfo(data)
if err != nil {
return nil, err
}
fmt.Fprintf(buffer, "package %s\n", PackageName)
fmt.Fprintln(buffer)
fmt.Fprintln(buffer, "import (")
fmt.Fprintln(buffer, "\t\"io\"")
fmt.Fprintln(buffer, "\t\"math\"")
fmt.Fprintln(buffer, ")")
fmt.Fprintln(buffer)
fmt.Fprintln(buffer, "var _ = math.Float64bits")
fmt.Fprintln(buffer, "var _ = io.EOF")
fmt.Fprintln(buffer, goqtpl.MaxLenConst(info.Max))
for i := range info.IDL.Types {
qtplStructs := make([]goqtpl.StructField, 0, len(info.IDL.Types))
for j := range info.IDL.Types[i].Fields {
if info.IDL.Types[i].Fields[j].FieldType == "array" {
qtplStructs = append(qtplStructs, goqtpl.StructField{
Name: info.IDL.IndexPathMap[info.IDL.Types[i].Fields[j].Index],
Type: "[" + strconv.Itoa(info.IDL.Types[i].Fields[j].ArrayLength) + "]" + ConvertToGoType(info.IDL.Types[i].Fields[j].CodeType),
})
} else if info.IDL.Types[i].Fields[j].FieldType == "list" {
qtplStructs = append(qtplStructs, goqtpl.StructField{
Name: info.IDL.IndexPathMap[info.IDL.Types[i].Fields[j].Index],
Type: "[]" + ConvertToGoType(info.IDL.Types[i].Fields[j].CodeType),
})
} else {
qtplStructs = append(qtplStructs, goqtpl.StructField{
Name: info.IDL.IndexPathMap[info.IDL.Types[i].Fields[j].Index],
Type: ConvertToGoType(info.IDL.Types[i].Fields[j].CodeType),
})
}
}
fmt.Fprintln(buffer, CleanCode(goqtpl.GenerateGoStruct(info.IDL.IndexPathMap[info.IDL.Types[i].Index], qtplStructs)))
}
for i := range info.Structs {
fmt.Fprintln(buffer, CleanCode(goqtpl.MakeMarshal(info.Structs[i].Name, info.Structs[i].Types)))
fmt.Fprintln(buffer, CleanCode(goqtpl.MakeUnmarshal(info.Structs[i].Name, info.Structs[i].Types)))
}
return []byte(CleanCode(buffer.String())), nil
}
func ConvertToGoType(RawType string) string {
switch RawType {
case "i8":
return "int8"
case "i16":
return "int16"
case "i32":
return "int32"
case "i64":
return "int64"
case "u8":
return "uint8"
case "u16":
return "uint16"
case "u32":
return "uint32"
case "u64":
return "uint64"
case "f32":
return "float32"
case "f64":
return "float64"
case "str":
return "string"
case "bytes":
return "[]byte"
}
return RawType
}
func CleanCode(in string) string {
buf := bytes.NewBuffer(nil)
buf.WriteString(in)
outbuf := bytes.NewBuffer(nil)
gfmt := exec.Command("gofmt")
gfmt.Stdin = buf
gfmt.Stdout = outbuf
gfmt.Start()
gfmt.Wait()
return outbuf.String()
}