Skip to content

Commit de1a33e

Browse files
authored
fixed migration name into camel case (#66)
Merging PR #66: Fixed migration name to use camelCase. Includes updated tests and merged with latest main.
1 parent 8ac2ef8 commit de1a33e

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

migration/migrate.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,17 @@ func Migrate(ctx *gofr.Context) (any, error) {
6868
return nil, errNameEmpty
6969
}
7070

71-
if err := createMigrationFile(ctx, migName); err != nil {
71+
camelCasedMigName := toCamelCase(migName)
72+
73+
if err := createMigrationFile(ctx, camelCasedMigName); err != nil {
7274
return nil, fmt.Errorf("error while creating migration file, err: %w", err)
7375
}
7476

7577
if err := createAllMigration(ctx); err != nil {
7678
return nil, fmt.Errorf("error while creating all.go file, err: %w", err)
7779
}
7880

79-
return fmt.Sprintf("Successfully created migration %v", migName), nil
81+
return fmt.Sprintf("Successfully created migration %v", camelCasedMigName), nil
8082
}
8183

8284
func createMigrationFile(ctx *gofr.Context, migrationName string) error {
@@ -190,9 +192,38 @@ func findMigrations(files []os.DirEntry) map[string]string {
190192
if len(fileParts) < 2 || file.Name() == allFile || fileParts[len(fileParts)-1] == "test.go" {
191193
continue
192194
}
193-
194-
existingMig[fileParts[0]] = strings.TrimSuffix(strings.Join(fileParts[1:], "_"), ".go")
195+
// convert second part (function) to camelCase, since migration files are now generated using camelCase
196+
existingMig[fileParts[0]] = toCamelCase(strings.TrimSuffix(strings.Join(fileParts[1:], "_"), ".go"))
195197
}
196198

197199
return existingMig
198200
}
201+
202+
// toCamelCase converts snake_case or kebab-case to camelCase. If input has no delimiters,
203+
// it preserves existing casing except lowercasing the first letter.
204+
func toCamelCase(migrationName string) string {
205+
if migrationName == "" {
206+
return migrationName
207+
}
208+
// If no delimiters, assume it's already camel/Pascal case or a single word; just lowercase first rune
209+
if !strings.Contains(migrationName, "_") && !strings.Contains(migrationName, "-") {
210+
return strings.ToLower(migrationName[:1]) + migrationName[1:]
211+
}
212+
213+
migrationName = strings.ReplaceAll(migrationName, "-", "_")
214+
parts := strings.FieldsFunc(migrationName, func(r rune) bool { return r == '_' || r == '-' })
215+
216+
if len(parts) == 0 {
217+
return ""
218+
}
219+
220+
result := strings.ToLower(parts[0])
221+
222+
for _, part := range parts[1:] {
223+
if part != "" {
224+
result += strings.ToUpper(part[:1]) + strings.ToLower(part[1:])
225+
}
226+
}
227+
228+
return result
229+
}

migration/migration_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package migration
2+
3+
import "testing"
4+
5+
func TestToCamelCase(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
in string
9+
out string
10+
}{
11+
{name: "snake_case", in: "create_employee_table", out: "createEmployeeTable"},
12+
{name: "kebab_case", in: "create-employee-table", out: "createEmployeeTable"},
13+
{name: "mixed_caps_snake", in: "Create_Employee_Table", out: "createEmployeeTable"},
14+
{name: "single_word_lower", in: "employee", out: "employee"},
15+
{name: "single_word_upper_first", in: "Employee", out: "employee"},
16+
{name: "no_delimiters_lower", in: "createemployeetable", out: "createemployeetable"},
17+
{name: "no_delimiters_camel", in: "CreateEmployeeTable", out: "createEmployeeTable"},
18+
{name: "empty", in: "", out: ""},
19+
{name: "leading_trailing_delims", in: "_create__employee_table_", out: "createEmployeeTable"},
20+
{name: "multiple_delims", in: "create---employee___table", out: "createEmployeeTable"},
21+
}
22+
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
got := toCamelCase(tt.in)
26+
if got != tt.out {
27+
t.Fatalf("toCamelCase(%q) = %q; want %q", tt.in, got, tt.out)
28+
}
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)