Skip to content

Commit

Permalink
feat(jzero): update rpc style patch for logic package name
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Sep 20, 2024
1 parent a8953e9 commit 66f7461
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions internal/gen/genrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ func (jr *JzeroRpc) Gen() error {

if jr.RpcStylePatch {
for _, file := range allServerFiles {
err = jr.rpcStylePatch(file)
err = jr.rpcStylePatchServer(file)
if err != nil {
return err
}
}
for _, file := range allLogicFiles {
err = jr.rpcStylePatchLogic(file)
if err != nil {
return err
}
Expand Down Expand Up @@ -236,7 +242,7 @@ func (jr *JzeroRpc) Gen() error {
return nil
}

func (jr *JzeroRpc) rpcStylePatch(file ServerFile) error {
func (jr *JzeroRpc) rpcStylePatchServer(file ServerFile) error {
fp := file.Path
if jr.RemoveSuffix {
// Get the new file name of the file (without the 5 characters(Server or server) before the ".go" extension)
Expand All @@ -257,7 +263,40 @@ func (jr *JzeroRpc) rpcStylePatch(file ServerFile) error {
astutil.DeleteImport(fset, f, fmt.Sprintf("%s/internal/logic/%s", jr.Module, strings.ToLower(file.Service)))

logicImportDir, _ := format.FileNamingFormat(jr.Style, file.Service)
astutil.AddImport(fset, f, fmt.Sprintf("%s/internal/logic/%s", jr.Module, strings.ToLower(logicImportDir)))
importLogicName, _ := format.FileNamingFormat("gozero", file.Service)
astutil.AddNamedImport(fset, f, importLogicName+"logic", fmt.Sprintf("%s/internal/logic/%s", jr.Module, strings.ToLower(logicImportDir)))

// Write the modified AST back to the file
buf := bytes.NewBuffer(nil)
if err := goformat.Node(buf, fset, f); err != nil {
return err
}

if err = os.WriteFile(fp, buf.Bytes(), 0o644); err != nil {
return err
}
return nil
}

func (jr *JzeroRpc) rpcStylePatchLogic(file LogicFile) error {
fp := file.Path
if jr.RemoveSuffix {
fp = fp[:len(fp)-8]
// patch
fp = strings.TrimSuffix(fp, "_")
fp = strings.TrimSuffix(fp, "-")
fp = fp + ".go"
}

fset := token.NewFileSet()

f, err := goparser.ParseFile(fset, fp, nil, goparser.ParseComments)
if err != nil {
return err
}

packageName, _ := format.FileNamingFormat(jr.Style, file.Group)
f.Name = ast.NewIdent(strings.ToLower(packageName))

// Write the modified AST back to the file
buf := bytes.NewBuffer(nil)
Expand Down

0 comments on commit 66f7461

Please sign in to comment.