Skip to content

Commit e6df514

Browse files
authored
feat: implement conversion for recently added types to proto and back (#124)
1 parent 32ce783 commit e6df514

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

types/interval_compound_type.go

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func (m IntervalCompoundType) Equals(rhs Type) bool {
4949
if o, ok := rhs.(IntervalCompoundType); ok {
5050
return o == m
5151
}
52+
if o, ok := rhs.(*IntervalCompoundType); ok {
53+
return *o == m
54+
}
5255
return false
5356
}
5457

types/types.go

+49
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,16 @@ func TypeFromProto(t *proto.Type) Type {
276276
TypeVariationRef: t.IntervalDay.TypeVariationReference,
277277
Precision: precision,
278278
}
279+
case *proto.Type_IntervalCompound_:
280+
precision, err := ProtoToTimePrecision(t.IntervalCompound.Precision)
281+
if err != nil {
282+
panic(fmt.Sprintf("Invalid precision %v", err))
283+
}
284+
return &IntervalCompoundType{
285+
nullability: t.IntervalCompound.Nullability,
286+
typeVariationRef: t.IntervalCompound.TypeVariationReference,
287+
precision: precision,
288+
}
279289
case *proto.Type_TimestampTz:
280290
return &TimestampTzType{
281291
Nullability: t.TimestampTz.Nullability,
@@ -311,6 +321,26 @@ func TypeFromProto(t *proto.Type) Type {
311321
Scale: t.Decimal.Scale,
312322
Precision: t.Decimal.Precision,
313323
}
324+
case *proto.Type_PrecisionTimestamp_:
325+
precision, err := ProtoToTimePrecision(t.PrecisionTimestamp.Precision)
326+
if err != nil {
327+
panic(fmt.Sprintf("Invalid precision %v", err))
328+
}
329+
return &PrecisionTimestampType{
330+
Nullability: t.PrecisionTimestamp.Nullability,
331+
TypeVariationRef: t.PrecisionTimestamp.TypeVariationReference,
332+
Precision: precision,
333+
}
334+
case *proto.Type_PrecisionTimestampTz:
335+
precision, err := ProtoToTimePrecision(t.PrecisionTimestampTz.Precision)
336+
if err != nil {
337+
panic(fmt.Sprintf("Invalid precision %v", err))
338+
}
339+
return &PrecisionTimestampTzType{PrecisionTimestampType{
340+
Nullability: t.PrecisionTimestampTz.Nullability,
341+
TypeVariationRef: t.PrecisionTimestampTz.TypeVariationReference,
342+
Precision: precision,
343+
}}
314344
case *proto.Type_Struct_:
315345
fields := make([]Type, len(t.Struct.Types))
316346
for i, f := range t.Struct.Types {
@@ -655,6 +685,13 @@ func TypeToProto(t Type) *proto.Type {
655685
Precision: &precision,
656686
Nullability: t.Nullability,
657687
TypeVariationReference: t.TypeVariationRef}}}
688+
case IntervalCompoundType:
689+
precision := t.precision.ToProtoVal()
690+
return &proto.Type{Kind: &proto.Type_IntervalCompound_{
691+
IntervalCompound: &proto.Type_IntervalCompound{
692+
Precision: precision,
693+
Nullability: t.nullability,
694+
TypeVariationReference: t.typeVariationRef}}}
658695
case *UUIDType:
659696
return &proto.Type{Kind: &proto.Type_Uuid{
660697
Uuid: &proto.Type_UUID{
@@ -680,6 +717,18 @@ func TypeToProto(t Type) *proto.Type {
680717
TypeVariationReference: t.TypeVariationRef}}}
681718
case *DecimalType:
682719
return t.ToProto()
720+
case *PrecisionTimestampType:
721+
return &proto.Type{Kind: &proto.Type_PrecisionTimestamp_{
722+
PrecisionTimestamp: &proto.Type_PrecisionTimestamp{
723+
Precision: int32(t.Precision),
724+
Nullability: t.Nullability,
725+
TypeVariationReference: t.TypeVariationRef}}}
726+
case *PrecisionTimestampTzType:
727+
return &proto.Type{Kind: &proto.Type_PrecisionTimestampTz{
728+
PrecisionTimestampTz: &proto.Type_PrecisionTimestampTZ{
729+
Precision: int32(t.Precision),
730+
Nullability: t.Nullability,
731+
TypeVariationReference: t.TypeVariationRef}}}
683732
case *StructType:
684733
return t.ToProto()
685734
case *ListType:

types/types_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ func TestTypeRoundtrip(t *testing.T) {
8787
&FixedBinaryType{Nullability: n, Length: 45},
8888
&IntervalDayType{Nullability: n, Precision: 5},
8989
&IntervalDayType{Nullability: n, Precision: 0},
90+
NewIntervalCompoundType().WithPrecision(PrecisionEMinus7Seconds).WithNullability(n),
9091

9192
&DecimalType{Nullability: n, Precision: 34, Scale: 3},
93+
&PrecisionTimestampType{Nullability: n, Precision: PrecisionEMinus4Seconds},
94+
&PrecisionTimestampTzType{PrecisionTimestampType: PrecisionTimestampType{Nullability: n, Precision: PrecisionEMinus5Seconds}},
9295
&MapType{Nullability: n, Key: &Int8Type{}, Value: &Int16Type{Nullability: n}},
9396
&ListType{Nullability: n, Type: &TimeType{Nullability: n}},
9497
&StructType{Nullability: n, Types: []Type{

0 commit comments

Comments
 (0)