14 Commits
dst ... v1.0.13

Author SHA1 Message Date
50edeb07ca fix: readme 2025-03-12 16:15:37 +07:00
31c5c49589 fix: sorting method generation 2025-03-12 16:08:36 +07:00
51d8531792 upd: README.md 2025-03-12 16:01:53 +07:00
da19da2d63 update: README.md 2025-03-12 15:58:34 +07:00
e1012c234c feat: sorting 2025-03-12 15:57:30 +07:00
43c4fe36c0 upd: README 2025-03-12 13:42:21 +07:00
a51a2e063d fix: imports weren't added 2025-03-12 13:41:51 +07:00
ec87f02976 upd: README 2025-03-12 13:14:14 +07:00
c141b2a941 upd: README 2025-03-12 12:47:25 +07:00
6e9f5dd1eb feat: README 2025-03-12 12:46:02 +07:00
4a5ecc647d Merge pull request 'upd: template, imports' (#2) from dst into main
Reviewed-on: #2
2025-03-12 12:28:10 +07:00
c1b1d2d5f1 delete swap file 2025-02-16 21:50:49 +07:00
17047d2c71 feat: license 2025-02-16 21:50:23 +07:00
aed112a117 Merge pull request 'Migration to DST library' (#1) from dst into main
Reviewed-on: #1
2025-02-16 21:04:52 +07:00
7 changed files with 68 additions and 21 deletions

21
LICENSE.md Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Georgiy Derbenev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

13
README.md Normal file
View File

@@ -0,0 +1,13 @@
# Crudgen
> Generate crud's at speed of thought
## Install
```shell
go install git.gogacoder.ru/NTO/crudgen/cmd/crudgen@v1.0.13
```
## Run
Specify path to internal directory
```shell
crudgen -p internal
```

1
go.mod
View File

@@ -9,6 +9,5 @@ require github.com/dave/dst v0.27.3
require ( require (
golang.org/x/mod v0.24.0 // indirect golang.org/x/mod v0.24.0 // indirect
golang.org/x/sync v0.12.0 // indirect golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/tools v0.31.0 // indirect golang.org/x/tools v0.31.0 // indirect
) )

13
go.sum
View File

@@ -6,20 +6,9 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=
golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY=
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU= golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ= golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=

View File

@@ -54,3 +54,9 @@ const CountRawTemplate = `func (service *{{.ServiceName}}) Count() (int64, error
amount, err := dal.{{.EntityType}}.Count() amount, err := dal.{{.EntityType}}.Count()
return amount, err return amount, err
}` }`
const SortedByOrderTemplate = `func (service *{{.ServiceName}}) SortedByOrder(fieldsSortOrder map[string]string) ([]*{{.EntityType}}, error) {
return utils.SortByOrder(fieldsSortOrder, {{.EntityType}}{})
}`
var implementedMethods = []string{CreateMethod, GetAllMethod, GetByIdMethod, UpdateMethod, DeleteMethod, CountMethod, SortedByOrderMethod}

View File

@@ -22,12 +22,14 @@ const GetByIdMethod = "GetById"
const UpdateMethod = "Update" const UpdateMethod = "Update"
const DeleteMethod = "Delete" const DeleteMethod = "Delete"
const CountMethod = "Count" const CountMethod = "Count"
const SortedByOrderMethod = "SortedByOrder"
var RawTemplates = map[string]string{ var RawTemplates = map[string]string{
CreateMethod: CreateRawTemplate, CreateMethod: CreateRawTemplate,
GetAllMethod: GetAllRawTemplate, GetAllMethod: GetAllRawTemplate,
GetByIdMethod: GetByIdRawTemplate, GetByIdMethod: GetByIdRawTemplate,
UpdateMethod: UpdateRawTemplate, UpdateMethod: UpdateRawTemplate,
DeleteMethod: DeleteRawTemplate, DeleteMethod: DeleteRawTemplate,
CountMethod: CountRawTemplate, CountMethod: CountRawTemplate,
SortedByOrderMethod: SortedByOrderTemplate,
} }

View File

@@ -170,9 +170,25 @@ func importExists(file *dst.File, importPath string) bool {
} }
func MaintainImports(file *dst.File) error { func MaintainImports(file *dst.File) error {
var importDecl *dst.GenDecl
for _, decl := range file.Decls {
if genDecl, ok := decl.(*dst.GenDecl); ok && genDecl.Tok == token.IMPORT {
importDecl = genDecl
break
}
}
if importDecl == nil {
importDecl = &dst.GenDecl{
Tok: token.IMPORT,
Specs: []dst.Spec{},
}
file.Decls = append([]dst.Decl{importDecl}, file.Decls...)
}
for _, importPath := range ServiceImports { for _, importPath := range ServiceImports {
if !importExists(file, importPath) { if !importExists(file, importPath) {
file.Imports = append(file.Imports, &dst.ImportSpec{ importDecl.Specs = append(importDecl.Specs, &dst.ImportSpec{
Path: &dst.BasicLit{ Path: &dst.BasicLit{
Kind: token.STRING, Kind: token.STRING,
Value: `"` + importPath + `"`, Value: `"` + importPath + `"`,
@@ -180,6 +196,7 @@ func MaintainImports(file *dst.File) error {
}) })
} }
} }
return nil return nil
} }
@@ -227,7 +244,7 @@ func ImplementCrudMethods(modelName string, serviceName string, file *dst.File,
EntityPlural: ToPlural(modelName), EntityPlural: ToPlural(modelName),
} }
for _, methodName := range []string{CreateMethod, GetAllMethod, GetByIdMethod, UpdateMethod, DeleteMethod, CountMethod} { for _, methodName := range implementedMethods {
methodCode := GenerateCrudMethodCode(methodName, templateContext) methodCode := GenerateCrudMethodCode(methodName, templateContext)
methodDecl, err := MethodCodeToDeclaration(methodCode) methodDecl, err := MethodCodeToDeclaration(methodCode)
fmt.Printf("%s\n", methodCode) fmt.Printf("%s\n", methodCode)