-
Notifications
You must be signed in to change notification settings - Fork 0
Feature implementation from commits 29e37e8..2d3126b #5
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
base: feature-base-branch-5
Are you sure you want to change the base?
Changes from all commits
8530bdb
681e943
1685c61
52a2c63
17207fc
8284a0e
63eb053
2abeda9
ab5d7cf
33a4336
dd2a40e
8b2fe32
ab15822
0dbab7b
2d3126b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,7 @@ func (v StringList) Len() int { | |||||||||||||||
| return len(v) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON | ||||||||||||||||
| func (v *StringList) UnmarshalJSON(data []byte) error { | ||||||||||||||||
| var strarray []string | ||||||||||||||||
| if err := json.Unmarshal(data, &strarray); err == nil { | ||||||||||||||||
|
|
@@ -43,10 +44,12 @@ type Address struct { | |||||||||||||||
| net.Address | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (v Address) MarshalJSON() ([]byte, error) { | ||||||||||||||||
| // MarshalJSON implements encoding/json.Marshaler.MarshalJSON | ||||||||||||||||
| func (v *Address) MarshalJSON() ([]byte, error) { | ||||||||||||||||
| return json.Marshal(v.Address.String()) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON | ||||||||||||||||
| func (v *Address) UnmarshalJSON(data []byte) error { | ||||||||||||||||
| var rawStr string | ||||||||||||||||
| if err := json.Unmarshal(data, &rawStr); err != nil { | ||||||||||||||||
|
|
@@ -81,6 +84,7 @@ func (v Network) Build() net.Network { | |||||||||||||||
|
|
||||||||||||||||
| type NetworkList []Network | ||||||||||||||||
|
|
||||||||||||||||
| // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON | ||||||||||||||||
| func (v *NetworkList) UnmarshalJSON(data []byte) error { | ||||||||||||||||
| var strarray []Network | ||||||||||||||||
| if err := json.Unmarshal(data, &strarray); err == nil { | ||||||||||||||||
|
|
@@ -169,6 +173,19 @@ func (v *PortRange) Build() *net.PortRange { | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // MarshalJSON implements encoding/json.Marshaler.MarshalJSON | ||||||||||||||||
| func (v *PortRange) MarshalJSON() ([]byte, error) { | ||||||||||||||||
| return json.Marshal(v.String()) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (port *PortRange) String() string { | ||||||||||||||||
| if port.From == port.To { | ||||||||||||||||
| return strconv.Itoa(int(port.From)) | ||||||||||||||||
| } else { | ||||||||||||||||
| return fmt.Sprintf("%d-%d", port.From, port.To) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON | ||||||||||||||||
| func (v *PortRange) UnmarshalJSON(data []byte) error { | ||||||||||||||||
| port, err := parseIntPort(data) | ||||||||||||||||
|
|
@@ -203,20 +220,21 @@ func (list *PortList) Build() *net.PortList { | |||||||||||||||
| return portList | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (v PortList) MarshalJSON() ([]byte, error) { | ||||||||||||||||
| return json.Marshal(v.String()) | ||||||||||||||||
| // MarshalJSON implements encoding/json.Marshaler.MarshalJSON | ||||||||||||||||
| func (v *PortList) MarshalJSON() ([]byte, error) { | ||||||||||||||||
| portStr := v.String() | ||||||||||||||||
| port, err := strconv.Atoi(portStr) | ||||||||||||||||
| if err == nil { | ||||||||||||||||
| return json.Marshal(port) | ||||||||||||||||
| } else { | ||||||||||||||||
| return json.Marshal(portStr) | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+226
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐛 Correctness Issue Inconsistent JSON Type Serialization. The new implementation creates inconsistent JSON types where single ports become numbers while ranges become strings, which can break clients expecting consistent types. Current Code (Diff): - port, err := strconv.Atoi(portStr)
- if err == nil {
- return json.Marshal(port)
- } else {
- return json.Marshal(portStr)
- }
+ return json.Marshal(portStr)📝 Committable suggestion
Suggested change
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (v PortList) String() string { | ||||||||||||||||
| ports := []string{} | ||||||||||||||||
| for _, port := range v.Range { | ||||||||||||||||
| if port.From == port.To { | ||||||||||||||||
| p := strconv.Itoa(int(port.From)) | ||||||||||||||||
| ports = append(ports, p) | ||||||||||||||||
| } else { | ||||||||||||||||
| p := fmt.Sprintf("%d-%d", port.From, port.To) | ||||||||||||||||
| ports = append(ports, p) | ||||||||||||||||
| } | ||||||||||||||||
| ports = append(ports, port.String()) | ||||||||||||||||
| } | ||||||||||||||||
| return strings.Join(ports, ",") | ||||||||||||||||
| } | ||||||||||||||||
|
|
@@ -277,7 +295,8 @@ type Int32Range struct { | |||||||||||||||
| To int32 | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (v Int32Range) MarshalJSON() ([]byte, error) { | ||||||||||||||||
| // MarshalJSON implements encoding/json.Marshaler.MarshalJSON | ||||||||||||||||
| func (v *Int32Range) MarshalJSON() ([]byte, error) { | ||||||||||||||||
| return json.Marshal(v.String()) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -289,6 +308,7 @@ func (v Int32Range) String() string { | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON | ||||||||||||||||
| func (v *Int32Range) UnmarshalJSON(data []byte) error { | ||||||||||||||||
| defer v.ensureOrder() | ||||||||||||||||
| var str string | ||||||||||||||||
|
|
||||||||||||||||
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.
🐛 Correctness Issue
Breaking API Change: Receiver Type Changed.
Changed from value receiver to pointer receiver which breaks backward compatibility for any code calling MarshalJSON on non-pointer values.
Current Code (Diff):
📝 Committable suggestion