Skip to content

Commit 1b34f4b

Browse files
fix: 生成代码后,去掉无用的导包信息
1 parent e895755 commit 1b34f4b

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

example/generic_example.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
package example
1616

17+
import "context"
18+
19+
// 用于添加 "context" 导包信息,以便在生成代码时判断是否成功去除该无用包信息
20+
var _ context.Context
21+
1722
//go:generate go run ../cmd/optioner/main.go -type GenericExample
1823
type GenericExample[T any, U comparable, V ~int] struct {
1924
A T `opt:"-"`

example/user.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
package example
1616

1717
import (
18+
"context"
1819
"github.com/chenmingyong0423/go-optioner/example/third_party"
1920
)
2021

22+
// 用于添加 "context" 导包信息,以便在生成代码时判断是否成功去除该无用包信息
23+
var _ context.Context
24+
2125
type Embedded struct{}
2226

2327
type Embedded2 struct{}

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ module github.com/chenmingyong0423/go-optioner
22

33
go 1.21.0
44

5-
require github.com/chenmingyong0423/gkit v0.5.0
5+
require (
6+
github.com/chenmingyong0423/gkit v0.5.0
7+
golang.org/x/tools v0.19.0
8+
)
9+
10+
require golang.org/x/mod v0.16.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
77
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
88
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
9+
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
10+
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
11+
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
12+
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
913
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1014
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

options/options_generator.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@ package options
1717
import (
1818
"bytes"
1919
"fmt"
20+
"github.com/chenmingyong0423/gkit/stringx"
21+
"github.com/chenmingyong0423/go-optioner/templates"
2022
"go/ast"
2123
"go/build"
22-
"go/format"
2324
"go/parser"
2425
"go/token"
26+
"golang.org/x/tools/imports"
2527
"html/template"
2628
"log"
2729
"os"
2830
"reflect"
2931
"strconv"
3032
"strings"
31-
32-
"github.com/chenmingyong0423/gkit/stringx"
33-
"github.com/chenmingyong0423/go-optioner/templates"
3433
)
3534

3635
type Generator struct {
@@ -189,9 +188,9 @@ func (g *Generator) OutputToFile() {
189188
}
190189

191190
func (g *Generator) forMart() []byte {
192-
source, err := format.Source(g.buf.Bytes())
191+
source, err := imports.Process("", g.buf.Bytes(), nil)
193192
if err != nil {
194-
log.Fatal(err)
193+
return nil
195194
}
196195
return source
197196
}

0 commit comments

Comments
 (0)