Refine the template to fix excessive newlines
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Test.lua
|
21
class.tmpl
21
class.tmpl
@@ -1,23 +1,22 @@
|
|||||||
---@diagnostic disable: missing-return
|
---@diagnostic disable: missing-return
|
||||||
{{range .Fields}}
|
{{range .Fields -}}
|
||||||
---@field {{.Name}} {{.Type}} {{.Comment}}
|
---@field {{.Name}} {{.Type}} {{.Comment}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{.ClassName}} = {
|
{{.ClassName}} = {
|
||||||
{{$n := len .Methods}}
|
{{$n := len .Methods -}}
|
||||||
{{$methods := len .Methods}}
|
{{$methods := len .Methods -}}
|
||||||
{{range $index, $method := .Methods}}
|
{{range $index, $method := .Methods -}}
|
||||||
---@param self {{$.ClassName}}
|
---@param self {{$.ClassName}}
|
||||||
{{range $param := $method.Params}}
|
{{range $param := $method.Params -}}
|
||||||
---@param {{.Name}} {{.Type}} {{.Comment}}
|
---@param {{.Name}} {{.Type}} {{.Comment}}
|
||||||
{{end}}
|
{{end -}}
|
||||||
{{range $ret := $method.Returns}}
|
{{range $ret := $method.Returns -}}
|
||||||
---@return {{.Type}} {{if ne .Comment ""}}#{{.Comment}}{{end}}
|
---@return {{.Type}} {{if ne .Comment ""}}#{{.Comment}}{{end}}
|
||||||
{{end}}
|
{{end -}}
|
||||||
{{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end,
|
{{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end,
|
||||||
{{if eq (plus1 $index) $n}}
|
{{- if ne (plus1 $index) $n}}
|
||||||
|
|
||||||
{{end}}
|
{{end}}{{end}}
|
||||||
{{end}}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
---@type ({{$.ClassName}} | fun(): {{$.ClassName}})
|
---@type ({{$.ClassName}} | fun(): {{$.ClassName}})
|
||||||
|
39
main.go
39
main.go
@@ -30,6 +30,12 @@ func init() {
|
|||||||
log.Lmicroseconds|log.Lshortfile)
|
log.Lmicroseconds|log.Lshortfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fns = template.FuncMap{
|
||||||
|
"plus1": func(x int) int {
|
||||||
|
return x + 1
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
files := flag.Args()
|
files := flag.Args()
|
||||||
@@ -49,25 +55,42 @@ func main() {
|
|||||||
{
|
{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
Params: []Param{
|
Params: []Param{
|
||||||
{Name: "a", Type: "int", Comment: "test"},
|
{Name: "a", Type: "int", Comment: "test param 1"},
|
||||||
{Name: "b", Type: "string", Comment: "test"},
|
{Name: "b", Type: "string", Comment: "test param 2"},
|
||||||
},
|
},
|
||||||
Returns: []Return{
|
Returns: []Return{
|
||||||
{Type: "int", Comment: "test"},
|
{Type: "int", Comment: "test return int"},
|
||||||
{Type: "string", Comment: "test"},
|
{Type: "string", Comment: "test return string"},
|
||||||
},
|
},
|
||||||
Comment: "test",
|
Comment: "test method",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "test2",
|
||||||
|
Params: []Param{
|
||||||
|
{Name: "a", Type: "int", Comment: "test param 1"},
|
||||||
|
{Name: "b", Type: "string", Comment: "test param 2"},
|
||||||
|
},
|
||||||
|
Returns: []Return{
|
||||||
|
{Type: "int", Comment: "test return int"},
|
||||||
|
{Type: "string", Comment: "test return string"},
|
||||||
|
},
|
||||||
|
Comment: "test method",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ltemplate, err := template.New("class").Parse(templatestr)
|
ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("Error parsing template: %v", err)
|
Error.Printf("Error parsing template: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("%#v", ltemplate)
|
|
||||||
ltemplate.Execute(os.Stdout, test)
|
outfile, err := test.GetOutFile()
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error creating output file: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ltemplate.Execute(outfile, test)
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
fmt.Println(file)
|
fmt.Println(file)
|
||||||
|
22
types.go
22
types.go
@@ -1,10 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Class struct {
|
Class struct {
|
||||||
ClassName string
|
ClassName string
|
||||||
Fields []Field
|
Fields []Field
|
||||||
Methods []Method
|
Methods []Method
|
||||||
|
Constructors []Constructor
|
||||||
}
|
}
|
||||||
Field struct {
|
Field struct {
|
||||||
Name string
|
Name string
|
||||||
@@ -26,4 +32,20 @@ type (
|
|||||||
Type string
|
Type string
|
||||||
Comment string
|
Comment string
|
||||||
}
|
}
|
||||||
|
Constructor struct {
|
||||||
|
Params []Param
|
||||||
|
Comment string
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (c *Class) GetOutFile() (*os.File, error) {
|
||||||
|
if c.ClassName == "" {
|
||||||
|
return nil, fmt.Errorf("ClassName is empty")
|
||||||
|
}
|
||||||
|
filename := fmt.Sprintf("%s.lua", c.ClassName)
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user