From 440a6e33e6a89b52e1f6405c4e240fd19defad75 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 12 Sep 2024 00:40:20 +0200 Subject: [PATCH] Refine the template to fix excessive newlines --- .gitignore | 1 + class.tmpl | 21 ++++++++++----------- main.go | 39 +++++++++++++++++++++++++++++++-------- types.go | 28 +++++++++++++++++++++++++--- 4 files changed, 67 insertions(+), 22 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..046dbb7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Test.lua diff --git a/class.tmpl b/class.tmpl index 28f13fd..73105b3 100644 --- a/class.tmpl +++ b/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}}) diff --git a/main.go b/main.go index 1eea614..7657e18 100644 --- a/main.go +++ b/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) diff --git a/types.go b/types.go index 16523af..bf43a5e 100644 --- a/types.go +++ b/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 +}