sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

JSON Parser fails for type override on complex package names

Open rupertevill opened this issue 6 years ago • 4 comments

The standard syntax for defining a type override in the lib at the moment is:

"overrides": [ { "go_type": "github.com/gofrs/uuid.UUID", "postgres_type": "uuid" } ]

This results in the following import statement and type (in the struct) in the auto gen code:

import (
    "github.com/gofrs/uuid"
)

type test struct {
    columnOne uuid.UUID
}

However, the parser fails if the package name containing the desired type override is more complex, for example "gopkg.in/guregu/null.v3".

Have looked at the parser as it is at the moment and the bug fix doesn't look to bad, let me if you want any contribution 👍

rupertevill avatar Jan 10 '20 09:01 rupertevill

I've closed #462 since I'm pretty sure it was a duplicate issue. Is the bug caused by the presence of a dot in the package name?

maxiride avatar Apr 26 '20 09:04 maxiride

I've closed #462 since I'm pretty sure it was a duplicate issue.

Is the bug caused by the presence of a dot in the package name?

Correct @Maxiride. The presence of a dot in the package name causes the issue.

rupertevill avatar Apr 26 '20 12:04 rupertevill

Too bad... those packages are my holy grail.

~~Anyway,~~

~~Have looked at the parser as it is at the moment and the bug fix doesn't look to bad, let me if you want any contribution 👍~~

~~@rupertevill did you eventually started working on a PR?~~


As a temporary (hopefully) fix I went down the dangerous path of issuing a replace all in the models and the functions generated.


I went on with a plausible hotfix myself.

maxiride avatar Apr 26 '20 13:04 maxiride

This has been fixed in main and will be released in the next version. Here's what it looks like:

{
  "version": "1",
  "packages": [
    {
      "path": "go",
      "name": "override",
      "engine": "postgresql",
      "schema": "schema.sql",
      "queries": "query.sql",
      "overrides": [
        {
          "column": "foo.bio",
          "nullable": true,
          "go_type": {
            "import": "gopkg.in/guregu/null.v4",
            "type": "String"
          },
        }
      ]
    }
  ]
}
import (
    null_v4 "gopkg.in/guregu/null.v4"
)

type test struct {
    columnOne null_v4.String
}

kyleconroy avatar Nov 15 '20 04:11 kyleconroy