forked from nmstate/kubernetes-nmstate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
state, filter: Do not panic on interpretation of ifaces names
In scenarios where an interface name is composed of numbers in scientific notation without a dot, 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. Note: this change does not solve the problem of incorrectly representing valid scientific numeric values like `10e+02` as strings (they are now represented back as full integers: "1000"). This is due to the YAML-to-JSON conversion which does not obey to the defined member type. ref: yaml/pyyaml#173 Signed-off-by: Edward Haas <[email protected]>
- Loading branch information
Showing
3 changed files
with
69 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 | ||
} |