Skip to content

Commit 9f71782

Browse files
committed
fixed descrepancies
1 parent 977c188 commit 9f71782

2 files changed

Lines changed: 49 additions & 60 deletions

File tree

store/README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ After generation, your project will have:
6161

6262
```
6363
stores/
64+
├── store.yaml # Central configuration
6465
├── all.go # Store registry factory (auto-generated)
6566
└── user/
6667
├── interface.go # UserStore interface
@@ -156,12 +157,12 @@ models:
156157
157158
| Field | Description | Required |
158159
|-------|-------------|----------|
159-
| `name` | Store identifier (used in registry) | Yes |
160-
| `package` | Go package name | Yes |
161-
| `output_dir` | Directory for generated files | Yes |
162-
| `interface` | Interface name — **recommended: `<Name>Store`** (e.g., `UserStore`) | Yes |
163-
| `implementation` | Implementation struct name (e.g., `userStore`) | Yes |
164-
| `queries` | Array of database queries | Yes |
160+
| `name` | Store identifier (used in registry) | **Yes** |
161+
| `package` | Go package name | **Yes** |
162+
| `output_dir` | Directory for generated files | Optional (defaults to `stores/<name>`) |
163+
| `interface` | Interface name — **recommended: `<Name>Store`** (e.g., `UserStore`) | Optional (defaults to `<Name>Store`) |
164+
| `implementation` | Implementation struct name (e.g., `userStore`) | Optional (defaults to `<name>Store`) |
165+
| `queries` | Array of database queries | **Yes** |
165166

166167
> **⚠️ Naming Convention:** The registry (`stores/all.go`) automatically appends `"Store"` when building constructor calls. To avoid compilation errors, always name your interface as `<Name>Store` (e.g., `UserStore`) and the generated constructor will be `New<Name>Store()`.
167168

@@ -200,7 +201,7 @@ models:
200201
- `delete` - DELETE queries
201202

202203
**Return Types:**
203-
- `single` - Returns `(*Model, error)`
204+
- `single` - Returns `(Model, error)`
204205
- `multiple` - Returns `([]Model, error)`
205206
- `count` - Returns `(int64, error)`
206207
- `custom` - Returns `(any, error)`
@@ -264,7 +265,7 @@ package user
264265
import "gofr.dev/pkg/gofr"
265266
266267
type UserStore interface {
267-
GetUserByID(ctx *gofr.Context, id int64) (*User, error)
268+
GetUserByID(ctx *gofr.Context, id int64) (User, error)
268269
GetAllUsers(ctx *gofr.Context) ([]User, error)
269270
}
270271
```
@@ -280,9 +281,11 @@ func NewUserStore() UserStore {
280281
return &userStore{}
281282
}
282283
283-
func (s *userStore) GetUserByID(ctx *gofr.Context, id int64) (*User, error) {
284+
func (s *userStore) GetUserByID(ctx *gofr.Context, id int64) (User, error) {
284285
// TODO: Implement query using ctx.SQL()
285-
return &User{}, nil
286+
var result User
287+
// err := ctx.SQL().QueryRowContext(ctx, sql, id).Scan(&result.ID, ...)
288+
return result, nil
286289
}
287290
```
288291

store/generator.go

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ func generateSingleStore(ctx *gofr.Context, cfg *Config, store *Info) error {
307307
return fmt.Errorf("failed to create output directory: %w", err)
308308
}
309309

310+
// Default implementation name if empty
311+
if store.Implementation == "" {
312+
store.Implementation = strings.ToLower(store.Name) + "Store"
313+
}
314+
310315
storeConfig := &Config{
311316
Version: cfg.Version,
312317
Models: cfg.Models,
@@ -974,81 +979,62 @@ func handleImportSection(lines, importsToAdd []string) []string {
974979
return lines
975980
}
976981

977-
importInsertIdx := findImportInsertionPoint(lines)
978-
if importInsertIdx > 0 {
979-
formattedImports := formatImports(importsToAdd)
980-
return insertLines(lines, importInsertIdx, formattedImports)
981-
}
982+
insertIdx := findImportInsertionPoint(lines)
983+
if insertIdx == -1 {
984+
// No import block found, let's look for the package line
985+
packageIdx := -1
982986

983-
return createImportSection(lines, importsToAdd)
984-
}
987+
for i, line := range lines {
988+
if strings.HasPrefix(strings.TrimSpace(line), "package ") {
989+
packageIdx = i
990+
break
991+
}
992+
}
985993

986-
// createImportSection creates a new import section.
987-
func createImportSection(lines, importsToAdd []string) []string {
988-
insertIdx := -1
994+
if packageIdx == -1 {
995+
return append([]string{"import ("}, append(importsToAdd, ")...")...)
996+
}
989997

990-
for i, line := range lines {
991-
if strings.HasPrefix(strings.TrimSpace(line), "package ") {
992-
insertIdx = i + 1
993-
break
998+
newLines := []string{"", "import ("}
999+
for _, imp := range importsToAdd {
1000+
newLines = append(newLines, fmt.Sprintf(" %q", canonicalizeImport(imp)))
9941001
}
995-
}
9961002

997-
if insertIdx == -1 {
998-
insertIdx = 1
999-
}
1003+
newLines = append(newLines, ")")
10001004

1001-
importSection := []string{""}
1002-
if len(importsToAdd) > 0 {
1003-
importSection = append(importSection, "import (")
1004-
formattedImports := formatImports(importsToAdd)
1005-
importSection = append(importSection, formattedImports...)
1006-
importSection = append(importSection, ")")
1005+
return insertLines(lines, packageIdx+1, newLines)
10071006
}
10081007

1009-
return insertLines(lines, insertIdx, importSection)
1010-
}
1011-
1012-
// formatImports formats a list of imports.
1013-
func formatImports(importsToAdd []string) []string {
1014-
formatted := make([]string, len(importsToAdd))
1015-
1008+
formattedImports := make([]string, len(importsToAdd))
10161009
for i, imp := range importsToAdd {
1017-
formattedImp := strings.TrimSpace(imp)
1018-
if !strings.HasPrefix(formattedImp, `"`) {
1019-
formattedImp = fmt.Sprintf("%q", formattedImp)
1020-
}
1021-
1022-
formatted[i] = fmt.Sprintf(` %s`, formattedImp)
1010+
formattedImports[i] = fmt.Sprintf(" %q", canonicalizeImport(imp))
10231011
}
10241012

