open-traffic-generator / openapiart

OpenAPI artifact generator
MIT License
6 stars 4 forks source link

Need support for marshalling and unmarshalling proto object with defaults #358

Closed ashutshkumr closed 1 year ago

ashutshkumr commented 2 years ago

Is your feature request related to a use case? Please describe. When implementing gRPC server, we receive request as a proto object without guarantees of defaults having been set. Hence a helper is needed to take in proto object and set defaults - similar to FromJson.

Without this, handler has to do redundant marshalling/unmarshalling for setting defaults.

Describe the solution you'd like

in := &otg.GetMetricsRequest{}
out := gosnappi.NewApi().NewMetricsRequest()

// will internally set proto object and set defaults
if err := out.FromProto(in); err != nil {
  return err
}

// should output proto object with defaults set (unlike out.Msg())
in, err := out.ToProto()
err != nil {
  return err
}

Describe alternatives you've considered

in := &otg.GetMetricsRequest{}
out := gosnappi.NewApi().NewMetricsRequest()

// this won't set the default values
out.SetMsg(in)

// hence serialize to pb text (this also won't set the default values)
pb, err := rbody.ToPbText()
if err != nil {
    return err
}
// deserialize from pb text (this will set the default values)
if err := out.FromPbText(pb); err != nil {
  return err
}
ashutshkumr commented 2 years ago

@ajbalogh @alakendu kindly review the proposal

ashutshkumr commented 1 year ago

Fixed in latest