Skip to content

Commit be66701

Browse files
authored
NOISSUE - Update nullable handling (#2999)
Signed-off-by: Dusan Borovcanin <[email protected]>
1 parent 0a18a58 commit be66701

File tree

14 files changed

+63
-54
lines changed

14 files changed

+63
-54
lines changed

channels/postgres/channels.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ func PageQuery(pm channels.Page) (string, error) {
12921292
if pm.Domain != "" {
12931293
query = append(query, "c.domain_id = :domain_id")
12941294
}
1295-
if pm.Group.Set {
1295+
if pm.Group.Valid {
12961296
switch {
12971297
case pm.Group.Value != "":
12981298
query = append(query, "c.parent_group_path <@ (SELECT path from groups where id = :group_id) ")
@@ -1360,7 +1360,7 @@ func toDBChannelsPage(pm channels.Page) (dbChannelsPage, error) {
13601360
Metadata: data,
13611361
Tag: pm.Tag,
13621362
Status: pm.Status,
1363-
GroupID: sql.NullString{Valid: pm.Group.Set, String: pm.Group.Value},
1363+
GroupID: sql.NullString{Valid: pm.Group.Valid, String: pm.Group.Value},
13641364
ClientID: pm.Client,
13651365
ConnType: pm.ConnectionType,
13661366
RoleName: pm.RoleName,

groups/api/grpc/endpoint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const port = 7004
3030
var (
3131
validID = testsutil.GenerateUUID(&testing.T{})
3232
valid = "valid"
33-
desc = nullable.Value[string]{Set: true, Value: valid}
33+
desc = nullable.New(valid)
3434
validGroupResp = groups.Group{
3535
ID: testsutil.GenerateUUID(&testing.T{}),
3636
Name: valid,

groups/api/http/endpoint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
)
3333

3434
var (
35-
desc = nullable.Value[string]{Set: true, Value: valid}
35+
desc = nullable.New(valid)
3636
validGroupResp = groups.Group{
3737
ID: testsutil.GenerateUUID(&testing.T{}),
3838
Name: valid,

groups/events/events.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (cge createGroupEvent) Encode() (map[string]interface{}, error) {
7878
if cge.Name != "" {
7979
val["name"] = cge.Name
8080
}
81-
if cge.Description.Set {
81+
if cge.Description.Valid {
8282
val["description"] = cge.Description
8383
}
8484
if cge.Metadata != nil {
@@ -120,7 +120,7 @@ func (uge updateGroupEvent) Encode() (map[string]interface{}, error) {
120120
if uge.Name != "" {
121121
val["name"] = uge.Name
122122
}
123-
if uge.Description.Set {
123+
if uge.Description.Valid {
124124
val["description"] = uge.Description
125125
}
126126
if uge.Metadata != nil {
@@ -184,7 +184,7 @@ func (vge viewGroupEvent) Encode() (map[string]interface{}, error) {
184184
if vge.Name != "" {
185185
val["name"] = vge.Name
186186
}
187-
if vge.Description.Set {
187+
if vge.Description.Valid {
188188
val["description"] = vge.Description
189189
}
190190
if vge.Metadata != nil {

groups/postgres/groups.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (repo groupRepository) Update(ctx context.Context, g groups.Group) (groups.
8585
if g.Name != "" {
8686
query = append(query, "name = :name,")
8787
}
88-
if g.Description.Set {
88+
if g.Description.Valid {
8989
query = append(query, "description = :description,")
9090
}
9191
if g.Metadata != nil {
@@ -1207,7 +1207,7 @@ func toDBGroup(g groups.Group) (dbGroup, error) {
12071207
Name: g.Name,
12081208
ParentID: parentID,
12091209
DomainID: g.Domain,
1210-
Description: sql.NullString{String: g.Description.Value, Valid: g.Description.Set},
1210+
Description: sql.NullString{String: g.Description.Value, Valid: g.Description.Valid},
12111211
Tags: tags,
12121212
Metadata: data,
12131213
Path: g.Path,
@@ -1254,7 +1254,7 @@ func toGroup(g dbGroup) (groups.Group, error) {
12541254
Name: g.Name,
12551255
Parent: parentID,
12561256
Domain: g.DomainID,
1257-
Description: nullable.Value[string]{Value: g.Description.String, Set: g.Description.Valid},
1257+
Description: nullable.Value[string]{Value: g.Description.String, Valid: g.Description.Valid},
12581258
Tags: tags,
12591259
Metadata: metadata,
12601260
Level: g.Level,

groups/postgres/groups_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ var (
2727
invalidID = strings.Repeat("a", 37)
2828
validTimestamp = time.Now().UTC().Truncate(time.Millisecond)
2929
description = strings.Repeat("a", 64)
30-
desc = nullable.Value[string]{Set: true, Value: description}
30+
desc = nullable.New(description)
3131
invalidDescription = strings.Repeat("a", 1025)
32-
invalidDesc = nullable.Value[string]{Set: true, Value: invalidDescription}
32+
invalidDesc = nullable.New(invalidDescription)
3333

3434
validGroup = groups.Group{
3535
ID: testsutil.GenerateUUID(&testing.T{}),

groups/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636
idProvider = uuid.New()
3737
namegen = namegenerator.NewGenerator()
3838
description = namegen.Generate()
39-
desc = nullable.Value[string]{Set: true, Value: description}
39+
desc = nullable.Value[string]{Valid: true, Value: description}
4040
validGroup = groups.Group{
4141
ID: testsutil.GenerateUUID(&testing.T{}),
4242
Name: namegen.Generate(),

internal/nullable/bench_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func BenchmarkPointerString(b *testing.B) {
2323

2424
func BenchmarkNullableString(b *testing.B) {
2525
n := func() Value[string] {
26-
return Value[string]{Set: true, Value: "test"}
26+
return Value[string]{Valid: true, Value: "test"}
2727
}()
2828

2929
for b.Loop() {
30-
if n.Set {
30+
if n.Valid {
3131
_ = n.Value + "test"
3232
}
3333
}
@@ -66,11 +66,11 @@ func BenchmarkPointerInt(b *testing.B) {
6666

6767
func BenchmarkNullableInt(b *testing.B) {
6868
n := func() Value[int] {
69-
return Value[int]{Set: true, Value: 42}
69+
return Value[int]{Valid: true, Value: 42}
7070
}()
7171

7272
for b.Loop() {
73-
if n.Set {
73+
if n.Valid {
7474
_ = n.Value + 1
7575
}
7676
}
@@ -113,11 +113,11 @@ func BenchmarkPointerFloat(b *testing.B) {
113113

114114
func BenchmarkNullableFloat(b *testing.B) {
115115
n := func() Value[float64] {
116-
return Value[float64]{Set: true, Value: 42}
116+
return Value[float64]{Valid: true, Value: 42}
117117
}()
118118

119119
for b.Loop() {
120-
if n.Set {
120+
if n.Valid {
121121
_ = n.Value + 1
122122
}
123123
}

internal/nullable/parsers.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import (
1111

1212
var ErrInvalidQueryParams = errors.New("invalid query parameters")
1313

14-
func Parse[T any](q url.Values, key string, parser FromString[T]) (Value[T], error) {
14+
type nullable interface {
15+
~string | ~int | ~uint | ~float64
16+
}
17+
18+
func Parse[T nullable](q url.Values, key string, parser Parser[T]) (Value[T], error) {
1519
vals, ok := q[key]
1620
if !ok {
1721
return Value[T]{}, nil
@@ -22,51 +26,51 @@ func Parse[T any](q url.Values, key string, parser FromString[T]) (Value[T], err
2226
s := vals[0]
2327
if s == "" {
2428
// The actual value is sent in query, so nullable is set, but empty.
25-
return Value[T]{Set: true}, nil
29+
return Value[T]{Valid: true}, nil
2630
}
2731
return parser(s)
2832
}
2933

3034
func ParseString(s string) (Value[string], error) {
31-
return Value[string]{Set: true, Value: s}, nil
35+
return Value[string]{Valid: true, Value: s}, nil
3236
}
3337

3438
func ParseInt(s string) (Value[int], error) {
3539
val, err := strconv.Atoi(s)
3640
if err != nil {
3741
return Value[int]{}, err
3842
}
39-
return Value[int]{Set: true, Value: val}, nil
43+
return Value[int]{Valid: true, Value: val}, nil
4044
}
4145

4246
func ParseFloat(s string) (Value[float64], error) {
4347
val, err := strconv.ParseFloat(s, 64)
4448
if err != nil {
4549
return Value[float64]{}, err
4650
}
47-
return Value[float64]{Set: true, Value: val}, nil
51+
return Value[float64]{Valid: true, Value: val}, nil
4852
}
4953

5054
func ParseBool(s string) (Value[bool], error) {
5155
b, err := strconv.ParseBool(s)
5256
if err != nil {
5357
return Value[bool]{}, err
5458
}
55-
return Value[bool]{Set: true, Value: b}, nil
59+
return Value[bool]{Valid: true, Value: b}, nil
5660
}
5761

5862
func ParseU16(s string) (Value[uint16], error) {
5963
val, err := strconv.ParseUint(s, 10, 16)
6064
if err != nil {
6165
return Value[uint16]{}, err
6266
}
63-
return Value[uint16]{Set: true, Value: uint16(val)}, nil
67+
return Value[uint16]{Valid: true, Value: uint16(val)}, nil
6468
}
6569

6670
func ParseU64(s string) (Value[uint64], error) {
6771
val, err := strconv.ParseUint(s, 10, 64)
6872
if err != nil {
6973
return Value[uint64]{}, err
7074
}
71-
return Value[uint64]{Set: true, Value: val}, nil
75+
return Value[uint64]{Valid: true, Value: val}, nil
7276
}

internal/nullable/parsers_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,59 @@ func TestParseHelpers(t *testing.T) {
1313
t.Run("ParseString", func(t *testing.T) {
1414
val, err := ParseString("hello")
1515
assert.NoError(t, err)
16-
assert.True(t, val.Set)
16+
assert.True(t, val.Valid)
1717
assert.Equal(t, "hello", val.Value)
1818
})
1919

2020
t.Run("ParseInt", func(t *testing.T) {
2121
val, err := ParseInt("42")
2222
assert.NoError(t, err)
23-
assert.True(t, val.Set)
23+
assert.True(t, val.Valid)
2424
assert.Equal(t, 42, val.Value)
2525

2626
val, err = ParseInt("notanint")
2727
assert.Error(t, err)
28-
assert.False(t, val.Set)
28+
assert.False(t, val.Valid)
2929
})
3030

3131
t.Run("ParseFloat", func(t *testing.T) {
3232
val, err := ParseFloat("3.14")
3333
assert.NoError(t, err)
34-
assert.True(t, val.Set)
34+
assert.True(t, val.Valid)
3535
assert.Equal(t, 3.14, val.Value)
3636
})
3737

3838
t.Run("ParseBool", func(t *testing.T) {
3939
val, err := ParseBool("true")
4040
assert.NoError(t, err)
41-
assert.True(t, val.Set)
41+
assert.True(t, val.Valid)
4242
assert.True(t, val.Value)
4343

4444
val, err = ParseBool("false")
4545
assert.NoError(t, err)
46-
assert.True(t, val.Set)
46+
assert.True(t, val.Valid)
4747
assert.False(t, val.Value)
4848

4949
val, err = ParseBool("maybe")
5050
assert.Error(t, err)
51-
assert.False(t, val.Set)
51+
assert.False(t, val.Valid)
5252
})
5353

5454
t.Run("ParseU16", func(t *testing.T) {
5555
val, err := ParseU16("65535")
5656
assert.NoError(t, err)
57-
assert.True(t, val.Set)
57+
assert.True(t, val.Valid)
5858
assert.Equal(t, uint16(65535), val.Value)
5959

6060
val, err = ParseU16("70000")
6161
assert.Error(t, err)
62-
assert.False(t, val.Set)
62+
assert.False(t, val.Valid)
6363
})
6464

6565
t.Run("ParseU64", func(t *testing.T) {
6666
val, err := ParseU64("1234567890")
6767
assert.NoError(t, err)
68-
assert.True(t, val.Set)
68+
assert.True(t, val.Valid)
6969
assert.Equal(t, uint64(1234567890), val.Value)
7070
})
7171
}
@@ -86,21 +86,21 @@ func TestParseQueryParam(t *testing.T) {
8686
query: url.Values{},
8787
key: "limit",
8888
parser: ParseInt,
89-
expect: Value[int]{Set: false},
89+
expect: Value[int]{Valid: false},
9090
},
9191
{
9292
name: "empty value",
9393
query: url.Values{"limit": {""}},
9494
key: "limit",
9595
parser: ParseInt,
96-
expect: Value[int]{Set: true},
96+
expect: Value[int]{Valid: true},
9797
},
9898
{
9999
name: "valid int",
100100
query: url.Values{"limit": {"10"}},
101101
key: "limit",
102102
parser: ParseInt,
103-
expect: Value[int]{Set: true, Value: 10},
103+
expect: Value[int]{Valid: true, Value: 10},
104104
},
105105
{
106106
name: "invalid int",

0 commit comments

Comments
 (0)