-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
122 lines (104 loc) · 2.96 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package luadoc
import (
"fmt"
"strings"
"text/template"
)
var templateLuaDocLib = template.Must(template.
New("luadoc").
Funcs(map[string]any{
"doc": func(s string) string {
var b strings.Builder
for _, line := range strings.Split(s, "\n") {
b.WriteString("--- ")
b.WriteString(line)
b.WriteString("\n")
}
return strings.TrimSpace(b.String())
},
"join": func(s []string, delim string) string {
return strings.Join(s, delim)
},
"params": func(s []*Param) []string {
var strings = make([]string, len(s))
for i, v := range s {
strings[i] = v.String()
}
return strings
},
"vartype": func(v *Var) string {
if v.Type == "" {
return v.Value.Type().String()
}
return v.Type
},
"defineTypes": func(vars []*Var) string {
var custom = make(map[string]*Var, 0)
for _, v := range vars {
if v.Type != "" {
custom[v.Type] = v
}
}
if len(custom) == 0 {
return ""
}
var b strings.Builder
for t, v := range custom {
b.WriteString(fmt.Sprintf("--- @alias %s %s\n", t, v.Value.Type().String()))
}
return strings.TrimSpace(b.String())
},
}).
Parse(`--- @meta
-- This file was automatically generated by Awirix.
-- DO NOT EDIT.
{{- define "lib" }}
{{- defineTypes .Vars }}
{{ doc .Description }}
{{ doc "@class" }} {{ .Name }}
{{- range $var := .Vars }}
{{ doc "@field" }} public {{ $var.Name }} {{ vartype $var }} {{ $var.Description }}
{{- end }}
{{- range $lib := .Libs }}
{{ doc "@field" }} public {{ $lib.Name }} {{ $lib.Name }} {{ $lib.Description }}
{{- end }}
local {{ .Name }} = {}
{{ range $class := .Classes }}
{{ doc $class.Description }}
{{ doc "@class" }} {{ $class.Name }}
local {{ $class.Name }} = {}
{{ range $method := $class.Methods }}
{{ doc $method.Description }}
{{- range $generic := $method.Generics }}
{{ doc "@generic" }} {{ $generic }}
{{- end }}
{{- range $param := $method.Params }}
{{ doc "@param" }} {{ $param.Name }} {{ $param.Type }}{{ if $param.Optional }}?{{ end }} {{ $param.Description }}
{{- end }}
{{- range $return := $method.Returns }}
{{ doc "@return" }} {{ $return.Type }}{{ if $return.Optional }}?{{ end }} {{ $return.Name }} {{ $return.Description }}
{{- end }}
function {{ $class.Name }}:{{ $method.Name }}({{ join (params $method.Params) ", " }})
end
{{ end }}
{{- end }}
{{- range $func := .Funcs }}
{{ doc $func.Description }}
{{- range $generic := $func.Generics }}
{{ doc "@generic" }} {{ $generic }}
{{- end }}
{{- range $param := $func.Params }}
{{ doc "@param" }} {{ $param.Name }} {{ $param.Type }}{{ if $param.Optional }}?{{ end }} {{ $param.Description }}
{{- end }}
{{- range $return := $func.Returns }}
{{ doc "@return" }} {{ $return.Type }}{{ if $return.Optional }}?{{ end }} {{ $return.Name }} {{ $return.Description }}
{{- end }}
function {{ $.Name }}.{{ $func.Name }}({{ join (params $func.Params) ", " }})
end
{{ end }}
{{- range $lib := .Libs }}
{{- template "lib" $lib }}
{{- end }}
{{- end }}
{{- template "lib" . }}
return {{ .Name }}`))