Documentation
¶
Index ¶
Constants ¶
const AllStoresTemplate = `` /* 668-byte string literal not displayed */
AllStoresTemplate is the template for generating all.go registry.
const ImplementationTemplate = `// Code generated by gofr.dev/cli/gofr. DO NOT EDIT.
package {{ .Store.Package }}
import (
{{- range .Imports }}
{{- if .Alias }}{{ .Alias }} "{{ .Path }}"{{ else }}"{{ .Path }}"{{ end }}
{{- end }}
)
// {{ .Store.Implementation }} implements {{ .Store.Interface }}
type {{ .Store.Implementation }} struct {
// Add any dependencies here (e.g., database connection)
}
// New{{ .Store.Interface }} creates a new instance of {{ .Store.Interface }}
func New{{ .Store.Interface }}() {{ .Store.Interface }} {
return &{{ .Store.Implementation }}{}
}
{{- range .Store.Queries }}
// {{ .Name }} {{ if .Description }}{{ .Description }}{{ else }}executes the {{ .Name }} query{{ end }}
func (s *{{ $.Store.Implementation }}) {{ .Name }}(ctx *gofr.Context{{- range .Params }}, {{ .Name }} {{ .Type }}` +
`{{- end }}) (
{{- if eq .Returns "single" }}{{ .Model | getModelType }}
{{- else if eq .Returns "multiple" }}[]{{ .Model | getModelType }}
{{- else if eq .Returns "count" }}int64
{{- else }}any
{{- end }}, error) {
// TODO: Implement {{ .Name }} query
// SQL: {{ .SQL }}
{{- if eq .Type "select" }}
{{- if eq .Returns "single" }}
var result {{ .Model | getModelType }}
// Implement single row selection using ctx.SQL()
// Example: err := ctx.SQL().QueryRowContext(ctx, "{{ .SQL }}", {{ range $i, $param := .Params }}{{if $i}}, ` +
`{{end}}{{ $param.Name }}{{end}}).Scan(&result.Field1, &result.Field2, ...)
return result, nil
{{- else if eq .Returns "multiple" }}
var results []{{ .Model | getModelType }}
// Implement multiple row selection using ctx.SQL()
// Example: rows, err := ctx.SQL().QueryContext(ctx, "{{ .SQL }}", {{ range $i, $param := .Params }}{{if $i}},` +
` {{end}}{{ $param.Name }}{{end}})
return results, nil
{{- else if eq .Returns "count" }}
var count int64
// Implement count query using ctx.SQL()
// Example: err := ctx.SQL().QueryRowContext(ctx, "{{ .SQL }}", {{ range $i, $param := .Params }}{{if $i}}, ` +
`{{end}}{{ $param.Name }}{{end}}).Scan(&count)
return count, nil
{{- else }}
// Implement custom return type
return nil, nil
{{- end }}
{{- else if eq .Type "insert" }}
// Implement insert operation using ctx.SQL()
// Example: result, err := ctx.SQL().ExecContext(ctx, "{{ .SQL }}", {{ range $i, $param := .Params }}{{if $i}}, ` +
`{{end}}{{ $param.Name }}{{end}})
return {{ if eq .Returns "single" }}{{ .Model }}{}, {{ else if eq .Returns "count" }}int64(0), {{ else }}nil, ` +
`{{ end }}nil
{{- else if eq .Type "update" }}
// Implement update operation using ctx.SQL()
// Example: result, err := ctx.SQL().ExecContext(ctx, "{{ .SQL }}", {{ range $i, $param := .Params }}{{if $i}},` +
` {{end}}{{ $param.Name }}{{end}})
return {{ if eq .Returns "count" }}int64(0), {{ else }}nil, {{ end }}nil
{{- else if eq .Type "delete" }}
// Implement delete operation using ctx.SQL()
// Example: result, err := ctx.SQL().ExecContext(ctx, "{{ .SQL }}", {{ range $i, $param := .Params }}{{if $i}}, ` +
`{{end}}{{ $param.Name }}{{end}})
return {{ if eq .Returns "count" }}int64(0), {{ else }}nil, {{ end }}nil
{{- end }}
}
{{- end }}
`
ImplementationTemplate is the template for generating store implementations.
const InitialInterfaceTemplate = `` /* 363-byte string literal not displayed */
InitialInterfaceTemplate is the template for initial interface generation.
const InitialStoreTemplate = `// Code generated by gofr.dev/cli/gofr.
package {{ .PackageName }}
// import (
// "gofr.dev/pkg/gofr"
// )
// {{ .ImplementationName }} implements the {{ .InterfaceName }} interface
type {{ .ImplementationName }} struct {
// Add any dependencies here (e.g., database connection)
}
// New{{ .InterfaceName }} creates a new instance of {{ .InterfaceName }}
func New{{ .InterfaceName }}() {{ .InterfaceName }} {
return &{{ .ImplementationName }}{}
}
// Add your store method implementations here
// Example:
// func (s *{{ .ImplementationName }}) GetUserByID(ctx *gofr.Context, id int64) (User, error) {
// // TODO: Implement GetUserByID query
// var result User
// err := ctx.SQL().QueryRowContext(ctx, "SELECT id, name FROM users WHERE id = ?", id).` +
`Scan(&result.ID, &result.Name)
// return result, err
// }
`
InitialStoreTemplate is the template for initial store implementation.
const InterfaceTemplate = `` /* 688-byte string literal not displayed */
InterfaceTemplate is the template for generating store interfaces.
const ModelTemplate = `// Code generated by gofr.dev/cli/gofr. DO NOT EDIT.
package {{ .Store.Package }}
import (
"time"
)
// {{ .Model.Name }} represents the {{ .Model.Name }} model
type {{ .Model.Name }} struct {
{{- range .Model.Fields }}
{{ .Name }} {{ .Type }} ` + "`{{ .Tag }}`" + `
{{- end }}
}
// TableName returns the table name for {{ .Model.Name }}
func ({{ .Model.Name }}) TableName() string {
return "{{ .Model.Name | lower }}"
}
`
ModelTemplate is the template for generating data models.
const StoreConfigTemplate = `` /* 1433-byte string literal not displayed */
StoreConfigTemplate is the template for generating store.yaml configuration.
Variables ¶
This section is empty.
Functions ¶
func GenerateStore ¶
GenerateStore generates store layer functions based on YAML configuration.
Types ¶
type Config ¶
type Config struct {
Version string `yaml:"version"`
Stores []Info `yaml:"stores"`
Models []Model `yaml:"models"`
}
Config represents the YAML configuration for store generation.
type Field ¶
type Field struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Tag string `yaml:"tag,omitempty"`
Nullable bool `yaml:"nullable,omitempty"`
}
Field represents a model field.
type ImportInfo ¶
ImportInfo represents an import with its path and optional alias.
type Info ¶
type Info struct {
Name string `yaml:"name"`
Package string `yaml:"package"`
OutputDir string `yaml:"output_dir"`
Interface string `yaml:"interface"`
Implementation string `yaml:"implementation"`
Queries []Query `yaml:"queries"`
}
Info contains store-level configuration.
type Model ¶
type Model struct {
Name string `yaml:"name"`
Fields []Field `yaml:"fields,omitempty"`
Path string `yaml:"path,omitempty"`
Package string `yaml:"package,omitempty"`
}
Model represents a data model.
type ModelAliasMap ¶
ModelAliasMap maps model names to their import aliases for type resolution.
type Query ¶
type Query struct {
Name string `yaml:"name"`
SQL string `yaml:"sql"`
Type string `yaml:"type"`
Model string `yaml:"model,omitempty"`
Params []QueryParam `yaml:"params,omitempty"`
Returns string `yaml:"returns,omitempty"`
Description string `yaml:"description,omitempty"`
Tags map[string]string `yaml:"tags,omitempty"`
UseSelect bool `yaml:"use_select,omitempty"`
Transaction bool `yaml:"transaction,omitempty"`
}
Query represents a database query.
type QueryParam ¶
QueryParam represents a query parameter.