Skip to content

Commit

Permalink
Merge pull request #22 from mittwald/bugfix/fix-uuid-nil
Browse files Browse the repository at this point in the history
Fix panic when a nil *uuid.UUID is provided
  • Loading branch information
elenz97 authored Jan 5, 2024
2 parents 001bc07 + 2899bb1 commit 9748058
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/onsi/gomega v1.30.0 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmt
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
Expand Down
19 changes: 18 additions & 1 deletion internal/valueutil/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package valueutil
import (
"fmt"
"github.com/hashicorp/terraform-plugin-framework/types"
"reflect"
)

func StringOrNull[T string](s T) types.String {
Expand All @@ -20,8 +21,24 @@ func StringPtrOrNull[T ~string](s *T) types.String {
}

func StringerOrNull(s fmt.Stringer) types.String {
if s == nil {
if isReallyNil(s) {
return types.StringNull()
}
return types.StringValue(s.String())
}

func isReallyNil(v any) bool {
if v == nil {
return true
}

rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return rv.IsNil()
case reflect.Array:
return rv.Len() == 0
default:
return false
}
}
31 changes: 31 additions & 0 deletions internal/valueutil/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package valueutil

import (
"github.com/google/uuid"
. "github.com/onsi/gomega"
"testing"
)

func TestStringerOrNullAcceptsStringer(t *testing.T) {
g := NewWithT(t)
u := uuid.New()
s := StringerOrNull(u)

g.Expect(s.IsNull()).To(BeFalse())
g.Expect(s.ValueString()).To(Equal(u.String()))
}

func TestStringerOrNullAcceptsNil(t *testing.T) {
g := NewWithT(t)
s := StringerOrNull(nil)

g.Expect(s.IsNull()).To(BeTrue())
}

func TestStringerOrNullAcceptsNilInterface(t *testing.T) {
g := NewWithT(t)
u := (*uuid.UUID)(nil)
s := StringerOrNull(u)

g.Expect(s.IsNull()).To(BeTrue())
}

0 comments on commit 9748058

Please sign in to comment.