Exemplo 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),
			},
		},
	})
}
Exemplo n.º 2
0
func (g *stubGenerator) getNormalizedResults(context *resolution.LocatorContext, funcType *ast.FuncType) ([]*ast.Field, error) {
	normalizedResults := []*ast.Field{}
	resultIndex := 1
	for result := range util.EachFieldInFieldList(funcType.Results) {
		count := util.FieldTypeReuseCount(result)
		for i := 0; i < count; i++ {
			fieldName := fmt.Sprintf("result%d", resultIndex)
			fieldType, err := g.resolver.ResolveType(context, result.Type)
			if err != nil {
				return nil, err
			}
			normalizedResult := util.CreateField(fieldName, fieldType)
			normalizedResults = append(normalizedResults, normalizedResult)
			resultIndex++
		}
	}
	return normalizedResults, nil
}
Exemplo n.º 3
0
func (g *stubGenerator) getNormalizedParams(context *resolution.LocatorContext, funcType *ast.FuncType) ([]*ast.Field, error) {
	normalizedParams := []*ast.Field{}
	paramIndex := 1
	for param := range util.EachFieldInFieldList(funcType.Params) {
		count := util.FieldTypeReuseCount(param)
		for i := 0; i < count; i++ {
			fieldName := fmt.Sprintf("arg%d", paramIndex)
			fieldType, err := g.resolver.ResolveType(context, param.Type)
			if err != nil {
				return nil, err
			}
			normalizedParam := util.CreateField(fieldName, fieldType)
			normalizedParams = append(normalizedParams, normalizedParam)
			paramIndex++
		}
	}
	return normalizedParams, nil
}
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()
}
func (b *MethodMutexFieldBuilder) Build() *ast.Field {
	return util.CreateField(b.fieldName, b.mutexType)
}
Exemplo n.º 6
0
func (b *GUIDFieldBuilder) Build() *ast.Field {
	return util.CreateField(b.fieldName, ast.NewIdent("int"))
}