detached 0299370

GIR: Generate GLib get-type

commit
02993709940347e959a44672206454d061e62cda
author
Jeroen Wijenbergh · · 2024-12-18 10:25 UTC
committer
Jeroen Wijenbergh · · 2025-04-07 12:23 UTC
parent
99a6c14
signature
unsigned
6 files · +80 -10
M internal/gir/pass/pass.go +2 -0
@@ -163,6 +163,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
163163 Constructors: constructors,
164164 Receivers: receivers,
165165 Fields: fields,
166+ TypeGetter: rec.GLibGetType,
166167 })
167168 recordLookup[name] = true
168169 }
@@ -298,6 +299,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
298299 Interfaces: interfaces,
299300 Functions: functions,
300301 Signals: signals,
302+ TypeGetter: cls.GLibGetType,
301303 })
302304 }
303305
@@ -163,6 +163,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
163 Constructors: constructors,163 Constructors: constructors,
164 Receivers: receivers,164 Receivers: receivers,
165 Fields: fields,165 Fields: fields,
166+ TypeGetter: rec.GLibGetType,
166 })167 })
167 recordLookup[name] = true168 recordLookup[name] = true
168 }169 }
@@ -298,6 +299,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
298 Interfaces: interfaces,299 Interfaces: interfaces,
299 Functions: functions,300 Functions: functions,
300 Signals: signals,301 Signals: signals,
302+ TypeGetter: cls.GLibGetType,
301 })303 })
302 }304 }
303 305
M internal/gir/types/interface.go +4 -3
@@ -35,9 +35,10 @@ func ConvertInterface(currns string, ins string, inter Interface, implemented ma
3535 }
3636 name := util.SnakeToCamel(inter.Name)
3737 return InterfaceTemplate{
38- Name: name,
39- Doc: inter.Doc.StringSafe(),
40- Methods: methods,
38+ Name: name,
39+ Doc: inter.Doc.StringSafe(),
40+ Methods: methods,
41+ TypeGetter: inter.GLibGetType,
4142 }
4243 }
4344
@@ -35,9 +35,10 @@ func ConvertInterface(currns string, ins string, inter Interface, implemented ma
35 }35 }
36 name := util.SnakeToCamel(inter.Name)36 name := util.SnakeToCamel(inter.Name)
37 return InterfaceTemplate{37 return InterfaceTemplate{
38- Name: name,38+ Name: name,
39- Doc: inter.Doc.StringSafe(),39+ Doc: inter.Doc.StringSafe(),
40- Methods: methods,40+ Methods: methods,
41+ TypeGetter: inter.GLibGetType,
41 }42 }
42 }43 }
43 44
M internal/gir/types/template.go +12 -0
@@ -162,6 +162,9 @@ type AliasTemplate struct {
162162
163163 // Value is the value for the alias as a Go type
164164 Value string
165+
166+ // TypeGetter is the function to get the GLib type
167+ TypeGetter string
165168 }
166169
167170 type RecordField struct {
@@ -187,6 +190,9 @@ type RecordTemplate struct {
187190
188191 // Fields is the list of record fields
189192 Fields []RecordField
193+
194+ // TypeGetter is the function to get the GLib type
195+ TypeGetter string
190196 }
191197
192198 type enumValues struct {
@@ -205,6 +211,8 @@ type EnumTemplate struct {
205211 Doc string
206212 // Values are the list of values for the enumeration
207213 Values []enumValues
214+ // TypeGetter is the function to get the GLib type
215+ TypeGetter string
208216 }
209217
210218 type ConstantTemplate struct {
@@ -368,6 +376,8 @@ type ClassTemplate struct {
368376 Functions []FuncTemplate
369377 // Signals are helpers for ConnectX receivers
370378 Signals []SignalsTemplate
379+ // TypeGetter is the function to get the GLib type
380+ TypeGetter string
371381 }
372382
373383 type InterfaceTemplate struct {
@@ -375,6 +385,8 @@ type InterfaceTemplate struct {
375385 Name string
376386 // Methods is the methods that this interface defines
377387 Methods []InterfaceFuncTemplate
388+ // TypeGetter is the function to get the GLib type
389+ TypeGetter string
378390 }
379391
380392 type TemplateArg struct {
@@ -162,6 +162,9 @@ type AliasTemplate struct {
162 162
163 // Value is the value for the alias as a Go type163 // Value is the value for the alias as a Go type
164 Value string164 Value string
165+
166+ // TypeGetter is the function to get the GLib type
167+ TypeGetter string
165 }168 }
166 169
167 type RecordField struct {170 type RecordField struct {
@@ -187,6 +190,9 @@ type RecordTemplate struct {
187 190
188 // Fields is the list of record fields191 // Fields is the list of record fields
189 Fields []RecordField192 Fields []RecordField
193+
194+ // TypeGetter is the function to get the GLib type
195+ TypeGetter string
190 }196 }
191 197
192 type enumValues struct {198 type enumValues struct {
@@ -205,6 +211,8 @@ type EnumTemplate struct {
205 Doc string211 Doc string
206 // Values are the list of values for the enumeration212 // Values are the list of values for the enumeration
207 Values []enumValues213 Values []enumValues
214+ // TypeGetter is the function to get the GLib type
215+ TypeGetter string
208 }216 }
209 217
210 type ConstantTemplate struct {218 type ConstantTemplate struct {
@@ -368,6 +376,8 @@ type ClassTemplate struct {
368 Functions []FuncTemplate376 Functions []FuncTemplate
369 // Signals are helpers for ConnectX receivers377 // Signals are helpers for ConnectX receivers
370 Signals []SignalsTemplate378 Signals []SignalsTemplate
379+ // TypeGetter is the function to get the GLib type
380+ TypeGetter string
371 }381 }
372 382
373 type InterfaceTemplate struct {383 type InterfaceTemplate struct {
@@ -375,6 +385,8 @@ type InterfaceTemplate struct {
375 Name string385 Name string
376 // Methods is the methods that this interface defines386 // Methods is the methods that this interface defines
377 Methods []InterfaceFuncTemplate387 Methods []InterfaceFuncTemplate
388+ // TypeGetter is the function to get the GLib type
389+ TypeGetter string
378 }390 }
379 391
380 type TemplateArg struct {392 type TemplateArg struct {
M internal/gir/types/types.go +8 -6
@@ -204,9 +204,10 @@ func (b *Bitfield) Template(ns string) EnumTemplate {
204204 }
205205 }
206206 return EnumTemplate{
207- Name: util.SnakeToCamel(b.Name),
208- Doc: b.Doc.StringSafe(),
209- Values: els,
207+ Name: util.SnakeToCamel(b.Name),
208+ Doc: b.Doc.StringSafe(),
209+ Values: els,
210+ TypeGetter: b.GLibGetType,
210211 }
211212 }
212213
@@ -474,9 +475,10 @@ func (e *Enum) Template(ns string) EnumTemplate {
474475 }
475476 }
476477 return EnumTemplate{
477- Name: util.SnakeToCamel(e.Name),
478- Doc: e.Doc.StringSafe(),
479- Values: els,
478+ Name: util.SnakeToCamel(e.Name),
479+ Doc: e.Doc.StringSafe(),
480+ Values: els,
481+ TypeGetter: e.GLibGetType,
480482 }
481483 }
482484
@@ -204,9 +204,10 @@ func (b *Bitfield) Template(ns string) EnumTemplate {
204 }204 }
205 }205 }
206 return EnumTemplate{206 return EnumTemplate{
207- Name: util.SnakeToCamel(b.Name),207+ Name: util.SnakeToCamel(b.Name),
208- Doc: b.Doc.StringSafe(),208+ Doc: b.Doc.StringSafe(),
209- Values: els,209+ Values: els,
210+ TypeGetter: b.GLibGetType,
210 }211 }
211 }212 }
212 213
@@ -474,9 +475,10 @@ func (e *Enum) Template(ns string) EnumTemplate {
474 }475 }
475 }476 }
476 return EnumTemplate{477 return EnumTemplate{
477- Name: util.SnakeToCamel(e.Name),478+ Name: util.SnakeToCamel(e.Name),
478- Doc: e.Doc.StringSafe(),479+ Doc: e.Doc.StringSafe(),
479- Values: els,480+ Values: els,
481+ TypeGetter: e.GLibGetType,
480 }482 }
481 }483 }
482 484
M internal/gir/util/util.go +1 -1
@@ -28,7 +28,7 @@ func StarsInFront(str string, n int) string {
2828 e := strings.Index(str, "]")
2929 stars := strings.Repeat("*", n)
3030 if b == 0 && e != -1 {
31- return str[b:e+1] + stars + str[e+1:len(str)]
31+ return str[b:e+1] + stars + str[e+1:]
3232 }
3333 return stars + str
3434 }
@@ -28,7 +28,7 @@ func StarsInFront(str string, n int) string {
28 e := strings.Index(str, "]")28 e := strings.Index(str, "]")
29 stars := strings.Repeat("*", n)29 stars := strings.Repeat("*", n)
30 if b == 0 && e != -1 {30 if b == 0 && e != -1 {
31- return str[b:e+1] + stars + str[e+1:len(str)]31+ return str[b:e+1] + stars + str[e+1:]
32 }32 }
33 return stars + str33 return stars + str
34 }34 }
M templates/go +53 -0
@@ -28,6 +28,13 @@ type {{.Name}} struct {
2828 {{end}}
2929 }
3030
31+{{if .TypeGetter}}
32+var x{{.Name}}GLibType func() types.GType
33+func {{.Name}}GLibType() types.GType {
34+ return x{{.Name}}GLibType()
35+}
36+{{end}}
37+
3138 func (x *{{.Name}}) GoPointer() uintptr {
3239 return uintptr(unsafe.Pointer(x))
3340 }
@@ -69,6 +76,14 @@ type {{.Name}} interface {
6976 {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Value}}
7077 {{end}}
7178 }
79+
80+{{if .TypeGetter}}
81+var x{{.Name}}GLibType func() types.GType
82+func {{.Name}}GLibType() types.GType {
83+ return x{{.Name}}GLibType()
84+}
85+{{end}}
86+
7287 type {{.Name}}Base struct {
7388 Ptr uintptr
7489 }
@@ -99,6 +114,12 @@ var {{.Namespace}}X{{.FullName}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret
99114 {{range .Aliases -}}
100115 {{.Doc}}
101116 type {{.Name}} = {{.Value}}
117+{{if .TypeGetter}}
118+var x{{.Name}}GLibType func() types.GType
119+func {{.Name}}GLibType() types.GType {
120+ return x{{.Name}}GLibType()
121+}
122+{{end}}
102123 {{end}}
103124
104125 {{if .Constants -}}
@@ -113,6 +134,12 @@ const (
113134 {{range .Enums -}}
114135 {{.Doc}}
115136 type {{.Name}} int
137+{{if .TypeGetter}}
138+var x{{.Name}}GLibType func() types.GType
139+func {{.Name}}GLibType() types.GType {
140+ return x{{.Name}}GLibType()
141+}
142+{{end}}
116143 const (
117144 {{$outer := .}}
118145 {{range .Values -}}
@@ -142,6 +169,13 @@ type {{.Name}} struct {
142169 {{end}}
143170 }
144171
172+{{if .TypeGetter -}}
173+var x{{.Name}}GLibType func() types.GType
174+func {{.Name}}GLibType() types.GType {
175+ return x{{.Name}}GLibType()
176+}
177+{{end}}
178+
145179 {{$outer := .}}
146180 func {{.Name}}NewFromInternalPtr(ptr uintptr) *{{.Name}} {
147181 cls := &{{.Name}}{}
@@ -241,12 +275,25 @@ func init() {
241275 if err != nil {
242276 panic(err)
243277 }
278+ {{range .Aliases -}}
279+ {{if .TypeGetter -}}
280+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
281+ {{end}}
282+ {{end}}
283+ {{range .Enums -}}
284+ {{if .TypeGetter -}}
285+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
286+ {{end}}
287+ {{end}}
244288 {{range .Functions -}}
245289 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
246290 {{end}}
247291
248292 {{range .Records -}}
249293 {{$rec := .}}
294+ {{if .TypeGetter -}}
295+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
296+ {{end}}
250297 {{range .Constructors -}}
251298 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
252299 {{end}}
@@ -257,6 +304,9 @@ func init() {
257304
258305 {{range .Classes -}}
259306 {{$cls := .}}
307+ {{if .TypeGetter -}}
308+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
309+ {{end}}
260310 {{range .Constructors -}}
261311 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
262312 {{end}}
@@ -269,6 +319,9 @@ func init() {
269319 {{end}}
270320
271321 {{range .Interfaces -}}
322+ {{if .TypeGetter -}}
323+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
324+ {{end}}
272325 {{range .Methods -}}
273326 core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}")
274327 {{end}}
@@ -28,6 +28,13 @@ type {{.Name}} struct {
28 {{end}}28 {{end}}
29 }29 }
30 30
31+{{if .TypeGetter}}
32+var x{{.Name}}GLibType func() types.GType
33+func {{.Name}}GLibType() types.GType {
34+ return x{{.Name}}GLibType()
35+}
36+{{end}}
37+
31 func (x *{{.Name}}) GoPointer() uintptr {38 func (x *{{.Name}}) GoPointer() uintptr {
32 return uintptr(unsafe.Pointer(x))39 return uintptr(unsafe.Pointer(x))
33 }40 }
@@ -69,6 +76,14 @@ type {{.Name}} interface {
69 {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Value}}76 {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Value}}
70 {{end}}77 {{end}}
71 }78 }
79+
80+{{if .TypeGetter}}
81+var x{{.Name}}GLibType func() types.GType
82+func {{.Name}}GLibType() types.GType {
83+ return x{{.Name}}GLibType()
84+}
85+{{end}}
86+
72 type {{.Name}}Base struct {87 type {{.Name}}Base struct {
73 Ptr uintptr88 Ptr uintptr
74 }89 }
@@ -99,6 +114,12 @@ var {{.Namespace}}X{{.FullName}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret
99 {{range .Aliases -}}114 {{range .Aliases -}}
100 {{.Doc}}115 {{.Doc}}
101 type {{.Name}} = {{.Value}}116 type {{.Name}} = {{.Value}}
117+{{if .TypeGetter}}
118+var x{{.Name}}GLibType func() types.GType
119+func {{.Name}}GLibType() types.GType {
120+ return x{{.Name}}GLibType()
121+}
122+{{end}}
102 {{end}}123 {{end}}
103 124
104 {{if .Constants -}}125 {{if .Constants -}}
@@ -113,6 +134,12 @@ const (
113 {{range .Enums -}}134 {{range .Enums -}}
114 {{.Doc}}135 {{.Doc}}
115 type {{.Name}} int136 type {{.Name}} int
137+{{if .TypeGetter}}
138+var x{{.Name}}GLibType func() types.GType
139+func {{.Name}}GLibType() types.GType {
140+ return x{{.Name}}GLibType()
141+}
142+{{end}}
116 const (143 const (
117 {{$outer := .}}144 {{$outer := .}}
118 {{range .Values -}}145 {{range .Values -}}
@@ -142,6 +169,13 @@ type {{.Name}} struct {
142 {{end}}169 {{end}}
143 }170 }
144 171
172+{{if .TypeGetter -}}
173+var x{{.Name}}GLibType func() types.GType
174+func {{.Name}}GLibType() types.GType {
175+ return x{{.Name}}GLibType()
176+}
177+{{end}}
178+
145 {{$outer := .}}179 {{$outer := .}}
146 func {{.Name}}NewFromInternalPtr(ptr uintptr) *{{.Name}} {180 func {{.Name}}NewFromInternalPtr(ptr uintptr) *{{.Name}} {
147 cls := &{{.Name}}{}181 cls := &{{.Name}}{}
@@ -241,12 +275,25 @@ func init() {
241 if err != nil {275 if err != nil {
242 panic(err)276 panic(err)
243 }277 }
278+ {{range .Aliases -}}
279+ {{if .TypeGetter -}}
280+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
281+ {{end}}
282+ {{end}}
283+ {{range .Enums -}}
284+ {{if .TypeGetter -}}
285+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
286+ {{end}}
287+ {{end}}
244 {{range .Functions -}}288 {{range .Functions -}}
245 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")289 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
246 {{end}}290 {{end}}
247 291
248 {{range .Records -}}292 {{range .Records -}}
249 {{$rec := .}}293 {{$rec := .}}
294+ {{if .TypeGetter -}}
295+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
296+ {{end}}
250 {{range .Constructors -}}297 {{range .Constructors -}}
251 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")298 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
252 {{end}}299 {{end}}
@@ -257,6 +304,9 @@ func init() {
257 304
258 {{range .Classes -}}305 {{range .Classes -}}
259 {{$cls := .}}306 {{$cls := .}}
307+ {{if .TypeGetter -}}
308+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
309+ {{end}}
260 {{range .Constructors -}}310 {{range .Constructors -}}
261 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")311 core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
262 {{end}}312 {{end}}
@@ -269,6 +319,9 @@ func init() {
269 {{end}}319 {{end}}
270 320
271 {{range .Interfaces -}}321 {{range .Interfaces -}}
322+ {{if .TypeGetter -}}
323+ core.PuregoSafeRegister(&x{{.Name}}GLibType, lib, "{{.TypeGetter}}")
324+ {{end}}
272 {{range .Methods -}}325 {{range .Methods -}}
273 core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}")326 core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}")
274 {{end}}327 {{end}}

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