Skip to content

Commit 6d0bb7b

Browse files
committed
refactor: improve error handling and code structure
1 parent aad5e1c commit 6d0bb7b

6 files changed

Lines changed: 326 additions & 143 deletions

File tree

.golangci.yaml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ linters:
7272
- goconst # finds repeated strings that could be replaced by a constant
7373
- gocritic # provides diagnostics that check for bugs, performance and style issues
7474
- gocyclo # computes and checks the cyclomatic complexity of functions
75-
- godot # checks if comments end in a period
7675
- gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
7776
- goprintffuncname # checks that printf-like functions are named with f at the end
7877
- gosec # inspects source code for security problems
@@ -433,8 +432,6 @@ linters:
433432
- common-false-positives
434433
# Excluding configuration per-path, per-linter, per-text and per-source.
435434
rules:
436-
- source: 'TODO'
437-
linters: [ godot ]
438435
- text: 'should have a package comment'
439436
linters: [ revive ]
440437
- text: 'exported \S+ \S+ should have comment( \(or a comment on this block\))? or be unexported'
@@ -445,8 +442,35 @@ linters:
445442
- text: 'comment on exported \S+ \S+ should be of the form ".+"'
446443
source: '// ?(nolint|TODO)'
447444
linters: [ revive, staticcheck ]
445+
446+
- path: 'cmd/assemble/.*\.go'
447+
linters:
448+
- forbidigo
449+
- gochecknoglobals
450+
- gosec
451+
452+
- path: 'example/.*\.go'
453+
linters:
454+
- forbidigo
455+
- gochecknoglobals
456+
- gosec
457+
458+
- path: 'test/.*\.go'
459+
linters:
460+
- forbidigo
461+
- gochecknoglobals
462+
- bodyclose
463+
- dupl
464+
- errcheck
465+
- funlen
466+
- goconst
467+
- gosec
468+
- noctx
469+
- wrapcheck
470+
448471
- path: '_test\.go'
449472
linters:
473+
- forbidigo
450474
- bodyclose
451475
- dupl
452476
- errcheck

cmd/assemble/main.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import (
1616

1717
func main() {
1818
var (
19-
pkgArg = flag.String("pkg", ".", "Go package (import path or ./relative)")
20-
varName = flag.String("var", "Core", "assemble.Module variable name to parse")
21-
out = flag.String("o", "gen.go", "output file (e.g., ./build_assemble_gen.go)")
19+
pkgArg = flag.String("pkg", ".", "Go package (import path or ./relative)")
20+
varName = flag.String("var", "Core", "assemble.Module variable name to parse")
21+
outputPath = flag.String("o", "gen.go", "output file (e.g., ./build_assemble_gen.go)")
2222
)
2323
flag.Parse()
24-
if *out == "" {
24+
if *outputPath == "" {
2525
log.Fatal("-o output file is required")
2626
}
2727

@@ -33,33 +33,33 @@ func main() {
3333
Tests: false,
3434
}
3535

36-
pkgs, err := packages.Load(cfg, *pkgArg)
37-
if err != nil {
38-
log.Fatalf("load: %v", err)
36+
pkgs, loadErr := packages.Load(cfg, *pkgArg)
37+
if loadErr != nil {
38+
log.Fatalf("load: %v", loadErr)
3939
}
4040
if packages.PrintErrors(pkgs) > 0 || len(pkgs) == 0 {
4141
log.Fatalf("failed loading package %q", *pkgArg)
4242
}
4343
p := pkgs[0]
4444

45-
mod, err := parser.ExtractModule(p, *varName, fset)
46-
if err != nil {
47-
log.Fatalf("parse module: %v", err)
45+
mod, extractErr := parser.ExtractModule(p, *varName, fset)
46+
if extractErr != nil {
47+
log.Fatalf("parse module: %v", extractErr)
4848
}
4949

50-
src, err := render.Emit(*varName, p.Name, *out, mod)
51-
if err != nil {
52-
log.Fatalf("render: %v", err)
50+
src, emitErr := render.Emit(*varName, p.Name, *outputPath, mod)
51+
if emitErr != nil {
52+
log.Fatalf("render: %v", emitErr)
5353
}
5454

55-
if err := os.MkdirAll(filepath.Dir(*out), 0o755); err != nil {
56-
log.Fatalf("mkdir: %v", err)
55+
if mkdirErr := os.MkdirAll(filepath.Dir(*outputPath), 0o755); mkdirErr != nil {
56+
log.Fatalf("mkdir: %v", mkdirErr)
5757
}
58-
if err := os.WriteFile(*out, []byte(src), 0o644); err != nil {
59-
log.Fatalf("write: %v", err)
58+
if writeErr := os.WriteFile(*outputPath, []byte(src), 0o644); writeErr != nil {
59+
log.Fatalf("write: %v", writeErr)
6060
}
6161

62-
fmt.Printf("assemblegen: wrote %s (%d bytes)\n", *out, len(src))
62+
fmt.Printf("assemblegen: wrote %s (%d bytes)\n", *outputPath, len(src))
6363
}
6464

6565
func dirFor(arg string) string {

container.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ func (c *Container) resolveViaBind(ctx context.Context, k Key) (any, error) {
250250
return nil, err
251251
}
252252
if !reflect.TypeOf(v).Implements(k.typ) {
253-
return nil, BindError{From: k.typ, To: pk.typ, Why: "implementation does not satisfy interface at runtime"}
253+
return nil, BindError{
254+
From: k.typ,
255+
To: pk.typ,
256+
Why: "implementation does not satisfy interface at runtime",
257+
}
254258
}
255259
return v, nil
256260
}

example/core_assemble_gen.go

Lines changed: 11 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/parser/parse.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ const (
4040
kindStop = "stop"
4141
)
4242

43+
const (
44+
provideFuncParamCount = 2 // (context.Context, assemble.Resolver)
45+
)
46+
4347
// ExtractModule scans the given package for a variable named `varName`
4448
// and attempts to parse its value as an assemble module definition.
4549
func ExtractModule(p *packages.Package, varName string, fset *token.FileSet) (Model, error) {
@@ -458,9 +462,10 @@ func fillProvideFromExpr(p *packages.Package, pr *Provide, expr ast.Expr) {
458462
}
459463

460464
func fillProvideFromSignature(p *packages.Package, pr *Provide, sig *types.Signature) {
461-
if sig.Params().Len() == 1 && isResolverLike(sig.Params().At(0).Type()) {
465+
if sig.Params().Len() == provideFuncParamCount && isResolverLike(sig.Params().At(1).Type()) {
462466
pr.TakesResolve = true
463467
}
468+
464469
switch sig.Results().Len() {
465470
case 1:
466471
pr.ResType = typeString(p, sig.Results().At(0).Type())

0 commit comments

Comments
 (0)