detached c57ccf3

Gen: Add constructors for records

commit
c57ccf3e8b0afef533c755ef4475aa570c906ac3
author
Peter Fern · · 2023-12-29 04:02 UTC
committer
Peter Fern · · 2024-01-03 01:44 UTC
parent
4682f32
signature
unsigned
3 files · +47 -3
M internal/gir/pass/pass.go +20 -3
@@ -96,9 +96,20 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
9696 recordLookup := make(map[string]bool)
9797 for _, rec := range ns.Records {
9898 name := util.SnakeToCamel(rec.Name)
99+ constructors := make([]types.FuncTemplate, len(rec.Constructors))
99100 fields := make([]types.RecordField, len(rec.Fields))
100101 fn := rec.FilenameSafe()
101102 files = append(files, fn)
103+ for i, c := range rec.Constructors {
104+ name := util.SnakeToCamel(c.Name)
105+ constructors[i] = types.FuncTemplate{
106+ Name: name,
107+ CName: c.CIdentifier,
108+ Doc: c.Doc.StringSafe(),
109+ Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws),
110+ Ret: c.ReturnValue.Template(ns.Name, "", p.Types, c.Throws),
111+ }
112+ }
102113 for _, f := range rec.Fields {
103114 _type := f.Translate(ns.Name, p.Types)
104115 if _type == "" {
@@ -121,9 +132,10 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
121132 })
122133 }
123134 records[fn] = append(records[fn], types.RecordTemplate{
124- Name: name,
125- Doc: rec.Doc.StringSafe(),
126- Fields: fields,
135+ Name: name,
136+ Doc: rec.Doc.StringSafe(),
137+ Constructors: constructors,
138+ Fields: fields,
127139 })
128140 recordLookup[name] = true
129141 }
@@ -267,6 +279,11 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
267279 methods += 1
268280 }
269281 }
282+ for _, i := range records[fn] {
283+ for range i.Constructors {
284+ methods += 1
285+ }
286+ }
270287 for _, i := range classes[fn] {
271288 for range i.Constructors {
272289 methods += 1
@@ -96,9 +96,20 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
96 recordLookup := make(map[string]bool)96 recordLookup := make(map[string]bool)
97 for _, rec := range ns.Records {97 for _, rec := range ns.Records {
98 name := util.SnakeToCamel(rec.Name)98 name := util.SnakeToCamel(rec.Name)
99+ constructors := make([]types.FuncTemplate, len(rec.Constructors))
99 fields := make([]types.RecordField, len(rec.Fields))100 fields := make([]types.RecordField, len(rec.Fields))
100 fn := rec.FilenameSafe()101 fn := rec.FilenameSafe()
101 files = append(files, fn)102 files = append(files, fn)
103+ for i, c := range rec.Constructors {
104+ name := util.SnakeToCamel(c.Name)
105+ constructors[i] = types.FuncTemplate{
106+ Name: name,
107+ CName: c.CIdentifier,
108+ Doc: c.Doc.StringSafe(),
109+ Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws),
110+ Ret: c.ReturnValue.Template(ns.Name, "", p.Types, c.Throws),
111+ }
112+ }
102 for _, f := range rec.Fields {113 for _, f := range rec.Fields {
103 _type := f.Translate(ns.Name, p.Types)114 _type := f.Translate(ns.Name, p.Types)
104 if _type == "" {115 if _type == "" {
@@ -121,9 +132,10 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
121 })132 })
122 }133 }
123 records[fn] = append(records[fn], types.RecordTemplate{134 records[fn] = append(records[fn], types.RecordTemplate{
124- Name: name,135+ Name: name,
125- Doc: rec.Doc.StringSafe(),136+ Doc: rec.Doc.StringSafe(),
126- Fields: fields,137+ Constructors: constructors,
138+ Fields: fields,
127 })139 })
128 recordLookup[name] = true140 recordLookup[name] = true
129 }141 }
@@ -267,6 +279,11 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
267 methods += 1279 methods += 1
268 }280 }
269 }281 }
282+ for _, i := range records[fn] {
283+ for range i.Constructors {
284+ methods += 1
285+ }
286+ }
270 for _, i := range classes[fn] {287 for _, i := range classes[fn] {
271 for range i.Constructors {288 for range i.Constructors {
272 methods += 1289 methods += 1
M internal/gir/types/template.go +3 -0
@@ -174,6 +174,9 @@ type RecordTemplate struct {
174174 // Doc is the documentation of the alias
175175 Doc string
176176
177+ // Constructors is the slice of functions that create the class struct
178+ Constructors []FuncTemplate
179+
177180 // Fields is the list of record fields
178181 Fields []RecordField
179182 }
@@ -174,6 +174,9 @@ type RecordTemplate struct {
174 // Doc is the documentation of the alias174 // Doc is the documentation of the alias
175 Doc string175 Doc string
176 176
177+ // Constructors is the slice of functions that create the class struct
178+ Constructors []FuncTemplate
179+
177 // Fields is the list of record fields180 // Fields is the list of record fields
178 Fields []RecordField181 Fields []RecordField
179 }182 }
M templates/go +24 -0
@@ -30,6 +30,21 @@ type {{.Name}} struct {
3030 {{.Name}} {{.Type}}
3131 {{end}}
3232 }
33+
34+{{$outer := .}}
35+
36+{{range .Constructors -}}
37+var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
38+
39+
40+{{.Doc}}
41+func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
42+ {{.Ret.Preamble $NotGLib}}
43+ {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}})
44+ {{.Ret.Fmt $NotGObject}}
45+}
46+{{end}}
47+
3348 {{end}}
3449
3550 {{range .Interfaces -}}
@@ -188,6 +203,14 @@ func init() {
188203 {{range .Functions -}}
189204 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
190205 {{end}}
206+
207+ {{range .Records -}}
208+ {{$cls := .}}
209+ {{range .Constructors -}}
210+ core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}")
211+ {{end}}
212+ {{end}}
213+
191214 {{range .Classes -}}
192215 {{$cls := .}}
193216 {{range .Constructors -}}
@@ -197,6 +220,7 @@ func init() {
197220 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")
198221 {{end}}
199222 {{end}}
223+
200224 {{range .Interfaces -}}
201225 {{range .Methods -}}
202226 core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}")
@@ -30,6 +30,21 @@ type {{.Name}} struct {
30 {{.Name}} {{.Type}}30 {{.Name}} {{.Type}}
31 {{end}}31 {{end}}
32 }32 }
33+
34+{{$outer := .}}
35+
36+{{range .Constructors -}}
37+var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
38+
39+
40+{{.Doc}}
41+func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
42+ {{.Ret.Preamble $NotGLib}}
43+ {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}})
44+ {{.Ret.Fmt $NotGObject}}
45+}
46+{{end}}
47+
33 {{end}}48 {{end}}
34 49
35 {{range .Interfaces -}}50 {{range .Interfaces -}}
@@ -188,6 +203,14 @@ func init() {
188 {{range .Functions -}}203 {{range .Functions -}}
189 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")204 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
190 {{end}}205 {{end}}
206+
207+ {{range .Records -}}
208+ {{$cls := .}}
209+ {{range .Constructors -}}
210+ core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}")
211+ {{end}}
212+ {{end}}
213+
191 {{range .Classes -}}214 {{range .Classes -}}
192 {{$cls := .}}215 {{$cls := .}}
193 {{range .Constructors -}}216 {{range .Constructors -}}
@@ -197,6 +220,7 @@ func init() {
197 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")220 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")
198 {{end}}221 {{end}}
199 {{end}}222 {{end}}
223+
200 {{range .Interfaces -}}224 {{range .Interfaces -}}
201 {{range .Methods -}}225 {{range .Methods -}}
202 core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}")226 core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}")

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close