Exemplo n.º 1
0
func (r *Resolver) resolveFuncType(context *resolution.LocatorContext, astType *ast.FuncType) (ast.Expr, error) {
	var err error
	for param := range util.EachFieldInFieldList(astType.Params) {
		param.Type, err = r.ResolveType(context, param.Type)
		if err != nil {
			return nil, err
		}
	}
	for result := range util.EachFieldInFieldList(astType.Results) {
		result.Type, err = r.ResolveType(context, result.Type)
		if err != nil {
			return nil, err
		}
	}
	return astType, nil
}
Exemplo n.º 2
0
func (r *Resolver) resolveInterfaceType(context *resolution.LocatorContext, astType *ast.InterfaceType) (ast.Expr, error) {
	var err error
	for field := range util.EachFieldInFieldList(astType.Methods) {
		field.Type, err = r.ResolveType(context, field.Type)
		if err != nil {
			return nil, err
		}
	}
	return astType, nil
}
Exemplo n.º 3
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.º 4
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
}
Exemplo n.º 5
0
func (g *stubGenerator) ProcessInterface(discovery resolution.TypeDiscovery) error {
	context := resolution.NewASTFileLocatorContext(discovery.File, discovery.Location)
	iFaceType, isIFace := discovery.Spec.Type.(*ast.InterfaceType)
	if !isIFace {
		return errors.New(fmt.Sprintf("Type '%s' in '%s' is not interface!", discovery.Spec.Name.String(), discovery.Location))
	}
	for field := range util.EachFieldInFieldList(iFaceType.Methods) {
		switch t := field.Type.(type) {
		case *ast.FuncType:
			g.processMethod(context, field.Names[0].String(), t)
		case *ast.Ident:
			g.processSubInterfaceIdent(context, t)
		case *ast.SelectorExpr:
			g.processSubInterfaceSelector(context, t)
		default:
			return errors.New("Unknown statement in interface declaration.")
		}
	}
	return nil
}