Skip to content

Commit

Permalink
vgo: Loader initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
myitcv committed Apr 12, 2018
1 parent 517b5a6 commit b7c7ecf
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 29 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
### `myitcv.io/...` mono-repo

<!-- __TEMPLATE: go list -f "{{${DOLLAR}ip := .ImportPath}}{{range .Deps}}{{if (eq \"myitcv.io/vgo\" .)}}{{${DOLLAR}ip}}{{end}}{{end}}" ./...
{{ with . }}
Please note the following packages current rely on `vgo` with https://go-review.googlesource.com/c/vgo/+/105855 applied:
```
{{. -}}
```
{{end -}}
-->

Please note the following packages current rely on `vgo` with https://go-review.googlesource.com/c/vgo/+/105855 applied:

```
myitcv.io/immutable/cmd/immutableVet
```
<!-- END -->

25 changes: 22 additions & 3 deletions _scripts/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ if [ "${CI:-}" == "true" ]
then
go get -u golang.org/x/vgo
pushd $(go list -f "{{.Dir}}" golang.org/x/vgo) > /dev/null
git checkout -qf $VGO_COMMIT

# git checkout -qf $VGO_COMMIT
git fetch -q https://go.googlesource.com/vgo refs/changes/55/105855/3 && git checkout -qf FETCH_HEAD
go install

popd > /dev/null

# so we can access Github without hitting rate limits
Expand Down Expand Up @@ -63,17 +66,33 @@ do
then
./_scripts/run_tests.sh
else
if [ -f ./_scripts/pre_run_tests.sh ]
then
./_scripts/pre_run_tests.sh
fi

$go generate ./...
$go test ./...

# we can remove this once we resolve https://github.com/golang/go/issues/24661
$go install ./...
if [ -f ./_scripts/post_run_tests.sh ]
then
./_scripts/post_run_tests.sh
fi
fi
popd > /dev/null
echo "----"
echo ""
done

# we use regular go to list here because of https://github.com/golang/go/issues/24749;
# this is also the reason why we need to change to the directory to do the vgo install
for i in $(go list -f "{{if eq .Name \"main\"}}{{.Dir}}{{end}}" ./...)
do
pushd $i > /dev/null
$go install
popd > /dev/null
done

