source

package
v0.0.0-...-47bdc06 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Field = map[string]func(*Source, reflect.StructField, int) string{
	"%Name": getName,
	"%Type": getType,
	"%With": func(src *Source, field reflect.StructField, index int) string {
		if field.Name == "Id" {
			return fmt.Sprintf("PRIMARY KEY %s", field.Tag.Get("with"))
		}
		return field.Tag.Get("with")
	},
	"%Tags": func(src *Source, field reflect.StructField, index int) string {
		return fmt.Sprintf("%v", field.Tag)
	},
	"%Value": func(src *Source, field reflect.StructField, index int) string {
		if src.V.Field(index).Kind() == reflect.Struct {
			table := registry.Tables[strcase.SnakeCase(strings.ReplaceAll(field.Name, "Fk", ""))]
			tableValue := reflect.ValueOf(table)
			return fmt.Sprintf("%v", tableValue.FieldByName("Id"))
		}
		if src.V.Field(index).Kind() == reflect.String {
			return fmt.Sprintf("%q", src.V.Field(index).Interface())
		}
		if field.Tag.Get("type") == "timestamp" {
			return fmt.Sprintf("FROM_UNIXTIME(%v)", src.V.Field(index).Interface())
		}
		return fmt.Sprintf("%v", src.V.Field(index).Interface())
	},
	"%RawName": func(src *Source, field reflect.StructField, index int) string {
		return field.Name
	},
	"%Variable": func(src *Source, field reflect.StructField, index int) string {
		return field.Type.Name()
	},
	"%SQLType": func(src *Source, field reflect.StructField, index int) string {
		t := getType(src, field, index)
		t = strings.ReplaceAll(t, "boolean", "tinyint(1)")
		return t
	},
	"%SQLName": func(src *Source, field reflect.StructField, index int) string {
		name := getName(src, field, index)
		switch getType(src, field, index) {
		case "timestamp":
			return fmt.Sprintf("UNIX_TIMESTAMP(%s)", name)
		}
		return name
	},
}

All available field formattings.

Functions

func GetSQLWith

func GetSQLWith(value string) string

Replace all keywords with how they are stored in the actual database.

Types

type Source

type Source struct {
	T reflect.Type
	V reflect.Value
}

Represents source of a certain variable.

func NewSource

func NewSource(element any) Source

Get a new Source of the type/value of an variable.

func (Source) DefinedFields

func (src Source) DefinedFields(format string) []string

Get all fields that were explicitly specified.

func (Source) Fields

func (src Source) Fields(format string) []string

Get slice of all fields formatted.

func (Source) FieldsContain

func (src Source) FieldsContain(format string, value string) bool

Check if formatted fields contain the specified value.

func (*Source) FormatField

func (src *Source) FormatField(format string, field reflect.StructField, index int) string

Formats the field based on source.Format.

func (Source) Name

func (src Source) Name() string

Get name of the variable.

Jump to

Keyboard shortcuts

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