Additional documentation for sub packages.
This commit is contained in:
parent
b10270b502
commit
556578cec9
19 changed files with 111 additions and 77 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package metadata
|
||||
|
||||
// MetaData interface
|
||||
type MetaData interface {
|
||||
Name() string
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package postgres_metadata
|
||||
package postgresmeta
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// ColumnInfo metadata struct
|
||||
type ColumnInfo struct {
|
||||
Name string
|
||||
IsNullable bool
|
||||
|
|
@ -14,6 +15,7 @@ type ColumnInfo struct {
|
|||
EnumName string
|
||||
}
|
||||
|
||||
// SqlBuilderColumnType returns type of jet sql builder column
|
||||
func (c ColumnInfo) SqlBuilderColumnType() string {
|
||||
switch c.DataType {
|
||||
case "boolean":
|
||||
|
|
@ -41,6 +43,7 @@ func (c ColumnInfo) SqlBuilderColumnType() string {
|
|||
}
|
||||
}
|
||||
|
||||
// GoBaseType returns model type for column info.
|
||||
func (c ColumnInfo) GoBaseType() string {
|
||||
switch c.DataType {
|
||||
case "USER-DEFINED":
|
||||
|
|
@ -72,6 +75,8 @@ func (c ColumnInfo) GoBaseType() string {
|
|||
}
|
||||
}
|
||||
|
||||
// GoModelType returns model type for column info with optional pointer if
|
||||
// column can be NULL.
|
||||
func (c ColumnInfo) GoModelType() string {
|
||||
typeStr := c.GoBaseType()
|
||||
if c.IsNullable {
|
||||
|
|
@ -81,6 +86,7 @@ func (c ColumnInfo) GoModelType() string {
|
|||
return typeStr
|
||||
}
|
||||
|
||||
// GoModelTag returns model field tag for column
|
||||
func (c ColumnInfo) GoModelTag(isPrimaryKey bool) string {
|
||||
tags := []string{}
|
||||
|
||||
|
|
@ -1,15 +1,17 @@
|
|||
package postgres_metadata
|
||||
package postgresmeta
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/generator/internal/metadata"
|
||||
)
|
||||
|
||||
// EnumInfo struct
|
||||
type EnumInfo struct {
|
||||
name string
|
||||
Values []string
|
||||
}
|
||||
|
||||
// Name returns enum name
|
||||
func (e EnumInfo) Name() string {
|
||||
return e.name
|
||||
}
|
||||
|
|
@ -1,10 +1,11 @@
|
|||
package postgres_metadata
|
||||
package postgresmeta
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/generator/internal/metadata"
|
||||
)
|
||||
|
||||
// SchemaInfo metadata struct
|
||||
type SchemaInfo struct {
|
||||
DatabaseName string
|
||||
Name string
|
||||
|
|
@ -12,6 +13,7 @@ type SchemaInfo struct {
|
|||
EnumInfos []metadata.MetaData
|
||||
}
|
||||
|
||||
// GetSchemaInfo returns schema information from db connection.
|
||||
func GetSchemaInfo(db *sql.DB, databaseName, schemaName string) (schemaInfo SchemaInfo, err error) {
|
||||
|
||||
schemaInfo.DatabaseName = databaseName
|
||||
|
|
@ -1,10 +1,11 @@
|
|||
package postgres_metadata
|
||||
package postgresmeta
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/internal/utils"
|
||||
)
|
||||
|
||||
// TableInfo metadata struct
|
||||
type TableInfo struct {
|
||||
SchemaName string
|
||||
name string
|
||||
|
|
@ -12,14 +13,17 @@ type TableInfo struct {
|
|||
Columns []ColumnInfo
|
||||
}
|
||||
|
||||
// Name returns table info name
|
||||
func (t TableInfo) Name() string {
|
||||
return t.name
|
||||
}
|
||||
|
||||
func (t TableInfo) IsPrimaryKey(columnName string) bool {
|
||||
return t.PrimaryKeys[columnName]
|
||||
// IsPrimaryKey returns if column is a part of primary key
|
||||
func (t TableInfo) IsPrimaryKey(column string) bool {
|
||||
return t.PrimaryKeys[column]
|
||||
}
|
||||
|
||||
// MutableColumns returns list of mutable columns for table
|
||||
func (t TableInfo) MutableColumns() []ColumnInfo {
|
||||
ret := []ColumnInfo{}
|
||||
|
||||
|
|
@ -34,6 +38,7 @@ func (t TableInfo) MutableColumns() []ColumnInfo {
|
|||
return ret
|
||||
}
|
||||
|
||||
// GetImports returns model imports for table.
|
||||
func (t TableInfo) GetImports() []string {
|
||||
imports := map[string]string{}
|
||||
|
||||
|
|
@ -57,10 +62,12 @@ func (t TableInfo) GetImports() []string {
|
|||
return ret
|
||||
}
|
||||
|
||||
// GoStructName returns go struct name for sql builder
|
||||
func (t TableInfo) GoStructName() string {
|
||||
return utils.ToGoIdentifier(t.name) + "Table"
|
||||
}
|
||||
|
||||
// GetTableInfo returns table info metadata
|
||||
func GetTableInfo(db *sql.DB, dbName, schemaName, tableName string) (tableInfo TableInfo, err error) {
|
||||
|
||||
tableInfo.SchemaName = schemaName
|
||||
|
|
@ -4,13 +4,13 @@ import (
|
|||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/generator/internal/metadata"
|
||||
"github.com/go-jet/jet/generator/internal/metadata/postgres-metadata"
|
||||
"github.com/go-jet/jet/generator/internal/metadata/postgresmeta"
|
||||
"github.com/go-jet/jet/internal/utils"
|
||||
_ "github.com/lib/pq"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// DBConnection contains postgres connection details
|
||||
type DBConnection struct {
|
||||
Host string
|
||||
Port string
|
||||
|
|
@ -23,10 +23,11 @@ type DBConnection struct {
|
|||
SchemaName string
|
||||
}
|
||||
|
||||
func Generate(destDir string, genData DBConnection) error {
|
||||
// Generate generates jet files at destination dir from database connection details
|
||||
func Generate(destDir string, dbConn DBConnection) error {
|
||||
|
||||
connectionString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s %s",
|
||||
genData.Host, genData.Port, genData.User, genData.Password, genData.DBName, genData.SslMode, genData.Params)
|
||||
dbConn.Host, dbConn.Port, dbConn.User, dbConn.Password, dbConn.DBName, dbConn.SslMode, dbConn.Params)
|
||||
|
||||
fmt.Println("Connecting to postgres database: " + connectionString)
|
||||
|
||||
|
|
@ -43,7 +44,7 @@ func Generate(destDir string, genData DBConnection) error {
|
|||
}
|
||||
|
||||
fmt.Println("Retrieving schema information...")
|
||||
schemaInfo, err := postgres_metadata.GetSchemaInfo(db, genData.DBName, genData.SchemaName)
|
||||
schemaInfo, err := postgresmeta.GetSchemaInfo(db, dbConn.DBName, dbConn.SchemaName)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -55,7 +56,7 @@ func Generate(destDir string, genData DBConnection) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
schemaGenPath := path.Join(destDir, genData.DBName, genData.SchemaName)
|
||||
schemaGenPath := path.Join(destDir, dbConn.DBName, dbConn.SchemaName)
|
||||
fmt.Println("Destination directory:", schemaGenPath)
|
||||
fmt.Println("Cleaning up destination directory...")
|
||||
err = utils.CleanUpGeneratedFiles(schemaGenPath)
|
||||
|
|
@ -99,7 +100,7 @@ func Generate(destDir string, genData DBConnection) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func generate(schemaInfo postgres_metadata.SchemaInfo, dirPath, packageName string, template string, metaDataList []metadata.MetaData) error {
|
||||
func generate(schemaInfo postgresmeta.SchemaInfo, dirPath, packageName string, template string, metaDataList []metadata.MetaData) error {
|
||||
modelDirPath := filepath.Join(dirPath, schemaInfo.DatabaseName, schemaInfo.Name, packageName)
|
||||
|
||||
err := utils.EnsureDirPath(modelDirPath)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue