ygot
ygot copied to clipboard
Preserve YANG elements order when generating structs
Hi all, I wonder which code path is responsible to sort alphabetically generated struct's fields?
consider the following module:
module test {
yang-version 1.1;
namespace "urn:srlinux:ndk:test";
prefix srl-labs;
container app {
leaf name {
type string;
}
leaf address {
type string;
}
}
}
The generated struct will have fields sorted as
type Foo struct {
Address string
Name string
}
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?
Hi, the fields are ordered here: https://github.com/openconfig/ygot/blob/8efc81471e0fe679c453aa0e8c03d752721733bc/gogen/gogen.go#L1032, and appended to an ordered slice here: https://github.com/openconfig/ygot/blob/8efc81471e0fe679c453aa0e8c03d752721733bc/gogen/gogen.go#L1259
and the writing to the template is done here by looping through the slice: https://github.com/openconfig/ygot/blob/8efc81471e0fe679c453aa0e8c03d752721733bc/gogen/gogen.go#L1325
If you trace back the dataflow from ParsedDirectory.Fields, then you'll eventually reach goyang's Entry
type with its Dir map[string]*Entry
field, whereupon you'll end up at https://github.com/openconfig/goyang/blob/5ad0d2feb9ce655fb39e414bd4e3696356780cdb/pkg/yang/entry.go#L410-L418, which is invoked in ToEntry
: https://github.com/openconfig/goyang/blob/5ad0d2feb9ce655fb39e414bd4e3696356780cdb/pkg/yang/entry.go#L712-L720, and here you'll finally find some of the original ordering, but even then they're grouped by node type (e.g. leaf vs. leaf-list): https://github.com/openconfig/goyang/blob/5ad0d2feb9ce655fb39e414bd4e3696356780cdb/pkg/yang/ast.go#L454
So fundamentally the issue is that the order of fields are not respected when the Entry
representation is converted from the original parsed AST (i.e. []*Statement).
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 https://github.com/openconfig/ygot/blob/8efc81471e0fe679c453aa0e8c03d752721733bc/gogen/gogen.go#L1032, unfortunately Entry
is not available in ParsedDirectory
, so you would need to populate this information into ParsedDirectory
from https://github.com/openconfig/ygot/blob/8efc81471e0fe679c453aa0e8c03d752721733bc/ygen/directory.go#L321, by accessing 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 Entry
AST or other Node types, and that would involve some more effort.