Skip to content

Commit 0bf4e64

Browse files
authored
Merge pull request #3 from devalexandre/feat/update-performance
update
2 parents 40996e0 + 875abbf commit 0bf4e64

2 files changed

Lines changed: 39 additions & 23 deletions

File tree

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ jobs:
2020
- name: golangci-lint
2121
uses: golangci/golangci-lint-action@v6
2222
with:
23-
version: v1.59
23+
version: v1.63.4
24+
args: --verbose # Added verbose logging

v1/pipe.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,71 @@
11
package v1
22

33
import (
4-
"errors"
4+
"fmt"
55
"reflect"
66
)
77

8-
// Pipeline type.
9-
type Pipeline func(...interface{}) (interface{}, error)
8+
// Pipeline define um pipeline que aceita argumentos iniciais e retorna um resultado ou erro.
9+
type Pipeline func(args ...interface{}) (interface{}, error)
1010

11-
// Pipe creates a pipeline from a series of functions.
11+
// Pipe cria um pipeline a partir de uma série de funções.
12+
// Cada função deve ter uma assinatura compatível com o encadeamento:
13+
// - Seus parâmetros serão preenchidos com os resultados da função anterior.
14+
// - Se uma função retornar um error não-nulo, o pipeline é interrompido.
1215
func Pipe(fs ...interface{}) Pipeline {
16+
// Valida se todos os elementos são funções.
17+
for i, f := range fs {
18+
if reflect.TypeOf(f).Kind() != reflect.Func {
19+
panic(fmt.Sprintf("elemento na posição %d não é uma função", i))
20+
}
21+
}
22+
23+
// Obter o tipo que representa o interface error (para evitar repetir esse cálculo).
24+
errorType := reflect.TypeOf((*error)(nil)).Elem()
25+
1326
return func(initialArgs ...interface{}) (interface{}, error) {
1427
currentArgs := initialArgs
15-
for _, f := range fs {
28+
29+
// Processa cada função do pipeline.
30+
for idx, f := range fs {
1631
fnVal := reflect.ValueOf(f)
1732
fnType := fnVal.Type()
1833
numIn := fnType.NumIn()
1934

20-
// Prepare the input arguments for the current function.
35+
// Prepara os argumentos para a função atual.
2136
in := make([]reflect.Value, numIn)
22-
for i := range numIn {
37+
for i := 0; i < numIn; i++ {
2338
if i < len(currentArgs) {
2439
in[i] = reflect.ValueOf(currentArgs[i])
25-
} else if i < len(initialArgs) { // Allow passing manual arguments if not enough currentArgs.
40+
} else if i < len(initialArgs) { // fallback para os argumentos iniciais, se necessário
2641
in[i] = reflect.ValueOf(initialArgs[i])
2742
} else {
28-
// If there are not enough arguments to pass to the function, return an error.
29-
return nil, errors.New("not enough arguments to pass to function")
43+
return nil, fmt.Errorf("argumentos insuficientes para a função na posição %d", idx)
3044
}
3145
}
3246

33-
// Call the current function in the pipeline.
47+
// Chama a função e obtém os resultados.
3448
results := fnVal.Call(in)
3549

36-
// Assume the last function call results will be used as next input.
37-
currentArgs = nil // Reset currentArgs for next function.
38-
for _, result := range results {
39-
if result.Type().Implements(reflect.TypeOf((*error)(nil)).Elem()) {
40-
if !result.IsNil() { // If the result is an error, return it.
41-
return nil, result.Interface().(error)
50+
// Prepara os argumentos para a próxima função, resetando currentArgs.
51+
currentArgs = make([]interface{}, 0, len(results))
52+
for _, res := range results {
53+
// Se o resultado implementa error e não é nil, interrompe o pipeline.
54+
if res.Type().Implements(errorType) {
55+
if !res.IsNil() {
56+
return nil, res.Interface().(error)
4257
}
43-
// If it's a nil error, ignore it for the output.
58+
// Caso o error seja nil, não o adiciona aos resultados.
4459
} else {
45-
currentArgs = append(currentArgs, result.Interface())
60+
currentArgs = append(currentArgs, res.Interface())
4661
}
4762
}
4863
}
4964

50-
// Return the final result which should match the last function's output type.
65+
// Se houver apenas um resultado, retorna-o diretamente; caso contrário, retorna um slice.
5166
if len(currentArgs) == 1 {
52-
return currentArgs[0], nil // Return single value if only one result.
67+
return currentArgs[0], nil
5368
}
54-
return currentArgs, nil // Return as slice if multiple values.
69+
return currentArgs, nil
5570
}
5671
}

0 commit comments

Comments
 (0)