Skip to content

Commit 31085d2

Browse files
authored
fix: Verify nil value consistently (#1478)
Instead of #1476 Closes #1475 The `typed nil` also can be observed in cloudquery/cloudquery#16221
1 parent 78219a6 commit 31085d2

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

scalar/inet.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,23 @@ func (s *Inet) Set(val any) error {
9292
}
9393
s.Value = ipnet
9494
case *net.IPNet:
95-
if err := s.Set(*value); err != nil {
96-
return err
95+
if value == nil {
96+
s.Valid = false
97+
return nil
9798
}
99+
return s.Set(*value)
98100
case *net.IP:
99-
if err := s.Set(*value); err != nil {
100-
return err
101+
if value == nil {
102+
s.Valid = false
103+
return nil
101104
}
105+
return s.Set(*value)
102106
case *string:
103-
if err := s.Set(*value); err != nil {
104-
return err
107+
if value == nil {
108+
s.Valid = false
109+
return nil
105110
}
111+
return s.Set(*value)
106112
default:
107113
if tv, ok := value.(encoding.TextMarshaler); ok {
108114
text, err := tv.MarshalText()

scalar/inet_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func TestInetSet(t *testing.T) {
7272
return &b
7373
}("127.0.0.1"), result: Inet{Value: mustParseInet(t, "127.0.0.1"), Valid: true}},
7474
{source: &Inet{Value: &net.IPNet{IP: net.ParseIP("::ffff:0.0.0.0"), Mask: net.CIDRMask(104, 128)}, Valid: true}, result: Inet{Value: &net.IPNet{IP: net.ParseIP("::ffff:0.0.0.0"), Mask: net.CIDRMask(104, 128)}, Valid: true}},
75+
{source: (*net.IPNet)(nil), result: Inet{Value: nil, Valid: false}},
7576
}
7677

7778
for i, tt := range successfulTests {

scalar/interval.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func (s *MonthInterval) Set(value any) error {
6565
return nil
6666
case *string:
6767
if v == nil {
68-
return s.Int.Set(nil)
68+
s.Valid = false
69+
return nil
6970
}
7071
return s.Set(*v)
7172
case map[string]any:

scalar/mac.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (s *Mac) Get() any {
4444

4545
func (s *Mac) Set(val any) error {
4646
if val == nil {
47+
s.Valid = false
4748
return nil
4849
}
4950

@@ -68,11 +69,13 @@ func (s *Mac) Set(val any) error {
6869
s.Value = addr
6970
case *net.HardwareAddr:
7071
if value == nil {
72+
s.Valid = false
7173
return nil
7274
}
7375
return s.Set(*value)
7476
case *string:
7577
if value == nil {
78+
s.Valid = false
7679
return nil
7780
}
7881
return s.Set(*value)

0 commit comments

Comments
 (0)