1025-
return formatted
1013+
return insertLines(lines, insertIdx, formattedImports)
10261014
}
10271015

10281016
// parseExistingAllFile parses the existing all.go file.
10291017
func parseExistingAllFile(lines []string) (existingStores, existingImports map[string]bool) {
10301018
existingStores = make(map[string]bool)
10311019
existingImports = make(map[string]bool)
1032-
inImportSection := false
1020+
1021+
inImport := false
10331022

10341023
for _, line := range lines {
10351024
trimmedLine := strings.TrimSpace(line)
10361025

1037-
if strings.Contains(trimmedLine, "import (") {
1038-
inImportSection = true
1026+
if trimmedLine == "import (" {
1027+
inImport = true
10391028
continue
10401029
}
10411030

1042-
if inImportSection {
1043-
if trimmedLine == ")" {
1044-
inImportSection = false
1045-
continue
1046-
}
1047-
1048-
if strings.Contains(trimmedLine, `"`) {
1049-
existingImports[strings.TrimSpace(trimmedLine)] = true
1050-
}
1031+
if inImport && trimmedLine == ")" {
1032+
inImport = false
1033+
continue
1034+
}
10511035

1036+
if inImport {
1037+
existingImports[trimmedLine] = true
10521038
continue
10531039
}
10541040

0 commit comments

Comments
 (0)