-
Notifications
You must be signed in to change notification settings - Fork 93
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 (#709)
* state: Interpret basic state through an explicit structure In order to ease the interpretation of the state yaml data, use a type structure that represents the basic root items of state: - interfaces - routes: - config - running In order to keep the generic nature of these items content, they use as value a generic Go interface{}. The filter helper functions, with this change, have been also made immutable. They now return the filtered values. Signed-off-by: Edward Haas <[email protected]> * state, filter: Introduce unit tests for numeric iface names Interfaces names with numeric values are accepted by the kernel. Introduce tests that checks the behavior when using numeric names. Signed-off-by: Edward Haas <[email protected]> * 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]> * state, filter: Fix interpretation of scientific numeric iface names This change solves the problem of incorrectly representing valid scientific numeric valuesi without a dot in them (e.g. `10e20`) as strings (e.g. represented back as `1000`). The problem originates from the YAML-to-JSON conversion which does not obey to the defined member type. Fixed by using the go-yaml package (which does not perform such a conversion from YAML to JSON). The go-yaml is used just to unmarshal the name from the original raw byte stream and update the state structure with the proper name string. Signed-off-by: Edward Haas <[email protected]>
- Loading branch information
Showing
3 changed files
with
131 additions
and
34 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package state | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type rootState struct { | ||
Interfaces []interfaceState `json:"interfaces" yaml:"interfaces"` | ||
Routes *routesState `json:"routes,omitempty" yaml:"routes,omitempty"` | ||
} | ||
|
||
type routesState struct { | ||
Config []interface{} `json:"config" yaml:"config"` | ||
Running []interface{} `json:"running" yaml:"running"` | ||
} | ||
|
||
type interfaceState struct { | ||
interfaceFields `yaml:",inline"` | ||
Data map[string]interface{} | ||
} | ||
|
||
// interfaceFields allows unmarshaling directly into the defined fields | ||
type interfaceFields struct { | ||
Name string `json:"name" yaml:"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.interfaceFields = ifaceFields | ||
return nil | ||
} |