Initial QSfera import
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
// Package structs provides some utility functions for dealing with structs.
|
||||
package structs
|
||||
|
||||
// CopyOrZeroValue returns a copy of s if s is not nil otherwise the zero value of T will be returned.
|
||||
func CopyOrZeroValue[T any](s *T) *T {
|
||||
cp := new(T)
|
||||
if s != nil {
|
||||
*cp = *s
|
||||
}
|
||||
return cp
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package structs
|
||||
|
||||
import "testing"
|
||||
|
||||
type example struct {
|
||||
Attribute1 string
|
||||
Attribute2 string
|
||||
}
|
||||
|
||||
func TestCopyOrZeroValue(t *testing.T) {
|
||||
var e *example
|
||||
|
||||
zv := CopyOrZeroValue(e)
|
||||
|
||||
if zv == nil {
|
||||
t.Error("CopyOrZeroValue returned nil")
|
||||
}
|
||||
|
||||
if zv.Attribute1 != "" || zv.Attribute2 != "" {
|
||||
t.Error("CopyOrZeroValue didn't return zero value")
|
||||
}
|
||||
|
||||
e2 := &example{Attribute1: "One", Attribute2: "Two"}
|
||||
|
||||
cp := CopyOrZeroValue(e2)
|
||||
|
||||
if cp == nil {
|
||||
t.Error("CopyOrZeroValue returned nil")
|
||||
}
|
||||
|
||||
if cp == e2 {
|
||||
t.Error("CopyOrZeroValue returned reference with same address")
|
||||
}
|
||||
|
||||
if cp.Attribute1 != e2.Attribute1 || cp.Attribute2 != e2.Attribute2 {
|
||||
t.Error("CopyOrZeroValue didn't correctly copy attributes")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user