tdconv

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2019 License: MIT Imports: 10 Imported by: 0

README

tdconv

CircleCI codecov GoDoc MIT License

A golang package and tool for converting table definitions to SQL and Go struct etc.

Description

Package tdconv

This package converts table definitions to SQL and Go struct etc. Currently this package supports SQL and Go format.

For example, if the table definition is like...

sample table

SQL is output as follows.

# This file generated by tdconv. DO NOT EDIT.
# See more details at https://github.com/takuoki/tdconv.
DROP TABLE IF EXISTS sample_table;
CREATE TABLE `sample_table` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'this is id!',
    `foo` VARCHAR(32) NOT NULL UNIQUE,
    `bar` VARCHAR(32),
    PRIMARY KEY (id),
    INDEX `bar_key` (bar)
);

Go struct is output as follows.

// This file generated by tdconv. DO NOT EDIT.
// See more details at https://github.com/takuoki/tdconv.
package main

import(
  "time"
)

type sampleTable struct {
  ID *int
  Foo *string
  Bar *string
}

You can change the header and footer text as you want. If you want to output with new format, you can do it with creating a new Formatter. And more, if the parsed Table data are not enough for you, you can modify them as you want. For usage of this package, see the standard tool which uses this package.

Tool tdconverter

This is a standard tool uses the package tdconv. For more details, see README.md.

Install

You can install this tool using go get. Before installing, enable the Go module feature.

go get github.com/takuoki/tdconv

Requirements

On parsing, this package uses Sheet objects in github.com/takuoki/gsheets package. For more details, see README.md in this package.

Usage

First, create a new Parser. If your sheet is different from the default sheet format, set ParseOption to match your sheet. And if you need some common columns to all tables, set them with SetCommonColumns method.

p, err := tdconv.NewParser()
if err != nil {
  return nil, fmt.Errorf("Unable to create new parser: %v", err)
}

err = p.SetCommonColumns(commonSheet)
if err != nil {
  return nil, fmt.Errorf("Unable to parse common sheet information: %v", err)
}

Then, parse your sheet with Parse method. Basically, just specify the sheet value returns by GetSheet method of the gsheets package. In case of parsing multiple sheets, loop it in your application.

var tables []*tdconv.Table
for _, sheetname := range sheets {
  sheet, err := gclient.GetSheet(ctx, id, sheetname)
  if err != nil {
    return nil, fmt.Errorf("Unable to get sheet values (sheetname=%s): %v", sheetname, err)
  }
  table, err := p.Parse(sheet)
  if err != nil {
    return nil, fmt.Errorf("Unable to parse sheet information (sheetname=%s): %v", sheetname, err)
  }
  tables = append(tables, table)
}

Finally, create TableSet based on some Tables you get above step, and output file(s) with formatter you need. For SQLFormatter or GoFormatter, you can change the header and footer text with SQLFormatOption or GoFormatOption. If the parsed Table data are not enough for you, you can modify them as you want before calling the Output function. If the multi flag, which is one of the arguments of the Output function, is true, one file is output for each Table. If false, one file is output for each TableSet.

tableSet := &tdconv.TableSet{
  Name:   title,
  Tables: tables,
}

f, err := tdconv.NewSQLFormatter()
if err != nil {
  return fmt.Errorf("Fail to create SQL formatter: %v", err)
}

if err := tdconv.Output(f, tableSet, multi, outdir); err != nil {
  return fmt.Errorf("Fail to output table definitions: %v", err)
}

If you create a new formatter, follow the Formatter interface below.

type Formatter interface {
  Fprint(w io.Writer, t *Table)
  Header(w io.Writer, ts *TableSet)
  TableHeader(w io.Writer, t *Table)
  TableFooter(w io.Writer, t *Table)
  Footer(w io.Writer, ts *TableSet)
  Extension() string
}
  • Fprint: Output table contents. This is main method of this interface.
  • Header: Output the header. This method is called only once for each file.
  • TableHeader: Output the table headers. This method is called before calling Fprint method, and called multiple times if the multi flag is false.
  • TableFooter: Output the table footers. This method is called after calling Fprint method, and called multiple times if the multi flag is false.
  • Footer: Output the footer. This method is called only once for each file.
  • Extension: Return file extension.

Documentation

Overview

Package tdconv converts table definitions to SQL and Go struct etc. Currently this package supports SQL and Go format. You can change the header and footer text as you want. If you want to output with new format, you can do it with creating a new Formatter. And more, if the parsed `Table` data are not enough for you, you can modify them as you want. For usage of this package, see the standard tool below which uses this package. - github.com/takuoki/tdconv/tools/tdconverter

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Output

func Output(f Formatter, ts *TableSet, multi bool, outdir string) error

Output outputs file(s) using Formatter.

Types

type Column

type Column struct {
	Name     string
	Type     string
	PKey     bool
	NotNull  bool
	Unique   bool
	Index    bool
	Option   string
	Comment  string
	IsCommon bool
}

Column is a struct of Column.

type Formatter

type Formatter interface {
	Fprint(w io.Writer, t *Table)
	Header(w io.Writer, ts *TableSet)
	TableHeader(w io.Writer, t *Table)
	TableFooter(w io.Writer, t *Table)
	Footer(w io.Writer, ts *TableSet)
	Extension() string
}

Formatter is an interface for formatting.

type GoFormatOption

type GoFormatOption func(*GoFormatter) error

GoFormatOption changes some parameters of the GoFormatter.

func GoFooter

func GoFooter(fc func(w io.Writer, ts *TableSet)) GoFormatOption

GoFooter changes the footer.

func GoHeader

func GoHeader(fc func(w io.Writer, ts *TableSet)) GoFormatOption

GoHeader changes the header.

func GoTableFooter

func GoTableFooter(fc func(w io.Writer, t *Table)) GoFormatOption

GoTableFooter changes the footer of each table.

func GoTableHeader

func GoTableHeader(fc func(w io.Writer, t *Table)) GoFormatOption

GoTableHeader changes the header of each table.

type GoFormatter

type GoFormatter struct {
	// contains filtered or unexported fields
}

GoFormatter is a formatter to output the table definision as Go struct.

func NewGoFormatter

func NewGoFormatter(options ...GoFormatOption) (*GoFormatter, error)

NewGoFormatter creates a new GoFormatter. You can change some parameters of the GoFormatter with GoFormatOption.

func (*GoFormatter) Extension

func (f *GoFormatter) Extension() string

Extension returns the extension of Go file.

func (*GoFormatter) Footer

func (f *GoFormatter) Footer(w io.Writer, ts *TableSet)

func (*GoFormatter) Fprint

func (f *GoFormatter) Fprint(w io.Writer, t *Table)

Fprint outputs the table definision as Go struct.

func (*GoFormatter) Header

func (f *GoFormatter) Header(w io.Writer, ts *TableSet)

func (*GoFormatter) TableFooter

func (f *GoFormatter) TableFooter(w io.Writer, t *Table)

func (*GoFormatter) TableHeader

func (f *GoFormatter) TableHeader(w io.Writer, t *Table)

type Key

type Key struct {
	Name    string
	Columns []string
}

Key is a struct of Key like Unique Key and Index Key.

type ParseOption

type ParseOption func(*Parser) error

ParseOption changes some parameters of the Parser.

func BoolString

func BoolString(str string) ParseOption

BoolString changes the bool string in the sheet.

func KeyNameFunc

func KeyNameFunc(f func(string) string) ParseOption

KeyNameFunc changes the function to convert the column name to the key name.

func StartRow

func StartRow(row int) ParseOption

StartRow changes the start row of column list.

func TableNamePos

func TableNamePos(row int, clm string) ParseOption

TableNamePos changes the position (row and column) of table name.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is a struct to parse the sheet values to the table object. Create it using NewParser function.

func NewParser

func NewParser(options ...ParseOption) (*Parser, error)

NewParser creates a new Parser. You can change some parameters of the Parser with ParseOption.

func (*Parser) Parse

func (p *Parser) Parse(s *gsheets.Sheet) (*Table, error)

Parse parses the sheet values to the table object.

func (*Parser) SetCommonColumns

func (p *Parser) SetCommonColumns(s *gsheets.Sheet) error

SetCommonColumns parses the common sheet values and sets them as common columns.

type SQLFormatOption

type SQLFormatOption func(*SQLFormatter) error

SQLFormatOption changes some parameters of the SQLFormatter.

func SQLFooter

func SQLFooter(fc func(w io.Writer, ts *TableSet)) SQLFormatOption

SQLFooter changes the footer.

func SQLHeader

func SQLHeader(fc func(w io.Writer, ts *TableSet)) SQLFormatOption

SQLHeader changes the header.

func SQLTableFooter

func SQLTableFooter(fc func(w io.Writer, t *Table)) SQLFormatOption

SQLTableFooter changes the footer of each table.

func SQLTableHeader

func SQLTableHeader(fc func(w io.Writer, t *Table)) SQLFormatOption

SQLTableHeader changes the header of each table.

type SQLFormatter

type SQLFormatter struct {
	// contains filtered or unexported fields
}

SQLFormatter is a formatter to output the table definision as SQL.

func NewSQLFormatter

func NewSQLFormatter(options ...SQLFormatOption) (*SQLFormatter, error)

NewSQLFormatter creates a new SQLFormatter. You can change some parameters of the SQLFormatter with SQLFormatOption.

func (*SQLFormatter) Extension

func (f *SQLFormatter) Extension() string

Extension returns the extension of SQL file.

func (*SQLFormatter) Footer

func (f *SQLFormatter) Footer(w io.Writer, ts *TableSet)

func (*SQLFormatter) Fprint

func (f *SQLFormatter) Fprint(w io.Writer, t *Table)

Fprint outputs the table definision as SQL.

func (*SQLFormatter) Header

func (f *SQLFormatter) Header(w io.Writer, ts *TableSet)

func (*SQLFormatter) TableFooter

func (f *SQLFormatter) TableFooter(w io.Writer, t *Table)

func (*SQLFormatter) TableHeader

func (f *SQLFormatter) TableHeader(w io.Writer, t *Table)

type Table

type Table struct {
	Name        string
	Columns     []Column
	PKeyColumns []string
	UniqueKeys  []Key
	IndexKeys   []Key
}

Table is a struct of table.

type TableSet

type TableSet struct {
	Name   string
	Tables []*Table
}

TableSet is a struct of a set of tables.

Directories

Path Synopsis
tools

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL