PaesslerAG / jsonpath

BSD 3-Clause "New" or "Revised" License
172 stars 37 forks source link

Problem with vendor folder #12

Closed shakedkaplan closed 5 years ago

shakedkaplan commented 5 years ago

To handle my dependencies I'm using a different repo than my main code and using git submodules to handle my dependencies I'm trying to fetch both json path and gval repositories into my project but getting the following error: cannot use evaluable (type “github.com/PaesslerAG/jsonpath/vendor/github.com/PaesslerAG/gval”.Evaluable) as type “github.com/PaesslerAG/gval”.Evaluable in field value

The only solution I found is deleting the vendor directory on the json path repo is there any other way around it?

jrudder commented 5 years ago

There is another option: modules.

go cannot tell that the gval in jsonpath's vendor directory is the same as the one in your GOPATH, and so it is saying that you cannot use them interchangeably. As you noted, one option is to remove the vendor directory from jsonpath, which will cause the go tool to use the one in your GOPATH (or potentially in your project's own vendor directory) for both your project and jsonpath.

Another option, if you are using Go 1.11, is to use go modules, which causes the go tool to ignore vendor directories by default. (Note that to setup your project as a module, it either needs to be outside of your GOPATH or you need to set GO111MODULE=on when running any go commands.)

Below is a simple example. (In this example, the directory is outside of GOPATH and thus does not need GO111MODULE=on):

main.go

package main

import (
    "fmt"
    "os"

    "github.com/PaesslerAG/gval"
    "github.com/PaesslerAG/jsonpath"
)

func main() {
    value, err := gval.Evaluate(`$["response-time"]`,
        map[string]interface{}{
            "response-time": 100,
        },
        jsonpath.Language(),
    )
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Print(value)
}

Before setting it up as a module, it will not compile because the go command will use the vendor directory:

$ go run main.go
# command-line-arguments
./main.go:16:20: cannot use jsonpath.Language() (type 
"github.com/PaesslerAG/jsonpath/vendor/github.com/PaesslerAG/gval".Language) as type 
"github.com/PaesslerAG/gval".Language in argument to
"github.com/PaesslerAG/gval".Evaluate

To solve this, make the project a module:

$ go mod init example.com/test
go: creating new go.mod: module example.com/test
$ go list
example.com/test
$ cat go.mod
module example.com/test

require (
    github.com/PaesslerAG/gval v0.1.1
    github.com/PaesslerAG/jsonpath v0.1.0
)

Now that we have done the module setup, there will only be one gval and the example compiles and runs as expected:

$ go run main.go
100
generikvault commented 5 years ago

thanks @jrudder