Skip to content

Commit cc9f77b

Browse files
committed
more wip
1 parent 78dd387 commit cc9f77b

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

cmd/checkapi/main.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
"go/ast"
1111
"go/parser"
1212
"go/token"
13-
"gopkg.in/yaml.v3"
1413
"io/fs"
1514
"log"
1615
"os"
1716
"path/filepath"
1817
"sort"
1918
"strings"
2019
"unicode"
20+
21+
"gopkg.in/yaml.v3"
2122
)
2223

2324
func main() {
@@ -76,7 +77,7 @@ func run(folder string, allowlistFilePath string) error {
7677
}
7778

7879
var componentType string
79-
if _, err := os.Stat(filepath.Join(base, "metadata.yaml")); errors.Is(err, os.ErrNotExist) {
80+
if _, err = os.Stat(filepath.Join(base, "metadata.yaml")); errors.Is(err, os.ErrNotExist) {
8081
componentType = "pkg"
8182
} else {
8283
m, err := os.ReadFile(filepath.Join(base, "metadata.yaml"))
@@ -215,10 +216,10 @@ func walkFolder(folder string, componentType string) error {
215216
return nil
216217
}
217218

218-
if len(result.Functions) > 1 && componentType != "pkg" {
219+
if len(result.Functions) > 1 && componentType != "pkg" && componentType != "cmd" {
219220
return fmt.Errorf("%s has more than one function: %q", folder, strings.Join(fnNames, ","))
220221
}
221-
if len(result.Functions) == 1 && componentType != "pkg" {
222+
if len(result.Functions) == 1 && componentType != "pkg" && componentType != "cmd" {
222223
if err := checkFactoryFunction(result.Functions[0], folder, componentType); err != nil {
223224
return err
224225
}
@@ -233,6 +234,33 @@ func walkFolder(folder string, componentType string) error {
233234

234235
// check the only exported function of the module is NewFactory, matching the signature of the factory expected by the collector builder.
235236
func checkFactoryFunction(newFactoryFn *function, folder string, componentType string) error {
237+
switch componentType {
238+
case "provider":
239+
return checkProviderFactoryFunction(newFactoryFn, folder, componentType)
240+
default:
241+
return checkComponentFactoryFunction(newFactoryFn, folder, componentType)
242+
}
243+
}
244+
245+
func checkProviderFactoryFunction(newFactoryFn *function, folder string, componentType string) error {
246+
if newFactoryFn.Name != "NewFactory" {
247+
return fmt.Errorf("%s does not define a NewFactory function as a %s", folder, componentType)
248+
}
249+
if newFactoryFn.Receiver != "" {
250+
return fmt.Errorf("%s associated NewFactory with a receiver type", folder)
251+
}
252+
if len(newFactoryFn.ReturnTypes) != 1 {
253+
return fmt.Errorf("%s NewFactory function returns more than one result", folder)
254+
}
255+
returnType := newFactoryFn.ReturnTypes[0]
256+
257+
if returnType != "confmap.ProviderFactory" {
258+
return fmt.Errorf("%s NewFactory function does not return a valid type: %s, expected confmap.ProviderFactory", folder, returnType)
259+
}
260+
return nil
261+
}
262+
263+
func checkComponentFactoryFunction(newFactoryFn *function, folder string, componentType string) error {
236264
if newFactoryFn.Name != "NewFactory" {
237265
return fmt.Errorf("%s does not define a NewFactory function as a %s", folder, componentType)
238266
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
status:
2+
class: pkg
23
codeowners:
34
active: [boostchicken]

0 commit comments

Comments
 (0)