Skip to content

Commit c4daee8

Browse files
authored
Libs(Go): add convenience constructor for static nullable strings (#1497)
Fixes <#1198> While this isn't quite the type of change that was requested in #1198, I'm hopeful it'll reduce a bit of the friction. A new constructor is now available as `svix.StaticNullableString()` which gives an `openapi.NullableString`. Note that the return type (not a pointer) is inconsistent with the rest of the functions that act as primative constructors, and in fact goes against the convention of constructors returning pointers to the value they initialize. The issue was raised, however, that all the places in the lib where a `NullableString` is needed, we'd have to dereference it anyway. Since this alt constructor is all about convenience, we may as well break convention. _In for a penny, in for a pound..._ The net effect is instead of: ```go appIn := svix.ApplicationIn{ ... Uid: *svix.NullableString(svix.String("myuid")) } ``` folks will now be able to write: ```go appIn := svix.ApplicationIn{ ... Uid: svix.StaticNullableString("myuid") } ```
2 parents 141d3cd + 6faacd3 commit c4daee8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

go/svix.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ func String(s string) *string {
4141
func NullableString(s *string) *openapi.NullableString {
4242
return openapi.NewNullableString(s)
4343
}
44+
func StaticNullableString(s string) openapi.NullableString {
45+
return *NullableString(String(s))
46+
}
4447
func NullableInt32(num *int32) *openapi.NullableInt32 {
4548
return openapi.NewNullableInt32(num)
4649
}

go/svix_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,23 @@ func TestKitchenSink(t *testing.T) {
9494
}
9595
}
9696
}
97+
98+
func TestStaticNullableString(t *testing.T) {
99+
app := &svix.ApplicationIn{
100+
Name: "test",
101+
Uid: svix.StaticNullableString("my-uid"),
102+
}
103+
104+
if !app.Uid.IsSet() {
105+
t.Fatalf("app.Uid is not set but should be")
106+
}
107+
108+
if *app.Uid.Get() != "my-uid" {
109+
t.Fatalf("app.Uid has unexpected value: `%s`", *app.Uid.Get())
110+
}
111+
112+
app.Uid.Unset()
113+
if app.Uid.IsSet() {
114+
t.Fatalf("app.Uid is set but shouldn't be")
115+
}
116+
}

0 commit comments

Comments
 (0)