Ejemplo n.º 1
0
func (b *MethodArgsFieldBuilder) Build() *ast.Field {
	return util.CreateField(b.fieldName, &ast.ArrayType{
		Elt: &ast.StructType{
			Fields: &ast.FieldList{
				List: util.FieldsWithoutEllipsis(b.params),
			},
		},
	})
}
Ejemplo n.º 2
0
func (b *ArgsMethodBuilder) Build() ast.Decl {
	mutexLockBuilder := NewMutexActionBuilder()
	mutexLockBuilder.SetMutexFieldSelector(b.mutexFieldSelector)
	mutexLockBuilder.SetAction("RLock")

	mutexUnlockBuilder := NewMutexActionBuilder()
	mutexUnlockBuilder.SetMutexFieldSelector(b.mutexFieldSelector)
	mutexUnlockBuilder.SetAction("RUnlock")
	mutexUnlockBuilder.SetDeferred(true)

	b.methodBuilder.SetType(&ast.FuncType{
		Params: &ast.FieldList{
			List: []*ast.Field{
				util.CreateField("index", ast.NewIdent("int")),
			},
		},
		Results: &ast.FieldList{
			List: util.FieldsWithoutEllipsis(util.FieldsAsAnonymous(b.params)),
		},
	})
	b.methodBuilder.AddStatementBuilder(mutexLockBuilder)
	b.methodBuilder.AddStatementBuilder(mutexUnlockBuilder)

	results := []ast.Expr{}
	for _, param := range b.params {
		results = append(results, &ast.SelectorExpr{
			X: &ast.IndexExpr{
				X:     b.argsFieldSelector,
				Index: ast.NewIdent("index"),
			},
			Sel: ast.NewIdent(param.Names[0].String()),
		})
	}
	b.methodBuilder.AddStatementBuilder(StatementToBuilder(&ast.ReturnStmt{
		Results: results,
	}))
	return b.methodBuilder.Build()
}
Ejemplo n.º 3
0
func (b *StubMethodBuilder) Build() ast.Decl {
	mutexLockBuilder := NewMutexActionBuilder()
	mutexLockBuilder.SetMutexFieldSelector(b.mutexFieldSelector)
	mutexLockBuilder.SetAction("Lock")

	mutexUnlockBuilder := NewMutexActionBuilder()
	mutexUnlockBuilder.SetMutexFieldSelector(b.mutexFieldSelector)
	mutexUnlockBuilder.SetAction("Unlock")
	mutexUnlockBuilder.SetDeferred(true)

	b.methodBuilder.SetType(&ast.FuncType{
		Params: &ast.FieldList{
			List: b.params,
		},
		Results: &ast.FieldList{
			List: util.FieldsAsAnonymous(b.results),
		},
	})
	b.methodBuilder.AddStatementBuilder(mutexLockBuilder)
	b.methodBuilder.AddStatementBuilder(mutexUnlockBuilder)

	paramSelectors := []ast.Expr{}
	for _, param := range b.params {
		paramSelectors = append(paramSelectors, ast.NewIdent(param.Names[0].String()))
	}

	b.methodBuilder.AddStatementBuilder(StatementToBuilder(&ast.AssignStmt{
		Lhs: []ast.Expr{
			b.argsFieldSelector,
		},
		Tok: token.ASSIGN,
		Rhs: []ast.Expr{
			&ast.CallExpr{
				Fun: ast.NewIdent("append"),
				Args: []ast.Expr{
					b.argsFieldSelector,
					&ast.CompositeLit{
						Type: &ast.StructType{
							Fields: &ast.FieldList{
								List: util.FieldsWithoutEllipsis(b.params),
							},
						},
						Elts: paramSelectors,
					},
				},
			},
		},
	}))

	hasEllipsis := false
	if parCount := len(b.params); parCount > 0 {
		if _, ok := b.params[parCount-1].Type.(*ast.Ellipsis); ok {
			hasEllipsis = true
		}
	}

	b.methodBuilder.AddStatementBuilder(StatementToBuilder(&ast.IfStmt{
		Cond: &ast.BinaryExpr{
			X:  b.stubFieldSelector,
			Op: token.NEQ,
			Y:  ast.NewIdent("nil"),
		},
		Body: b.buildCallStubMethodCode(paramSelectors, hasEllipsis),
		Else: b.buildReturnReturnsCode(),
	}))

	return b.methodBuilder.Build()
}