Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Goctl can't generate the type of decimal. #3441

Open
shaoqingyang opened this issue Jul 24, 2023 · 4 comments
Open

feat:Goctl can't generate the type of decimal. #3441

shaoqingyang opened this issue Jul 24, 2023 · 4 comments

Comments

@shaoqingyang
Copy link

As we all know, if using int or float instead of decimal, there may be precision loss, which is fatal for finance. We hope thatdevelopers can enable goct to generate the type of decimal.Decimal to resolve this question.

@zzZZzzz888
Copy link
Contributor

The issue can be fixed if you add decimal type to variable type map

1. The decimal type is added to goctl

1.1 You can add decimal type as below,

diff --git a/tools/goctl/api/parser/g4/gen/api/baseparser.go b/tools/goctl/api/parser/g4/gen/api/baseparser.go
index fb34930a..df76777d 100644
--- a/tools/goctl/api/parser/g4/gen/api/baseparser.go
+++ b/tools/goctl/api/parser/g4/gen/api/baseparser.go
@@ -38,6 +38,7 @@ var (
                "string":     holder,
                "byte":       holder,
                "rune":       holder,
+               "decimal":    holder,
        }
 )

1.2 And build goctl with below command,

go build goctl.go

2. How to use decimal type

I use decimal library (https://github.com/shopspring/decimal) to support decimal type

3. Example

3.1 Create greet.api as below

type (		
	Request {	
		Aaa decimal `path:"name"`
	}	
		
	Response {	
		Message string `json:"message"`
	}	
)		
		
service greet-api {		
	@handler GreetHandler	
	get /greet/from/:name(Request) returns (Response)	
}		

3.2 Generate code

go mod tidy		
go get github.com/shopspring/decimal		
goctl api go -api greet.api -dir greet	

the generated files look like:

├── greet
│ ├── etc
│ │ └── greet-api.yaml // configuration file
│ ├── greet.go // main file
│ └── internal
│ ├── config
│ │ └── config.go // configuration definition
│ ├── handler
│ │ ├── greethandler.go // get/put/post/delete routes are defined here
│ │ └── routes.go // routes list
│ ├── logic
│ │ └── greetlogic.go // request logic can be written here
│ ├── svc
│ │ └── servicecontext.go // service context, mysql/redis can be passed in here
│ └── types
│ └── types.go // request/response defined here
└── greet.api // api description file

3.3 decimal change for the file type.go

we need import decimal library and change decimal type to decimal.Decimal in file internal/types/types.go

@@ -1,8 +1,12 @@
 // Code generated by goctl. DO NOT EDIT.
 package types
 
+import (
+        "github.com/shopspring/decimal"
+)
+
 type Request struct {
-	Aaa decimal `path:"name"`
+	Aaa decimal.Decimal `path:"name"`
 }

3.4 Update result(not neccessary) in the file internal/logic/greetlogic.go

@@ -25,6 +25,9 @@
 
 func (l *GreetLogic) Greet(req *types.Request) (resp *types.Response, err error) {
 	// todo: add your logic here and delete this line
+	return &types.Response{
+        Message: "Hello go-zero[" + req.Aaa.String() + "]",
+    }, nil
 
- 	return
 }

3.5 Run and check

run the server
go run greet.go -f etc/greet-api.yaml

you can check result by curl

curl -i -X GET  http://localhost:8888/greet/from/3000.9999
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Traceparent: 00-ea80cf4d2f7cc21b2ee7e54ba2753cf0-b857e184bb198aee-00
Date: Sun, 03 Dec 2023 03:04:42 GMT
Content-Length: 38

{"message":"Hello go-zero[3000.9999]"}

Thanks,

@shaoqingyang
Copy link
Author

You're welcome, this is much better than saving money with the int64 type. Thank you for taking my advice!

@kesonan
Copy link
Collaborator

kesonan commented Apr 10, 2024

see features #4062

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


see features #4062

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants