Hello,
I've been working on a utility to generate constructors for me when I provide it with a path to the file containing the struct and it `Ident`. The idea was that I would just create a ast.Node (FuncDecl) with all the necessary fields but I'm not able to figure out the positioning of the commas.
The function that I'm working on looks something like this:
```Go
func generateConstructor(typeName string, fields ...*ast.Field) *ast.FuncDecl {
var elts []ast.Expr
for _, field := range fields {
elts = append(elts, &ast.KeyValueExpr{
Key: field.Names[0],
Value: field.Names[0],
})
}
return &ast.FuncDecl{
Doc: nil,
Recv: nil,
Name: ast.NewIdent("New" + typeName),
Type: &ast.FuncType{
TypeParams: nil,
Params: &ast.FieldList{
List: fields,
},
Results: nil,
},
Body: &ast.BlockStmt{
Lbrace: 1,
List: []ast.Stmt{
&ast.ReturnStmt{
Results: []ast.Expr{
&ast.UnaryExpr{
Op: token.AND,
X: &ast.CompositeLit{
Type: ast.NewIdent(typeName),
Elts: elts,
Rbrace: 1,
},
},
},
},
},
Rbrace: 2,
},
}
}
```
And when I provide it with a struct like this:
```Go
type Data struct {
something string
data string
dd int
}
```
I get this when I do `format.Node` on the generated node `functionDecl`.
```Go
func NewData(something string,
data string,
dd int) {
return &Data{something: something,
data: data,
dd: dd}
}
```
However, I was thinking that it would look something like this.
```Go
func NewData(something string,
data string,
dd int,
) {
return &Data{something: something,
data: data,
dd: dd,
}
}
```
(wishful thinking :joy:)
Anyways, I tried to figure out how the `token.COMMA` is used in the parser and how the ast is affected but I couldn't figure it out. Maybe it is related to the `Lbrace` and `Rbrace` values which I'm deliberately missing. Any help would be much appreciated :smile: :pray:
PS: I'm not aware of any utility out there so have to roll my own :joy: and learn whilst at it; if people are aware of it, please let me know and I'll inspect their code and figure things out :smile: