types

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package types provides PostgreSQL-specific type mappings.

Package types provides language-agnostic type representations and mappings between SQL types and programming language types.

Package types provides SQLite-specific type mappings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractPackageName

func ExtractPackageName(importPath string) string

ExtractPackageName extracts the package name from a Go import path e.g., "github.com/example/types" -> "types"

func FormatPostgresArray

func FormatPostgresArray(slice any) string

FormatPostgresArray formats a Go slice for PostgreSQL array parameter.

func GetCommonNullTypes

func GetCommonNullTypes() map[string]string

GetCommonNullTypes returns commonly used pgtype NULL wrapper types.

func GetDriverImport

func GetDriverImport() string

GetDriverImport returns the import path for the PostgreSQL driver.

func GetDriverPackage

func GetDriverPackage() string

GetDriverPackage returns the package name for the PostgreSQL driver.

func GetNullPackage

func GetNullPackage() (importPath, packageName string)

GetNullPackage returns the package to use for NULL handling. For pgx, we use pgtype package.

func GetPgxScanFunc

func GetPgxScanFunc(semantic SemanticType) string

GetPgxScanFunc returns the appropriate pgx scan function for the type.

func GetPostgresArrayType

func GetPostgresArrayType(elementType string) string

GetPostgresArrayType returns the pgtype array type name.

func IsPgxNativeType

func IsPgxNativeType(semantic SemanticType) bool

IsPgxNativeType returns true if the type is natively supported by pgx.

func PostgresConnectionString

func PostgresConnectionString(host string, port int, database, user, password string) string

PostgresConnectionString formats a connection string for PostgreSQL.

func PostgresDriverType

func PostgresDriverType() string

PostgresDriverType returns the driver type string for sql.Open.

Types

type FieldDef

type FieldDef struct {
	Name string
	Type SemanticType
}

FieldDef defines a field in a composite type

type GoMapper

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

GoMapper converts semantic types to Go types

func NewGoMapper

func NewGoMapper(customTypes []config.CustomTypeMapping, emitPointersForNull bool) *GoMapper

NewGoMapper creates a new Go type mapper

func (*GoMapper) GetDefaultValue

func (m *GoMapper) GetDefaultValue(semantic SemanticType) string

GetDefaultValue returns the default zero value for a Go type

func (*GoMapper) Map

func (m *GoMapper) Map(semantic SemanticType) LanguageType

Map converts a semantic type to a Go type

func (*GoMapper) Name

func (m *GoMapper) Name() string

Name returns the language identifier

type GoPostgresMapper

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

GoPostgresMapper converts semantic types to Go types for PostgreSQL (pgx).

func NewGoPostgresMapper

func NewGoPostgresMapper(customTypes []config.CustomTypeMapping) *GoPostgresMapper

NewGoPostgresMapper creates a new Go type mapper for PostgreSQL.

func (*GoPostgresMapper) GetScanType

func (m *GoPostgresMapper) GetScanType(semantic SemanticType) string

GetScanType returns the type used for scanning from pgx.Rows. For nullable types, pgx often uses pgtype types.

func (*GoPostgresMapper) Map

func (m *GoPostgresMapper) Map(semantic SemanticType) LanguageType

Map converts a semantic type to a Go type for PostgreSQL.

func (*GoPostgresMapper) Name

func (m *GoPostgresMapper) Name() string

Name returns the mapper name.

type LanguageMapper

type LanguageMapper interface {
	// Map converts a semantic type to a language-specific type
	Map(semantic SemanticType) LanguageType

	// Name returns the language identifier (e.g., "go", "rust", "typescript")
	Name() string
}

LanguageMapper is the interface for language-specific type mappings

type LanguageType

type LanguageType struct {
	// Name is the type name as it appears in code (e.g., "int64", "String")
	Name string

	// Import is the import path if an external package is needed
	// (e.g., "github.com/google/uuid", "time")
	Import string

	// Package is the package/module name for the import
	// (e.g., "uuid", "time", "std::collections")
	Package string

	// PointerPrefix is added before the type for pointers/references
	// (e.g., "*" for Go, "Option<" for Rust)
	PointerPrefix string

	// PointerSuffix is added after the type for pointers/references
	// (e.g., ">" for Rust Option, empty for Go pointers)
	PointerSuffix string

	// IsNullable indicates if this type naturally supports null
	IsNullable bool
}

LanguageType represents a type in a specific programming language

func (LanguageType) FullType

func (lt LanguageType) FullType(nullable bool) string

FullType returns the type name with pointer/null handling

type PostgresMapper

type PostgresMapper struct{}

PostgresMapper converts PostgreSQL type names to semantic types. Supports PostgreSQL-specific types like arrays, JSONB, UUID, etc.

func NewPostgresMapper

func NewPostgresMapper() *PostgresMapper

NewPostgresMapper creates a new PostgreSQL type mapper.

func (*PostgresMapper) Map

func (m *PostgresMapper) Map(sqlType string, nullable bool) SemanticType

Map converts a PostgreSQL type declaration to a semantic type. PostgreSQL types are case-insensitive and may include type modifiers.

type RustMapper

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

RustMapper (for future use) Placeholder showing how to add new languages.

func NewRustMapper

func NewRustMapper() *RustMapper

NewRustMapper creates a new Rust type mapper.

func (*RustMapper) Map

func (m *RustMapper) Map(semantic SemanticType) LanguageType

Map converts a semantic type to a Rust type.

func (*RustMapper) Name

func (m *RustMapper) Name() string

Name returns the language identifier.

type SQLiteMapper

type SQLiteMapper struct{}

SQLiteMapper converts SQLite type names to semantic types. SQLite uses dynamic typing, so we infer the semantic meaning from the declared type name.

func NewSQLiteMapper

func NewSQLiteMapper() *SQLiteMapper

NewSQLiteMapper creates a new SQLite type mapper.

func (*SQLiteMapper) Map

func (m *SQLiteMapper) Map(sqlType string, nullable bool) SemanticType

Map converts a SQLite type declaration to a semantic type. SQLite types are case-insensitive and may include length constraints.

type SemanticType

type SemanticType struct {
	Category SemanticTypeCategory
	Nullable bool

	// Precision is the total number of digits for decimal types
	Precision int

	// Scale is the number of digits after the decimal point
	Scale int

	// MaxLength is the maximum length for string types (-1 = unlimited)
	MaxLength int

	// EnumValues contains the allowed values for enum types
	EnumValues []string

	// ElementType is the type of array elements (for CategoryArray)
	ElementType *SemanticType

	// Fields contains the field definitions for composite types
	Fields []FieldDef

	// CustomName is the name of custom type (for CategoryCustom)
	CustomName string

	// CustomPackage is the package/module for custom type
	CustomPackage string
}

SemanticType represents a type with semantic meaning, independent of SQL dialect or programming language.

func (SemanticType) Clone

func (s SemanticType) Clone() SemanticType

Clone creates a deep copy of the semantic type

func (SemanticType) IsNumeric

func (s SemanticType) IsNumeric() bool

IsNumeric returns true if this is a numeric type

func (SemanticType) IsTemporal

func (s SemanticType) IsTemporal() bool

IsTemporal returns true if this is a date/time type

func (SemanticType) IsText

func (s SemanticType) IsText() bool

IsText returns true if this is a text/string type

type SemanticTypeCategory

type SemanticTypeCategory int

SemanticTypeCategory represents the semantic meaning of a type, independent of any programming language or database system.

const (
	// CategoryUnknown represents an unrecognized or unspecified type.
	CategoryUnknown SemanticTypeCategory = iota

	// CategoryInteger represents a 32-bit signed integer.
	CategoryInteger
	// CategoryBigInteger represents a 64-bit signed integer.
	CategoryBigInteger
	// CategorySmallInteger represents a 16-bit signed integer.
	CategorySmallInteger
	// CategoryTinyInteger represents an 8-bit signed integer.
	CategoryTinyInteger
	// CategoryDecimal represents an exact decimal with precision/scale.
	CategoryDecimal
	// CategoryFloat represents a 32-bit IEEE 754 float.
	CategoryFloat
	// CategoryDouble represents a 64-bit IEEE 754 float.
	CategoryDouble
	// CategoryNumeric represents a generic numeric (database decides precision).
	CategoryNumeric

	// CategoryText represents a variable-length text.
	CategoryText
	// CategoryChar represents a fixed-length character.
	CategoryChar
	// CategoryVarchar represents a variable-length string with max size.
	CategoryVarchar
	// CategoryBlob represents binary data.
	CategoryBlob
	// CategoryBytea represents PostgreSQL binary type.
	CategoryBytea

	// CategoryTimestamp represents a date and time with timezone.
	CategoryTimestamp
	// CategoryTimestampTZ represents a timestamp with timezone.
	CategoryTimestampTZ
	// CategoryDate represents a date only.
	CategoryDate
	// CategoryTime represents a time only.
	CategoryTime
	// CategoryTimeTZ represents a time with timezone.
	CategoryTimeTZ
	// CategoryInterval represents a time duration.
	CategoryInterval

	// CategoryBoolean represents a boolean type.
	CategoryBoolean

	// CategoryUUID represents a UUID type.
	CategoryUUID
	// CategoryJSON represents a JSON type.
	CategoryJSON
	// CategoryJSONB represents binary JSON (PostgreSQL).
	CategoryJSONB
	// CategoryXML represents an XML type.
	CategoryXML
	// CategoryEnum represents an enumeration type.
	CategoryEnum
	// CategoryArray represents an array of another type.
	CategoryArray
	// CategoryComposite represents a struct-like composite type.
	CategoryComposite
	// CategoryCustom represents a user-defined custom type.
	CategoryCustom
	// CategorySerial represents an auto-incrementing integer.
	CategorySerial
	// CategoryBigSerial represents an auto-incrementing big integer.
	CategoryBigSerial

	// MySQL-specific categories
	// CategoryMediumInteger represents a 24-bit signed integer (MySQL MEDIUMINT).
	CategoryMediumInteger
	// CategoryTinyText represents a tiny text (MySQL TINYTEXT).
	CategoryTinyText
	// CategoryMediumText represents a medium text (MySQL MEDIUMTEXT).
	CategoryMediumText
	// CategoryLongText represents a long text (MySQL LONGTEXT).
	CategoryLongText
	// CategoryTinyBlob represents a tiny blob (MySQL TINYBLOB).
	CategoryTinyBlob
	// CategoryMediumBlob represents a medium blob (MySQL MEDIUMBLOB).
	CategoryMediumBlob
	// CategoryLongBlob represents a long blob (MySQL LONGBLOB).
	CategoryLongBlob
	// CategoryBinary represents a binary type (MySQL BINARY/VARBINARY).
	CategoryBinary
	// CategoryDateTime represents a date and time (MySQL DATETIME).
	CategoryDateTime
	// CategoryYear represents a year type (MySQL YEAR).
	CategoryYear
	// CategorySet represents a set type (MySQL SET).
	CategorySet
)

func (SemanticTypeCategory) String

func (c SemanticTypeCategory) String() string

String returns a human-readable name for the category

Jump to

Keyboard shortcuts

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