63 lines
1.0 KiB
Go
63 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"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
|
|
}
|
|
}
|
|
|
|
type (
|
|
Function struct {
|
|
Name string
|
|
Arguments []Parameter
|
|
Returns []Parameter
|
|
}
|
|
Parameter struct {
|
|
Name string
|
|
Type string
|
|
Description string
|
|
}
|
|
)
|
|
|
|
func (f *Function) ResolveFileName() string {
|
|
return f.Name + ".lua"
|
|
}
|
|
func (f *Function) WriteFile() error {
|
|
if f.Name == "" {
|
|
return fmt.Errorf("function name is empty of %+v", f)
|
|
}
|
|
|
|
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
|
|
}
|