Skip to content

Commit

Permalink
move gen package to root
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Dec 19, 2022
1 parent 4d1a424 commit 05ec50a
Show file tree
Hide file tree
Showing 34 changed files with 696 additions and 35 deletions.
6 changes: 3 additions & 3 deletions orm/README.md → gen/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Bob ORM
# Bob Code Generator (ORM, Factory)

Generate an ORM based on your database schema

Expand All @@ -14,7 +14,7 @@ Pending features
### PostgreSQL

```sh
PSQL_DSN=postgres://user:pass@host:port/dbname go run github.com/go-bob/bobgen-psql@latest
PSQL_DSN=postgres://user:pass@host:port/dbname go run github.com/stephenafamo/bob/gen/psql@latest
```

## About
Expand Down Expand Up @@ -87,7 +87,7 @@ like [sql-migrate](https://github.com/rubenv/sql-migrate) or some other migratio

| Database | Driver Location |
| ----------------- | --------------- |
| PostgreSQL | <https://github.com/go-bob/bobgen-psql> |
| PostgreSQL | [LINK][gen/psql] |

## Configuration

Expand Down
2 changes: 1 addition & 1 deletion orm/gen/aliases.go → gen/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"unicode"
"unicode/utf8"

"github.com/stephenafamo/bob/orm/gen/drivers"
"github.com/stephenafamo/bob/gen/drivers"
"github.com/volatiletech/strmangle"
)

Expand Down
3 changes: 2 additions & 1 deletion orm/gen/bobgen.go → gen/bobgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"strings"
"text/template"

"github.com/stephenafamo/bob/gen/drivers"
"github.com/stephenafamo/bob/orm"
"github.com/stephenafamo/bob/orm/gen/drivers"
"github.com/volatiletech/strmangle"
)

Expand Down Expand Up @@ -97,6 +97,7 @@ func (s *State[T]) Run() error {
Tags: s.Config.Tags,
RelationTag: s.Config.RelationTag,
Schema: s.Schema,
ModelsPackage: s.Config.ModelsPackage,
}

for _, v := range s.Config.TagIgnore {
Expand Down
4 changes: 2 additions & 2 deletions orm/gen/bobgen_test.go → gen/bobgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"strconv"
"testing"

"github.com/stephenafamo/bob/orm/gen/drivers"
"github.com/stephenafamo/bob/orm/gen/drivers/mocks"
"github.com/stephenafamo/bob/gen/drivers"
"github.com/stephenafamo/bob/gen/drivers/mocks"
)

