From be0a8daf41a9890bd812001256a6802d35809100 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sat, 11 Nov 2017 21:16:14 -0800 Subject: [PATCH] Add json tags to structs --- drivers/column.go | 22 +++++++------- drivers/interface.go | 25 ++++++++-------- drivers/keys.go | 24 ++++++++-------- drivers/relationships.go | 62 ++++++++++++++++++++-------------------- drivers/table.go | 16 +++++------ 5 files changed, 75 insertions(+), 74 deletions(-) diff --git a/drivers/column.go b/drivers/column.go index 4fe454dc4..0e4407c8f 100644 --- a/drivers/column.go +++ b/drivers/column.go @@ -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. diff --git a/drivers/interface.go b/drivers/interface.go index 299d06d5c..4aaa3e015 100644 --- a/drivers/interface.go +++ b/drivers/interface.go @@ -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 ( @@ -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 @@ -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) } } diff --git a/drivers/keys.go b/drivers/keys.go index 0286ed442..6a600750a 100644 --- a/drivers/keys.go +++ b/drivers/keys.go @@ -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. diff --git a/drivers/relationships.go b/drivers/relationships.go index 4c2884d34..19decbddf 100644 --- a/drivers/relationships.go +++ b/drivers/relationships.go @@ -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 diff --git a/drivers/table.go b/drivers/table.go index 5d35a6511..a49b6f0fb 100644 --- a/drivers/table.go +++ b/drivers/table.go @@ -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).