Skip to content

Commit

Permalink
Add json tags to structs
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondl committed Nov 12, 2017
1 parent 872f7e7 commit be0a8da
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 74 deletions.
22 changes: 11 additions & 11 deletions drivers/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
// Column holds information about a database column.
// Types are Go types, converted by TranslateColumnType.
type Column struct {
Name string
Type string
DBType string
Default string
Nullable bool
Unique bool
Validated bool
Name string `json:"name"`
Type string `json:"type"`
DBType string `json:"db_type"`
Default string `json:"default"`
Nullable bool `json:"nullable"`
Unique bool `json:"unique"`
Validated bool `json:"validated"`

// Postgres only extension bits
// ArrType is the underlying data type of the Postgres
// ARRAY type. See here:
// https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html
ArrType *string
UDTName string
ArrType *string `json:"arr_type"`
UDTName string `json:"udt_name"`

// MySQL only bits
// Used to get full type, ex:
// tinyint(1) instead of tinyint
// Used for "tinyint-as-bool" flag
FullDBType string
FullDBType string `json:"full_db_type"`

// MS SQL only bits
// Used to indicate that the value
// for this column is auto generated by database on insert (i.e. - timestamp (old) or rowversion (new))
AutoGenerated bool
AutoGenerated bool `json:"auto_generated"`
}

// ColumnNames of the columns.
Expand Down
25 changes: 13 additions & 12 deletions drivers/interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package drivers supplies the sql(b)oiler (d)ata(b)ase abstractions.
// Package drivers talks to various database backends and retrieves table,
// column, type, and foreign key information
package drivers

import (
Expand All @@ -11,24 +12,24 @@ import (
// Interface abstracts either a side-effect imported driver or a binary
// that is called in order to produce the data required for generation.
type Interface interface {
Assemble(config map[string]interface{}) (*Assembly, error)
Assemble(config map[string]interface{}) (*DBInfo, error)
}

// Assembly is the database's properties and the table data from within.
type Assembly struct {
Tables []Table
Dialect Dialect
// DBInfo is the database's table data and dialect.
type DBInfo struct {
Tables []Table `json:"tables"`
Dialect Dialect `json:"dialect"`
}

// Dialect describes the databases requirements in terms of which features
// it speaks and what kind of quoting mechanisms it uses.
type Dialect struct {
LQ rune
RQ rune
LQ string `json:"lq"`
RQ string `json:"rq"`

UseIndexPlaceholders bool
UseLastInsertID bool
UseTopClause bool
UseIndexPlaceholders bool `json:"use_index_placeholders"`
UseLastInsertID bool `json:"use_last_insert_id"`
UseTopClause bool `json:"use_top_clause"`
}

// Constructor breaks down the functionality required to implement a driver
Expand Down Expand Up @@ -103,7 +104,7 @@ func filterForeignKeys(t *Table, whitelist, blacklist []string) {
var fkeys []ForeignKey
for _, fkey := range t.FKeys {
if (len(whitelist) == 0 || strmangle.SetInclude(fkey.ForeignTable, whitelist)) &&
(len(blacklist) == 0 || !strmangle.SetInclude(fkey.ForeignTable, blacklist)) {
(len(blacklist) == 0 || !strmangle.SetInclude(fkey.ForeignTable, blacklist)) {
fkeys = append(fkeys, fkey)
}
}
Expand Down
24 changes: 12 additions & 12 deletions drivers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import "fmt"

// PrimaryKey represents a primary key constraint in a database
type PrimaryKey struct {
Name string
Columns []string
Name string `json:"name"`
Columns []string `json:"columns"`
}

// ForeignKey represents a foreign key constraint in a database
type ForeignKey struct {
Table string
Name string
Column string
Nullable bool
Unique bool

ForeignTable string
ForeignColumn string
ForeignColumnNullable bool
ForeignColumnUnique bool
Table string `json:"table"`
Name string `json:"name"`
Column string `json:"column"`
Nullable bool `json:"nullable"`
Unique bool `json:"unique"`

ForeignTable string `json:"foreign_table"`
ForeignColumn string `json:"foreign_column"`
ForeignColumnNullable bool `json:"foreign_column_nullable"`
ForeignColumnUnique bool `json:"foreign_column_unique"`
}

// SQLColumnDef formats a column name and type like an SQL column definition.
Expand Down
62 changes: 31 additions & 31 deletions drivers/relationships.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
package drivers

// ToOneRelationship describes a relationship between two tables where the local
// table has no id, and the foregin table has an id that matches a column in the
// local table, that column is also unique which changes the dynamic into a
// table has no id, and the foreign table has an id that matches a column in the
// local table, that column can also be unique which changes the dynamic into a
// one-to-one style, not a to-many.
type ToOneRelationship struct {
Table string
Column string
Nullable bool
Unique bool

ForeignTable string
ForeignColumn string
ForeignColumnNullable bool
ForeignColumnUnique bool
Table string `json:"table"`
Column string `json:"column"`
Nullable bool `json:"nullable"`
Unique bool `json:"unique"`

ForeignTable string `json:"foreign_table"`
ForeignColumn string `json:"foreign_column"`
ForeignColumnNullable bool `json:"foreign_column_nullable"`
ForeignColumnUnique bool `json:"foreign_column_unique"`
}

// ToManyRelationship describes a relationship between two tables where the
// local table has no id, and the foreign table has an id that matches a column
// in the local table.
type ToManyRelationship struct {
Table string
Column string
Nullable bool
Unique bool

ForeignTable string
ForeignColumn string
ForeignColumnNullable bool
ForeignColumnUnique bool

ToJoinTable bool
JoinTable string

JoinLocalColumn string
JoinLocalColumnNullable bool
JoinLocalColumnUnique bool

JoinForeignColumn string
JoinForeignColumnNullable bool
JoinForeignColumnUnique bool
Table string `json:"table"`
Column string `json:"column"`
Nullable bool `json:"nullable"`
Unique bool `json:"unique"`

ForeignTable string `json:"foreign_table"`
ForeignColumn string `json:"foreign_column"`
ForeignColumnNullable bool `json:"foreign_column_nullable"`
ForeignColumnUnique bool `json:"foreign_column_unique"`

ToJoinTable bool `json:"to_join_table"`
JoinTable string `json:"join_table"`

JoinLocalColumn string `json:"join_local_column"`
JoinLocalColumnNullable bool `json:"join_local_column_nullable"`
JoinLocalColumnUnique bool `json:"join_local_column_unique"`

JoinForeignColumn string `json:"join_foreign_column"`
JoinForeignColumnNullable bool `json:"join_foreign_column_nullable"`
JoinForeignColumnUnique bool `json:"join_foreign_column_unique"`
}

// ToOneRelationships relationship lookups
Expand Down
16 changes: 8 additions & 8 deletions drivers/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import "fmt"

// Table metadata from the database schema.
type Table struct {
Name string
Name string `json:"name"`
// For dbs with real schemas, like Postgres.
// Example value: "schema_name"."table_name"
SchemaName string
Columns []Column
SchemaName string `json:"schema_name"`
Columns []Column `json:"columns"`

PKey *PrimaryKey
FKeys []ForeignKey
PKey *PrimaryKey `json:"p_key"`
FKeys []ForeignKey `json:"f_keys"`

IsJoinTable bool
IsJoinTable bool `json:"is_join_table"`

ToOneRelationships []ToOneRelationship
ToManyRelationships []ToManyRelationship
ToOneRelationships []ToOneRelationship `json:"to_one_relationships"`
ToManyRelationships []ToManyRelationship `json:"to_many_relationships"`
}

// GetTable by name. Panics if not found (for use in templates mostly).
Expand Down

0 comments on commit be0a8da

Please sign in to comment.