Gir + Templates: Generate more Go friendly constructor names
Previously we just added the snake case of the constructor name + the record/class name. This leads to confusing names (old -> new): gtk.NewFromStringBuilder -> gtk.NewBuilderFromString gdkpixbuf.NewFromFilePixbuf -> gdkpixbuf.NewPixbufFromFile Now we just check if it begins with New and if so add the class/record name directly after "New"
- commit
- 562a1aaaabb6eb59c197184719b28b807c3bccd0
- author
- jwijenbergh · · 2024-01-14 14:34 UTC
- parent
- db0ebd3
- signature
- GPG Unverified
1A2298F6C34F96E5The key is unknown
3 files · +25 -12
M internal/gir/pass/pass.go +2 -4
| @@ -109,9 +109,8 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 109 | 109 | fn := rec.FilenameSafe() | |
| 110 | 110 | files = append(files, fn) | |
| 111 | 111 | for i, c := range rec.Constructors { | |
| 112 | - | name := util.SnakeToCamel(c.Name) | |
| 113 | 112 | constructors[i] = types.FuncTemplate{ | |
| 114 | - | Name: name, | |
| 113 | + | Name: util.ConstructorName(c.Name, rec.Name), | |
| 115 | 114 | CName: c.CIdentifier, | |
| 116 | 115 | Doc: c.Doc.StringSafe(), | |
| 117 | 116 | Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws), | |
| @@ -245,10 +244,9 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 245 | 244 | files = append(files, fn) | |
| 246 | 245 | ||
| 247 | 246 | for i, c := range cls.Constructors { | |
| 248 | - | name := util.SnakeToCamel(c.Name) | |
| 249 | 247 | c.ReturnValue.AnyType.Type.Name = cls.Name | |
| 250 | 248 | constructors[i] = types.FuncTemplate{ | |
| 251 | - | Name: name, | |
| 249 | + | Name: util.ConstructorName(c.Name, cls.Name), | |
| 252 | 250 | CName: c.CIdentifier, | |
| 253 | 251 | Doc: c.Doc.StringSafe(), | |
| 254 | 252 | Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws), | |
| @@ -109,9 +109,8 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 109 | fn := rec.FilenameSafe() | 109 | fn := rec.FilenameSafe() | ||
| 110 | files = append(files, fn) | 110 | files = append(files, fn) | ||
| 111 | for i, c := range rec.Constructors { | 111 | for i, c := range rec.Constructors { | ||
| 112 | - | name := util.SnakeToCamel(c.Name) | |||
| 113 | constructors[i] = types.FuncTemplate{ | 112 | constructors[i] = types.FuncTemplate{ | ||
| 114 | - | Name: name, | 113 | + | Name: util.ConstructorName(c.Name, rec.Name), |
| 115 | CName: c.CIdentifier, | 114 | CName: c.CIdentifier, | ||
| 116 | Doc: c.Doc.StringSafe(), | 115 | Doc: c.Doc.StringSafe(), | ||
| 117 | Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws), | 116 | Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws), | ||
| @@ -245,10 +244,9 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 245 | files = append(files, fn) | 244 | files = append(files, fn) | ||
| 246 | 245 | ||||
| 247 | for i, c := range cls.Constructors { | 246 | for i, c := range cls.Constructors { | ||
| 248 | - | name := util.SnakeToCamel(c.Name) | |||
| 249 | c.ReturnValue.AnyType.Type.Name = cls.Name | 247 | c.ReturnValue.AnyType.Type.Name = cls.Name | ||
| 250 | constructors[i] = types.FuncTemplate{ | 248 | constructors[i] = types.FuncTemplate{ | ||
| 251 | - | Name: name, | 249 | + | Name: util.ConstructorName(c.Name, cls.Name), |
| 252 | CName: c.CIdentifier, | 250 | CName: c.CIdentifier, | ||
| 253 | Doc: c.Doc.StringSafe(), | 251 | Doc: c.Doc.StringSafe(), | ||
| 254 | Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws), | 252 | Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws), | ||
M internal/gir/util/util.go +15 -0
| @@ -91,3 +91,18 @@ func ConvertArgsComma(a []string) string { | |||
| 91 | 91 | } | |
| 92 | 92 | return ", " + strings.Join(a, ", ") | |
| 93 | 93 | } | |
| 94 | + | ||
| 95 | + | // ConstructorName returns a Go friendly constructor name given the raw constructor name `name` and the class/record name `outer` | |
| 96 | + | func ConstructorName(name string, outer string) string { | |
| 97 | + | cname := SnakeToCamel(name) | |
| 98 | + | // construct the final constructor name | |
| 99 | + | // for example if we have gtk_builder | |
| 100 | + | // gtk_builder_new_from_file | |
| 101 | + | // cname will be NewFromFile | |
| 102 | + | // we convert it to NewBuilderFromFile | |
| 103 | + | if strings.HasPrefix(cname, "New") { | |
| 104 | + | return "New" + outer + cname[3:] | |
| 105 | + | } | |
| 106 | + | // the default is just a concatenation if the constructor doesn't start with New | |
| 107 | + | return outer + cname | |
| 108 | + | } | |
| @@ -91,3 +91,18 @@ func ConvertArgsComma(a []string) string { | |||||
| 91 | } | 91 | } | ||
| 92 | return ", " + strings.Join(a, ", ") | 92 | return ", " + strings.Join(a, ", ") | ||
| 93 | } | 93 | } | ||
| 94 | + | ||||
| 95 | + | // ConstructorName returns a Go friendly constructor name given the raw constructor name `name` and the class/record name `outer` | |||
| 96 | + | func ConstructorName(name string, outer string) string { | |||
| 97 | + | cname := SnakeToCamel(name) | |||
| 98 | + | // construct the final constructor name | |||
| 99 | + | // for example if we have gtk_builder | |||
| 100 | + | // gtk_builder_new_from_file | |||
| 101 | + | // cname will be NewFromFile | |||
| 102 | + | // we convert it to NewBuilderFromFile | |||
| 103 | + | if strings.HasPrefix(cname, "New") { | |||
| 104 | + | return "New" + outer + cname[3:] | |||
| 105 | + | } | |||
| 106 | + | // the default is just a concatenation if the constructor doesn't start with New | |||
| 107 | + | return outer + cname | |||
| 108 | + | } | |||
M templates/go +8 -8
| @@ -35,13 +35,13 @@ func (x *{{.Name}}) GoPointer() uintptr { | |||
| 35 | 35 | {{$outer := .}} | |
| 36 | 36 | ||
| 37 | 37 | {{range .Constructors -}} | |
| 38 | - | var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |
| 38 | + | var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | 41 | {{.Doc}} | |
| 42 | - | func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |
| 42 | + | func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |
| 43 | 43 | {{.Ret.Preamble $NotGLib}} | |
| 44 | - | {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}}) | |
| 44 | + | {{if .Ret.Value}}cret :={{end}}x{{.Name}}({{conv .Args.API.Call}}) | |
| 45 | 45 | {{.Ret.Fmt $NotGObject}} | |
| 46 | 46 | } | |
| 47 | 47 | {{end}} | |
| @@ -150,13 +150,13 @@ func {{.Name}}NewFromInternalPtr(ptr uintptr) *{{.Name}} { | |||
| 150 | 150 | } | |
| 151 | 151 | ||
| 152 | 152 | {{range .Constructors -}} | |
| 153 | - | var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |
| 153 | + | var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |
| 154 | 154 | ||
| 155 | 155 | ||
| 156 | 156 | {{.Doc}} | |
| 157 | - | func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |
| 157 | + | func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |
| 158 | 158 | {{.Ret.Preamble $NotGLib}} | |
| 159 | - | {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}}) | |
| 159 | + | {{if .Ret.Value}}cret :={{end}}x{{.Name}}({{conv .Args.API.Call}}) | |
| 160 | 160 | {{.Ret.Fmt $NotGObject}} | |
| 161 | 161 | } | |
| 162 | 162 | {{end}} | |
| @@ -240,7 +240,7 @@ func init() { | |||
| 240 | 240 | {{range .Records -}} | |
| 241 | 241 | {{$rec := .}} | |
| 242 | 242 | {{range .Constructors -}} | |
| 243 | - | core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}") | |
| 243 | + | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | |
| 244 | 244 | {{end}} | |
| 245 | 245 | {{range .Receivers -}} | |
| 246 | 246 | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}") | |
| @@ -250,7 +250,7 @@ func init() { | |||
| 250 | 250 | {{range .Classes -}} | |
| 251 | 251 | {{$cls := .}} | |
| 252 | 252 | {{range .Constructors -}} | |
| 253 | - | core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}") | |
| 253 | + | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | |
| 254 | 254 | {{end}} | |
| 255 | 255 | {{range .Receivers -}} | |
| 256 | 256 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | |
| @@ -35,13 +35,13 @@ func (x *{{.Name}}) GoPointer() uintptr { | |||||
| 35 | {{$outer := .}} | 35 | {{$outer := .}} | ||
| 36 | 36 | ||||
| 37 | {{range .Constructors -}} | 37 | {{range .Constructors -}} | ||
| 38 | - | var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | 38 | + | var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} |
| 39 | 39 | ||||
| 40 | 40 | ||||
| 41 | {{.Doc}} | 41 | {{.Doc}} | ||
| 42 | - | func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | 42 | + | func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { |
| 43 | {{.Ret.Preamble $NotGLib}} | 43 | {{.Ret.Preamble $NotGLib}} | ||
| 44 | - | {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}}) | 44 | + | {{if .Ret.Value}}cret :={{end}}x{{.Name}}({{conv .Args.API.Call}}) |
| 45 | {{.Ret.Fmt $NotGObject}} | 45 | {{.Ret.Fmt $NotGObject}} | ||
| 46 | } | 46 | } | ||
| 47 | {{end}} | 47 | {{end}} | ||
| @@ -150,13 +150,13 @@ func {{.Name}}NewFromInternalPtr(ptr uintptr) *{{.Name}} { | |||||
| 150 | } | 150 | } | ||
| 151 | 151 | ||||
| 152 | {{range .Constructors -}} | 152 | {{range .Constructors -}} | ||
| 153 | - | var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | 153 | + | var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} |
| 154 | 154 | ||||
| 155 | 155 | ||||
| 156 | {{.Doc}} | 156 | {{.Doc}} | ||
| 157 | - | func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | 157 | + | func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { |
| 158 | {{.Ret.Preamble $NotGLib}} | 158 | {{.Ret.Preamble $NotGLib}} | ||
| 159 | - | {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}}) | 159 | + | {{if .Ret.Value}}cret :={{end}}x{{.Name}}({{conv .Args.API.Call}}) |
| 160 | {{.Ret.Fmt $NotGObject}} | 160 | {{.Ret.Fmt $NotGObject}} | ||
| 161 | } | 161 | } | ||
| 162 | {{end}} | 162 | {{end}} | ||
| @@ -240,7 +240,7 @@ func init() { | |||||
| 240 | {{range .Records -}} | 240 | {{range .Records -}} | ||
| 241 | {{$rec := .}} | 241 | {{$rec := .}} | ||
| 242 | {{range .Constructors -}} | 242 | {{range .Constructors -}} | ||
| 243 | - | core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}") | 243 | + | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") |
| 244 | {{end}} | 244 | {{end}} | ||
| 245 | {{range .Receivers -}} | 245 | {{range .Receivers -}} | ||
| 246 | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}") | 246 | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}") | ||
| @@ -250,7 +250,7 @@ func init() { | |||||
| 250 | {{range .Classes -}} | 250 | {{range .Classes -}} | ||
| 251 | {{$cls := .}} | 251 | {{$cls := .}} | ||
| 252 | {{range .Constructors -}} | 252 | {{range .Constructors -}} | ||
| 253 | - | core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}") | 253 | + | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") |
| 254 | {{end}} | 254 | {{end}} | ||
| 255 | {{range .Receivers -}} | 255 | {{range .Receivers -}} | ||
| 256 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | 256 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | ||