-
Notifications
You must be signed in to change notification settings - Fork 107
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
Preserve YANG elements order when generating structs #942
Comments
Hi, the fields are ordered here: Line 1032 in 8efc814
Line 1259 in 8efc814
and the writing to the template is done here by looping through the slice: Line 1325 in 8efc814
If you trace back the dataflow from ParsedDirectory.Fields, then you'll eventually reach goyang's So fundamentally the issue is that the order of fields are not respected when the TLDR: The easiest way I can see is to loop through the set of Entry.Node.Statement().SubStatements() to find the ordering, and use that order at Line 1032 in 8efc814
Entry is not available in ParsedDirectory , so you would need to populate this information into ParsedDirectory from Line 321 in 8efc814
Entry from https://github.com/openconfig/ygot/blob/8efc81471e0fe679c453aa0e8c03d752721733bc/ygen/directory.go#L223C4-L223C9
Any other solution would involve adding a new field to either the |
@hellt Did you make any progress on this? I think we may also need it, so we could collaborate on this |
@nleiva unfortunately I haven't... |
No worries. We are exploring avenues to address non-RFC7951 vendor implementations 1 when serializing YANG to JSON. However, even if the generated struct fields follow a specific order, // structJSON marshals a GoStruct to a map[string]any which can be
// handed to JSON marshal. parentMod specifies the module that the supplied
// GoStruct is defined within such that RFC7951 format JSON is able to consider
// whether to prepend the name of the module to an element. The format of JSON to
// be produced and whether such module names are prepended is controlled through the
// supplied jsonOutputConfig. Returns an error if the GoStruct cannot be rendered
// to JSON.
func structJSON(s GoStruct, parentMod string, args jsonOutputConfig) (map[string]any, error) {
// ...
} The Junos schema defines certain configuration objects as lists. In JSON configuration data, a list instance is encoded as a name/array pair, and the array elements are JSON objects. Generally, the order of members in a JSON-encoded list entry is arbitrary because JSON objects are fundamentally unordered collections of members. However, the Junos schema requires that list keys precede any other siblings within a list entry and appear in the order specified by the schema. source |
Hi all,
I wonder which code path is responsible to sort alphabetically generated struct's fields?
consider the following module:
The generated struct will have fields sorted as
and I would like to maintain the order of the struct fields (Name first, Address second). Would you hint me where the relevant code path is so that we might implement a
preserve-yang-elements-order
flag?The text was updated successfully, but these errors were encountered: