Add PathAndStruct helper for generated non-leaf PathStruct types.
A common use case is where a user would like to construct a path and populate its corresponding GoStruct at the same time. This helper obviates the need to retrieve the struct using the GetOrCreateXXX functions on top of using the path API.
This can be convenient for long paths such as the following example:
Before
d := &structAPI.Device{}
addr := d.GetOrCreateInterface("Ethernet0").GetOrCreateSubinterface(0).GetOrCreateIpv4().GetOrCreateAddress("10.10.10.10")
addr.PrefixLength = ygot.Uint8(24)
p, addr := pathAPI.Interface("Ethernet0").Subinterface(0).Ipv4().Address("10.10.10.10").Replace(addr)
After
p, addr := pathAPI.Interface("Ethernet0").Subinterface(0).Ipv4().Address("10.10.10.10").PathAndStruct()
addr.PrefixLength = ygot.Uint8(24)
p.Replace(addr)
This helper is not generated for leaves, since their types can be accessed using ygot helper functions such as ygot.Int32().
Coverage decreased (-0.01%) to 90.575% when pulling 319f976a6ccbc527c64ea2382ddb043bdbb573fb on path-and-struct into 75ebd158f7f0b9403c225e55801090f8661ecece on master.
One alternative is for Replace on an intermediate node to always construct a new value and accept a lambda that can modify it, e.g.
pathAPI.Interface("Ethernet0").Subinterface(0).Ipv4().Address("10.10.10.10").Replace(t, func(addr A) {
addr.PrefixLength = ygot.Uint8(24)
})
Not advocating for it, just thinking through the options.
Wen, is this change defunct at this point -- or should we try and merge it?
When generics is ready a better design for this might be just to provide the function
func ygot.GoStruct[T GoStruct](PathStruct[T]) T {}