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
|
||||
{{range .Fields}}
|
||||
{{range .Fields -}}
|
||||
---@field {{.Name}} {{.Type}} {{.Comment}}
|
||||
{{end}}
|
||||
{{.ClassName}} = {
|
||||
{{$n := len .Methods}}
|
||||
{{$methods := len .Methods}}
|
||||
{{range $index, $method := .Methods}}
|
||||
{{$n := len .Methods -}}
|
||||
{{$methods := len .Methods -}}
|
||||
{{range $index, $method := .Methods -}}
|
||||
---@param self {{$.ClassName}}
|
||||
{{range $param := $method.Params}}
|
||||
{{range $param := $method.Params -}}
|
||||
---@param {{.Name}} {{.Type}} {{.Comment}}
|
||||
{{end}}
|
||||
{{range $ret := $method.Returns}}
|
||||
{{end -}}
|
||||
{{range $ret := $method.Returns -}}
|
||||
---@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,
|
||||
{{if eq (plus1 $index) $n}}
|
||||
{{- if ne (plus1 $index) $n}}
|
||||
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}{{end}}
|
||||
}
|
||||
|
||||
---@type ({{$.ClassName}} | fun(): {{$.ClassName}})
|
||||
|
39
main.go
39
main.go
@@ -30,6 +30,12 @@ func init() {
|
||||
log.Lmicroseconds|log.Lshortfile)
|
||||
}
|
||||
|
||||
var fns = template.FuncMap{
|
||||
"plus1": func(x int) int {
|
||||
return x + 1
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
files := flag.Args()
|
||||
@@ -49,25 +55,42 @@ func main() {
|
||||
{
|
||||
Name: "test",
|
||||
Params: []Param{
|
||||
{Name: "a", Type: "int", Comment: "test"},
|
||||
{Name: "b", Type: "string", Comment: "test"},
|
||||
{Name: "a", Type: "int", Comment: "test param 1"},
|
||||
{Name: "b", Type: "string", Comment: "test param 2"},
|
||||
},
|
||||
Returns: []Return{
|
||||
{Type: "int", Comment: "test"},
|
||||
{Type: "string", Comment: "test"},
|
||||
{Type: "int", Comment: "test return int"},
|
||||
{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 {
|
||||
Error.Printf("Error parsing template: %v", err)
|
||||
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 {
|
||||
fmt.Println(file)
|
||||
|
28
types.go
28
types.go
@@ -1,10 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type (
|
||||
Class struct {
|
||||
ClassName string
|
||||
Fields []Field
|
||||
Methods []Method
|
||||
ClassName string
|
||||
Fields []Field
|
||||
Methods []Method
|
||||
Constructors []Constructor
|
||||
}
|
||||
Field struct {
|
||||
Name string
|
||||
@@ -26,4 +32,20 @@ type (
|
||||
Type 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