var (
Expand Down
65 changes: 64 additions & 1 deletion orm/gen/config.go → gen/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package gen

import (
"bytes"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os/exec"
"path"
"strings"
"text/template"

"github.com/spf13/cast"
"github.com/stephenafamo/bob/gen/drivers"
"github.com/stephenafamo/bob/orm"
"github.com/stephenafamo/bob/orm/gen/drivers"
"golang.org/x/mod/modfile"
)

// Config for the running of the commands
Expand All @@ -30,6 +37,7 @@ type Config[T any] struct {
Generator string `toml:"generator" json:"generator"`
Outputs []*Output `toml:"package" json:"package"`
CustomTemplateFuncs template.FuncMap `toml:"-" json:"-"`
ModelsPackage string `toml:"models_package" json:"models_package"`
}

type Output struct {
Expand Down Expand Up @@ -229,6 +237,61 @@ func ConvertRelationships(i interface{}) map[string][]orm.Relationship {
return relationships
}

func ModelsPackage(relPath string) (string, error) {
modFile, err := goModInfo()
if err != nil {
return "", fmt.Errorf("getting mod details: %w", err)
}

return path.Join(modFile.Module.Mod.Path, relPath), nil
}

// goModInfo returns the main module's root directory
// and the parsed contents of the go.mod file.
func goModInfo() (*modfile.File, error) {
goModPath, err := findGoMod()
if err != nil {
return nil, fmt.Errorf("cannot find main module: %w", err)
}

data, err := ioutil.ReadFile(goModPath)
if err != nil {
return nil, fmt.Errorf("cannot read main go.mod file: %w", err)
}

modf, err := modfile.Parse(goModPath, data, nil)
if err != nil {
return nil, fmt.Errorf("could not parse go.mod: %w", err)
}

return modf, nil
}

func findGoMod() (string, error) {
var outData, errData bytes.Buffer

c := exec.Command("go", "env", "GOMOD")
c.Stdout = &outData
c.Stderr = &errData
c.Dir = "."
err := c.Run()
if err != nil {
var exitError *exec.ExitError
if errors.As(err, &exitError) && errData.Len() > 0 {
return "", errors.New(strings.TrimSpace(errData.String()))
}

return "", fmt.Errorf("cannot run go env GOMOD: %w", err)
}

out := strings.TrimSpace(outData.String())
if out == "" {
return "", errors.New("no go.mod file found in any parent directory")
}

return out, nil
}

func relSideSliceFromInterface(i any) []orm.RelSide {
slice := cast.ToSlice(i)
if len(slice) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion orm/gen/config_test.go → gen/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

"github.com/stephenafamo/bob/orm/gen/drivers"
"github.com/stephenafamo/bob/gen/drivers"
)

func TestConvertAliases(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion orm/gen/drivers/column.go → gen/drivers/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package drivers
import (
"strings"

"github.com/stephenafamo/bob/orm/gen/importers"
"github.com/stephenafamo/bob/gen/importers"
"github.com/volatiletech/strmangle"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mocks

import (
"github.com/stephenafamo/bob/orm/gen/drivers"
"github.com/stephenafamo/bob/gen/drivers"
"github.com/volatiletech/strmangle"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion orm/gen/output.go → gen/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"text/template"

"github.com/stephenafamo/bob/orm/gen/importers"
"github.com/stephenafamo/bob/gen/importers"
)

// Copied from the go source
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions orm/gen/templates.go → gen/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/stephenafamo/bob/gen/drivers"
"github.com/stephenafamo/bob/gen/importers"
"github.com/stephenafamo/bob/orm"
"github.com/stephenafamo/bob/orm/gen/drivers"
"github.com/stephenafamo/bob/orm/gen/importers"
"github.com/volatiletech/strmangle"
)

Expand All @@ -22,7 +22,7 @@ var templates embed.FS

//nolint:gochecknoglobals
var (
ModelTemplates fs.FS
ModelTemplates fs.FS
)

func init() {
Expand Down Expand Up @@ -95,7 +95,8 @@ type templateData[T any] struct {
TagIgnore map[string]struct{}

// Supplied by the driver
ExtraInfo T
ExtraInfo T
ModelsPackage string
}

func (t *templateData[T]) ResetImports() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion orm/gen/text_helpers.go → gen/text_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package gen
import (
"strings"

"github.com/stephenafamo/bob/gen/drivers"
"github.com/stephenafamo/bob/orm"
"github.com/stephenafamo/bob/orm/gen/drivers"
"github.com/volatiletech/strmangle"
)

Expand Down
41 changes: 34 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,57 @@ go 1.18
require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/aarondl/opt v0.0.0-20220611032941-846a5a057d8c
github.com/google/go-cmp v0.5.8
github.com/friendsofgo/errors v0.9.2
github.com/google/go-cmp v0.5.9
github.com/jackc/pgx/v4 v4.17.2
github.com/jinzhu/copier v0.3.5
github.com/lib/pq v1.10.7
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
github.com/stephenafamo/scan v0.0.0-20220903213716-8307e83853ef
github.com/stretchr/testify v1.8.1
github.com/volatiletech/strmangle v0.0.4
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
golang.org/x/tools v0.1.12
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/stretchr/testify v1.7.1 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 05ec50a

Please sign in to comment.