-
Notifications
You must be signed in to change notification settings - Fork 780
feat: clean code #789
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: clean code #789
Conversation
1de513b to
32690f2
Compare
|
|
||
| # There is a bug if we define the same json tag in the struct. | ||
| # Disable structtag temporarily. | ||
| # see: https://github.com/golang/go/issues/40102 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not a bug, is a feature
Duplicate json tags, making the intent of the code unclear.
think this
func TestSameJsonTag(t *testing.T) {
data := []byte(`{
"i":"1"
}`)
type V struct {
Ii int `json:"i"`
S string `json:"i"`
}
var v V
if err := json.Unmarshal(data, &v); err != nil {
t.Fatal(err)
}
if v.Ii != 0 {
t.Errorf("i expected %d, got %d", 0, v.Ii)
}
if v.S != "1" {
t.Errorf("i expected %s, got %s", "1", v.S)
}
}|
Cannot remove simplejson func TestCaseInsensitiveJsonTag(t *testing.T) {
// before data
data := []byte(`{
"n":"1"
}`)
type V struct {
Name string `json:"n"`
}
var v V
if err := json.Unmarshal(data, &v); err != nil {
t.Fatal(err)
}
if v.Name != "1" {
t.Errorf("Name expected %s, got %s", "1", v.Name)
}
// after data
data = []byte(`{
"n":"1",
"N":"2"
}`)
var v2 V
if err := json.Unmarshal(data, &v2); err != nil {
t.Fatal(err)
}
if v2.Name != "1" {
t.Errorf("Name V2 expected %s, got %s", "1", v2.Name)
}
}
|
|
@0x0001 The case sensitive tag would work if both n and N are defined. For example, type V struct {
Name string `json:"n"`
Name2 string `json:"N"`
}This issue may occur if a required key is missing in our code and the key is case-sensitive. However, I believe it should work correctly with the current JSON encoding.Or is there anything I might be missing? |
Yes, but we've had cases where binance updated the response and added new fields. |
|
maybe we can use json/v2 replace encoding/json github.com/go-json-experiment/json Since you are doing this work, I think we can replace all interface{} with any. What do you think? |
|
@0x0001 I’m not sure it’s a good idea to use the v2 JSON given the case-sensitivity issue. If the Binance data were correct, we wouldn’t be facing this problem. I've replaced interface{} to any. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request implements code cleanup and several enhancements to the Binance Go SDK. The changes focus on modernizing the codebase, adding demo environment support, fixing a process deadlock issue, and removing the simplejson dependency.
Changes:
- Replaced
interface{}withanythroughout the codebase for better readability (Go 1.18+) - Added demo environment support with new
UseDemoflag and corresponding endpoints across all API modules - Fixed a stuck process issue in
WsUserDataServeSignatureby adding a goroutine to properly handle connection closure - Removed simplejson dependency in futures and portfolio packages, using standard JSON unmarshaling with proper struct tags
Reviewed changes
Copilot reviewed 51 out of 51 changed files in this pull request and generated 13 comments.
Show a summary per file
| File | Description |
|---|---|
| v2/websocket_service.go | Added demo endpoints and fixed stuck process with proper connection cleanup goroutine |
| v2/client.go | Added UseDemo flag and demo API endpoint with selection logic |
| v2/request.go | Replaced interface{} with any in params and method signatures |
| v2/futures/websocket_service.go | Removed simplejson dependency, added JSON tags to structs, added demo endpoint support |
| v2/futures/client.go | Added demo API endpoint and selection logic |
| v2/futures/exchange_info_service.go | Replaced interface{} with any in struct field types |
| v2/delivery/websocket_service.go | Added demo endpoint support and replaced interface{} with any |
| v2/delivery/client.go | Added demo API endpoint and selection logic |
| v2/delivery/exchange_info_service.go | Replaced interface{} with any in struct field types |
| v2/options/websocket_service.go | Added demo endpoint constants (empty) and replaced interface{} with any |
| v2/options/exchange_info_service.go | Replaced interface{} with any in struct field types |
| v2/portfolio/websocket_service.go | Removed simplejson dependency and added JSON tag |
| v2/common/websocket/types.go | Replaced interface{} with any in request parameters |
| examples/config.go | Added UseDemo configuration with default value |
| examples/watcher_futures.go | New example demonstrating futures user data stream with demo environment |
| check.sh | Added go vet structtag=false flag to work around JSON tag issue |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if UseTestnet { | ||
| return baseWsTestnetUrl | ||
| } | ||
| if UseDemo { | ||
| return baseWsDemoUrl | ||
| } | ||
| return baseWsMainUrl |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. However, since the demo URLs are empty strings, returning an empty string when UseDemo is true will cause connection failures. Consider adding validation to prevent UseDemo from being used when the endpoint is not configured.
| if UseTestnet { | ||
| return BaseWsTestnetURL | ||
| } | ||
| if UseDemo { | ||
| return BaseWsDemoURL | ||
| } | ||
| return BaseWsMainURL |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. Consider adding validation to ensure only one environment flag is active at a time, or document this priority explicitly.
| if UseTestnet { | ||
| return BaseCombinedTestnetURL | ||
| } | ||
| if UseDemo { | ||
| return BaseCombinedDemoURL | ||
| } | ||
| return BaseCombinedMainURL |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. Consider adding validation to ensure only one environment flag is active at a time, or document this priority explicitly.
| if UseTestnet { | ||
| return BaseWsApiTestnetURL | ||
| } | ||
| if UseDemo { | ||
| return BaseWsApiDemoURL | ||
| } | ||
| return BaseWsApiMainURL |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. Consider adding validation to ensure only one environment flag is active at a time, or document this priority explicitly.
| APIKey: getEnvOrDefault("BINANCE_API_KEY", ""), | ||
| SecretKey: getEnvOrDefault("BINANCE_SECRET_KEY", ""), | ||
| UseTestnet: getEnvOrDefault("BINANCE_USE_TESTNET", "true") == "true", | ||
| UseDemo: getEnvOrDefault("BINANCE_USE_DEMO", "true") == "true", |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value for UseDemo is set to "true", which means the demo environment will be used by default when BINANCE_USE_DEMO is not set. This is likely unintended as most users would expect the production environment by default. Consider changing the default to "false".
| UseDemo: getEnvOrDefault("BINANCE_USE_DEMO", "true") == "true", | |
| UseDemo: getEnvOrDefault("BINANCE_USE_DEMO", "false") == "true", |
| if UseTestnet { | ||
| return BaseCombinedTestnetURL | ||
| } | ||
| if UseDemo { | ||
| return BaseCombinedDemoURL | ||
| } | ||
| return BaseCombinedMainURL |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. Consider adding validation to ensure only one environment flag is active at a time, or document this priority explicitly.
| if UseTestnet { | ||
| return BaseApiTestnetUrl | ||
| } | ||
| if UseDemo { | ||
| return BaseApiDemoURL | ||
| } | ||
| return BaseApiMainUrl |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. Consider adding validation to ensure only one environment flag is active at a time, or document this priority explicitly.
| if UseTestnet { | ||
| return BaseApiTestnetUrl | ||
| } | ||
| if UseDemo { | ||
| return BaseApiDemoURL | ||
| } | ||
| return BaseApiMainUrl |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. Consider adding validation to ensure only one environment flag is active at a time, or document this priority explicitly.
| if UseTestnet { | ||
| return BaseWsTestnetUrl | ||
| } | ||
| if UseDemo { | ||
| return BaseWsDemoURL | ||
| } | ||
| return BaseWsMainUrl |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. Consider adding validation to ensure only one environment flag is active at a time, or document this priority explicitly.
| if UseTestnet { | ||
| return BaseAPITestnetURL | ||
| } | ||
| if UseDemo { | ||
| return BaseAPIDemoURL | ||
| } | ||
| return BaseAPIMainURL |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint selection logic has a potential issue: if both UseTestnet and UseDemo are set to true, UseTestnet takes precedence and UseDemo is ignored. Consider adding validation to ensure only one environment flag is active at a time, or document this priority explicitly.
changes: