Skip to content

Commit

Permalink
Switch to go modules
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Jan 26, 2019
1 parent a2796d0 commit 31541f9
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 119 deletions.
33 changes: 3 additions & 30 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
version: 2
jobs:
go1.7: &base
build:
docker:
- image: golang:1.7
working_directory: /go/src/github.com/vektah/dataloaden
- image: golang:1.11
working_directory: /projects/dataloaden
steps: &steps
- checkout
- run: >
curl -L -o /bin/dep https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 &&
chmod +x /bin/dep &&
go get -u github.com/alecthomas/gometalinter &&
gometalinter --install &&
dep ensure --vendor-only
- run: go install -v .
- run: go generate ./example/... && if [[ $(git diff) ]] ; then echo "you need to run go generate" ; git diff ; exit 1 ; fi
- run: go vet ./example/...
- run: go test -bench=. -benchmem -v ./example/...
- run: go test -bench=. -benchmem -v ./example/... -race
- run: go test -coverprofile=coverage.txt -covermode=atomic ./example && bash <(curl -s https://codecov.io/bash)
- run: gometalinter --disable gocyclo ./example

go1.8:
<<: *base
docker:
- image: golang:1.8

go1.9:
<<: *base
docker:
- image: golang:1.9

workflows:
version: 2
build:
jobs:
- "go1.7"
- "go1.8"
- "go1.9"
58 changes: 0 additions & 58 deletions Gopkg.lock

This file was deleted.

11 changes: 0 additions & 11 deletions Gopkg.toml

This file was deleted.

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### The DATALOADer gENerator [![CircleCI](https://circleci.com/gh/Vektah/dataloaden.svg?style=svg)](https://circleci.com/gh/Vektah/dataloaden) [![Go Report Card](https://goreportcard.com/badge/github.com/vektah/dataloaden)](https://goreportcard.com/report/github.com/vektah/dataloaden) [![codecov](https://codecov.io/gh/vektah/dataloaden/branch/master/graph/badge.svg)](https://codecov.io/gh/vektah/dataloaden)

Requires golang 1.11+ for modules support.

This is a tool for generating type safe data loaders for go, inspired by https://github.com/facebook/dataloader.

Expand Down Expand Up @@ -58,6 +59,22 @@ dataloaden -slice github.com/dataloaden/example.User

Now each key is expected to return a slice of values and the `fetch` function has the return type `[][]User`.

#### Using with go modules

Create a tools.go that looks like this:
```go
// +build tools

package main

import _ "github.com/vektah/dataloaden"
```

This will allow go modules to see the dependency.

You can invoke it from anywhere within your module now using `go run github.com/vektah/dataloaden` and
always get the pinned version.

#### Wait, how do I use context with this?

I don't think context makes sense to be passed through a data loader. Consider a few scenarios:
Expand Down
7 changes: 3 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ version: "{build}"
# Source Config

skip_branch_with_pr: true
clone_folder: c:\gopath\src\github.com\vektah\dataloaden
clone_folder: c:\projects\dataloaden

# Build host

environment:
GOPATH: c:\gopath
GOVERSION: 1.10
GOVERSION: 1.11.5
PATH: '%PATH%;c:\gopath\bin'

init:
Expand All @@ -28,6 +28,5 @@ build: false
deploy: false

test_script:
- go get -t ./...
- go generate ./...
- go test ./...
- go test -parallel 8 ./...
2 changes: 1 addition & 1 deletion example/pkgname/user.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package differentpkg

//go:generate go run ../../dataloaden.go -keys string github.com/vektah/dataloaden/example.User
//go:generate go run github.com/vektah/dataloaden -keys string github.com/vektah/dataloaden/example.User
2 changes: 1 addition & 1 deletion example/slice/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run ../../dataloaden.go -keys int -slice github.com/vektah/dataloaden/example.User
//go:generate go run github.com/vektah/dataloaden -keys int -slice github.com/vektah/dataloaden/example.User

package slice

Expand Down
2 changes: 1 addition & 1 deletion example/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run ../dataloaden.go -keys string github.com/vektah/dataloaden/example.User
//go:generate go run github.com/vektah/dataloaden -keys string github.com/vektah/dataloaden/example.User

package example

Expand Down
5 changes: 3 additions & 2 deletions example/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -80,8 +81,8 @@ func TestUserLoader(t *testing.T) {
defer mu.Unlock()

require.Len(t, fetches, 2)
require.Len(t, fetches[0], 5)
require.Len(t, fetches[1], 3)
assert.Len(t, fetches[0], 5)
assert.Len(t, fetches[1], 3)
})

t.Run("fetch more", func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/vektah/dataloaden

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.1
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golangci/golangci-lint v1.13.1 h1:e4khAARYjxlcEtEZgPqqaeoIVWlHmsZ4c+g5nJUpdUQ=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6 h1:iZgcI2DDp6zW5v9Z/5+f0NuqoxNdmzg4hivjk2WLXpY=
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
30 changes: 19 additions & 11 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package generator
import (
"bytes"
"fmt"
"go/build"
"io/ioutil"
"path/filepath"
"strings"
"unicode"

"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
)

Expand Down Expand Up @@ -49,9 +49,15 @@ func getData(typeName string, keyType string, slice bool, wd string) (templateDa
return templateData{}, fmt.Errorf("type must be in the form package.Name")
}

pkgData := getPackage(wd)
name := parts[len(parts)-1]
data.Package = pkgData
importPath := strings.Join(parts[:len(parts)-1], ".")

genPkg := getPackage(wd)
if genPkg == nil {
return templateData{}, fmt.Errorf("unable to find package info for " + wd)
}

data.Package = genPkg.Name
data.LoaderName = name + "Loader"
data.BatchName = lcFirst(name) + "Batch"
data.Name = lcFirst(name)
Expand All @@ -66,24 +72,26 @@ func getData(typeName string, keyType string, slice bool, wd string) (templateDa
}

// if we are inside the same package as the type we don't need an import and can refer directly to the type
pkgName := strings.Join(parts[:len(parts)-1], ".")
if strings.HasSuffix(filepath.ToSlash(wd), pkgName) {
if genPkg.PkgPath == importPath {
data.ValType = prefix + name
} else {
data.Import = pkgName
data.Import = importPath
data.ValType = prefix + filepath.Base(data.Import) + "." + name
}

return data, nil
}

func getPackage(wd string) string {
result, err := build.ImportDir(wd, build.IgnoreVendor)
if err != nil {
return filepath.Base(wd)
func getPackage(dir string) *packages.Package {
p, _ := packages.Load(&packages.Config{
Dir: dir,
}, ".")

if len(p) != 1 {
return nil
}

return result.Name
return p[0]
}

func writeTemplate(filepath string, data templateData) error {
Expand Down

0 comments on commit 31541f9

Please sign in to comment.