Skip to content

Commit

Permalink
Libs(Go): add convenience constructor for static nullable strings
Browse files Browse the repository at this point in the history
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")
}
```
  • Loading branch information
svix-onelson committed Oct 29, 2024
1 parent 141d3cd commit 6faacd3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go/svix.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func String(s string) *string {
func NullableString(s *string) *openapi.NullableString {
return openapi.NewNullableString(s)
}
func StaticNullableString(s string) openapi.NullableString {
return *NullableString(String(s))
}
func NullableInt32(num *int32) *openapi.NullableInt32 {
return openapi.NewNullableInt32(num)
}
Expand Down
20 changes: 20 additions & 0 deletions go/svix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,23 @@ func TestKitchenSink(t *testing.T) {
}
}
}

func TestStaticNullableString(t *testing.T) {
app := &svix.ApplicationIn{
Name: "test",
Uid: svix.StaticNullableString("my-uid"),
}

if !app.Uid.IsSet() {
t.Fatalf("app.Uid is not set but should be")
}

if *app.Uid.Get() != "my-uid" {
t.Fatalf("app.Uid has unexpected value: `%s`", *app.Uid.Get())
}

app.Uid.Unset()
if app.Uid.IsSet() {
t.Fatalf("app.Uid is set but shouldn't be")
}
}

0 comments on commit 6faacd3

Please sign in to comment.