Skip to content

Commit

Permalink
fix: 生成代码后,去掉无用的导包信息
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmingyong0423 committed Mar 11, 2024
1 parent e895755 commit 1b34f4b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions example/generic_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package example

import "context"

// 用于添加 "context" 导包信息,以便在生成代码时判断是否成功去除该无用包信息
var _ context.Context

//go:generate go run ../cmd/optioner/main.go -type GenericExample
type GenericExample[T any, U comparable, V ~int] struct {
A T `opt:"-"`
Expand Down
4 changes: 4 additions & 0 deletions example/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package example

import (
"context"
"github.com/chenmingyong0423/go-optioner/example/third_party"
)

// 用于添加 "context" 导包信息,以便在生成代码时判断是否成功去除该无用包信息
var _ context.Context

type Embedded struct{}

type Embedded2 struct{}
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/chenmingyong0423/go-optioner

go 1.21.0

require github.com/chenmingyong0423/gkit v0.5.0
require (
github.com/chenmingyong0423/gkit v0.5.0
golang.org/x/tools v0.19.0
)

require golang.org/x/mod v0.16.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 5 additions & 6 deletions options/options_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ package options
import (
"bytes"
"fmt"
"github.com/chenmingyong0423/gkit/stringx"
"github.com/chenmingyong0423/go-optioner/templates"
"go/ast"
"go/build"
"go/format"
"go/parser"
"go/token"
"golang.org/x/tools/imports"
"html/template"
"log"
"os"
"reflect"
"strconv"
"strings"

"github.com/chenmingyong0423/gkit/stringx"
"github.com/chenmingyong0423/go-optioner/templates"
)

type Generator struct {
Expand Down Expand Up @@ -189,9 +188,9 @@ func (g *Generator) OutputToFile() {
}

func (g *Generator) forMart() []byte {
source, err := format.Source(g.buf.Bytes())
source, err := imports.Process("", g.buf.Bytes(), nil)
if err != nil {
log.Fatal(err)
return nil
}
return source
}
Expand Down

0 comments on commit 1b34f4b

Please sign in to comment.