Update VDLIsZero and native zero semantics.
This CL adds extra configuration to vdltool.Config to describe
native types. We add a Kind field to specify what kind of type
the native type is, which is useful when generating zero values,
and will also be useful in the future if we choose to support
pointer or interface native types.
We also add a Zero field to to specify zero semantics. This lets
us avoid wire/native conversions when checking zero values, and
also lets us use the Go zero value when setting zero values, both
of which should give us a speedup. Even more importantly, for
checking zero values we require that we can either compare
directly against the Go zero value, or an IsZero field or method
is specified; we no longer support wire/native conversions for
zero checking in generated code. This simplifies the code
generation, and is easily satisfied by all current native types.
Future native types can also easily satisfy this constraint, by
adding an IsZero method or function as appropriate.
Related to these changes, we remove the error return argument
from VDLIsZero, so we're left with the simpler method:
VDLIsZero() bool
This also simplifies code generation, and also provides a more
intuitive API for the user. The only downside is that users
can't return errors if the native representation is invalid; they
should just return false in this case, and let the actual native
conversion report the error.
Also fixed a bug in the VDLWrite codegen for optional and any
types. We'd screwed up the handling of StartValue/FinishValue.
The resulting code and codegen logic are both simpler.
MultiPart: 5/5
Change-Id: I61c390eefd1584c4683fb06647f9ae11d6dddb2f
diff --git a/go/src/v.io/x/sensorlog/internal/sbmodel/sbmodel.vdl.go b/go/src/v.io/x/sensorlog/internal/sbmodel/sbmodel.vdl.go
index 4aad326..c7e5a64 100644
--- a/go/src/v.io/x/sensorlog/internal/sbmodel/sbmodel.vdl.go
+++ b/go/src/v.io/x/sensorlog/internal/sbmodel/sbmodel.vdl.go
@@ -17,7 +17,7 @@
"fmt"
"time"
"v.io/v23/vdl"
- time_2 "v.io/v23/vdlroot/time"
+ vdltime "v.io/v23/vdlroot/time"
)
var _ = __VDLInit() // Must be first; see __VDLInit comments for details.
@@ -141,8 +141,8 @@
return nil
}
-func (x VDeviceCfg) VDLIsZero() (bool, error) {
- return x == VDeviceCfg{}, nil
+func (x VDeviceCfg) VDLIsZero() bool {
+ return x == VDeviceCfg{}
}
func (x VDeviceCfg) VDLWrite(enc vdl.Encoder) error {
@@ -153,7 +153,7 @@
if err := enc.NextField("Desc"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.Desc); err != nil {
@@ -167,7 +167,7 @@
if err := enc.NextField("SgPublishSb"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.SgPublishSb); err != nil {
@@ -313,8 +313,8 @@
return nil
}
-func (x KDeviceCfg) VDLIsZero() (bool, error) {
- return x == KDeviceCfg{}, nil
+func (x KDeviceCfg) VDLIsZero() bool {
+ return x == KDeviceCfg{}
}
func (x KDeviceCfg) VDLWrite(enc vdl.Encoder) error {
@@ -325,7 +325,7 @@
if err := enc.NextField("DevId"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.DevId); err != nil {
@@ -415,12 +415,12 @@
}
}
}
- var wireValue5 time_2.Time
- if err := time_2.TimeFromNative(&wireValue5, m.Start); err != nil {
+ var wireValue5 vdltime.Time
+ if err := vdltime.TimeFromNative(&wireValue5, m.Start); err != nil {
return err
}
- var8 := (wireValue5 == time_2.Time{})
+ var8 := (wireValue5 == vdltime.Time{})
if var8 {
if err := fieldsTarget1.ZeroField("Start"); err != nil && err != vdl.ErrFieldNoExist {
return err
@@ -440,12 +440,12 @@
}
}
}
- var wireValue9 time_2.Duration
- if err := time_2.DurationFromNative(&wireValue9, m.Interval); err != nil {
+ var wireValue9 vdltime.Duration
+ if err := vdltime.DurationFromNative(&wireValue9, m.Interval); err != nil {
return err
}
- var12 := (wireValue9 == time_2.Duration{})
+ var12 := (wireValue9 == vdltime.Duration{})
if var12 {
if err := fieldsTarget1.ZeroField("Interval"); err != nil && err != vdl.ErrFieldNoExist {
return err
@@ -478,8 +478,8 @@
type SamplerDefTarget struct {
Value *SamplerDef
scriptTarget vdl.StringTarget
- startTarget time_2.TimeTarget
- intervalTarget time_2.DurationTarget
+ startTarget vdltime.TimeTarget
+ intervalTarget vdltime.DurationTarget
vdl.TargetBase
vdl.FieldsTargetBase
}
@@ -518,22 +518,10 @@
t.Value.Script = ""
return nil
case "Start":
- t.Value.Start = func() time.Time {
- var native time.Time
- if err := vdl.Convert(&native, time_2.Time{}); err != nil {
- panic(err)
- }
- return native
- }()
+ t.Value.Start = time.Time{}
return nil
case "Interval":
- t.Value.Interval = func() time.Duration {
- var native time.Duration
- if err := vdl.Convert(&native, time_2.Duration{}); err != nil {
- panic(err)
- }
- return native
- }()
+ t.Value.Interval = time.Duration(0)
return nil
default:
return fmt.Errorf("field %s not in struct v.io/x/sensorlog/internal/sbmodel.SamplerDef", name)
@@ -544,25 +532,17 @@
return nil
}
-func (x SamplerDef) VDLIsZero() (bool, error) {
+func (x SamplerDef) VDLIsZero() bool {
if x.Script != "" {
- return false, nil
+ return false
}
- var wireStart time_2.Time
- if err := time_2.TimeFromNative(&wireStart, x.Start); err != nil {
- return false, err
+ if !x.Start.IsZero() {
+ return false
}
- if wireStart != (time_2.Time{}) {
- return false, nil
+ if x.Interval != time.Duration(0) {
+ return false
}
- var wireInterval time_2.Duration
- if err := time_2.DurationFromNative(&wireInterval, x.Interval); err != nil {
- return false, err
- }
- if wireInterval != (time_2.Duration{}) {
- return false, nil
- }
- return true, nil
+ return true
}
func (x SamplerDef) VDLWrite(enc vdl.Encoder) error {
@@ -573,7 +553,7 @@
if err := enc.NextField("Script"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.Script); err != nil {
@@ -583,27 +563,27 @@
return err
}
}
- var wireStart time_2.Time
- if err := time_2.TimeFromNative(&wireStart, x.Start); err != nil {
- return err
- }
- if wireStart != (time_2.Time{}) {
+ if !x.Start.IsZero() {
if err := enc.NextField("Start"); err != nil {
return err
}
- if err := wireStart.VDLWrite(enc); err != nil {
+ var wire vdltime.Time
+ if err := vdltime.TimeFromNative(&wire, x.Start); err != nil {
+ return err
+ }
+ if err := wire.VDLWrite(enc); err != nil {
return err
}
}
- var wireInterval time_2.Duration
- if err := time_2.DurationFromNative(&wireInterval, x.Interval); err != nil {
- return err
- }
- if wireInterval != (time_2.Duration{}) {
+ if x.Interval != time.Duration(0) {
if err := enc.NextField("Interval"); err != nil {
return err
}
- if err := wireInterval.VDLWrite(enc); err != nil {
+ var wire vdltime.Duration
+ if err := vdltime.DurationFromNative(&wire, x.Interval); err != nil {
+ return err
+ }
+ if err := wire.VDLWrite(enc); err != nil {
return err
}
}
@@ -641,19 +621,19 @@
return err
}
case "Start":
- var wire time_2.Time
+ var wire vdltime.Time
if err := wire.VDLRead(dec); err != nil {
return err
}
- if err := time_2.TimeToNative(wire, &x.Start); err != nil {
+ if err := vdltime.TimeToNative(wire, &x.Start); err != nil {
return err
}
case "Interval":
- var wire time_2.Duration
+ var wire vdltime.Duration
if err := wire.VDLRead(dec); err != nil {
return err
}
- if err := time_2.DurationToNative(wire, &x.Interval); err != nil {
+ if err := vdltime.DurationToNative(wire, &x.Interval); err != nil {
return err
}
default:
@@ -810,21 +790,17 @@
return nil
}
-func (x VStreamDef) VDLIsZero() (bool, error) {
+func (x VStreamDef) VDLIsZero() bool {
if x.Desc != "" {
- return false, nil
+ return false
}
- isZeroSampler, err := x.Sampler.VDLIsZero()
- if err != nil {
- return false, err
- }
- if !isZeroSampler {
- return false, nil
+ if !x.Sampler.VDLIsZero() {
+ return false
}
if x.Enabled {
- return false, nil
+ return false
}
- return true, nil
+ return true
}
func (x VStreamDef) VDLWrite(enc vdl.Encoder) error {
@@ -835,7 +811,7 @@
if err := enc.NextField("Desc"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.Desc); err != nil {
@@ -845,11 +821,7 @@
return err
}
}
- isZeroSampler, err := x.Sampler.VDLIsZero()
- if err != nil {
- return err
- }
- if !isZeroSampler {
+ if !x.Sampler.VDLIsZero() {
if err := enc.NextField("Sampler"); err != nil {
return err
}
@@ -861,7 +833,7 @@
if err := enc.NextField("Enabled"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*bool)(nil))); err != nil {
+ if err := enc.StartValue(vdl.BoolType); err != nil {
return err
}
if err := enc.EncodeBool(x.Enabled); err != nil {
@@ -1039,8 +1011,8 @@
return nil
}
-func (x KStreamDef) VDLIsZero() (bool, error) {
- return x == KStreamDef{}, nil
+func (x KStreamDef) VDLIsZero() bool {
+ return x == KStreamDef{}
}
func (x KStreamDef) VDLWrite(enc vdl.Encoder) error {
@@ -1051,7 +1023,7 @@
if err := enc.NextField("DevId"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.DevId); err != nil {
@@ -1065,7 +1037,7 @@
if err := enc.NextField("StreamId"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.StreamId); err != nil {
@@ -1183,12 +1155,12 @@
}
}
}
- var wireValue8 time_2.Time
- if err := time_2.TimeFromNative(&wireValue8, m.Timestamp); err != nil {
+ var wireValue8 vdltime.Time
+ if err := vdltime.TimeFromNative(&wireValue8, m.Timestamp); err != nil {
return err
}
- var11 := (wireValue8 == time_2.Time{})
+ var11 := (wireValue8 == vdltime.Time{})
if var11 {
if err := fieldsTarget1.ZeroField("Timestamp"); err != nil && err != vdl.ErrFieldNoExist {
return err
@@ -1222,7 +1194,7 @@
Value *KDataPoint
devIdTarget vdl.StringTarget
streamIdTarget vdl.StringTarget
- timestampTarget time_2.TimeTarget
+ timestampTarget vdltime.TimeTarget
vdl.TargetBase
vdl.FieldsTargetBase
}
@@ -1264,13 +1236,7 @@
t.Value.StreamId = ""
return nil
case "Timestamp":
- t.Value.Timestamp = func() time.Time {
- var native time.Time
- if err := vdl.Convert(&native, time_2.Time{}); err != nil {
- panic(err)
- }
- return native
- }()
+ t.Value.Timestamp = time.Time{}
return nil
default:
return fmt.Errorf("field %s not in struct v.io/x/sensorlog/internal/sbmodel.KDataPoint", name)
@@ -1281,21 +1247,17 @@
return nil
}
-func (x KDataPoint) VDLIsZero() (bool, error) {
+func (x KDataPoint) VDLIsZero() bool {
if x.DevId != "" {
- return false, nil
+ return false
}
if x.StreamId != "" {
- return false, nil
+ return false
}
- var wireTimestamp time_2.Time
- if err := time_2.TimeFromNative(&wireTimestamp, x.Timestamp); err != nil {
- return false, err
+ if !x.Timestamp.IsZero() {
+ return false
}
- if wireTimestamp != (time_2.Time{}) {
- return false, nil
- }
- return true, nil
+ return true
}
func (x KDataPoint) VDLWrite(enc vdl.Encoder) error {
@@ -1306,7 +1268,7 @@
if err := enc.NextField("DevId"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.DevId); err != nil {
@@ -1320,7 +1282,7 @@
if err := enc.NextField("StreamId"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.StreamId); err != nil {
@@ -1330,15 +1292,15 @@
return err
}
}
- var wireTimestamp time_2.Time
- if err := time_2.TimeFromNative(&wireTimestamp, x.Timestamp); err != nil {
- return err
- }
- if wireTimestamp != (time_2.Time{}) {
+ if !x.Timestamp.IsZero() {
if err := enc.NextField("Timestamp"); err != nil {
return err
}
- if err := wireTimestamp.VDLWrite(enc); err != nil {
+ var wire vdltime.Time
+ if err := vdltime.TimeFromNative(&wire, x.Timestamp); err != nil {
+ return err
+ }
+ if err := wire.VDLWrite(enc); err != nil {
return err
}
}
@@ -1387,11 +1349,11 @@
return err
}
case "Timestamp":
- var wire time_2.Time
+ var wire vdltime.Time
if err := wire.VDLRead(dec); err != nil {
return err
}
- if err := time_2.TimeToNative(wire, &x.Timestamp); err != nil {
+ if err := vdltime.TimeToNative(wire, &x.Timestamp); err != nil {
return err
}
default:
@@ -1414,7 +1376,7 @@
// __VDLReflect describes the VDataPoint union type.
__VDLReflect(__VDataPointReflect)
FillVDLTarget(vdl.Target, *vdl.Type) error
- VDLIsZero() (bool, error)
+ VDLIsZero() bool
VDLWrite(vdl.Encoder) error
}
// VDataPointValue represents field Value of the VDataPoint union type.
@@ -1546,12 +1508,12 @@
return nil, fmt.Errorf("got %T, want *VDataPoint", union)
}
-func (x VDataPointValue) VDLIsZero() (bool, error) {
- return x.Value == 0, nil
+func (x VDataPointValue) VDLIsZero() bool {
+ return x.Value == 0
}
-func (x VDataPointError) VDLIsZero() (bool, error) {
- return false, nil
+func (x VDataPointError) VDLIsZero() bool {
+ return false
}
func (x VDataPointValue) VDLWrite(enc vdl.Encoder) error {
@@ -1561,7 +1523,7 @@
if err := enc.NextField("Value"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*float64)(nil))); err != nil {
+ if err := enc.StartValue(vdl.Float64Type); err != nil {
return err
}
if err := enc.EncodeFloat(x.Value); err != nil {
@@ -1583,7 +1545,7 @@
if err := enc.NextField("Error"); err != nil {
return err
}
- if err := enc.StartValue(vdl.TypeOf((*string)(nil))); err != nil {
+ if err := enc.StartValue(vdl.StringType); err != nil {
return err
}
if err := enc.EncodeString(x.Value); err != nil {