func (p *parser) parseParamDecl(n *parse.Node, scope *ast.Scope) *ast.Field { field := ast.Field{} if n.Child(0).Is(type_) { field.Type = p.parseType(n.Child(0)) } else { field.Names = p.parseIdentList(n.Child(0)) if n.Child(1).Is(type_) { field.Type = p.parseType(n.Child(1)) } else { // TODO ... field.Type = p.parseType(n.Child(2)) } } p.declare(field, nil, scope, ast.Var, field.Names...) if field.Type != nil { p.resolve(field.Type) } return &field }
// Struct creates a struct{} expression. The arguments are a series // of name/type/tag tuples. Name must be of type *ast.Ident, type // must be of type ast.Expr, and tag must be of type *ast.BasicLit, // The number of arguments must be a multiple of 3, or a run-time // panic will occur. func Struct(args ...ast.Expr) *ast.StructType { fields := new(ast.FieldList) if len(args)%3 != 0 { panic("Number of args to FieldList must be a multiple of 3, got " + strconv.Itoa(len(args))) } for i := 0; i < len(args); i += 3 { var field ast.Field name, typ, tag := args[i], args[i+1], args[i+2] if name != nil { field.Names = []*ast.Ident{name.(*ast.Ident)} } if typ != nil { field.Type = typ } if tag != nil { field.Tag = tag.(*ast.BasicLit) } fields.List = append(fields.List, &field) } return &ast.StructType{Fields: fields} }
func mockField(idx int, f *ast.Field) *ast.Field { if f.Names == nil { if idx < 0 { return f } // Edit the field directly to ensure the same name is used in the mock // struct. f.Names = []*ast.Ident{{Name: fmt.Sprintf(inputFmt, idx)}} return f } // Here, we want a copy, so that we can use altered names without affecting // field names in the mock struct. newField := &ast.Field{Type: f.Type} for _, n := range f.Names { name := n.Name if name == receiverName { name += "_" } newField.Names = append(newField.Names, &ast.Ident{Name: name}) } return newField }