Add template and saving functions
This commit is contained in:
2
function.tmpl
Normal file
2
function.tmpl
Normal file
@@ -0,0 +1,2 @@
|
||||
---@diagnostic disable: missing-return, lowercase-global
|
||||
function {{.Name}}({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}) end
|
51
types.go
51
types.go
@@ -1,13 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"log"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var fns = template.FuncMap{
|
||||
"plus1": func(x int) int {
|
||||
return x + 1
|
||||
},
|
||||
}
|
||||
|
||||
//go:embed function.tmpl
|
||||
var templatestr string
|
||||
|
||||
var functionTemplate *template.Template
|
||||
func init() {
|
||||
var err error
|
||||
functionTemplate, err = template.New("class").Funcs(fns).Parse(templatestr)
|
||||
if err != nil {
|
||||
Error.Printf("Error parsing template: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("%#v", functionTemplate)
|
||||
}
|
||||
|
||||
type (
|
||||
Function struct {
|
||||
Name string
|
||||
Arguments []Argument
|
||||
Returns []Argument
|
||||
}
|
||||
Argument struct {
|
||||
Name string
|
||||
Type string
|
||||
Name string
|
||||
Type string
|
||||
Description string
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
func (f *Function) ResolveFileName() string {
|
||||
return f.Name + ".lua"
|
||||
}
|
||||
func (f *Function) WriteFile() error {
|
||||
file, err := os.Create(f.ResolveFileName())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
err = functionTemplate.Execute(file, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user