-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
state, filter: Fix the interpretation of ifaces names
In scenarios where an interface has only numeric characters, the application panics with a failure to extract a string type from a Go interface (which is actually a float64). In order to fix the problem, the parsing of the interface name is performed using a dedicated interface-state structure. Regular numeric values (integers) are now interpreted as strings and represented back with double quotes. Note: this change does not solve the problem of incorrectly representing valid numeric values like `0xfe` and `1.0` as strings (they are now represented back as 254 and 1). This is due to the YAML-to-JSON conversion which does not obey the to the defined member type. Signed-off-by: Edward Haas <[email protected]>
- Loading branch information
Showing
3 changed files
with
91 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,47 @@ | ||
package state | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type rootState struct { | ||
Interfaces []interface{} `json:"interfaces"` | ||
Routes *routesState `json:"routes,omitempty"` | ||
Interfaces []interfaceState `json:"interfaces"` | ||
Routes *routesState `json:"routes,omitempty"` | ||
} | ||
|
||
type routesState struct { | ||
Config []interface{} `json:"config"` | ||
Running []interface{} `json:"running"` | ||
} | ||
|
||
type interfaceState struct { | ||
interfaceFields | ||
Data map[string]interface{} | ||
} | ||
|
||
// interfaceFields allows unmarshaling directly into the defined fields | ||
type interfaceFields struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
func (i interfaceState) MarshalJSON() (output []byte, err error) { | ||
i.Data["name"] = i.Name | ||
return json.Marshal(i.Data) | ||
} | ||
|
||
func (i *interfaceState) UnmarshalJSON(b []byte) error { | ||
if err := yaml.Unmarshal(b, &i.Data); err != nil { | ||
return fmt.Errorf("failed Unmarshaling b: %w", err) | ||
} | ||
|
||
var ifaceFields interfaceFields | ||
if err := yaml.Unmarshal(b, &ifaceFields); err != nil { | ||
return fmt.Errorf("failed Unmarshaling raw: %w", err) | ||
} | ||
i.Data["name"] = ifaceFields.Name | ||
i.Name = ifaceFields.Name | ||
return nil | ||
} |