Skip to content

Can't override with type in same package #177

Closed

Description

@mightyguava

I wanted to create a type for an enum, e.g. State

# job.sql
create table job (
  id bigserial primary key,
  state int
)
# job.sql.go
type Job struct {
  ID string `json:"id"`
  State State `json:"state"`
}
# enums.go
type State string
const (
  StateRunning State = iota
  StateFailed
  StateDone
)

It's convenient to have enums.go in the same package as the generated code, but there doesn't seem to be a way to tell the generator to not create an import. I end up with the package importing itself which Go reports as an import cycle.

Activity

  1. alibitek commented on Mar 11, 2020

    @alibitek

    Indeed, for some reason the enum type is NOT set as the struct field type in some cases.
    Don't know exactly why.
    In order to fix this I tried to set an override, but unfortunately I can't, because the enum from db is generated in the same package as the code using it.

    First test:

            {
              "go_type": "gitlab.com/company/project/database.ECustomEnum",
              "db_type": "e_custom_enum"
            }
    

    this gives error: import cycle not allowed , because the package where the code is generated, namely: database is trying to import itself via the override.

    Second test:

            {
              "go_type": "gitlab.com/company/project/ECustomEnum",
              "db_type": "e_custom_enum"
            }
    

    this gives error: cannot load gitlab: cannot find module providing package gitlab
    Inside querier.go I get a weird import like:

    import (
       "context"
       "github.com/google/uuid"
       "gitlab"
    )
    

    Probably because the override has the format: package.type and the package is missing.

  2. added a commit that references this issue on May 4, 2020
  3. kyleconroy commented on Nov 15, 2020

    @kyleconroy
    Collaborator

    This should now on main. You'll need to specify the go_type using an object instead of just a string

            {
              "go_type": {"type": "ECustomEnum"},
              "db_type": "e_custom_enum"
            }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions