struct2schema

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	TypeMappings   map[string]string            `yaml:"type_mappings"`
	CustomDefaults map[string]map[string]string `yaml:"custom_defaults"`
	TableNaming    TableNamingConfig            `yaml:"table_naming"`
}

Config represents the configuration for struct2schema conversion

func GetDefaultConfig

func GetDefaultConfig() *Config

GetDefaultConfig returns a default configuration

func LoadConfig

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

LoadConfig loads configuration from a YAML file

func (*Config) AddCustomDefault

func (c *Config) AddCustomDefault(database, key, value string)

AddCustomDefault adds a custom default value for a database

func (*Config) AddTypeMapping

func (c *Config) AddTypeMapping(goType, sqlType string)

AddTypeMapping adds a custom type mapping

func (*Config) GetCustomDefault

func (c *Config) GetCustomDefault(database, key string) (string, bool)

GetCustomDefault gets a custom default value

func (*Config) GetTypeMapping

func (c *Config) GetTypeMapping(goType string) (string, bool)

GetTypeMapping gets a custom type mapping

func (*Config) SaveConfig

func (c *Config) SaveConfig(filename string) error

SaveConfig saves configuration to a YAML file

type ForeignKeyInfo

type ForeignKeyInfo struct {
	Table    string
	Column   string
	OnDelete string
	OnUpdate string
}

ForeignKeyInfo contains foreign key relationship information

type Generator

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

Generator handles generation of YAML schema from Go structs

func NewGenerator

func NewGenerator(typeMapper *TypeMapper, tagParser *TagParser, verbose bool) *Generator

NewGenerator creates a new schema generator

func (*Generator) GenerateSchema

func (g *Generator) GenerateSchema(structs []GoStruct, relationships []Relationship) (*types.Schema, error)

GenerateSchema generates a complete schema from Go structs and relationships

type GoField

type GoField struct {
	Name           string
	Type           string
	Tag            string
	IsPointer      bool
	IsSlice        bool
	IsEmbedded     bool
	IsExported     bool
	UnderlyingType string // For custom types
	Position       token.Pos
}

GoField represents a field within a Go struct

type GoStruct

type GoStruct struct {
	Name       string
	Package    string
	Fields     []GoField
	Tags       map[string]string // struct-level tags
	SourceFile string
	Position   token.Pos
}

GoStruct represents a parsed Go struct with metadata

type ManyToManyInfo

type ManyToManyInfo struct {
	Table      string
	JoinTable  string
	ForeignKey string
	References string
}

ManyToManyInfo contains many-to-many relationship information

type Processor

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

Processor handles the conversion from Go structs to YAML schema

func NewProcessor

func NewProcessor(config ProcessorConfig) (*Processor, error)

NewProcessor creates a new struct2schema processor

func (*Processor) Process

func (p *Processor) Process() error

Process executes the complete struct2schema conversion pipeline

type ProcessorConfig

type ProcessorConfig struct {
	InputDir   string
	OutputFile string
	ConfigFile string
	TargetDB   string
	DryRun     bool
	Verbose    bool
}

ProcessorConfig holds configuration for the struct2schema processor

type Relationship

type Relationship struct {
	Type          RelationshipType
	SourceStruct  string
	SourceTable   string
	SourceField   string
	TargetStruct  string
	TargetTable   string
	TargetField   string
	JunctionTable string
	OnDelete      string
	OnUpdate      string
}

Relationship represents a relationship between two structs/tables

type RelationshipDetector

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

RelationshipDetector analyzes Go structs to detect relationships

func NewRelationshipDetector

func NewRelationshipDetector(verbose bool) *RelationshipDetector

NewRelationshipDetector creates a new relationship detector

func (*RelationshipDetector) DetectRelationships

func (rd *RelationshipDetector) DetectRelationships(structs []GoStruct) []Relationship

DetectRelationships analyzes structs to identify relationships between them

func (*RelationshipDetector) GetRelationshipsBySource

func (rd *RelationshipDetector) GetRelationshipsBySource(relationships []Relationship, sourceStruct string) []Relationship

GetRelationshipsBySource filters relationships by source struct

func (*RelationshipDetector) GetRelationshipsByTarget

func (rd *RelationshipDetector) GetRelationshipsByTarget(relationships []Relationship, targetStruct string) []Relationship

GetRelationshipsByTarget filters relationships by target struct

func (*RelationshipDetector) GetRelationshipsByType

func (rd *RelationshipDetector) GetRelationshipsByType(relationships []Relationship, relType RelationshipType) []Relationship

GetRelationshipsByType filters relationships by type

type RelationshipType

type RelationshipType int

RelationshipType represents the type of relationship between structs

const (
	RelationshipForeignKey RelationshipType = iota
	RelationshipManyToMany
)

type Scanner

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

Scanner handles scanning Go source files and extracting struct definitions

func NewScanner

func NewScanner(verbose bool) *Scanner

NewScanner creates a new Go source code scanner

func (*Scanner) ScanDirectory

func (s *Scanner) ScanDirectory(rootDir string) ([]GoStruct, error)

ScanDirectory recursively scans a directory for Go files and extracts struct definitions

type TableNamingConfig

type TableNamingConfig struct {
	ConvertCase string `yaml:"convert_case"` // snake_case, camelCase, PascalCase
	Prefix      string `yaml:"prefix"`
	Suffix      string `yaml:"suffix"`
}

TableNamingConfig configures how table names are generated

type TagInfo

type TagInfo struct {
	ColumnName string
	Type       string
	Length     int
	Precision  int
	Scale      int
	PrimaryKey bool
	Nullable   *bool
	Default    string
	AutoCreate bool
	AutoUpdate bool
	ForeignKey *ForeignKeyInfo
	ManyToMany *ManyToManyInfo
	Index      bool
	Unique     bool
	Ignore     bool
}

TagInfo contains parsed information from struct field tags

type TagParser

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

TagParser handles parsing of struct field tags

func NewTagParser

func NewTagParser() *TagParser

NewTagParser creates a new tag parser with priority ordering

func (*TagParser) GetTableName

func (tp *TagParser) GetTableName(tags map[string]string) string

GetTableName extracts table name from struct tags

func (*TagParser) ParseTags

func (tp *TagParser) ParseTags(tagString string) TagInfo

ParseTags extracts tag information from a struct field tag string

type TypeMapper

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

TypeMapper handles mapping Go types to SQL types

func NewTypeMapper

func NewTypeMapper(configFile, targetDB string) (*TypeMapper, error)

NewTypeMapper creates a new type mapper

func (*TypeMapper) CreateDefaultsForAllDBs

func (tm *TypeMapper) CreateDefaultsForAllDBs() types.Defaults

CreateDefaultsForAllDBs creates default mappings for all supported databases

func (*TypeMapper) GetDefaults

func (tm *TypeMapper) GetDefaults() map[string]string

GetDefaults returns the default values for the target database

func (*TypeMapper) MapType

func (tm *TypeMapper) MapType(goType string, isPointer, isSlice bool, tagInfo TagInfo) (string, int, int, int, *bool)

MapType maps a Go type to a SQL type

type TypeMapping

type TypeMapping struct {
	SQLType   string
	Length    int
	Precision int
	Scale     int
	Nullable  *bool
}

TypeMapping represents a mapping from Go type to SQL type

type Writer

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

Writer handles writing schema files and previewing output

func NewWriter

func NewWriter(verbose bool) *Writer

NewWriter creates a new schema writer

func (*Writer) CreateOutputDirectory

func (w *Writer) CreateOutputDirectory(outputPath string) error

CreateOutputDirectory creates the output directory if it doesn't exist

func (*Writer) PreviewSchema

func (w *Writer) PreviewSchema(schema *types.Schema) error

PreviewSchema prints the schema to stdout for dry-run mode

func (*Writer) ValidateOutputPath

func (w *Writer) ValidateOutputPath(outputPath string) error

ValidateOutputPath validates that the output path is suitable for writing

func (*Writer) WriteSchema

func (w *Writer) WriteSchema(schema *types.Schema, outputPath string) error

WriteSchema writes a schema to a YAML file

Jump to

Keyboard shortcuts

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