ygot icon indicating copy to clipboard operation
ygot copied to clipboard

Add PathAndStruct helper for generated non-leaf PathStruct types.

Open wenovus opened this issue 4 years ago • 4 comments

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().

wenovus avatar May 26 '21 19:05 wenovus

Coverage Status

Coverage decreased (-0.01%) to 90.575% when pulling 319f976a6ccbc527c64ea2382ddb043bdbb573fb on path-and-struct into 75ebd158f7f0b9403c225e55801090f8661ecece on master.

coveralls avatar May 26 '21 19:05 coveralls

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.

greg-dennis avatar Jun 30 '21 20:06 greg-dennis

Wen, is this change defunct at this point -- or should we try and merge it?

robshakir avatar Nov 07 '21 00:11 robshakir

When generics is ready a better design for this might be just to provide the function

func ygot.GoStruct[T GoStruct](PathStruct[T]) T {}

wenovus avatar Nov 16 '21 19:11 wenovus