echo Checking markdown files are current
# by this point we will have mdreplace installed. Hence check that
# committed .md files are "fresh"
Expand Down
5 changes: 3 additions & 2 deletions cmd/mdreplace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ _To see this in action, look at the [source of the
### Usage
```
{{.}}
{{. -}}
```
-->
### Usage
Expand All @@ -69,12 +69,13 @@ Usage:
When called with no file arguments, mdreplace works with stdin
Flags:
-debug
whether to print debug information of not
-strip
whether to strip special comments from the file
-w whether to write back to input files (cannot be used when reading from
stdin)
```
<!-- END -->

Expand Down
3 changes: 2 additions & 1 deletion cmd/mdreplace/mdreplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
var (
fWrite = flag.Bool("w", false, "whether to write back to input files (cannot be used when reading from stdin)")
fStrip = flag.Bool("strip", false, "whether to strip special comments from the file")
fDebug = flag.Bool("debug", false, "whether to print debug information of not")
)

//go:generate pkgconcat -out gen_cliflag.go myitcv.io/_tmpls/cliflag
Expand Down Expand Up @@ -150,7 +151,7 @@ func infof(format string, args ...interface{}) {
}

func debugf(format string, args ...interface{}) {
if debug {
if debug || *fDebug {
fmt.Fprintf(os.Stderr, format, args...)
}
}
10 changes: 2 additions & 8 deletions immutable/_scripts/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@ rm -f !(_vendor)/**/gen_*.go
$go install myitcv.io/immutable/cmd/immutableGen
$go install myitcv.io/immutable/cmd/immutableVet

# this step is needed because _testFiles is not walked by ./...
pushd cmd/immutableVet/_testFiles
$go generate
popd

$go generate ./...

# for immutable this step needs to be before the tests (because some of the
# tests rely on the myitcv.io/immutable package to have been installed)

# we can remove this once we resolve https://github.com/golang/go/issues/24661
$go install ./...

$go test ./...

$go install myitcv.io/immutable/cmd/immutableVet

immutableVet myitcv.io/immutable/example

37 changes: 22 additions & 15 deletions immutable/cmd/immutableVet/immutableVet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/printer"
"go/token"
Expand All @@ -22,13 +21,16 @@ import (
"myitcv.io/gogenerate"
"myitcv.io/immutable"
"myitcv.io/immutable/util"
"myitcv.io/vgo"
)

const (
skipFileComment = "//" + immutable.CmdImmutableVet + ":skipFile"
)

var fset = token.NewFileSet()
var (
fset = token.NewFileSet()
)

type immutableVetter struct {
pkgs map[string]*ast.Package
Expand All @@ -51,6 +53,9 @@ type immutableVetter struct {
vcls map[*ast.CompositeLit]bool

errlist []immErr

importer types.ImporterFrom
immIntf *types.Interface
}

var typesCache = map[string]bool{
Expand All @@ -64,8 +69,6 @@ type immErr struct {

type errors []immErr

var immIntf *types.Interface

func main() {
flag.Parse()

Expand All @@ -87,9 +90,11 @@ func main() {
}
}

func loadImmIntf() {
func loadImmIntf(dir string) (types.ImporterFrom, *types.Interface) {
ip := "myitcv.io/immutable"

importer := vgo.NewTestLoader(dir)

bpkg, err := build.Import(ip, "", 0)
if err != nil {
fatalf("failed to import %v: %v", ip, err)
Expand All @@ -115,7 +120,7 @@ func loadImmIntf() {

conf := types.Config{
FakeImportC: true,
Importer: importer.For("gc", nil),
Importer: importer,
}

tpkg, err := conf.Check(ip, fset, files, nil)
Expand Down Expand Up @@ -144,14 +149,12 @@ func loadImmIntf() {
fatalf("Underlying type is not a *types.Interface: %T", nmd.Underlying())
}

immIntf = intf
return importer, intf
}

func vet(wd string, specs []string) []immErr {
var emsgs []immErr

loadImmIntf()

// vetting phase: vet all packages packages passed in through the command line
for _, spec := range specs {

Expand Down Expand Up @@ -420,11 +423,15 @@ func newImmutableVetter(ipkg *build.Package, wd string) *immutableVetter {
fatalf("could not parse package directory for %v", ipkg.Name)
}

importer, immIntf := loadImmIntf(ipkg.Dir)

return &immutableVetter{
pkgs: pkgs,
bpkg: ipkg,
vcls: make(map[*ast.CompositeLit]bool),
wd: wd,
immIntf: immIntf,
importer: importer,
skipFiles: make(map[string]bool),
}
}
Expand All @@ -451,7 +458,7 @@ func (iv *immutableVetter) vetPackages() []immErr {

// check types for the core package
conf := types.Config{
Importer: importer.Default(),
Importer: iv.importer,
}
info := &types.Info{
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Expand Down Expand Up @@ -497,7 +504,7 @@ func (iv *immutableVetter) vetPackages() []immErr {

for i := 0; i < st.NumFields(); i++ {
f := st.Field(i)
if !isImmType(f.Type()) {
if !iv.isImmType(f.Type()) {
msg := fmt.Sprintf("immutable struct field must be immutable type; %v is not", f.Type())
iv.errorf(f.Pos(), msg)
}
Expand Down Expand Up @@ -575,7 +582,7 @@ func (iv *immutableVetter) vetPackages() []immErr {
return iv.errlist
}

func isImmType(t types.Type) bool {
func (iv *immutableVetter) isImmType(t types.Type) bool {
if v, ok := typesCache[t.String()]; ok {
return v
}
Expand All @@ -585,7 +592,7 @@ func isImmType(t types.Type) bool {

typesCache[t.String()] = true

v := isImmType(t.Underlying())
v := iv.isImmType(t.Underlying())
typesCache[t.String()] = v

return v
Expand All @@ -598,14 +605,14 @@ func isImmType(t types.Type) bool {
case *types.Struct:
for i := 0; i < t.NumFields(); i++ {
f := t.Field(i)
if !isImmType(f.Type()) {
if !iv.isImmType(f.Type()) {
return false
}
}

return true
case *types.Interface:
return types.Implements(t, immIntf)
return types.Implements(t, iv.immIntf)
case *types.Signature:
return false
default:
Expand Down
5 changes: 5 additions & 0 deletions immutable/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ require (
"github.com/kisielk/gotool" v1.0.0
"golang.org/x/tools" v0.0.0-20180401165715-ac136b6c2db7
"myitcv.io/gogenerate" v0.0.0-20180405194116-cdcde380fd71d44b727692b77c152bb2e9df079c
"myitcv.io/vgo" v0.0.0
)

replace (
"myitcv.io/vgo" v0.0.0 => "../vgo"
)
3 changes: 3 additions & 0 deletions vgo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "myitcv.io/vgo"

require "golang.org/x/net" v0.0.0-20180406214816-61147c48b25b
Loading

0 comments on commit b7c7ecf

Please sign in to comment.