-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,554 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package arcgengo | ||
|
||
import ( | ||
"go/ast" | ||
"go/printer" | ||
"go/token" | ||
"io" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
errorz "github.com/kunitsucom/util.go/errors" | ||
|
||
"github.com/kunitsucom/arcgen/internal/arcgen/lang/util" | ||
"github.com/kunitsucom/arcgen/internal/config" | ||
"github.com/kunitsucom/arcgen/pkg/errors" | ||
) | ||
|
||
func fprintCRUD(osFile osFile, buf buffer, arcSrcSet *ARCSourceSet) error { | ||
content, err := generateCRUDFileContent(buf, arcSrcSet) | ||
if err != nil { | ||
return errorz.Errorf("generateCRUDFileContent: %w", err) | ||
} | ||
|
||
// write to file | ||
if _, err := io.WriteString(osFile, content); err != nil { | ||
return errorz.Errorf("io.WriteString: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
//nolint:funlen | ||
func generateCRUDFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error) { | ||
if arcSrcSet == nil || arcSrcSet.PackageName == "" { | ||
return "", errors.ErrInvalidSourceSet | ||
} | ||
astFile := &ast.File{ | ||
// package | ||
Name: &ast.Ident{ | ||
Name: config.GoCRUDPackageName(), | ||
}, | ||
// methods | ||
Decls: []ast.Decl{}, | ||
} | ||
|
||
structPackagePath, err := util.GetPackagePath(filepath.Dir(arcSrcSet.Filename)) | ||
if err != nil { | ||
return "", errorz.Errorf("GetPackagePath: %w", err) | ||
} | ||
|
||
// import | ||
astFile.Decls = append(astFile.Decls, | ||
// import ( | ||
// "context" | ||
// "fmt" | ||
// | ||
// dao "path/to/your/dao" | ||
// ) | ||
&ast.GenDecl{ | ||
Tok: token.IMPORT, | ||
Specs: []ast.Spec{ | ||
&ast.ImportSpec{ | ||
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote("context")}, | ||
}, | ||
&ast.ImportSpec{ | ||
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote("fmt")}, | ||
}, | ||
&ast.ImportSpec{ | ||
Name: &ast.Ident{Name: "dao"}, | ||
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(structPackagePath)}, | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
generateCREATEContent(astFile, arcSrcSet) | ||
generateREADContent(astFile, arcSrcSet) | ||
generateUPDATEContent(astFile, arcSrcSet) | ||
generateDELETEContent(astFile, arcSrcSet) | ||
|
||
if err := printer.Fprint(buf, token.NewFileSet(), astFile); err != nil { | ||
return "", errorz.Errorf("printer.Fprint: %w", err) | ||
} | ||
|
||
// add header comment | ||
content := arcSrcSet.generateGoFileHeader() + buf.String() | ||
|
||
// add blank line between methods | ||
content = strings.ReplaceAll(content, "\n}\nfunc ", "\n}\n\nfunc ") | ||
|
||
return content, nil | ||
} |
Oops, something went wrong.