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 | |||
| 96 | 96 | recordLookup := make(map[string]bool) | |
| 97 | 97 | for _, rec := range ns.Records { | |
| 98 | 98 | name := util.SnakeToCamel(rec.Name) | |
| 99 | + | constructors := make([]types.FuncTemplate, len(rec.Constructors)) | |
| 99 | 100 | fields := make([]types.RecordField, len(rec.Fields)) | |
| 100 | 101 | fn := rec.FilenameSafe() | |
| 101 | 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 | 113 | for _, f := range rec.Fields { | |
| 103 | 114 | _type := f.Translate(ns.Name, p.Types) | |
| 104 | 115 | if _type == "" { | |
| @@ -121,9 +132,10 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 121 | 132 | }) | |
| 122 | 133 | } | |
| 123 | 134 | 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, | |
| 127 | 139 | }) | |
| 128 | 140 | recordLookup[name] = true | |
| 129 | 141 | } | |
| @@ -267,6 +279,11 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 267 | 279 | methods += 1 | |
| 268 | 280 | } | |
| 269 | 281 | } | |
| 282 | + | for _, i := range records[fn] { | |
| 283 | + | for range i.Constructors { | |
| 284 | + | methods += 1 | |
| 285 | + | } | |
| 286 | + | } | |
| 270 | 287 | for _, i := range classes[fn] { | |
| 271 | 288 | for range i.Constructors { | |
| 272 | 289 | 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] = true | 140 | recordLookup[name] = true | ||
| 129 | } | 141 | } | ||
| @@ -267,6 +279,11 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 267 | methods += 1 | 279 | 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 += 1 | 289 | methods += 1 | ||
M internal/gir/types/template.go +3 -0
| @@ -174,6 +174,9 @@ type RecordTemplate struct { | |||
| 174 | 174 | // Doc is the documentation of the alias | |
| 175 | 175 | Doc string | |
| 176 | 176 | ||
| 177 | + | // Constructors is the slice of functions that create the class struct | |
| 178 | + | Constructors []FuncTemplate | |
| 179 | + | ||
| 177 | 180 | // Fields is the list of record fields | |
| 178 | 181 | Fields []RecordField | |
| 179 | 182 | } | |
| @@ -174,6 +174,9 @@ type RecordTemplate struct { | |||||
| 174 | // Doc is the documentation of the alias | 174 | // Doc is the documentation of the alias | ||
| 175 | Doc string | 175 | 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 fields | 180 | // Fields is the list of record fields | ||
| 178 | Fields []RecordField | 181 | Fields []RecordField | ||
| 179 | } | 182 | } | ||
M templates/go +24 -0
| @@ -30,6 +30,21 @@ type {{.Name}} struct { | |||
| 30 | 30 | {{.Name}} {{.Type}} | |
| 31 | 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 | 48 | {{end}} | |
| 34 | 49 | ||
| 35 | 50 | {{range .Interfaces -}} | |
| @@ -188,6 +203,14 @@ func init() { | |||
| 188 | 203 | {{range .Functions -}} | |
| 189 | 204 | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | |
| 190 | 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 | 214 | {{range .Classes -}} | |
| 192 | 215 | {{$cls := .}} | |
| 193 | 216 | {{range .Constructors -}} | |
| @@ -197,6 +220,7 @@ func init() { | |||
| 197 | 220 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | |
| 198 | 221 | {{end}} | |
| 199 | 222 | {{end}} | |
| 223 | + | ||
| 200 | 224 | {{range .Interfaces -}} | |
| 201 | 225 | {{range .Methods -}} | |
| 202 | 226 | 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}}") | ||