src

package
v0.0.0-...-2b38c4f Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTabToSlice

func AddTabToSlice(values *[]string)

AddTabToSlice adds a tab character ("\t") to the beginning of each string in the given slice.//+ //+ The function takes a pointer to a slice of strings as input. It iterates over each string in the slice//+ and prepends a tab character to it. The modified slice is then returned.//+ //+ Parameters://+ - values: A pointer to a slice of strings. The function modifies this slice in-place.//+ //+ Return://+ The function does not return a value, but it modifies the slice pointed to by the 'values' parameter.//+

func CreateInterface

func CreateInterface(sql SQL) string

CreateInterface generates a TypeScript interface based on the provided SQL table definition. The function iterates through the columns of the SQL table and constructs the interface fields.

Parameters: - sql: A SQL struct containing the table name and column details.

Return: - string: A string representing the TypeScript interface definition.

The interface definition follows the format:

interface TableName {
    columnName: columnType,
    ...
}

func CreateStruct

func CreateStruct(sql SQL) string

CreateStruct generates a Go struct based on the provided SQL table definition. It iterates through the columns of the SQL table and constructs the struct fields.

Parameters: - sql: A SQL struct containing the table name and column details.

Return: - string: A string representing the Go struct definition.

The struct definition follows the format:

type TableName struct {
    columnName columnType
    ...
}

func GetFileContent

func GetFileContent(dir string, fileName string) (string, error)

GetFileContent retrieves the content of a file within a specified directory.

Parameters: - dir: The path to the directory where the file is located. - fileName: The name of the file to be read.

Returns: - A string containing the content of the file. - An error if any occurred during file reading.

func GetFiles

func GetFiles(dir string) ([]os.DirEntry, error)

GetFiles retrieves a list of files within a specified directory.

dir: The path to the directory to be scanned.

Returns: - A slice of os.DirEntry objects representing the files and directories found within the specified directory. - An error if any occurred during the directory scanning process.

func IsColumnIgnored

func IsColumnIgnored(fileName string, columnName string, ignoreColumns map[string][]string) bool

IsColumnIgnored checks if a given column name is present in a list of ignored columns for a specific file.//+ It returns true if the column is ignored, and false otherwise.//+ //+ Parameters://+ - fileName: The name of the file to check.//+ - columnName: The name of the column to check.//+ - ignoreColumns: A map where the keys are file names and the values are slices of strings containing the names of the columns to be ignored.//+ //+ Return://+ - A boolean value indicating whether the column is ignored. If true, the column is ignored.//+

func IsDir

func IsDir(filePath string) (bool, error)

IsDir checks if the specified file path represents a directory.

filePath: The path to the file or directory to be checked.

Returns:

  • A boolean value indicating whether the specified file path represents a directory. Returns true if the path is a directory, false otherwise.
  • An error if any occurred during the file or directory check. If the function completes successfully, it returns nil.

func IsFileIgnored

func IsFileIgnored(fileName string, ignoreFiles []string) bool

IsFileIgnored checks if a given file name is present in a list of ignored files.//+ It returns true if the file is ignored, and false otherwise.//+ //+ Parameters://+ - fileName: The name of the file to check.//+ - ignoreFiles: A slice of strings containing the names of the files to be ignored.//+ //+ Return://+ - A boolean value indicating whether the file is ignored. If true, the file is ignored.//+

func SaveFile

func SaveFile(dir string, fileName string, fileContent string) error

SaveFile writes the provided file content to a specified file within a directory.

Parameters: - dir: The path to the directory where the file will be saved. - fileName: The name of the file to be created or overwritten. - fileContent: The content to be written to the file.

Returns: - An error if any occurred during file writing. If the function completes successfully, it returns nil.

func StringToInterfaceSlice

func StringToInterfaceSlice(arr []string) []interface{}

StringToInterfaceSlice converts a slice of strings to a slice of empty interfaces.//+ //+ The function takes a slice of strings as input and returns a new slice of empty interfaces.//+ Each element in the input slice is assigned to the corresponding element in the output slice.//+ //+ Parameters://+ - arr: A slice of strings to be converted.//+ //+ Return://+ - A new slice of empty interfaces, where each element is the corresponding element from the input slice.//+

func TypeMapper

func TypeMapper(definitionType string, colType string) string

TypeMapper maps SQL column types to their corresponding TypeScript types. It takes a string representing an SQL column type as input and returns a string representing the corresponding TypeScript type.

Parameters: - colType (string): The SQL column type to be mapped.

Return: - string: The corresponding typescript or go type.

func ValidateCreateStatement

func ValidateCreateStatement(statement string) error
VALIDATE

ValidateCreateStatement checks if the given SQL statement is a valid CREATE TABLE statement.

Parameters: - statement: A string representing the SQL statement to be validated.

Return:

  • error: An error indicating an invalid CREATE TABLE statement if the statement is not valid. Returns nil if the statement is valid.

func ValidateInputOutput

func ValidateInputOutput(input string, output string) error

ValidateInputOutput checks if the input and output file paths are not empty.

Parameters: - input: A string representing the input file path. - output: A string representing the output file path.

Return:

  • error: An error indicating that either the input or output file path is empty. Returns nil if both paths are not empty.

func ValueInSlice

func ValueInSlice(value interface{}, slice []interface{}) bool

ValueInSlice checks if a given value exists in a slice of empty interfaces.//+ //+ The function iterates over each element in the provided slice and compares it with the given value.//+ If a match is found, the function returns true. Otherwise, it returns false.//+ //+ Parameters://+ - value: The value to be checked for existence in the slice.//+ - slice: The slice of empty interfaces to search for the given value.//+ //+ Return://+ - A boolean value indicating whether the given value exists in the slice.//+

Types

type ArbitraryField

type ArbitraryField struct {
	Name   string `yaml:"name"`
	TypeGo string `yaml:"type_go"`
	TypeTs string `yaml:"type_ts"`
}

type Column

type Column struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

func CombineTables

func CombineTables(interfaceName string, interfaceDefinitions ...SQL) []Column

CombineTables combines multiple SQL table definitions into a single SQL table definition. It takes an interface name and a variable number of SQL table definitions as input. The function returns a slice of Column structs representing the combined SQL table definition.

Parameters: - interfaceName (string): The name of the interface that will be created for the combined table. - interfaceDefinitions (SQL)...: A variable number of SQL table definitions to be combined.

Return: - []Column: A slice of Column structs representing the combined SQL table definition.

type Combiner

type Combiner struct {
	Tables              []string `json:"tables"`
	Amount              int      `json:"amount"`
	InterfaceName       string   `json:"interface_name"`
	TableDefinitions    []SQL    `json:"table_definitions"`
	ConvertSingleTables bool     `json:"convert_single_tables"`
}

type Config

type Config struct {
	IgnoreFiles     []string                             `yaml:"ignore_files"`
	IgnoreColumns   map[string][]string                  `yaml:"ignore_columns"`
	CombineTables   map[string]TableCombine              `yaml:"combine_tables"`
	Input           string                               `yaml:"input"`
	Output          map[string]map[string]string         `yaml:"output"`
	SingleFile      bool                                 `yaml:"single_file"`
	ArbitraryFields map[string]map[string]ArbitraryField `yaml:"arbitrary_fields"`
}

func LoadConfig

func LoadConfig(filePath string) (*Config, error)

LoadConfig reads a YAML configuration file and unmarshals its content into a Config struct.

filePath: The path to the YAML configuration file.

Returns: - A pointer to a Config struct containing the unmarshalled configuration data. - An error if any occurred during file reading or unmarshalling.

type ConvertedStructure

type ConvertedStructure struct {
	StructureDefinition map[string]string
	StructureNames      map[string][]string
}

type Field

type Field struct {
	Name string `yaml:"name"`
	Type string `yaml:"type"`
}

type SQL

type SQL struct {
	FileName  string   `json:"file_name"`
	TableName string   `json:"table_name"`
	Columns   []Column `json:"columns"`
}

type SQL2Interface

type SQL2Interface struct {
	Config   *Config
	Sql      SQL
	Combiner map[string][]Combiner
}

func NewSQL2Interface

func NewSQL2Interface(confDir string) *SQL2Interface

NewSQL2Interface initializes a new SQL2Interface instance with the provided configuration directory, source, and target. It loads the configuration and combiner settings, and returns a pointer to the new instance.

confDir: The directory path where the configuration file is located. source: The source directory or file path for SQL files to be converted. target: The target directory where the converted interface files will be saved.

Returns: A pointer to a new SQL2Interface instance.

func (*SQL2Interface) AddArbitraryFields

func (s2i *SQL2Interface) AddArbitraryFields(sql *SQL, definitionType string)

AddArbitraryFields adds arbitrary fields to the SQL table definition based on the configuration. It iterates through the arbitrary fields specified in the configuration and adds them to the SQL table definition.

Parameters: - sql: A pointer to the SQL struct representing the table definition to which arbitrary fields will be added.

The function checks if the file name of the SQL table matches the file name specified in the configuration. If a match is found, it prints a message indicating the addition of the arbitrary field and appends the field to the SQL table definition.

func (*SQL2Interface) AddInterfaceExports

func (s2i *SQL2Interface) AddInterfaceExports(content *string, interfaceNames []string)

AddInterfaceExports adds export statements for the given interface names to the content string. It first adds a tab to each interface name in the slice. Then it formats the content string with the export statements for the interface names.

Parameters: - content: A pointer to a string representing the content where the export statements will be added. - interfaceNames: A slice of strings representing the names of the interfaces to be exported.

func (*SQL2Interface) AddToCombiner

func (s2i *SQL2Interface) AddToCombiner(definitionType string, definition SQL) (bool, int)

AddToCombiner checks if the SQL table definition's file name is in the list of tables to combine. If it is, the function appends the table definition to the corresponding combiner and returns true along with the index of the combiner. If the file name is not found in any of the combiners, the function returns false and -1.

Parameters: - definition (SQL): The SQL table definition to be added to the combiner.

Return: - bool: Indicates whether the table definition was added to a combiner (true) or not (false). - int: The index of the combiner in the SQL2Interface's Combiner slice. If no combiner was found, it returns -1.

func (*SQL2Interface) CombinerToStructure

func (s2i *SQL2Interface) CombinerToStructure(output *ConvertedStructure) error

CombinerToStructure converts combined SQL table definitions to interfaces or structs. It iterates through the combiners for each output type (TypeScript and Go) and combines the table definitions. For each combiner, it creates a new SQL table definition with the combined columns and appends it to the output structure. If the output type is TypeScript, it adds the interface definition to the TypeScript section of the output structure. If the output type is Go, it adds the struct definition to the Go section of the output structure. Finally, it returns nil to indicate successful execution.

func (*SQL2Interface) Convert

func (s2i *SQL2Interface) Convert(files []fs.DirEntry)

Convert processes a single SQL file and converts it into an interface or struct based on the configuration settings. It checks if the file should be ignored, retrieves the file content, parses the SQL, adds the parsed data to the combiner, and creates and saves the interface or struct files based on the configuration settings.

Parameters: fileName (string): The name of the SQL file to be converted. If the source directory is a file, this parameter should be an empty string.

Return: This function does not return any value. However, it prints error messages if any errors occur during the conversion process.

func (*SQL2Interface) ConvertSingleTable

func (s2i *SQL2Interface) ConvertSingleTable(definitionType string, index int) bool

ConvertSingleTable checks if the single table conversion is enabled for a combiner.

Parameters: - index (int): The index of the combiner in the SQL2Interface's Combiner slice.

Return: - bool: Indicates whether single table conversion is enabled for the combiner (true) or not (false).

func (*SQL2Interface) LoadCombiner

func (s2i *SQL2Interface) LoadCombiner()

LoadCombiner initializes and loads the combiner configuration from the SQL2Interface instance. It iterates through the combine_tables configuration and populates the Combiner slice with the parsed data. If the combine_tables configuration is not found or is empty, it prints a message and returns without any further action.

func (*SQL2Interface) LoadConfig

func (s2i *SQL2Interface) LoadConfig(confDir string)

LoadConfig initializes and loads the configuration from the provided directory path.

Parameters: confDir (string): The directory path where the configuration file is located.

Returns: This function does not return any value. However, it sets the Config field of the SQL2Interface instance to the loaded configuration. If an error occurs during the loading process, it panics with the error message.

func (*SQL2Interface) ParseRawTableName

func (s2i *SQL2Interface) ParseRawTableName(rawTableDefinition string) string

ParseRawTableName extracts and processes the table name from a raw SQL table definition. It removes SQL keywords like "CREATE", "TABLE", "IF", "NOT", and "EXISTS", and converts the remaining string to PascalCase.

Parameters: - rawTableDefinition (string): The raw SQL table definition string.

Return: - string: The processed table name in PascalCase.

func (*SQL2Interface) ParseRowColumnDefinitions

func (s2i *SQL2Interface) ParseRowColumnDefinitions(definitionType string, fileName string, rawColumnDefinitions string) ([]Column, error)

ParseRowColumnDefinitions parses a raw SQL column definitions string into a slice of Column structs. It extracts column names and types from the raw string, applies type mapping, and ignores specified columns.

Parameters: - fileName (string): The name of the SQL file being parsed. Used for logging and ignoring columns. - rawColumnDefinitions (string): The raw SQL column definitions string to be parsed.

Return: - []Column: A slice of Column structs containing the parsed column names and types. - error: An error encountered during the parsing process, or nil if no error occurred.

func (*SQL2Interface) ParseSQL

func (s2i *SQL2Interface) ParseSQL(definitionType string, fileName string, rawSQL string) (SQL, error)

ParseSQL parses a raw SQL string into a SQL struct and returns it along with any encountered error. It extracts the table name and column details from the raw SQL string and populates the SQL struct accordingly.

Parameters: - fileName (string): The name of the SQL file being parsed. - rawSQL (string): The raw SQL string to be parsed.

Return: - SQL: A SQL struct containing the parsed table name and column details. - error: An error encountered during the parsing process, or nil if no error occurred.

func (*SQL2Interface) Run

func (s2i *SQL2Interface) Run()

Run starts the conversion process for SQL files to TypeScript and Go interfaces/structs. It retrieves the list of SQL files from the input directory specified in the configuration. If any error occurs during file retrieval, it prints the error message. Finally, it calls the Convert function to perform the actual conversion.

type TableCombine

type TableCombine struct {
	Name                string   `yaml:"name"`
	Tables              []string `yaml:"tables"`
	ConvertSingleTables bool     `yaml:"convert_single_tables"`
}

Jump to

Keyboard shortcuts

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